nomlib
Loading...
Searching...
No Matches
nom::StateMachine Class Reference

Finite State Machine manager class. More...

#include <StateMachine.hpp>

Public Member Functions

 StateMachine ()
 Default constructor.
 ~StateMachine ()
 Destructor.
uint32 count () const
 Get the number of active states.
bool empty () const
 Check if the state stack is empty.
uint32 current_state_id (void) const
 Obtain the current state's identifier.
uint32 next_state_id (void) const
 Obtain the next pending state's identifier.
StateSymbol current_state_symbol () const
 Obtain the current active state symbol.
void register_state (const StateDescriptor &desc)
 Register a state factory and optional parent relationship.
void add_transition (StateSymbol active, EventSymbol event, StateSymbol stack_top, const Transition &transition)
 Add an exact event-driven transition to the machine's table.
void add_transition (StateSymbol active, EventSymbol event, const Transition &transition)
 Add a wildcard event-driven transition to the machine's table.
void set_transition_fallback (const TransitionTable::Callback &callback)
 Install a fallback transition resolver.
void set_input_mapper (InputStateMapper *mapper)
 Install the input mapper used to auto-manage per-state input contexts during state transitions.
InputStateMapper * input_mapper () const
 Obtain the installed input mapper, or nullptr.
void set_state (std::unique_ptr< IState > state, void_ptr data)
void pop_state (void_ptr data)
void push_state (StateSymbol symbol, void_ptr data=nullptr)
 Push a registered state onto the stack by symbol.
void push (StateSymbol child, void_ptr data=nullptr)
 Push a registered child state onto the active parent.
void pop (StateSymbol parent, void_ptr data=nullptr)
 Pop states until the requested parent state is active.
void replace (StateSymbol symbol, void_ptr data=nullptr)
 Replace the active state with a registered state by symbol.
void on_event (const Event &ev)
 Event handling logic for the active state.
void on_event (EventSymbol event_symbol, const Event *ev)
 Event handling logic with transition-table evaluation.
void update (float delta)
 State logic handling.
void draw (RenderWindow &)
 State rendering handling.

Private Member Functions

void set_deferred_state (std::unique_ptr< IState > state, void_ptr data)
 Set the next state.
void activate_input_context (const IState &state)
void disable_input_context (const IState &state)
void erase_input_context (const IState &state)
IState::unique_ptr make_state (StateSymbol symbol, void_ptr data=nullptr) const
 Construct a registered state by symbol.
bool can_push (StateSymbol child, StateSymbol parent) const
 Validate that child is declared as a child of the given parent.

Private Attributes

priv::DeferredState next_state_
 The pending state to be set on the next frame update.
std::vector< std::unique_ptr< IState > > states_
 Stack of states.
std::map< StateSymbol, StateDescriptor > registry_
 Registered state factories keyed by symbol.
TransitionTable transitions_
 Event-driven transition table.
InputStateMapper * input_mapper_ = nullptr

Detailed Description

Finite State Machine manager class.

Definition at line 171 of file StateMachine.hpp.

Member Function Documentation

◆ add_transition()

void nom::StateMachine::add_transition ( StateSymbol active,
EventSymbol event,
const Transition & transition )

Add a wildcard event-driven transition to the machine's table.

The stack top symbol is ignored when matching this transition.

◆ count()

uint32 nom::StateMachine::count ( ) const

Get the number of active states.

Returns
Total number of states in the stack.

◆ current_state_id()

uint32 nom::StateMachine::current_state_id ( void ) const

Obtain the current state's identifier.

Returns
The identifier of the active (top-most) state, or 0 if the stack is empty.
Remarks
The state identifier is user-defined. A state with id 0 is indistinguishable from the empty-stack sentinel.

◆ current_state_symbol()

StateSymbol nom::StateMachine::current_state_symbol ( ) const

Obtain the current active state symbol.

Returns
The symbol of the active (top-most) state, or NO_STATE if the stack is empty.

◆ make_state()

IState::unique_ptr nom::StateMachine::make_state ( StateSymbol symbol,
void_ptr data = nullptr ) const
private

Construct a registered state by symbol.

Parameters
symbolRegistered symbol to construct.
dataUser-defined data passed to the registered factory.
Returns
A valid state instance, or nullptr if the symbol is not registered.

◆ next_state_id()

uint32 nom::StateMachine::next_state_id ( void ) const

Obtain the next pending state's identifier.

Returns
The identifier of the state queued by set_state/pop_state if a transition is pending; otherwise the identifier of the current active state, or 0 if the stack is empty.
Remarks
This reflects the deferred transition target, not the state immediately below the active one on the stack.

◆ on_event() [1/2]

void nom::StateMachine::on_event ( const Event & ev)

Event handling logic for the active state.

Forwards the event to the active state's on_event. No transition-table lookup is performed; use on_event(EventSymbol, const Event&) to drive event-driven transitions.

See also
nom::IState::on_event.

◆ on_event() [2/2]

void nom::StateMachine::on_event ( EventSymbol event_symbol,
const Event * ev )

Event handling logic with transition-table evaluation.

Queries the transition table using the active state symbol, the provided event symbol, and the current stack-top symbol. If a transition matches, it is applied and the event is consumed. If no transition matches and ev is not nullptr, the event is forwarded to the active state's on_event.

◆ pop()

void nom::StateMachine::pop ( StateSymbol parent,
void_ptr data = nullptr )

Pop states until the requested parent state is active.

All intermediate states are exited. If the target parent is already active, this is a no-op.

Parameters
parentSymbol of the parent state to make active.
dataUser-defined data passed to lifecycle hooks.

◆ pop_state()

void nom::StateMachine::pop_state ( void_ptr data)
Parameters
stateThe nom::IState deriving object to be set as the current state.
dataUser-defined data; used for passing data in between states.
See also
StateMachine::set_deferred_state

◆ push()

void nom::StateMachine::push ( StateSymbol child,
void_ptr data = nullptr )

Push a registered child state onto the active parent.

The child must be registered with a parent matching the active state's symbol, or the request is rejected.

Parameters
childSymbol of the child state to push.
dataUser-defined data passed to on_init.

◆ push_state()

void nom::StateMachine::push_state ( StateSymbol symbol,
void_ptr data = nullptr )

Push a registered state onto the stack by symbol.

Parameters
symbolSymbol registered with register_state().
dataUser-defined data passed to on_init.
Remarks
Equivalent to set_state for states registered as Child of the current active state; if no parent relationship is declared, the symbol is simply pushed as a Child overlay.

◆ register_state()

void nom::StateMachine::register_state ( const StateDescriptor & desc)

Register a state factory and optional parent relationship.

Registered states can be pushed by symbol via push_state and can act as targets for event-driven transitions.

Parameters
descState descriptor containing the symbol, optional parent, and factory.

◆ replace()

void nom::StateMachine::replace ( StateSymbol symbol,
void_ptr data = nullptr )

Replace the active state with a registered state by symbol.

Parameters
symbolSymbol registered with register_state().
dataUser-defined data passed to lifecycle hooks.

◆ set_deferred_state()

void nom::StateMachine::set_deferred_state ( std::unique_ptr< IState > state,
void_ptr data )
private

Set the next state.

Parameters
stateThe state to be put onto the stack. If NULL is passed, the current state exits and the next one after that is resumed.
Note
Where on the stack the state is put is dependent upon the type of state defined (see also: nom::IState::Type enumeration).
Remarks
The transition of the state must be deferred until the next frame update in order for engine callback systems, i.e.: nom::InputMapper and nom::UIWidget, to be used within application-level code that expects to be able to change its state from within said callback system. If not deferred, the result is invalid memory access (a crash if you are lucky!) due to the deletion of the state before the callback is completed.

◆ set_input_mapper()

void nom::StateMachine::set_input_mapper ( InputStateMapper * mapper)

Install the input mapper used to auto-manage per-state input contexts during state transitions.

Parameters
mapperNon-owned pointer; it must outlive this machine or be reset via set_input_mapper(nullptr). May be nullptr (default).
Remarks
For every state whose nom::IState::input_context() key is not empty: the context is activated after on_init and on_resume, disabled after on_pause, and erased after on_exit. States with an empty context key manage their contexts manually.

◆ set_state()

void nom::StateMachine::set_state ( std::unique_ptr< IState > state,
void_ptr data )
Parameters
stateThe nom::IState deriving object to be set as the current state.
dataUser-defined data; used for passing data in between states.
See also
StateMachine::set_deferred_state

◆ set_transition_fallback()

void nom::StateMachine::set_transition_fallback ( const TransitionTable::Callback & callback)

Install a fallback transition resolver.

Called when no exact or wildcard transition exists.

◆ update()

void nom::StateMachine::update ( float delta)

State logic handling.

Remarks
Deferred state transition logic is handled here.

Member Data Documentation

◆ input_mapper_

InputStateMapper* nom::StateMachine::input_mapper_ = nullptr
private

Definition at line 376 of file StateMachine.hpp.

◆ next_state_

priv::DeferredState nom::StateMachine::next_state_
private

The pending state to be set on the next frame update.

Definition at line 365 of file StateMachine.hpp.

◆ registry_

std::map<StateSymbol, StateDescriptor> nom::StateMachine::registry_
private

Registered state factories keyed by symbol.

Definition at line 371 of file StateMachine.hpp.

◆ states_

std::vector<std::unique_ptr<IState> > nom::StateMachine::states_
private

Stack of states.

Definition at line 368 of file StateMachine.hpp.

◆ transitions_

TransitionTable nom::StateMachine::transitions_
private

Event-driven transition table.

Definition at line 374 of file StateMachine.hpp.


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