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> { ... }

Public Member Typedefs Index

template <typename StateType>
usingEngineRoleTag = helios::engine::common::tags::ManagerRole

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::UpdateContext &updateContext) noexcept

Processes pending state commands. More...

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

Submits a state command for processing. More...

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

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

template <typename StateType>
voidreset ()

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 68 of file StateManager.ixx.

Public Member Typedefs

EngineRoleTag

template <typename StateType>
using helios::engine::state::StateManager< StateType >::EngineRoleTag = helios::engine::common::tags::ManagerRole

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 155 of file StateManager.ixx.

155 explicit StateManager(std::span<const StateTransitionRule<StateType>> rules)
156 : 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 165 of file StateManager.ixx.

166 listeners_.push_back(std::move(listener));
167 return *this;
168 }

flush()

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

Processes pending state commands.

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

Parameters
updateContext

The current frame's update context.

Definition at line 178 of file StateManager.ixx.

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

init()

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

Initializes the manager and registers with GameWorld.

Parameters
gameWorld

The game world to register with.

Definition at line 240 of file StateManager.ixx.

reset()

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

Clears all pending commands.

Definition at line 247 of file StateManager.ixx.

247 void reset() {
248 // intentionally left empty. Clearing the pending queue would also mean
249 // that any pending state transisions **required by the reset** are nuked.
250 }

submit()

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

Submits a state command for processing.

Parameters
stateCommand

The command to queue.

Returns

True (always accepts commands).

Definition at line 228 of file StateManager.ixx.

228 bool submit(
229 const StateCommand<StateType> stateCommand
230 ) noexcept {
231 pending_.push_back(stateCommand);
232 return true;
233 };

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 134 of file StateManager.ixx.

134 void signalEnter(
135 const StateType from,
136 const StateType to,
137 const StateTransitionIdType<StateType> transitionId,
139
140 for (auto& listener : listeners_) {
141 listener->onStateEnter(updateContext, to);
142
143 }
144 }

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 93 of file StateManager.ixx.

93 void signalExit(
94 const StateType from,
95 const StateType to,
96 const StateTransitionIdType<StateType> transitionId,
98
99 for (auto& listener : listeners_) {
100 listener->onStateExit(updateContext, from);
101 }
102 }

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 112 of file StateManager.ixx.

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

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 78 of file StateManager.ixx.

78 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 73 of file StateManager.ixx.

73 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 83 of file StateManager.ixx.

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