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)
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 (const CommandType &cmd) const noexcept

Directly submits a command to 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(const 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 89 of file CommandHandlerRegistry.ixx.

Public Member Functions

handleCommands()

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

Definition at line 136 of file CommandHandlerRegistry.ixx.

136 void handleCommands(OwningT& owner) {
137 (registerHandler<CommandType>(owner), ...);
138 }

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 148 of file CommandHandlerRegistry.ixx.

149 const auto idx = CommandTypeId::id<CommandType>().value();
150
151 if (idx >= entries_.size()) {
152 return false;
153 }
154
155 const auto& entry = entries_[idx];
156 return entry.owner && entry.submitFn;
157 }

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(const 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 114 of file CommandHandlerRegistry.ixx.

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

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 (const CommandType & cmd)
inline noexcept

Directly submits a command to its registered handler.

Template Parameters
CommandType

The command type.

Parameters
cmd

The command instance.

Returns

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

Definition at line 193 of file CommandHandlerRegistry.ixx.

193 bool submit(const CommandType& cmd) const noexcept {
194 if (auto handler = tryHandler<CommandType>()) {
195 return handler.submit(cmd);
196 }
197 return false;
198 }

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 167 of file CommandHandlerRegistry.ixx.

168 const auto idx = CommandTypeId::id<CommandType>().value();
169
170 if (idx >= entries_.size()) {
171 return {};
172 }
173
174 const auto& entry = entries_[idx];
175
176 if (!entry.owner || !entry.submitFn) {
177 return {};
178 }
179
181 }

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

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