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

has()

template <typename CommandType>
bool helios::engine::runtime::messaging::command::CommandHandlerRegistry::has ()
inline nodiscard 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 143 of file CommandHandlerRegistry.ixx.

143 [[nodiscard]] bool has() const noexcept {
144 const auto idx = CommandTypeId::id<CommandType>().value();
145
146 if (idx >= entries_.size()) {
147 return false;
148 }
149
150 const auto& entry = entries_[idx];
151 return entry.owner && entry.submitFn;
152 }

References helios::engine::runtime::messaging::command::types::CommandTypeId::id and helios::engine::runtime::messaging::command::types::CommandTypeId::value.

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::messaging::command::types::CommandTypeId::id, submit and helios::engine::runtime::messaging::command::types::CommandTypeId::value.

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

188 bool submit(const CommandType& cmd) const noexcept {
189 if (auto handler = tryHandler<CommandType>()) {
190 return handler.submit(cmd);
191 }
192 return false;
193 }

Reference tryHandler.

Referenced by registerHandler.

tryHandler()

template <typename CommandType>
CommandHandlerRef< CommandType > helios::engine::runtime::messaging::command::CommandHandlerRegistry::tryHandler ()
inline nodiscard 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 162 of file CommandHandlerRegistry.ixx.

162 [[nodiscard]] CommandHandlerRef<CommandType> tryHandler() const noexcept {
163 const auto idx = CommandTypeId::id<CommandType>().value();
164
165 if (idx >= entries_.size()) {
166 return {};
167 }
168
169 const auto& entry = entries_[idx];
170
171 if (!entry.owner || !entry.submitFn) {
172 return {};
173 }
174
175 return CommandHandlerRef<CommandType>{ entry.owner, entry.submitFn };
176 }

References helios::engine::runtime::messaging::command::types::CommandTypeId::id and helios::engine::runtime::messaging::command::types::CommandTypeId::value.

Referenced by submit.

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