Observable Reference
Generic observable objects for C++
Public Member Functions | List of all members
observable::expr::expression_node< ResultType > Class Template Referencefinal

Description

template<typename ResultType>
class observable::expr::expression_node< ResultType >

Expression nodes can form a tree to evaluate arbitrary expressions.

Expressions are formed from n-ary, user-supplied operators and operands.

Operands can be constants, observable values or other expression nodes.

The tree will propagate change notifications upwards, so any change to any value<ValueType, EqualityComparator> contained in the tree will be propagated upward, to the root node.

When evaluating the root node, only nodes that have been changed will be evaluated.

Warning
None of the methods in this class can be safely called concurrently.

Public Member Functions

template<typename ValueType , typename E = std::enable_if_t< !is_value<ValueType>::value && !is_expression_node<ValueType>::value>>
 expression_node (ValueType &&constant)
 Create a new node from a constant value. More...
 
template<typename ValueType , typename ... Rest>
 expression_node (value< ValueType, Rest ... > &value)
 Create a new node from an observable value. More...
 
template<typename OpType , typename ... ValueType>
 expression_node (OpType &&op, expression_node< ValueType > &&... nodes)
 Create a new node from a number of nodes and an n-ary operation. More...
 
void eval () const
 Execute the stored operation and update the node's result value. More...
 
auto get () const
 Retrieve the expression node's result value. More...
 
template<typename Observer >
auto subscribe (Observer &&callable)
 Subscribe to change notifications from this node. More...
 
 expression_node ()=default
 Expression nodes are not default-constructible. More...
 
 expression_node (expression_node const &)=default
 Expression nodes are copy-constructible. More...
 
auto operator= (expression_node const &) -> expression_node &=default
 Expression nodes are copy-assignable. More...
 
 expression_node (expression_node &&other) noexcept=default
 Expression nodes are move-constructible. More...
 
auto operator= (expression_node &&other) noexcept -> expression_node &=default
 Expression nodes are move-assignable. More...
 

Constructor & Destructor Documentation

◆ expression_node() [1/6]

template<typename ResultType>
template<typename ValueType , typename E = std::enable_if_t< !is_value<ValueType>::value && !is_expression_node<ValueType>::value>>
observable::expr::expression_node< ResultType >::expression_node ( ValueType &&  constant)
inlineexplicit

Create a new node from a constant value.

Nodes created with this constructor are called constant nodes. These nodes never notify their subscribers of value changes.

Parameters
[in]constantA constant value that will become the node's evaluated value.

◆ expression_node() [2/6]

template<typename ResultType>
template<typename ValueType , typename ... Rest>
observable::expr::expression_node< ResultType >::expression_node ( value< ValueType, Rest ... > &  value)
inlineexplicit

Create a new node from an observable value.

Nodes created with this constructor are called value nodes. These nodes notify their subscribers of value changes as long as the value is alive.

Parameters
[in]valueAn observable value who will become the node's evaluated value. This value needs to be kept alive for at least the duration of this constructor call.
Note
If the value is destroyed, the node will keep returning the last evaluated value indefinitely.
If the value is moved, the node will use the new, moved-into, value.

◆ expression_node() [3/6]

template<typename ResultType>
template<typename OpType , typename ... ValueType>
observable::expr::expression_node< ResultType >::expression_node ( OpType &&  op,
expression_node< ValueType > &&...  nodes 
)
inlineexplicit

Create a new node from a number of nodes and an n-ary operation.

Nodes created with this constructor are called n-ary nodes. These nodes notify their subscribers of changes to the child nodes provided to this constructor.

Parameters
[in]opAn n-ary operation with the signature below:
              ResultType (ValueType const & ...)
[in]nodes... Expression nodes who's valuewWW will be the operand to the operation.

◆ expression_node() [4/6]

template<typename ResultType>
observable::expr::expression_node< ResultType >::expression_node ( )
default

Expression nodes are not default-constructible.

◆ expression_node() [5/6]

template<typename ResultType>
observable::expr::expression_node< ResultType >::expression_node ( expression_node< ResultType > const &  )
default

Expression nodes are copy-constructible.

Warning
This copy constructor will produce a shallow copy.

◆ expression_node() [6/6]

template<typename ResultType>
observable::expr::expression_node< ResultType >::expression_node ( expression_node< ResultType > &&  other)
defaultnoexcept

Expression nodes are move-constructible.

Member Function Documentation

◆ eval()

template<typename ResultType>
void observable::expr::expression_node< ResultType >::eval ( ) const
inline

Execute the stored operation and update the node's result value.

◆ get()

template<typename ResultType>
auto observable::expr::expression_node< ResultType >::get ( ) const
inline

Retrieve the expression node's result value.

This call will not evaluate the node, so this value might be stale. You can call eval() to make sure that the expression has an updated result value.

◆ subscribe()

template<typename ResultType>
template<typename Observer >
auto observable::expr::expression_node< ResultType >::subscribe ( Observer &&  callable)
inline

Subscribe to change notifications from this node.

◆ operator=() [1/2]

template<typename ResultType>
auto observable::expr::expression_node< ResultType >::operator= ( expression_node< ResultType > const &  ) -> expression_node &=default
default

Expression nodes are copy-assignable.

Warning
This copy assignment operator will produce a shallow copy.

◆ operator=() [2/2]

template<typename ResultType>
auto observable::expr::expression_node< ResultType >::operator= ( expression_node< ResultType > &&  other) -> expression_node &=default
defaultnoexcept

Expression nodes are move-assignable.