Skip to main content

CommandHandlerRegistry Class

Registry that maps CommandType types to handler instances via function pointers. More...

Declaration

class helios::engine::runtime::messaging::command::CommandHandlerRegistry { ... }

Public Member Functions Index

template <typename CommandType, typename OwningT>
voidregisterHandler (OwningT &owner)

Registers an object as the handler for a specific command type. More...

template <typename... CommandType, typename OwningT>
voidhandleCommands (OwningT &owner)

Registers one owner as handler for multiple command types. More...

template <typename CommandType>
boolhas () const noexcept

Checks if a handler is registered for the specified command type. More...

template <typename CommandType>
auto tryHandler () const noexcept -> CommandHandlerRef< CommandType >

Retrieves a typed reference to the registered handler. More...

template <typename CommandType>
boolsubmit (CommandType &&cmd) const noexcept

Directly submits and consumes a command via its registered handler. More...

Private Member Attributes Index

std::vector< CommandHandlerEntry >entries_

Dense vector of handler entries, indexed by CommandTypeId value. More...

Description

Registry that maps CommandType types to handler instances via function pointers.

The CommandHandlerRegistry provides a mechanism to decouple command producers (systems) from command consumers (managers). Handlers are registered by reference, and the registry stores a type-erased "trampoline" function that allows invoking the handler's submit(Cmd&&) method without knowing the concrete handler type at the call site.

This avoids virtual inheritance (TypedCommandHandler) and allows any class with a matching submit signature to act as a handler.

Lookup is O(1) based on the CommandTypeId.

Definition at line 92 of file CommandHandlerRegistry.ixx.

Public Member Functions

handleCommands()

template <typename... CommandType, typename OwningT>
void helios::engine::runtime::messaging::command::CommandHandlerRegistry::handleCommands (OwningT & owner)
inline

Registers one owner as handler for multiple command types.

Template Parameters
CommandType

Command types to register.

OwningT

Concrete owner type implementing matching submit(...) overloads.

Parameters
owner

Handler owner instance.

Definition at line 148 of file CommandHandlerRegistry.ixx.

148 void handleCommands(OwningT& owner) {
149 (registerHandler<CommandType>(owner), ...);
150 }

Reference helios::engine::runtime::registerComponents.

has()

template <typename CommandType>
bool helios::engine::runtime::messaging::command::CommandHandlerRegistry::has ()
inline noexcept

Checks if a handler is registered for the specified command type.

Template Parameters
CommandType

The command type to check.

Returns

True if a valid handler exists.

Definition at line 160 of file CommandHandlerRegistry.ixx.

161 const auto idx = CommandTypeId::id<CommandType>().value();
162
163 if (idx >= entries_.size()) {
164 return false;
165 }
166
167 const auto& entry = entries_[idx];
168 return entry.owner && entry.submitFn;
169 }

Reference helios::engine::runtime::registerComponents.

registerHandler()

template <typename CommandType, typename OwningT>
void helios::engine::runtime::messaging::command::CommandHandlerRegistry::registerHandler (OwningT & owner)
inline

Registers an object as the handler for a specific command type.

The handler object must provide a method: bool submit(CommandType&&) noexcept. Upon registration, a static lambda (trampoline) is generated to handle type erasure and casting.

Template Parameters
CommandType

The command type to handle.

OwningT

The concrete type of the handler object.

Parameters
owner

Reference to the handler instance. Must outlive the registry (usually Owned by GameWorld/ResourceRegistry).

Precondition

No handler is currently registered for this command type.

Definition at line 117 of file CommandHandlerRegistry.ixx.

117 void registerHandler(OwningT& owner) {
118 static_assert(requires(OwningT& x, CommandType&& c) {
119 { x.submit(std::move(c)) } noexcept -> std::same_as<bool>;
120 });
121
122 const auto idx = CommandTypeId::id<CommandType>().value();
123
124 if (entries_.size() <= idx) {
125 entries_.resize(idx + 1);
126 }
127
128 assert(entries_[idx].owner == nullptr && "Handler already registered for this command type");
129
130 entries_[idx] = CommandHandlerEntry{
131 &owner,
132 +[](void* owner, void* cmd) noexcept -> bool {
133 return static_cast<OwningT*>(owner)->submit(
134 std::move(*static_cast<CommandType*>(cmd))
135 );
136 }
137 };
138 }

References helios::engine::runtime::registerComponents and submit.

Referenced by helios::engine::runtime::lifecycle::WorldLifecycleManager::init, helios::engine::runtime::timing::TimerManager::init and helios::engine::state::StateManager< StateType >::init.

submit()

template <typename CommandType>
bool helios::engine::runtime::messaging::command::CommandHandlerRegistry::submit (CommandType && cmd)
inline noexcept

Directly submits and consumes a command via its registered handler.

Template Parameters
CommandType

The command type.

Parameters
cmd

The command instance to forward.

Returns

True if a handler was found and it returned true; false otherwise.

Definition at line 205 of file CommandHandlerRegistry.ixx.

205 bool submit(CommandType&& cmd) const noexcept {
206 using Cmd = std::remove_cvref_t<CommandType>;
207
208 if (auto handler = tryHandler<Cmd>()) {
209 return handler.submit(std::move(cmd));
210 }
211 return false;
212 }

Reference helios::engine::runtime::registerComponents.

Referenced by registerHandler.

tryHandler()

template <typename CommandType>
CommandHandlerRef< CommandType > helios::engine::runtime::messaging::command::CommandHandlerRegistry::tryHandler ()
inline noexcept

Retrieves a typed reference to the registered handler.

Template Parameters
CommandType

The command type.

Returns

A CommandHandlerRef wrapper. Can be checked for validity via operator bool().

Definition at line 179 of file CommandHandlerRegistry.ixx.

180 const auto idx = CommandTypeId::id<CommandType>().value();
181
182 if (idx >= entries_.size()) {
183 return {};
184 }
185
186 const auto& entry = entries_[idx];
187
188 if (!entry.owner || !entry.submitFn) {
189 return {};
190 }
191
193 }

References helios::engine::runtime::messaging::command::CommandHandlerRef< CommandType >::owner and helios::engine::runtime::registerComponents.

Private Member Attributes

entries_

std::vector<CommandHandlerEntry> helios::engine::runtime::messaging::command::CommandHandlerRegistry::entries_

Dense vector of handler entries, indexed by CommandTypeId value.

Definition at line 97 of file CommandHandlerRegistry.ixx.

97 std::vector<CommandHandlerEntry> entries_;

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


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.