Skip to main content

StateManager Class Template

Manages state transitions using a rule-based system. More...

Declaration

template <typename StateType> class helios::engine::state::StateManager<StateType> { ... }

Base classes

classManager

Abstract base class for managers that process deferred operations. More...

classTypedStateCommandHandler<StateType>

Typed interface for handling state commands. More...

Public Constructors Index

template <typename StateType>
StateManager (std::span< const StateTransitionRule< StateType > > rules)

Constructs a state manager with transition rules. More...

Public Member Functions Index

template <typename StateType>
StateManager &addStateListener (std::unique_ptr< StateTransitionListener< StateType > > listener) noexcept

Registers a state transition listener. More...

template <typename StateType>
voidflush (helios::engine::runtime::world::GameWorld &gameWorld, helios::engine::runtime::world::UpdateContext &updateContext) noexcept override

Processes pending state commands. More...

template <typename StateType>
boolsubmit (const StateCommand< StateType > &stateCommand) noexcept override

Submits a state command for processing. More...

template <typename StateType>
voidinit (helios::engine::runtime::world::GameWorld &gameWorld) override

Initializes the manager and registers with GameWorld. More...

template <typename StateType>
voidreset () override

Clears all pending commands. More...

Private Member Functions Index

template <typename StateType>
voidsignalExit (const StateType from, const StateType to, const StateTransitionIdType< StateType > transitionId, helios::engine::runtime::world::UpdateContext &updateContext)

Notifies listeners of state exit. More...

template <typename StateType>
voidsignalTransition (const StateType from, const StateType to, const StateTransitionIdType< StateType > transitionId, helios::engine::runtime::world::UpdateContext &updateContext)

Notifies listeners of the transition. More...

template <typename StateType>
voidsignalEnter (const StateType from, const StateType to, const StateTransitionIdType< StateType > transitionId, helios::engine::runtime::world::UpdateContext &updateContext)

Notifies listeners of state entry. More...

Private Member Attributes Index

template <typename StateType>
std::vector< StateCommand< StateType > >pending_

Queue of pending state commands. More...

template <typename StateType>
std::vector< std::unique_ptr< StateTransitionListener< StateType > > >listeners_

Registered transition listeners. More...

template <typename StateType>
std::vector< StateTransitionRule< StateType > >rules_

Transition rules defining valid state changes. More...

Description

Manages state transitions using a rule-based system.

Implements both Manager and TypedStateCommandHandler interfaces. Processes state transition commands by matching against registered rules, executing guards, and notifying listeners.

## Transition Flow

1. Commands are submitted via submit() and queued 2. During flush(), the last pending command is processed 3. Rules are checked to find a matching transition 4. If a guard is present, it must return true 5. Listeners are notified: onStateExit -> onStateTransition -> onStateEnter 6. Session state is updated

Template Parameters
StateType

The state enum type.

See Also

StateTransitionRule

See Also

StateTransitionListener

See Also

StateCommand

Definition at line 64 of file StateManager.ixx.

Public Constructors

StateManager()

template <typename StateType>
helios::engine::state::StateManager< StateType >::StateManager (std::span< const StateTransitionRule< StateType > > rules)
inline explicit

Constructs a state manager with transition rules.

Parameters
rules

Span of valid transition rules.

Definition at line 151 of file StateManager.ixx.

151 explicit StateManager(std::span<const StateTransitionRule<StateType>> rules)
152 : rules_(rules.begin(), rules.end()) {}

Public Member Functions

addStateListener()

template <typename StateType>
StateManager & helios::engine::state::StateManager< StateType >::addStateListener (std::unique_ptr< StateTransitionListener< StateType > > listener)
inline noexcept

Registers a state transition listener.

Parameters
listener

The listener to add.

Returns

Reference to this manager for chaining.

Definition at line 161 of file StateManager.ixx.

162 listeners_.push_back(std::move(listener));
163 return *this;
164 }

flush()

template <typename StateType>
void helios::engine::state::StateManager< StateType >::flush (helios::engine::runtime::world::GameWorld & gameWorld, helios::engine::runtime::world::UpdateContext & updateContext)
inline noexcept virtual

Processes pending state commands.

Processes the last pending command, finds matching rules, executes guards, and triggers the transition if valid.

Parameters
gameWorld

The game world.

updateContext

The current frame's update context.

Definition at line 175 of file StateManager.ixx.

175 void flush(
178 ) noexcept override {
179
180 if (pending_.empty()) {
181 return;
182 }
183
184 auto command = pending_.back();
185
186 auto transitionRequest = command.transitionRequest();
187
188 auto& session = gameWorld.session();
189 auto currentFrom = session.state<StateType>();
190 auto from = transitionRequest.from();
191 auto transitionId = transitionRequest.transitionId();
192
193
194 if (currentFrom != from) {
195 pending_.clear();
196 return;
197 }
198
199
200 for (auto& rule : rules_) {
201 if (rule.from() == from && rule.transitionId() == transitionId) {
202
203 if (rule.guard()) {
204 if (!rule.guard()(updateContext, transitionRequest)) {
205 break;
206 }
207 }
208
209 signalExit(from, rule.to(), transitionId, updateContext);
210 signalTransition(from, rule.to(), transitionId, updateContext);
211 session.setStateFrom<StateType>(StateTransitionContext<StateType>{rule.from(), rule.to(), transitionId});
212 signalEnter(from, rule.to(), transitionId, updateContext);
213 }
214 }
215
216 pending_.clear();
217 }

init()

template <typename StateType>
void helios::engine::state::StateManager< StateType >::init (helios::engine::runtime::world::GameWorld & gameWorld)
inline virtual

Initializes the manager and registers with GameWorld.

Parameters
gameWorld

The game world to register with.

Definition at line 238 of file StateManager.ixx.

239 gameWorld.registerStateCommandHandler<StateType>(*this);
240 }

reset()

template <typename StateType>
void helios::engine::state::StateManager< StateType >::reset ()
inline virtual

Clears all pending commands.

Definition at line 245 of file StateManager.ixx.

245 void reset() override {
246 pending_.clear();
247 }

submit()

template <typename StateType>
bool helios::engine::state::StateManager< StateType >::submit (const StateCommand< StateType > & stateCommand)
inline noexcept virtual

Submits a state command for processing.

Parameters
stateCommand

The command to queue.

Returns

True (always accepts commands).

Definition at line 226 of file StateManager.ixx.

226 bool submit(
227 const StateCommand<StateType>& stateCommand
228 ) noexcept override {
229 pending_.push_back(stateCommand);
230 return true;
231 };

Private Member Functions

signalEnter()

template <typename StateType>
void helios::engine::state::StateManager< StateType >::signalEnter (const StateType from, const StateType to, const StateTransitionIdType< StateType > transitionId, helios::engine::runtime::world::UpdateContext & updateContext)
inline

Notifies listeners of state entry.

Parameters
from

The source state.

to

The state being entered.

transitionId

The transition identifier.

updateContext

The current frame's update context.

Definition at line 131 of file StateManager.ixx.

131 void signalEnter(
132 const StateType from,
133 const StateType to,
134 const StateTransitionIdType<StateType> transitionId,
136
137 for (auto& listener : listeners_) {
138 listener->onStateEnter(updateContext, to);
139
140 }
141 }

signalExit()

template <typename StateType>
void helios::engine::state::StateManager< StateType >::signalExit (const StateType from, const StateType to, const StateTransitionIdType< StateType > transitionId, helios::engine::runtime::world::UpdateContext & updateContext)
inline

Notifies listeners of state exit.

Parameters
from

The state being exited.

to

The target state.

transitionId

The transition identifier.

updateContext

The current frame's update context.

Definition at line 90 of file StateManager.ixx.

90 void signalExit(
91 const StateType from,
92 const StateType to,
93 const StateTransitionIdType<StateType> transitionId,
95
96 for (auto& listener : listeners_) {
97 listener->onStateExit(updateContext, from);
98 }
99 }

signalTransition()

template <typename StateType>
void helios::engine::state::StateManager< StateType >::signalTransition (const StateType from, const StateType to, const StateTransitionIdType< StateType > transitionId, helios::engine::runtime::world::UpdateContext & updateContext)
inline

Notifies listeners of the transition.

Parameters
from

The source state.

to

The target state.

transitionId

The transition identifier.

updateContext

The current frame's update context.

Definition at line 109 of file StateManager.ixx.

109 void signalTransition(
110 const StateType from,
111 const StateType to,
112 const StateTransitionIdType<StateType> transitionId,
114
115 for (auto& listener : listeners_) {
116 listener->onStateTransition(
117 updateContext,
118 StateTransitionContext<StateType>{from, to, transitionId}
119 );
120 }
121 }

Private Member Attributes

listeners_

template <typename StateType>
std::vector<std::unique_ptr<StateTransitionListener<StateType> > > helios::engine::state::StateManager< StateType >::listeners_

Registered transition listeners.

Definition at line 75 of file StateManager.ixx.

75 std::vector<std::unique_ptr<StateTransitionListener<StateType>>> listeners_;

pending_

template <typename StateType>
std::vector<StateCommand<StateType> > helios::engine::state::StateManager< StateType >::pending_

Queue of pending state commands.

Definition at line 70 of file StateManager.ixx.

70 std::vector<StateCommand<StateType>> pending_;

rules_

template <typename StateType>
std::vector<StateTransitionRule<StateType> > helios::engine::state::StateManager< StateType >::rules_

Transition rules defining valid state changes.

Definition at line 80 of file StateManager.ixx.

80 std::vector<StateTransitionRule<StateType>> rules_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.