|
CPP-TOOLBOX
|
A minimal and flexible finite state machine (FSM) implementation. More...
#include <state_machine.hpp>
Classes | |
| struct | Event |
| Represents a state-related event emitted during an update. More... | |
| struct | Transition |
| Represents a possible transition from one state to another. More... | |
Public Types | |
| enum class | EventType { ENTER_STATE , UPDATE_STATE , EXIT_STATE , TRANSITION } |
| The type of event emitted by the state machine. More... | |
| enum class | Mode { MULTI_EVENT , SINGLE_EVENT } |
| Determines how many events are emitted per update tick. More... | |
| using | Condition = std::function<bool()> |
Public Member Functions | |
| PureStateMachine (StateType initial_state, Mode mode=Mode::MULTI_EVENT) | |
| Constructs a new PureStateMachine with an initial state and mode. | |
| void | add_state (StateType state) |
| Adds a new state to the machine. | |
| void | add_transition (StateType from, StateType to, Condition condition) |
| Adds a conditional transition between two states. | |
| std::vector< Event > | update_pure () |
| Updates the state machine and emits events describing what happened. | |
| StateType | get_state () const |
| Gets the current state. | |
| void | set_mode (Mode new_mode) |
| Sets the current operational mode of the FSM. | |
| Mode | get_mode () const |
| Gets the current operational mode. | |
A minimal and flexible finite state machine (FSM) implementation.
This class provides a generic state machine framework that can operate in two modes:
It does not perform side effects directly—only emits Event objects describing what happened. Users can then handle these events externally in their own systems.
| StateType | The enum or integral type representing all possible states. |
| using PureStateMachine< StateType >::Condition = std::function<bool()> |
|
strong |
|
strong |
|
inlineexplicit |
Constructs a new PureStateMachine with an initial state and mode.
| initial_state | The starting state of the machine. |
| mode | The event emission mode (default: MULTI_EVENT). |
|
inline |
Adds a new state to the machine.
| state | The state to add. |
|
inline |
Adds a conditional transition between two states.
| from | The state from which the transition originates. |
| to | The state to transition to. |
| condition | A callable returning true if the transition should occur. |
|
inlinenodiscard |
Gets the current operational mode.
|
inlinenodiscard |
Gets the current state.
|
inline |
Sets the current operational mode of the FSM.
| new_mode | The new mode to use. |
|
inlinenodiscard |
Updates the state machine and emits events describing what happened.
The emitted events depend on the current mode:
MULTI_EVENT mode, multiple events may be emitted per tick (enter, update, transition, etc.).SINGLE_EVENT mode, only one event is emitted per tick.