template<typename Derived, typename... States>
class Acts::FiniteStateMachine< Derived, States >
Implementation of a finite state machine engine.
Allows setting up a system of states and transitions between them. States are definedd as empty structs (footprint: 1 byte). Transitions call functions using overload resolution. This works by subclassing this class, providing the deriving type as the first template argument (CRTP) and providing methods like
std::optional< StateVariant > event_return
Type alias for event return type (optional state variant).
Definition FiniteStateMachine.hpp:118
The arguments are the state S and the triggered event E. Their values can be discarded (you can attach values to events of course, if you like) The return type of these functions is effectively std::optional<State>, so you can either return std::nullopt to remain in the same state, or an instance of another state. That state will then become active.
You can also define a method template, which will serve as a catch-all handler (due to the fact that it will match any state/event combination):
template <typename State, typename Event>
}
Contractual termination state.
Definition FiniteStateMachine.hpp:104
If for a given state and event no suitable overload of on_event (and you also haven't defined a catch-all as described above), a transition to Terminated will be triggered. This is essentially equivalent to the method template above.
If this triggers, it will switch to the Terminated state (which is always included in the FSM).
Additionally, the FSM will attempt to call functions like
void on_enter(const State&);
void on_exit(const State&);
when entering/exiting a state. This can be used to perform actions regardless of the source or destination state in a transition to a given state. This is also fired in case a transition to Terminated occurs.
The base class also calls
void on_process(const Event&);
void on_process(const State&, const Event&);
void on_process(const State1& const Event&, const State2&);
during event processing, and allow for things like event and transition logging.
The on_event, on_enter, on_exit and on_process methods need to be implemented exhaustively, i.e. for all state/event combinations. This might require you to add catch-all no-op functions like
template <typename...Args>
and so on.
The public interface for the user of the FSM are the
template <typename... Args>
template <typename Event, typename... Args>
void dispatch(Event&& event, Args&&... args) {
void dispatch(Event &&event, Args &&... args)
Public interface to handle an event.
Definition FiniteStateMachine.hpp:219
void setState(State state, Args &&... args)
Sets the state to a given one.
Definition FiniteStateMachine.hpp:142
std::variant< Terminated, States... > StateVariant
Variant type allowing tagged type erased storage of the current state of the FSM.
Definition FiniteStateMachine.hpp:111
setState triggers a transition to a given state, dispatch triggers processing on an event from the given state. Both will call the appropriate on_exit and on_enter overloads. Both also accept an arbitrary number of additional arguments that are passed to the on_event, on_exit and on_enter overloads.
- Template Parameters
-
| Derived | Class deriving from the FSM |
| States | Argument pack with the state types that the FSM can be handled. |