Skip to main content

EntityMutationCommandBuffer.ixx File

Deferred command buffer for structural entity mutations (add/remove components, activate/deactivate). More...

Included Headers

Namespaces Index

namespacehelios
namespaceengine
namespaceruntime
namespacemessaging
namespacecommand

Classes Index

classEntityMutationCommandBuffer<TEntityManager>

Collects deferred entity-mutation commands and dispatches them in bulk. More...

classInternalBuffer<TCommandType>

Per-command-type storage and dispatch unit. More...

Description

Deferred command buffer for structural entity mutations (add/remove components, activate/deactivate).

File Listing

The file content with the documentation metadata removed is:

1
5module;
6
7#include <memory>
8#include <vector>
9#include <cassert>
10
11
13
14export module helios.engine.runtime.messaging.command.EntityMutationCommandBuffer;
15
16import helios.engine.runtime.world.ManagerRegistry;
17import helios.engine.runtime.world.EntityMutationManager;
18import helios.engine.runtime.messaging.command.CommandBuffer;
19import helios.engine.runtime.messaging.command.types.CommandBufferTypeId;
20import helios.engine.runtime.messaging.command.CommandBufferRegistry;
21
22import helios.engine.runtime.messaging.command.CommandHandlerRegistry;
23import helios.engine.runtime.timing.TimerManager;
24
25import helios.engine.runtime.world.UpdateContext;
26import helios.engine.runtime.messaging.command.tags;
27import helios.engine.runtime.messaging.command.TypedCommandBuffer;
28import helios.ecs.commands;
29
35
48 template<typename TEntityManager>
50
52 CommandBufferRegistry commandBufferRegistry_{};
53
55 CommandHandlerRegistry* commandHandlerRegistry_{nullptr};
56
58 ManagerRegistry* managerRegistry_{nullptr};
59
69 template<typename TCommandType>
70 class InternalBuffer {
71
73 CommandHandlerRegistry* commandHandlerRegistry_{nullptr};
74
76 ManagerRegistry* managerRegistry_{nullptr};
77
79 bool handlerRegistered_{false};
80
82 EntityMutationManager<TEntityManager>* entityMutationManager_{nullptr};
83
84 std::vector<TCommandType> commands_;
85
86 public:
89
91 InternalBuffer() {
93 }
94
102 void flush(UpdateContext& updateContext) {
103
104 if (!handlerRegistered_) {
105 auto* entityMutationManager_ = managerRegistry_->item<EntityMutationManager<TEntityManager>>();
106 assert(entityMutationManager_ && "EntityMutationManager for the specified handle is not initialized.");
107 commandHandlerRegistry_->handleCommands<TCommandType>(*entityMutationManager_);
108 handlerRegistered_ = true;
109 }
110
111 for (auto& cmd : commands_) {
112 commandHandlerRegistry_->submit<TCommandType>(std::move(cmd));
113 }
114
115 clear();
116 }
117
119 void clear() {
120 commands_.clear();
121 }
122
129 void init(CommandHandlerRegistry& commandHandlerRegistry, ManagerRegistry& managerRegistry) {
130 commandHandlerRegistry_ = &commandHandlerRegistry;
131 managerRegistry_ = &managerRegistry;
132 }
133
140 template<typename ... TArgs>
141 void add(TArgs&&... args) {
142 commands_.emplace_back(std::forward<TArgs>(args)...);
143 }
144
145 };
146
153 template<typename TCommand>
154 auto* modelFor() {
155
156 auto* model = commandBufferRegistry_.item<InternalBuffer<TCommand>>();
157
158 if (!model) {
159 auto& created =
160 commandBufferRegistry_.add<InternalBuffer<TCommand>>(
162 );
163
164 assert(commandHandlerRegistry_
165 && managerRegistry_
166 && "CommandHandlerRegistry and ManagerRegistry must be initialized before adding commands.");
167 created.init(*commandHandlerRegistry_,*managerRegistry_);
168
169 return &created;
170 }
171
172 return model;
173 }
174
175 public:
176
178 using Handle_type = typename TEntityManager::Handle_type;
179
182
192 template<typename TCommand, typename... Args>
193 requires std::is_same_v<typename TCommand::Handle_type, Handle_type>
194 void add(Args&&... args) {
195
196 auto* model = modelFor<TCommand>();
197
198 model->add(std::forward<Args>(args)...);
199 }
200
209 template<typename TCommand>
211 return modelFor<TCommand>();
212 }
213
222 void init(CommandHandlerRegistry& commandHandlerRegistry, ManagerRegistry& managerRegistry) {
223
224 commandHandlerRegistry_ = &commandHandlerRegistry;
225 managerRegistry_ = &managerRegistry;
226
227 for (auto* buffer : commandBufferRegistry_.items()) {
228 buffer->init(commandHandlerRegistry, managerRegistry);
229 }
230 }
231
238 for (auto* buffer : commandBufferRegistry_.items()) {
239 buffer->flush(updateContext);
240 }
241 }
242
246 void clear() {
247 for (auto* buffer : commandBufferRegistry_.items()) {
248 buffer->clear();
249 }
250 }
251
252 };
253
254}

Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.9.8.