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

Entity manager responsible for entity creation, destruction, and validation. More...

#include <manager.hpp>

Public Member Functions

 EntityManager ()=default
 EntityManager (const EntityManager &other)
 EntityManager (EntityManager &&other) noexcept
 ~EntityManager ()=default
EntityManageroperator= (const EntityManager &other)
EntityManageroperator= (EntityManager &&other) noexcept
void Clear () noexcept
 Clears all entities.
void Flush ()
 Creates reserved entities in the metadata.
template<typename F>
requires std::invocable<F&, Entity>
void Flush (const F &callback)
 Creates reserved entities and invokes callback for each flushed entity.
void Reserve (size_t count)
 Reserves space for entities to minimize allocations.
Entity ReserveEntity ()
 Reserves an entity ID that can be used immediately.
Entity Create ()
 Creates a new entity.
template<typename OutputIt>
requires std::output_iterator<OutputIt, Entity>
OutputIt Create (size_t count, OutputIt &&out)
 Creates multiple entities at once and outputs them via an output iterator.
void Destroy (Entity entity)
 Destroys an entity by incrementing its generation.
template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, Entity>
void Destroy (const R &entities)
 Destroys an entity by incrementing its generation.
bool Validate (Entity entity) const noexcept
 Checks if an entity exists and is valid.
size_t Count () const noexcept
 Gets the current number of living entities.
Entity::GenerationType GetGeneration (Entity::IndexType index) const noexcept
 Returns current generation value for a given index (or kInvalidGeneration if out of range).

Detailed Description

Entity manager responsible for entity creation, destruction, and validation.

Manages entity lifecycle with generation counters to handle entity recycling safely.

Note
Partially thread-safe.

Definition at line 26 of file manager.hpp.

Constructor & Destructor Documentation

◆ EntityManager() [1/3]

helios::ecs::EntityManager::EntityManager ( )
default

◆ EntityManager() [2/3]

helios::ecs::EntityManager::EntityManager ( const EntityManager & other)
inline

Definition at line 187 of file manager.hpp.

◆ EntityManager() [3/3]

helios::ecs::EntityManager::EntityManager ( EntityManager && other)
inlinenoexcept

Definition at line 194 of file manager.hpp.

◆ ~EntityManager()

helios::ecs::EntityManager::~EntityManager ( )
default

Member Function Documentation

◆ Clear()

void helios::ecs::EntityManager::Clear ( )
inlinenoexcept

Clears all entities.

Destroys all entities and resets the manager state.

Note
Not thread-safe.

Definition at line 243 of file manager.hpp.

◆ Count()

size_t helios::ecs::EntityManager::Count ( ) const
inlinenodiscardnoexcept

Gets the current number of living entities.

Returns count of entities that are currently alive.

Note
Thread-safe.
Returns
Number of living entities

Definition at line 159 of file manager.hpp.

◆ Create() [1/2]

Entity helios::ecs::EntityManager::Create ( )
inlinenodiscard

Creates a new entity.

Reuses dead entity slots when available, otherwise creates new ones.

Note
Not thread-safe.
Returns
Newly created entity with valid index and generation

Definition at line 428 of file manager.hpp.

◆ Create() [2/2]

template<typename OutputIt>
requires std::output_iterator<OutputIt, Entity>
OutputIt helios::ecs::EntityManager::Create ( size_t count,
OutputIt && out )
inline

Creates multiple entities at once and outputs them via an output iterator.

Batch creation is more efficient than individual calls. Entities are written to the provided output iterator, avoiding internal allocations.

Note
Not thread-safe.
Template Parameters
OutputItOutput iterator type that accepts Entity values
Parameters
countNumber of entities to create
outOutput iterator to write created entities to
Returns
Output iterator pointing past the last written entity
std::vector<Entity> entities;
entities.reserve(100);
manager.Create(100, std::back_inserter(entities));
// Or with pre-allocated array:
std::array<Entity, 10> arr;
manager.Create(10, arr.begin());
// Or with span output:
Entity buffer[50];
manager.Create(50, std::begin(buffer));
Unique identifier for entities with generation counter to handle recycling.
Definition entity.hpp:22

Definition at line 349 of file manager.hpp.

◆ Destroy() [1/2]

template<std::ranges::range R>
requires std::same_as<std::ranges::range_value_t<R>, Entity>
void helios::ecs::EntityManager::Destroy ( const R & entities)
inline

Destroys an entity by incrementing its generation.

Marks entity as dead and adds its index to the free list for reuse. Entities that do not exist or are already destroyed are ignored.

Note
Not thread-safe.
Warning
Triggers assertion if any entity is invalid.
Template Parameters
RRange type containing Entity elements
Parameters
entitiesEntities to destroy

Definition at line 324 of file manager.hpp.

◆ Destroy() [2/2]

void helios::ecs::EntityManager::Destroy ( Entity entity)
inline

Destroys an entity by incrementing its generation.

Marks entity as dead and adds its index to the free list for reuse. Entities that do not exist or are already destroyed are ignored.

Note
Not thread-safe.
Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity to destroy

Definition at line 307 of file manager.hpp.

◆ Flush() [1/2]

void helios::ecs::EntityManager::Flush ( )
inline

Creates reserved entities in the metadata.

Processes all reserved entity IDs and creates their metadata.

Note
Not thread-safe.

Definition at line 48 of file manager.hpp.

◆ Flush() [2/2]

template<typename F>
requires std::invocable<F&, Entity>
void helios::ecs::EntityManager::Flush ( const F & callback)
inline

Creates reserved entities and invokes callback for each flushed entity.

Processes all reserved entity IDs, initializes their metadata, and invokes callback exactly once per newly flushed entity.

Note
Not thread-safe.
Template Parameters
FCallable type invocable with Entity
Parameters
callbackCallback invoked for each newly flushed entity

Definition at line 253 of file manager.hpp.

◆ GetGeneration()

Entity::GenerationType helios::ecs::EntityManager::GetGeneration ( Entity::IndexType index) const
inlinenodiscardnoexcept

Returns current generation value for a given index (or kInvalidGeneration if out of range).

Warning
Triggers assertion if index is invalid.

Definition at line 421 of file manager.hpp.

◆ operator=() [1/2]

EntityManager & helios::ecs::EntityManager::operator= ( const EntityManager & other)
inline

Definition at line 205 of file manager.hpp.

◆ operator=() [2/2]

EntityManager & helios::ecs::EntityManager::operator= ( EntityManager && other)
inlinenoexcept

Definition at line 222 of file manager.hpp.

◆ Reserve()

void helios::ecs::EntityManager::Reserve ( size_t count)
inline

Reserves space for entities to minimize allocations.

Pre-allocates storage for the specified number of entities.

Note
Not thread-safe.
Parameters
countNumber of entities to reserve space for

Definition at line 287 of file manager.hpp.

◆ ReserveEntity()

Entity helios::ecs::EntityManager::ReserveEntity ( )
inlinenodiscard

Reserves an entity ID that can be used immediately.

The actual entity creation is deferred until Flush() is called.

Note
Thread-safe.
Returns
Reserved entity with valid index and generation

Definition at line 294 of file manager.hpp.

◆ Validate()

bool helios::ecs::EntityManager::Validate ( Entity entity) const
inlinenodiscardnoexcept

Checks if an entity exists and is valid.

Validates both the entity structure and its current generation.

Note
Thread-safe for read operations.
Parameters
entityEntity to validate
Returns
True if entity exists and is valid, false otherwise

Definition at line 410 of file manager.hpp.