CPP-TOOLBOX
Loading...
Searching...
No Matches
PureStateMachine< StateType > Class Template Reference

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< Eventupdate_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.
 

Detailed Description

template<typename StateType>
class PureStateMachine< StateType >

A minimal and flexible finite state machine (FSM) implementation.

This class provides a generic state machine framework that can operate in two modes:

  • MULTI_EVENT: Emits multiple events per update (enter, update, exit, and transition).
  • SINGLE_EVENT: Emits only one event per update, suitable for systems that process one event at a time.

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.

Template Parameters
StateTypeThe enum or integral type representing all possible states.

Member Typedef Documentation

◆ Condition

template<typename StateType>
using PureStateMachine< StateType >::Condition = std::function<bool()>

Member Enumeration Documentation

◆ EventType

template<typename StateType>
enum class PureStateMachine::EventType
strong

The type of event emitted by the state machine.

Enumerator
ENTER_STATE 

Indicates entry into a new state.

UPDATE_STATE 

Indicates the current state is being updated.

EXIT_STATE 

Indicates exit from a previous state.

TRANSITION 

Indicates a transition between two states.

◆ Mode

template<typename StateType>
enum class PureStateMachine::Mode
strong

Determines how many events are emitted per update tick.

Enumerator
MULTI_EVENT 

Default mode: may emit multiple events per tick.

SINGLE_EVENT 

Alternate mode: only one event per tick.

Constructor & Destructor Documentation

◆ PureStateMachine()

template<typename StateType>
PureStateMachine< StateType >::PureStateMachine ( StateType initial_state,
Mode mode = Mode::MULTI_EVENT )
inlineexplicit

Constructs a new PureStateMachine with an initial state and mode.

Parameters
initial_stateThe starting state of the machine.
modeThe event emission mode (default: MULTI_EVENT).

Member Function Documentation

◆ add_state()

template<typename StateType>
void PureStateMachine< StateType >::add_state ( StateType state)
inline

Adds a new state to the machine.

Parameters
stateThe state to add.

◆ add_transition()

template<typename StateType>
void PureStateMachine< StateType >::add_transition ( StateType from,
StateType to,
Condition condition )
inline

Adds a conditional transition between two states.

Parameters
fromThe state from which the transition originates.
toThe state to transition to.
conditionA callable returning true if the transition should occur.

◆ get_mode()

template<typename StateType>
Mode PureStateMachine< StateType >::get_mode ( ) const
inlinenodiscard

Gets the current operational mode.

Returns
The current mode.

◆ get_state()

template<typename StateType>
StateType PureStateMachine< StateType >::get_state ( ) const
inlinenodiscard

Gets the current state.

Returns
The current state.

◆ set_mode()

template<typename StateType>
void PureStateMachine< StateType >::set_mode ( Mode new_mode)
inline

Sets the current operational mode of the FSM.

Parameters
new_modeThe new mode to use.

◆ update_pure()

template<typename StateType>
std::vector< Event > PureStateMachine< StateType >::update_pure ( )
inlinenodiscard

Updates the state machine and emits events describing what happened.

The emitted events depend on the current mode:

  • In MULTI_EVENT mode, multiple events may be emitted per tick (enter, update, transition, etc.).
  • In SINGLE_EVENT mode, only one event is emitted per tick.
Returns
A vector of events representing what occurred during the update.

The documentation for this class was generated from the following file: