All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
Valuable::State< Enum > Class Template Reference

State machine that doesn't define any transitions or triggers, but implements thread-safe access and monitoring callbacks. More...

#include <Valuable/State.hpp>

Public Types

typedef std::function< void(Enum,
int)> 
CallbackType
 Type of the callback used in monitor functions, new state is given as a first parameter.
 

Public Member Functions

int generation () const
 
long onChange (CallbackType callback, Node::ListenerType type=Node::AFTER_UPDATE, bool initialInvoke=false)
 Adds a monitor to the state. More...
 
long onStateMask (int stateMask, CallbackType callback, bool once=true, Node::ListenerType type=Node::AFTER_UPDATE)
 Adds a monitor to the state with given stateMask. More...
 
 operator Enum () const
 Implicit conversion to the wrapped enum value, same as State::state(). More...
 
Stateoperator= (State &&state)
 Move assignment operator, not thread-safe.
 
State< Enum > & operator= (Enum value)
 Same as State::setState(Enum), thread-safe.
 
bool removeListener (long id)
 Removes a listener from the state. More...
 
void setState (Enum state)
 Sets the current state and triggers all necessary callbacks. More...
 
 State (Enum initialState)
 Constructs a new State object. More...
 
 State (State &&state)
 Move constructor, not thread-safe.
 
Enum state () const
 Reads the current state. More...
 

Detailed Description

template<typename Enum>
class Valuable::State< Enum >

State machine that doesn't define any transitions or triggers, but implements thread-safe access and monitoring callbacks.

It's basically just an enum wrapper. It is important that the Enum values form a bitmask, values must not share any bits. See LoadingEnum as an example. This class guarantees that all "once" callbacks are called exactly once, and other callbacks called only when the state is changed to any of the monitored states. All callbacks are called in right order.

Constructor & Destructor Documentation

template<typename Enum >
Valuable::State< Enum >::State ( Enum  initialState)

Constructs a new State object.

Parameters
initialStateenum value used to initialize the state

Member Function Documentation

template<typename Enum >
int Valuable::State< Enum >::generation ( ) const
inline
Returns
how many times state has changed
template<typename Enum >
long Valuable::State< Enum >::onChange ( CallbackType  callback,
Node::ListenerType  type = Node::AFTER_UPDATE,
bool  initialInvoke = false 
)
inline

Adds a monitor to the state.

This function is thread-safe

Parameters
callbackcalled when ever the state changes
typeListener type, allowed values: DIRECT or AFTER_UPDATE
initialInvokecall the callback once in the beginning with the current state
Returns
a listener id
template<typename Enum >
long Valuable::State< Enum >::onStateMask ( int  stateMask,
CallbackType  callback,
bool  once = true,
Node::ListenerType  type = Node::AFTER_UPDATE 
)
inline

Adds a monitor to the state with given stateMask.

If the current state already matches to the monitored state, the callback is triggered immediately, still honoring the given listener type. This function is thread-safe

Parameters
stateMaskcallback is triggered only when the new state is included in stateMask. you can monitor multiple values by using bitwise or operation, for example HEADER | HEADER_READY.
callbackcalled when ever the state changes to a monitored state
onceshould the callback be removed after it has been called once
typeListener type, allowed values: DIRECT or AFTER_UPDATE
Returns
a listener id
template<typename Enum >
Valuable::State< Enum >::operator Enum ( ) const
inline

Implicit conversion to the wrapped enum value, same as State::state().

This function is thread-safe

template<typename Enum >
bool Valuable::State< Enum >::removeListener ( long  id)
inline

Removes a listener from the state.

This function is thread-safe

Parameters
idlistener id, returned from onChange or onStateMask functions
Returns
true if the listener was removed
template<typename Enum >
void Valuable::State< Enum >::setState ( Enum  state)
inline

Sets the current state and triggers all necessary callbacks.

This function is thread-safe

template<typename Enum >
Enum Valuable::State< Enum >::state ( ) const
inline

Reads the current state.

This function is thread-safe