Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::ecs::EntityCmdBuffer< Allocator > Class Template Reference

#include <entity_command_buffer.hpp>

Public Types

using allocator_type = Allocator
 

Public Member Functions

 EntityCmdBuffer (World &world, details::SystemLocalStorage &local_storage, Allocator alloc=Allocator{})
 Creates command buffer for a new reserved entity.
 
 EntityCmdBuffer (Entity entity, details::SystemLocalStorage &local_storage, Allocator alloc=Allocator{})
 Creates command buffer for entity operations on existing entity.
 
 EntityCmdBuffer (const EntityCmdBuffer &)=delete
 
 EntityCmdBuffer (EntityCmdBuffer &&) noexcept=default
 
 ~EntityCmdBuffer () noexcept
 Destructor that automatically flushes pending commands.
 
EntityCmdBufferoperator= (const EntityCmdBuffer &)=delete
 
EntityCmdBufferoperator= (EntityCmdBuffer &&) noexcept=default
 
void Flush () noexcept
 Flushes all pending commands to the system local storage.
 
void Destroy ()
 Destroys entity and removes it from the world.
 
void TryDestroy ()
 Tries to destroy entity if it exists.
 
template<ComponentTrait T>
void AddComponent (T &&component)
 Adds component to the entity.
 
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void AddComponents (Ts &&... components)
 Adds components to the entity.
 
template<ComponentTrait T>
void TryAddComponent (T &&component)
 Tries to add a component to the entity.
 
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void TryAddComponents (Ts &&... components)
 Tries to add multiple components to the entity.
 
template<ComponentTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void EmplaceComponent (Args &&... args)
 Emplaces and adds component to the entity.
 
template<ComponentTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void TryEmplaceComponent (Args &&... args)
 Tries to emplace and add component to the entity.
 
template<ComponentTrait T>
void RemoveComponent ()
 Removes component from the entity.
 
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void RemoveComponents ()
 Removes components from the entity.
 
template<ComponentTrait T>
void TryRemoveComponent ()
 Tries to remove a component from the entity.
 
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void TryRemoveComponents ()
 Tries to remove multiple components from the entity (idempotent).
 
void ClearComponents ()
 Removes all components from the entity.
 
bool Empty () const noexcept
 Checks if there are no pending commands.
 
size_t Size () const noexcept
 Gets the number of pending commands.
 
Entity GetEntity () const noexcept
 Gets the entity this command buffer operates on.
 
allocator_type get_allocator () const noexcept
 Gets the allocator used by this buffer.
 

Static Public Member Functions

static EntityCmdBuffer FromWorld (World &world, details::SystemLocalStorage &local_storage, Allocator alloc=Allocator{})
 
static EntityCmdBuffer FromEntity (Entity entity, details::SystemLocalStorage &local_storage, Allocator alloc=Allocator{})
 

Detailed Description

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
class helios::ecs::EntityCmdBuffer< Allocator >

Definition at line 48 of file entity_command_buffer.hpp.

Member Typedef Documentation

◆ allocator_type

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
using helios::ecs::EntityCmdBuffer< Allocator >::allocator_type = Allocator

Constructor & Destructor Documentation

◆ EntityCmdBuffer() [1/4]

template<typename Allocator >
helios::ecs::EntityCmdBuffer< Allocator >::EntityCmdBuffer ( World world,
details::SystemLocalStorage local_storage,
Allocator  alloc = Allocator{} 
)
inline

Creates command buffer for a new reserved entity.

Reserves an entity ID and creates a command buffer for operations on it. The entity will be created when commands are flushed during World::Update().

Parameters
worldWorld to reserve entity in
local_storageReference to system local storage
allocAllocator instance for internal storage
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 335 of file entity_command_buffer.hpp.

337 : entity_(world.ReserveEntity()), local_storage_(local_storage), commands_(alloc), alloc_(alloc) {}
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481

◆ EntityCmdBuffer() [2/4]

template<typename Allocator >
helios::ecs::EntityCmdBuffer< Allocator >::EntityCmdBuffer ( Entity  entity,
details::SystemLocalStorage local_storage,
Allocator  alloc = Allocator{} 
)
inline

Creates command buffer for entity operations on existing entity.

Creates command buffer for operations on the specified entity.

Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity to operate on
local_storageReference to system local storage
allocAllocator instance for internal storage

Definition at line 340 of file entity_command_buffer.hpp.

342 : entity_(entity), local_storage_(local_storage), commands_(alloc), alloc_(alloc) {
343 HELIOS_ASSERT(entity.Valid(), "Failed to construct entity command buffer: Entity is invalid!");
344 // Note: We don't check world.Exists(entity) here because this constructor is also used
345 // for reserved entities (via ReserveEntity()), which don't exist in the world yet.
346 // The entity will be created when commands are flushed during World::Update().
347}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140

◆ EntityCmdBuffer() [3/4]

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
helios::ecs::EntityCmdBuffer< Allocator >::EntityCmdBuffer ( const EntityCmdBuffer< Allocator > &  )
delete

◆ EntityCmdBuffer() [4/4]

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
helios::ecs::EntityCmdBuffer< Allocator >::EntityCmdBuffer ( EntityCmdBuffer< Allocator > &&  )
defaultnoexcept

◆ ~EntityCmdBuffer()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
helios::ecs::EntityCmdBuffer< Allocator >::~EntityCmdBuffer ( )
inlinenoexcept

Destructor that automatically flushes pending commands.

Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 114 of file entity_command_buffer.hpp.

114{ Flush(); }
void Flush() noexcept
Flushes all pending commands to the system local storage.

Member Function Documentation

◆ AddComponent()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
template<ComponentTrait T>
void helios::ecs::EntityCmdBuffer< Allocator >::AddComponent ( T &&  component)
inline

Adds component to the entity.

Queues command to add component to entity. If entity already has this component type, it will be replaced during command execution.

Warning
Triggers assertion during command execution if entity does not exist.
Template Parameters
TComponent type to add
Parameters
componentComponent to add
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 151 of file entity_command_buffer.hpp.

151 {
152 commands_.push_back(
153 std::make_unique<details::AddComponentCmd<std::remove_cvref_t<T>>>(entity_, std::forward<T>(component)));
154 }

◆ AddComponents()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void helios::ecs::EntityCmdBuffer< Allocator >::AddComponents ( Ts &&...  components)
inline

Adds components to the entity.

Queues command to add all specified components to entity in a single operation. If entity already has this component type, it will be replaced during command execution. More efficient than multiple AddComponent calls.

Warning
Triggers assertion during command execution if entity does not exist.
Template Parameters
TsComponent types to add (must be unique)
Parameters
componentsComponents to add
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 167 of file entity_command_buffer.hpp.

167 {
168 commands_.push_back(std::make_unique<details::AddComponentsCmd<std::remove_cvref_t<Ts>...>>(
169 entity_, std::forward<Ts>(components)...));
170 }

◆ ClearComponents()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
void helios::ecs::EntityCmdBuffer< Allocator >::ClearComponents ( )
inline

Removes all components from the entity.

Queues command to remove all components from entity, leaving it with no components. The entity itself will remain valid unless explicitly destroyed.

Warning
Triggers assertion during command execution if entity does not exist.
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 289 of file entity_command_buffer.hpp.

289{ commands_.push_back(std::make_unique<details::ClearComponentsCmd>(entity_)); }

◆ Destroy()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
void helios::ecs::EntityCmdBuffer< Allocator >::Destroy ( )
inline

Destroys entity and removes it from the world.

Queues command to destroy the entity and remove all its components.

Warning
Triggers assertion during command execution if entity does not exist. After execution of this command, the entity will become invalid.
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 132 of file entity_command_buffer.hpp.

132{ commands_.push_back(std::make_unique<details::DestroyEntityCmd>(entity_)); }

◆ EmplaceComponent()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
template<ComponentTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void helios::ecs::EntityCmdBuffer< Allocator >::EmplaceComponent ( Args &&...  args)
inline

Emplaces and adds component to the entity.

Queues command that will construct and add the component to the entity.

Warning
Triggers assertion during command execution if entity does not exist.
Template Parameters
TComponent type to construct and add
ArgsArgument types for T's constructor
Parameters
argsArguments perfectly forwarded to the component constructor
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 212 of file entity_command_buffer.hpp.

212 {
213 commands_.push_back(std::make_unique<details::AddComponentCmd<T>>(entity_, T{std::forward<Args>(args)...}));
214 }

◆ Empty()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
bool helios::ecs::EntityCmdBuffer< Allocator >::Empty ( ) const
inlinenoexcept

Checks if there are no pending commands.

Returns
True if no commands are buffered, false otherwise
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 295 of file entity_command_buffer.hpp.

295{ return commands_.empty(); }

◆ Flush()

template<typename Allocator >
void helios::ecs::EntityCmdBuffer< Allocator >::Flush ( )
inlinenoexcept

Flushes all pending commands to the system local storage.

Moves all locally buffered commands to SystemLocalStorage for later execution. Called automatically by destructor. Safe to call multiple times.

Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 350 of file entity_command_buffer.hpp.

350 {
351 for (auto& cmd : commands_) {
352 if (cmd != nullptr) {
353 local_storage_.AddCommand(std::move(cmd));
354 }
355 }
356 commands_.clear();
357}
void AddCommand(std::unique_ptr< Command > command)
Adds a pre-constructed command to the local buffer.

◆ FromEntity()

template<typename Allocator >
auto helios::ecs::EntityCmdBuffer< Allocator >::FromEntity ( Entity  entity,
details::SystemLocalStorage local_storage,
Allocator  alloc = Allocator{} 
)
inlinestatic
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 329 of file entity_command_buffer.hpp.

330 {
331 return EntityCmdBuffer(entity, local_storage, std::move(alloc));
332}
EntityCmdBuffer(World &world, details::SystemLocalStorage &local_storage, Allocator alloc=Allocator{})
Creates command buffer for a new reserved entity.

◆ FromWorld()

template<typename Allocator >
auto helios::ecs::EntityCmdBuffer< Allocator >::FromWorld ( World world,
details::SystemLocalStorage local_storage,
Allocator  alloc = Allocator{} 
)
inlinestatic

◆ get_allocator()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
allocator_type helios::ecs::EntityCmdBuffer< Allocator >::get_allocator ( ) const
inlinenoexcept

Gets the allocator used by this buffer.

Returns
Copy of the allocator
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 313 of file entity_command_buffer.hpp.

313{ return alloc_; }

◆ GetEntity()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
Entity helios::ecs::EntityCmdBuffer< Allocator >::GetEntity ( ) const
inlinenoexcept

Gets the entity this command buffer operates on.

Returns
Entity that this command buffer targets
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 307 of file entity_command_buffer.hpp.

307{ return entity_; }

◆ operator=() [1/2]

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
EntityCmdBuffer & helios::ecs::EntityCmdBuffer< Allocator >::operator= ( const EntityCmdBuffer< Allocator > &  )
delete

◆ operator=() [2/2]

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
EntityCmdBuffer & helios::ecs::EntityCmdBuffer< Allocator >::operator= ( EntityCmdBuffer< Allocator > &&  )
defaultnoexcept

◆ RemoveComponent()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
template<ComponentTrait T>
void helios::ecs::EntityCmdBuffer< Allocator >::RemoveComponent ( )
inline

Removes component from the entity.

Queues command to remove the specified component type from entity.

Warning
Triggers assertion during command execution in next cases:
  • Entity does not exist.
  • Entity does not have the specified component.
Template Parameters
TComponent type to remove
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 239 of file entity_command_buffer.hpp.

239 {
240 commands_.push_back(std::make_unique<details::RemoveComponentCmd<T>>(entity_));
241 }

◆ RemoveComponents()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void helios::ecs::EntityCmdBuffer< Allocator >::RemoveComponents ( )
inline

Removes components from the entity.

Queues command to remove all specified component types from entity. More efficient than multiple RemoveComponent calls.

Warning
Triggers assertion during command execution in next cases:
  • Entity does not exist.
  • Entity lacks any of the specified components.
Template Parameters
TsComponent types to remove (must be unique)
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 254 of file entity_command_buffer.hpp.

254 {
255 commands_.push_back(std::make_unique<details::RemoveComponentsCmd<Ts...>>(entity_));
256 }

◆ Size()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
size_t helios::ecs::EntityCmdBuffer< Allocator >::Size ( ) const
inlinenoexcept

Gets the number of pending commands.

Returns
Number of commands in the buffer
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 301 of file entity_command_buffer.hpp.

301{ return commands_.size(); }

◆ TryAddComponent()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
template<ComponentTrait T>
void helios::ecs::EntityCmdBuffer< Allocator >::TryAddComponent ( T &&  component)
inline

Tries to add a component to the entity.

Queues command that will add the component only if the entity does not already have one during command execution.

Warning
Triggers assertion during command execution if entity does not exist.
Template Parameters
TComponent type to add
Parameters
componentComponent instance to add
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 181 of file entity_command_buffer.hpp.

181 {
182 commands_.push_back(
183 std::make_unique<details::TryAddComponentCmd<std::remove_cvref_t<T>>>(entity_, std::forward<T>(component)));
184 }

◆ TryAddComponents()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void helios::ecs::EntityCmdBuffer< Allocator >::TryAddComponents ( Ts &&...  components)
inline

Tries to add multiple components to the entity.

Queues command that attempts to add all provided components. Any component types already present are ignored during command execution. No per-component success flags are returned—inspection after World::Update() should be done via HasComponent().

Warning
Triggers assertion during command execution if entity does not exist.
Template Parameters
TsComponent types to add (must be unique)
Parameters
componentsComponent instances to add
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 197 of file entity_command_buffer.hpp.

197 {
198 commands_.push_back(std::make_unique<details::TryAddComponentsCmd<std::remove_cvref_t<Ts>...>>(
199 entity_, std::forward<Ts>(components)...));
200 }

◆ TryDestroy()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
void helios::ecs::EntityCmdBuffer< Allocator >::TryDestroy ( )
inline

Tries to destroy entity if it exists.

Queues command to destroy the entity only if it currently exists in the world. If entity does not exist during command execution, the command is ignored. After execution of this command, the entity will become invalid if it existed.

Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 140 of file entity_command_buffer.hpp.

140{ commands_.push_back(std::make_unique<details::TryDestroyEntityCmd>(entity_)); }

◆ TryEmplaceComponent()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
template<ComponentTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void helios::ecs::EntityCmdBuffer< Allocator >::TryEmplaceComponent ( Args &&...  args)
inline

Tries to emplace and add component to the entity.

Queues command that will construct and add the component to the entity.

Warning
Triggers assertion during command execution if entity does not exist.
Template Parameters
TComponent type to construct and add
ArgsArgument types for T's constructor
Parameters
argsArguments to forward to component constructor
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 226 of file entity_command_buffer.hpp.

226 {
227 commands_.push_back(std::make_unique<details::TryAddComponentCmd<T>>(entity_, T{std::forward<Args>(args)...}));
228 }

◆ TryRemoveComponent()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
template<ComponentTrait T>
void helios::ecs::EntityCmdBuffer< Allocator >::TryRemoveComponent ( )
inline

Tries to remove a component from the entity.

Queues command that attempts to remove the specified component type. If the entity lacks the component during command execution the command does nothing.

Warning
Triggers assertion during command execution if entity does not exist.
Template Parameters
TComponent type to remove
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 266 of file entity_command_buffer.hpp.

266 {
267 commands_.push_back(std::make_unique<details::TryRemoveComponentCmd<T>>(entity_));
268 }

◆ TryRemoveComponents()

template<typename Allocator = std::allocator<std::unique_ptr<Command>>>
template<ComponentTrait... Ts>
requires utils::UniqueTypes<Ts...>
void helios::ecs::EntityCmdBuffer< Allocator >::TryRemoveComponents ( )
inline

Tries to remove multiple components from the entity (idempotent).

Queues a deferred command that attempts to remove all specified component types. Any components not present are ignored silently.

Warning
Triggers assertion during command execution if entity does not exist.
Template Parameters
TsComponent types to remove (must be unique)
Examples
/home/runner/work/HeliosEngine/HeliosEngine/src/core/include/helios/core/ecs/entity_command_buffer.hpp.

Definition at line 279 of file entity_command_buffer.hpp.

279 {
280 commands_.push_back(std::make_unique<details::TryRemoveComponentsCmd<Ts...>>(entity_));
281 }