Skip to main content

CommandBuffer Class

Queue for deferred execution of game commands. More...

Declaration

class helios::engine::runtime::messaging::command::CommandBuffer { ... }

Public Member Functions Index

template <typename T>
CommandBuffer &addDispatcher (std::unique_ptr< helios::engine::runtime::messaging::command::TypedTargetedCommandDispatcher< T > > d)

Registers a dispatcher for a specific TargetedCommand type. More...

template <typename T>
boolhasDispatcher () const

Checks if a dispatcher is registered for a specific TargetedCommand type. More...

template <typename T>
CommandBuffer &addDispatcher (std::unique_ptr< helios::engine::runtime::messaging::command::TypedWorldCommandDispatcher< T > > d)

Registers a dispatcher for a specific WorldCommand type. More...

template <typename T>
boolhasDispatcher () const

Checks if a dispatcher is registered for a specific WorldCommand type. More...

template <typename T, typename... Args>
voidadd (const helios::engine::ecs::EntityHandle &entityHandle, Args &&...args)

Adds a command targeting a GameObject identified by Guid. More...

template <typename T, typename... Args>
voidadd (Args &&...args)

Adds a world command to the buffer. More...

CommandBuffer &flush (helios::engine::runtime::world::GameWorld &gameWorld)

Executes all buffered commands against the GameWorld and clears the buffer. More...

CommandBuffer &clear ()

Clears all buffered commands without executing them. More...

Private Member Attributes Index

std::deque< TargetedCommandProxy >targetedCommandBuffer_

Internal queue storing buffered commands. More...

std::deque< WorldCommandProxy >worldCommandBuffer_

Internal queue storing buffered world commands. More...

std::unordered_map< std::type_index, std::unique_ptr< helios::engine::runtime::messaging::command::TargetedCommandDispatcher > >targetedCommandDispatchers_

Registry of dispatchers for TargetedCommand types. More...

std::unordered_map< std::type_index, std::unique_ptr< helios::engine::runtime::messaging::command::WorldCommandDispatcher > >worldCommandDispatchers_

Registry of dispatchers for WorldCommand types. More...

Private Static Attributes Index

static const helios::util::log::Logger &logger_ = ...

The logger used with this CommandBuffer instance. More...

Description

Queue for deferred execution of game commands.

The `CommandBuffer` provides a queue for game commands, decoupling their creation from their execution. Commands are buffered along with their target `GameObject` identifiers (`Guid`) and are executed in a single batch when `flush()` is called against a `GameWorld`.

Typical usage pattern: ```cpp helios::engine::runtime::messaging::command::CommandBuffer cmdBuffer; helios::engine::runtime::world::GameWorld world;

// Queue commands during input processing cmdBuffer.add(player.guid(), std::make_unique<MoveCommand>(direction, speed)); cmdBuffer.add(enemy.guid(), std::make_unique<AttackCommand>(target));

// Execute all commands at end of frame cmdBuffer.flush(world); // Executes and clears buffer ```

info

Commands targeting non-existent GameObjects are silently skipped during flush(). This behavior will be logged once the logging system is available.

Definition at line 61 of file CommandBuffer.ixx.

Public Member Functions

add()

template <typename T, typename... Args>
void helios::engine::runtime::messaging::command::CommandBuffer::add (const helios::engine::ecs::EntityHandle & entityHandle, Args &&... args)
inline

Adds a command targeting a GameObject identified by Guid.

Parameters
guid

The unique identifier of the target GameObject.

command

The command to execute. Ownership is transferred to the buffer.

Precondition

command must not be nullptr.

info

If command is nullptr, the call is silently ignored (will be logged in future).

info

This overload is useful when the GameObject instance is not directly available, such as in networked scenarios or when processing serialized commands.

Definition at line 218 of file CommandBuffer.ixx.

218 void add(const helios::engine::ecs::EntityHandle& entityHandle, Args&& ...args) {
219
220 const std::type_index key{ typeid(T) };
221 auto cmd = std::make_unique<T>(std::forward<Args>(args)...);
222
223 targetedCommandBuffer_.push_back(TargetedCommandProxy{key, entityHandle, std::move(cmd)});
224 }

add()

template <typename T, typename... Args>
void helios::engine::runtime::messaging::command::CommandBuffer::add (Args &&... args)
inline

Adds a world command to the buffer.

Template Parameters
T

The concrete WorldCommand type to create.

Args

Constructor argument types for T.

Parameters
args

Arguments forwarded to the T constructor.

WorldCommands operate on the entire GameWorld rather than a specific GameObject. They are executed before TargetedCommands during flush().

Definition at line 239 of file CommandBuffer.ixx.

239 void add(Args&& ...args) {
240
241 const std::type_index key{ typeid(T) };
242 auto cmd = std::make_unique<T>(std::forward<Args>(args)...);
243
244 worldCommandBuffer_.push_back(WorldCommandProxy{key, std::move(cmd)});
245 }

addDispatcher()

template <typename T>
CommandBuffer & helios::engine::runtime::messaging::command::CommandBuffer::addDispatcher (std::unique_ptr< helios::engine::runtime::messaging::command::TypedTargetedCommandDispatcher< T > > d)
inline

Registers a dispatcher for a specific TargetedCommand type.

When flush() processes a command of type T, it will route the command through this dispatcher instead of calling execute() directly.

Template Parameters
T

The concrete TargetedCommand type to dispatch.

Parameters
d

The dispatcher instance to register. Ownership is transferred.

Precondition

No dispatcher for type T must already be registered.

Returns

A reference to **this** CommandBuffer.

Definition at line 138 of file CommandBuffer.ixx.

139
140 assert(!hasDispatcher<T>() && "Dispatcher already added");
141
142 const std::type_index key{ typeid(T) };
143 targetedCommandDispatchers_[key] = std::move(d);
144
145 return *this;
146 }

Reference hasDispatcher.

addDispatcher()

template <typename T>
CommandBuffer & helios::engine::runtime::messaging::command::CommandBuffer::addDispatcher (std::unique_ptr< helios::engine::runtime::messaging::command::TypedWorldCommandDispatcher< T > > d)
inline

Registers a dispatcher for a specific WorldCommand type.

When flush() processes a command of type T, it will route the command through this dispatcher instead of calling execute() directly.

Template Parameters
T

The concrete WorldCommand type to dispatch.

Parameters
d

The dispatcher instance to register. Ownership is transferred.

Precondition

No dispatcher for type T must already be registered.

Returns

A reference to **this** CommandBuffer.

Definition at line 179 of file CommandBuffer.ixx.

180
181 assert(!hasDispatcher<T>() && "Dispatcher already added");
182
183 const std::type_index key{ typeid(T) };
184 worldCommandDispatchers_[key] = std::move(d);
185
186 return *this;
187 }

Reference hasDispatcher.

clear()

CommandBuffer & helios::engine::runtime::messaging::command::CommandBuffer::clear ()
inline

Clears all buffered commands without executing them.

Returns

Reference to this CommandBuffer for method chaining.

info

This destroys all buffered commands. Use this to discard commands without executing them (e.g., when reverting to a previous game state).

Definition at line 314 of file CommandBuffer.ixx.

315 targetedCommandBuffer_.clear();
316 worldCommandBuffer_.clear();
317 return *this;
318 }

Reference clear.

Referenced by clear and flush.

flush()

CommandBuffer & helios::engine::runtime::messaging::command::CommandBuffer::flush (helios::engine::runtime::world::GameWorld & gameWorld)
inline

Executes all buffered commands against the GameWorld and clears the buffer.

Parameters
gameWorld

The world containing target GameObjects.

Returns

Reference to this CommandBuffer for method chaining.

Execution proceeds in two phases: 1. **WorldCommands** are processed first. For each command, if a dispatcher is registered for its type, the command is routed via accept(). Otherwise, execute() is called directly. 2. **TargetedCommands** are processed second. Each command's target GameObject is resolved by Guid. If found and a dispatcher exists, the command is routed via accept(). Otherwise, execute() is called directly.

info

Commands targeting non-existent GameObjects (i.e., find() returns nullptr) are skipped and a warning is logged.

info

All commands are cleared after execution, regardless of success or failure.

warning

Command execution should be noexcept. If execute() throws, the flush operation aborts and remaining commands are not executed.

Definition at line 269 of file CommandBuffer.ixx.

270
271 // World commands are processed first
272 for (auto& worldCommandProxy : worldCommandBuffer_) {
273
274 auto it = worldCommandDispatchers_.find(worldCommandProxy.typeIdx);
275
276 if (it != worldCommandDispatchers_.end()) {
277 worldCommandProxy.worldCommand->accept(gameWorld, *(it->second));
278 } else {
279 worldCommandProxy.worldCommand->execute(gameWorld);
280 }
281 }
282
283 // Targeted commands are processed second
284 for (auto& targetedCommandProxy : targetedCommandBuffer_) {
285
286 auto gameObject = gameWorld.find(targetedCommandProxy.entityHandle);
287
288 if (!gameObject) {
289 logger_.warn("GameObject with entityHandle not found, skipping command");
290 continue;
291 }
292
293 auto it = targetedCommandDispatchers_.find(targetedCommandProxy.typeIdx);
294
295 if (it != targetedCommandDispatchers_.end()) {
296 targetedCommandProxy.targetedCommand->accept(*gameObject, *(it->second));
297 } else {
298 targetedCommandProxy.targetedCommand->execute(*gameObject);
299 }
300 }
301
302 clear();
303 return *this;
304 }

References clear and helios::engine::runtime::world::GameWorld::find.

hasDispatcher()

template <typename T>
bool helios::engine::runtime::messaging::command::CommandBuffer::hasDispatcher ()
inline nodiscard

Checks if a dispatcher is registered for a specific TargetedCommand type.

Template Parameters
T

The concrete TargetedCommand type to check.

Returns

true if a dispatcher is registered for type T, false otherwise.

Definition at line 157 of file CommandBuffer.ixx.

157 [[nodiscard]] bool hasDispatcher() const {
158 auto it = targetedCommandDispatchers_.find(typeid(T));
159
160 return it != targetedCommandDispatchers_.end();
161 }

Referenced by addDispatcher and addDispatcher.

hasDispatcher()

template <typename T>
bool helios::engine::runtime::messaging::command::CommandBuffer::hasDispatcher ()
inline nodiscard

Checks if a dispatcher is registered for a specific WorldCommand type.

Template Parameters
T

The concrete WorldCommand type to check.

Returns

true if a dispatcher is registered for type T, false otherwise.

Definition at line 198 of file CommandBuffer.ixx.

198 [[nodiscard]] bool hasDispatcher() const {
199 auto it = worldCommandDispatchers_.find(typeid(T));
200
201 return it != worldCommandDispatchers_.end();
202 }

Private Member Attributes

targetedCommandBuffer_

std::deque<TargetedCommandProxy> helios::engine::runtime::messaging::command::CommandBuffer::targetedCommandBuffer_

Internal queue storing buffered commands.

Uses std::deque for efficient push_back and iteration.

Definition at line 85 of file CommandBuffer.ixx.

85 std::deque<TargetedCommandProxy> targetedCommandBuffer_;

targetedCommandDispatchers_

std::unordered_map<std::type_index, std::unique_ptr<helios::engine::runtime::messaging::command::TargetedCommandDispatcher> > helios::engine::runtime::messaging::command::CommandBuffer::targetedCommandDispatchers_

Registry of dispatchers for TargetedCommand types.

Maps command type indices to their corresponding dispatchers. When a command is flushed and a dispatcher is registered for its type, the command is routed through the dispatcher instead of executing directly.

Definition at line 109 of file CommandBuffer.ixx.

109 std::unordered_map<std::type_index, std::unique_ptr<helios::engine::runtime::messaging::command::TargetedCommandDispatcher>> targetedCommandDispatchers_;

worldCommandBuffer_

std::deque<WorldCommandProxy> helios::engine::runtime::messaging::command::CommandBuffer::worldCommandBuffer_

Internal queue storing buffered world commands.

Uses std::deque for efficient push_back and iteration.

Definition at line 92 of file CommandBuffer.ixx.

92 std::deque<WorldCommandProxy> worldCommandBuffer_;

worldCommandDispatchers_

std::unordered_map<std::type_index, std::unique_ptr<helios::engine::runtime::messaging::command::WorldCommandDispatcher> > helios::engine::runtime::messaging::command::CommandBuffer::worldCommandDispatchers_

Registry of dispatchers for WorldCommand types.

Maps command type indices to their corresponding dispatchers. When a command is flushed and a dispatcher is registered for its type, the command is routed through the dispatcher instead of executing directly.

Definition at line 118 of file CommandBuffer.ixx.

118 std::unordered_map<std::type_index, std::unique_ptr<helios::engine::runtime::messaging::command::WorldCommandDispatcher>> worldCommandDispatchers_;

Private Static Attributes

logger_

const helios::util::log::Logger& helios::engine::runtime::messaging::command::CommandBuffer::logger_
static

The logger used with this CommandBuffer instance.

Initialiser

Defaults to HELIOS_LOG_SCOPE.

Definition at line 99 of file CommandBuffer.ixx.


The documentation for this class was generated from the following file:


Generated via doxygen2docusaurus 2.0.0 by Doxygen 1.15.0.