Template Class StateMachine

Class Documentation

template<typename State, typename Trigger>
class StateMachine

Requires: std::copy_constructible<State> && std::copy_constructible<Trigger> && std::equality_comparable<State> && std::equality_comparable<Trigger>

A transition table for one typed state domain.

Rules are tested in insertion order; the first matching rule whose guard succeeds wins. Guards and actions may inspect other machines, allowing a game to compose state domains without requiring a controller or machine hierarchy. The machine is owned by its caller and is not synchronized or reentrant during trigger(). Stored callbacks own their captures; borrowed targets must survive use/removal. state() borrows this machine. Guards may add rules for later consideration in the same trigger; hooks for a chosen transition are copied before calling exit. Order is exit -> state assignment -> action -> enter. Exceptions propagate and stop that sequence: exit failure keeps old state, action/enter failure keeps new state; throwing State assignment follows that type’s guarantee. No rollback.

Public Types

using Guard = std::function<bool()>
using Action = std::function<void()>

Public Functions

inline explicit StateMachine(State initial)
inline void add_transition(State from, Trigger trigger, State to, Guard guard = {}, Action action = {})
inline void on_enter(State state, Action action)
inline void on_exit(State state, Action action)
inline const State &state() const

Declaration: [[nodiscard]]

inline bool trigger(const Trigger &event)