Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::ecs::details::CmdQueue Class Reference

Command queue for deferred ECS operations. More...

#include <command_queue.hpp>

Public Member Functions

 CmdQueue ()=default
 
 CmdQueue (const CmdQueue &)=delete
 
 CmdQueue (CmdQueue &&) noexcept=default
 
 ~CmdQueue ()=default
 
CmdQueueoperator= (const CmdQueue &)=delete
 
CmdQueueoperator= (CmdQueue &&) noexcept=default
 
void Clear () noexcept
 Clears all pending commands from the queue.
 
void Reserve (size_t capacity)
 Reserves capacity for commands.
 
template<CommandTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void Emplace (Args &&... args)
 Constructs and enqueues a command in-place.
 
void Enqueue (std::unique_ptr< Command > command)
 Enqueues a pre-constructed command.
 
template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, std::unique_ptr<Command>>
void EnqueueBulk (R &&commands)
 Enqueues multiple commands in bulk.
 
auto DequeueAll () noexcept -> std::vector< std::unique_ptr< Command > >
 Gets all commands and clears the queue.
 
bool Empty () const noexcept
 Checks if the queue is empty.
 
size_t Size () const noexcept
 Gets the number of commands in the queue.
 

Detailed Description

Command queue for deferred ECS operations.

Provides a queue for commands that will be executed during World::Update(). Commands are executed in the order they were enqueued, ensuring predictable behavior.

Features:

  • Simple vector-based implementation for main thread usage
  • Bulk operations for better performance
  • Type-safe command emplacement
Note
Not thread-safe. All operations must be performed on the main thread.

Definition at line 32 of file command_queue.hpp.

Constructor & Destructor Documentation

◆ CmdQueue() [1/3]

helios::ecs::details::CmdQueue::CmdQueue ( )
default

◆ CmdQueue() [2/3]

helios::ecs::details::CmdQueue::CmdQueue ( const CmdQueue )
delete

◆ CmdQueue() [3/3]

helios::ecs::details::CmdQueue::CmdQueue ( CmdQueue &&  )
defaultnoexcept

◆ ~CmdQueue()

helios::ecs::details::CmdQueue::~CmdQueue ( )
default

Member Function Documentation

◆ Clear()

void helios::ecs::details::CmdQueue::Clear ( )
inlinenoexcept

Clears all pending commands from the queue.

Removes all commands currently in the queue.

Definition at line 46 of file command_queue.hpp.

46{ commands_.clear(); }

◆ DequeueAll()

auto helios::ecs::details::CmdQueue::DequeueAll ( ) -> std::vector<std::unique_ptr<Command>>
inlinenoexcept

Gets all commands and clears the queue.

Moves all commands out of the queue, leaving it empty.

Returns
Vector containing all commands that were in the queue

Definition at line 93 of file command_queue.hpp.

93 {
94 return std::exchange(commands_, {});
95 }

◆ Emplace()

template<CommandTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void helios::ecs::details::CmdQueue::Emplace ( Args &&...  args)
inline

Constructs and enqueues a command in-place.

Creates a command of type T by forwarding arguments to its constructor.

Template Parameters
TCommand type that derives from Command
ArgsArgument types for T's constructor
Parameters
argsArguments to forward to T's constructor

Definition at line 64 of file command_queue.hpp.

64 {
65 commands_.push_back(std::make_unique<T>(std::forward<Args>(args)...));
66 }
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481

◆ Empty()

bool helios::ecs::details::CmdQueue::Empty ( ) const
inlinenoexcept

Checks if the queue is empty.

Returns true if there are no commands in the queue.

Returns
True if queue is empty, false otherwise

Definition at line 102 of file command_queue.hpp.

102{ return commands_.empty(); }

◆ Enqueue()

void helios::ecs::details::CmdQueue::Enqueue ( std::unique_ptr< Command command)
inline

Enqueues a pre-constructed command.

Adds an already constructed command to the queue.

Warning
Triggers assertion if command is nullptr.
Parameters
commandUnique pointer to command to enqueue

Definition at line 115 of file command_queue.hpp.

115 {
116 HELIOS_ASSERT(command != nullptr, "Failed to enqueue command: command is nullptr!");
117 commands_.push_back(std::move(command));
118}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140

◆ EnqueueBulk()

template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, std::unique_ptr<Command>>
void helios::ecs::details::CmdQueue::EnqueueBulk ( R &&  commands)
inline

Enqueues multiple commands in bulk.

Efficiently enqueues a range of commands in a single operation. The range is consumed (moved from) during this operation.

Warning
Triggers assertion if any command in range is nullptr.
Template Parameters
RRange type containing unique_ptr<Command> elements
Parameters
commandsRange of commands to enqueue (will be moved from)

Definition at line 122 of file command_queue.hpp.

122 {
123 if constexpr (std::ranges::sized_range<R>) {
124 // Reserve space to avoid multiple reallocations
125 commands_.reserve(commands_.size() + std::ranges::size(commands));
126 }
127
128 // Move all commands into the queue
129 for (auto&& command : commands) {
130 HELIOS_ASSERT(command != nullptr, "Failed to enqueue commands in bulk: one of the commands is nullptr!");
131 commands_.push_back(std::move(command));
132 }
133}

◆ operator=() [1/2]

CmdQueue & helios::ecs::details::CmdQueue::operator= ( CmdQueue &&  )
defaultnoexcept

◆ operator=() [2/2]

CmdQueue & helios::ecs::details::CmdQueue::operator= ( const CmdQueue )
delete

◆ Reserve()

void helios::ecs::details::CmdQueue::Reserve ( size_t  capacity)
inline

Reserves capacity for commands.

Pre-allocates memory to avoid reallocations during enqueue operations.

Parameters
capacityNumber of commands to reserve space for

Definition at line 53 of file command_queue.hpp.

53{ commands_.reserve(capacity); }

◆ Size()

size_t helios::ecs::details::CmdQueue::Size ( ) const
inlinenoexcept

Gets the number of commands in the queue.

Returns the exact size of the queue.

Returns
Number of commands in queue

Definition at line 109 of file command_queue.hpp.

109{ return commands_.size(); }