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

154 void handleCommands(OwningT& owner) {
155 (registerHandler<CommandType>(owner), ...);
156 }

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

167 const auto idx = CommandTypeId::id<CommandType>().value();
168
169 if (idx >= entries_.size()) {
170 return false;
171 }
172
173 const auto& entry = entries_[idx];
174 return entry.owner && entry.submitFn;
175 }

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.

info

this method is not threadsafe and should not be called concurrently.

Definition at line 119 of file CommandHandlerRegistry.ixx.

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

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

211 bool submit(CommandType&& cmd) const noexcept {
212 using Cmd = std::remove_cvref_t<CommandType>;
213
214 if (auto handler = tryHandler<Cmd>()) {
215 return handler.submit(std::move(cmd));
216 }
217 return false;
218 }

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

186 const auto idx = CommandTypeId::id<CommandType>().value();
187
188 if (idx >= entries_.size()) {
189 return {};
190 }
191
192 const auto& entry = entries_[idx];
193
194 if (!entry.owner || !entry.submitFn) {
195 return {};
196 }
197
199 }

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.