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

A state machine wrapper that binds actions (callbacks/lambdas) to state lifecycle events. More...

#include <state_machine.hpp>

Classes

struct  State
 

Public Types

using Action = std::function<void()>
 
using Condition = std::function<bool()>
 
using Pure = PureStateMachine<StateType>
 

Public Member Functions

 StateMachine (StateType initial_state)
 
void add_state (StateType state, std::optional< Action > on_enter=std::nullopt, std::optional< Action > on_update=std::nullopt, std::optional< Action > on_exit=std::nullopt)
 
void add_transition (StateType from, StateType to, Condition condition)
 
void update ()
 Performs one update tick:
 
StateType get_state () const
 
const Pureget_pure_fsm () const
 

Detailed Description

template<typename StateType>
class StateMachine< StateType >

A state machine wrapper that binds actions (callbacks/lambdas) to state lifecycle events.

This class wraps a PureStateMachine and executes side effects (callbacks) corresponding to the emitted events from the pure FSM. It’s suitable for real-time or game logic, where on_enter, on_update, and on_exit actions should trigger immediate effects.

Template Parameters
StateTypeEnum or integral type representing states.

Member Typedef Documentation

◆ Action

template<typename StateType>
using StateMachine< StateType >::Action = std::function<void()>

◆ Condition

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

◆ Pure

template<typename StateType>
using StateMachine< StateType >::Pure = PureStateMachine<StateType>

Constructor & Destructor Documentation

◆ StateMachine()

template<typename StateType>
StateMachine< StateType >::StateMachine ( StateType initial_state)
inlineexplicit

Member Function Documentation

◆ add_state()

template<typename StateType>
void StateMachine< StateType >::add_state ( StateType state,
std::optional< Action > on_enter = std::nullopt,
std::optional< Action > on_update = std::nullopt,
std::optional< Action > on_exit = std::nullopt )
inline

◆ add_transition()

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

◆ get_pure_fsm()

template<typename StateType>
const Pure & StateMachine< StateType >::get_pure_fsm ( ) const
inlinenodiscard

◆ get_state()

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

◆ update()

template<typename StateType>
void StateMachine< StateType >::update ( )
inline

Performs one update tick:

  • Runs pure logic (no side effects)
  • Executes callbacks corresponding to emitted events

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