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>
boolsubmit (const DelayedStateCommand< StateType > stateCommand) noexcept

Submits a delayed 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 6. Session state is updated via StateComponent 7. Listeners are notified: onStateEnter (session already reflects new state)

Template Parameters
StateType

The state enum type.

See Also

StateTransitionRule

See Also

StateTransitionListener

See Also

StateCommand

Definition at line 69 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 156 of file StateManager.ixx.

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

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

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

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

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

reset()

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

Clears all pending commands.

Definition at line 269 of file StateManager.ixx.

269 void reset() {
270 // intentionally left empty. Clearing the pending queue would also mean
271 // that any pending state transisions **required by the reset** are nuked.
272 }

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

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

submit()

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

Submits a delayed state command for processing.

Extracts the transition request from the delayed command and queues it as a regular StateCommand. The timer ID is not retained by the manager. The delayed command is guaranteed to be ready for processing when submitted here.

Parameters
stateCommand

The delayed command to queue.

Returns

True (always accepts commands).

Definition at line 249 of file StateManager.ixx.

249 bool submit(
250 const DelayedStateCommand<StateType> stateCommand
251 ) noexcept {
252 pending_.push_back(StateCommand<StateType>(stateCommand.transitionRequest()));
253 return true;
254 };

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

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

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

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

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

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

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

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

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

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