Single-enum state machine with enter/exit callbacks. More...
#include <StateManager.h>
Public Member Functions | |
| StateManager ()=default | |
Default constructor — initial state is the zero-value of StateEnum. | |
| template<typename T = StateEnum> | |
| StateManager (std::enable_if_t<!std::is_void_v< T >, T > initialState) | |
| Construct with an explicit initial state. | |
| template<typename T = StateEnum> | |
| std::enable_if_t<!std::is_void_v< T >, T > | getState () const |
| Return the current state value. | |
| template<typename T = StateEnum> | |
| std::enable_if_t<!std::is_void_v< T >, bool > | isState (T state) const |
Return true if currently in state. | |
| template<typename T = StateEnum> | |
| std::enable_if_t<!std::is_void_v< T > > | onStateEnter (std::function< void(T)> callback) |
| Register a callback invoked after each state transition. | |
| template<typename T = StateEnum> | |
| std::enable_if_t<!std::is_void_v< T > > | onStateExit (std::function< void(T)> callback) |
| Register a callback invoked before each state transition. | |
| template<typename T = StateEnum> | |
| std::enable_if_t<!std::is_void_v< T > > | setState (T newState) |
| Transition to a new state. | |
Single-enum state machine with enter/exit callbacks.
StateManager<StateEnum> stores one active state value and fires optional callbacks on every transition. It is used in two places:
Customizable<Manifest> inherits StateManager<Manifest::State> to give traits their own state machine.MultiCustomStateManager uses SingleStateManager (a similar type) to aggregate multiple state machines for components.All methods are guarded by std::enable_if so they exist only when StateEnum is not void. The void specialization at the bottom of this file is a no-op that compiles cleanly when the manifest declares no State enum.
| StateEnum | An enum class type representing the possible states. Defaults to void (selects the no-op specialization). |
Definition at line 84 of file StateManager.h.
|
explicit |
Construct with an explicit initial state.
| initialState | The state to start in. The enter callback is not fired during construction. |
|
default |
Default constructor — initial state is the zero-value of StateEnum.
| std::enable_if_t<!std::is_void_v< T >, T > ml::StateManager< StateEnum >::getState | ( | ) | const |
Return the current state value.
StateEnum value. | std::enable_if_t<!std::is_void_v< T >, bool > ml::StateManager< StateEnum >::isState | ( | T | state | ) | const |
Return true if currently in state.
| state | The state value to compare against. |
true if the current state equals state. | std::enable_if_t<!std::is_void_v< T > > ml::StateManager< StateEnum >::onStateEnter | ( | std::function< void(T)> | callback | ) |
Register a callback invoked after each state transition.
The callback receives the newly entered state. Only one enter callback is active at a time; calling this again replaces it.
| callback | Function called with the new state after each transition. |
| std::enable_if_t<!std::is_void_v< T > > ml::StateManager< StateEnum >::onStateExit | ( | std::function< void(T)> | callback | ) |
Register a callback invoked before each state transition.
The callback receives the state being left. Only one exit callback is active at a time; calling this again replaces it.
| callback | Function called with the old state before each transition. |
| std::enable_if_t<!std::is_void_v< T > > ml::StateManager< StateEnum >::setState | ( | T | newState | ) |
Transition to a new state.
Fires the exit callback with the current state, updates the stored state to newState, then fires the enter callback with the new state. Either callback may be unset (nullptr), in which case it is skipped.
| newState | The state to transition into. |