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::runtime::world::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::messaging::command::CommandHandlerRegistry &commandHandlerRegistry)

Initializes the manager and registers command handlers. 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.

Definition at line 70 of file StateManager.ixx.

Public Member Typedefs

EngineRoleTag

template <typename StateType>
using helios::engine::state::StateManager< StateType >::EngineRoleTag = helios::engine::runtime::world::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 157 of file StateManager.ixx.

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

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

Reference helios::registerComponents.

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

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

References helios::engine::state::types::StateTransitionContext< StateType >::from and helios::registerComponents.

init()

template <typename StateType>
void helios::engine::state::StateManager< StateType >::init (helios::engine::runtime::messaging::command::CommandHandlerRegistry & commandHandlerRegistry)
inline

Initializes the manager and registers command handlers.

Parameters
commandHandlerRegistry

The command-handler registry to register with.

Definition at line 262 of file StateManager.ixx.

263 commandHandlerRegistry.registerHandler<StateCommand<StateType>>(*this);
264 commandHandlerRegistry.registerHandler<DelayedStateCommand<StateType>>(*this);
265 }

Reference helios::engine::runtime::messaging::command::CommandHandlerRegistry::registerHandler.

reset()

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

Clears all pending commands.

Definition at line 270 of file StateManager.ixx.

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

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

230 bool submit(
232 ) noexcept {
233 pending_.push_back(stateCommand);
234 return true;
235 };

Reference helios::registerComponents.

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

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

Reference helios::registerComponents.

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

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

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

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

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

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

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

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

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

85 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.9.8.