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... | |
| State & | operator= (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... | |
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.
| Valuable::State< Enum >::State | ( | Enum | initialState | ) |
Constructs a new State object.
| initialState | enum value used to initialize the state |
|
inline |
|
inline |
Adds a monitor to the state.
This function is thread-safe
| callback | called when ever the state changes |
| type | Listener type, allowed values: DIRECT or AFTER_UPDATE |
| initialInvoke | call the callback once in the beginning with the current state |
|
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
| stateMask | callback 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. |
| callback | called when ever the state changes to a monitored state |
| once | should the callback be removed after it has been called once |
| type | Listener type, allowed values: DIRECT or AFTER_UPDATE |
|
inline |
Implicit conversion to the wrapped enum value, same as State::state().
This function is thread-safe
|
inline |
Removes a listener from the state.
This function is thread-safe
| id | listener id, returned from onChange or onStateMask functions |
|
inline |
Sets the current state and triggers all necessary callbacks.
This function is thread-safe
|
inline |
Reads the current state.
This function is thread-safe