Skip to main content

CommandHandlerRegistry.ixx File

Registry for mapping command types to their handlers. More...

Included Headers

#include <concepts> #include <vector> #include <cassert> #include <helios.engine.runtime.messaging.command.types> #include <helios.ecs.types.ComponentTypeId>

Namespaces Index

namespacehelios
namespaceengine
namespaceruntime
namespacemessaging
namespacecommand

Classes Index

structCommandHandlerEntry

Type-erased storage entry for a registered command handler. More...

structCommandHandlerRef<CommandType>

Typed reference wrapper for invoking a registered handler. More...

classCommandHandlerRegistry

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

Description

Registry for mapping command types to their handlers.

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <concepts>
8#include <vector>
9#include <cassert>
10
11export module helios.engine.runtime.messaging.command.CommandHandlerRegistry;
12
13import helios.ecs.types.ComponentTypeId;
14import helios.engine.runtime.messaging.command.types;
15
16using namespace helios::ecs::types;
18
19
21
29 void* owner = nullptr;
30
34 bool (*submitFn)(void*, const void*) noexcept = nullptr;
35 };
36
42 template<typename CommandType>
44
48 void* owner = nullptr;
49
53 bool (*submitFn)(void*, const void*) noexcept = nullptr;
54
60 [[nodiscard]] explicit operator bool() const noexcept {
61 return owner && submitFn;
62 }
63
70 bool submit(const CommandType& cmd) const noexcept {
71 return submitFn(owner, &cmd);
72 }
73
74 };
75
90
94 std::vector<CommandHandlerEntry> entries_;
95
96 public:
97
113 template<typename CommandType, typename OwningT>
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 }
134
135 template<typename... CommandType, typename OwningT>
136 void handleCommands(OwningT& owner) {
137 (registerHandler<CommandType>(owner), ...);
138 }
139
147 template<typename CommandType>
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 }
158
166 template<typename CommandType>
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 }
182
192 template<typename CommandType>
193 bool submit(const CommandType& cmd) const noexcept {
194 if (auto handler = tryHandler<CommandType>()) {
195 return handler.submit(cmd);
196 }
197 return false;
198 }
199
200 };
201
202} // namespace

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.