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

Unique identifier for entities with generation counter to handle recycling. More...

#include <entity.hpp>

Public Types

using IndexType = uint32_t
 
using GenerationType = uint32_t
 

Public Member Functions

constexpr Entity () noexcept=default
 Constructs an invalid entity.
 
constexpr Entity (IndexType index, GenerationType generation) noexcept
 Constructs entity with specific index and generation.
 
constexpr Entity (const Entity &) noexcept=default
 
constexpr Entity (Entity &&) noexcept=default
 
constexpr ~Entity () noexcept=default
 
constexpr Entityoperator= (const Entity &) noexcept=default
 
constexpr Entityoperator= (Entity &&) noexcept=default
 
constexpr bool operator== (const Entity &) const noexcept=default
 
constexpr bool operator!= (const Entity &) const noexcept=default
 
constexpr bool operator< (const Entity &other) const noexcept
 
constexpr bool Valid () const noexcept
 Checks if the entity is valid.
 
constexpr size_t Hash () const noexcept
 Generates a hash value for this entity.
 
constexpr IndexType Index () const noexcept
 Gets the index component of the entity.
 
constexpr GenerationType Generation () const noexcept
 Gets the generation component of the entity.
 

Static Public Attributes

static constexpr IndexType kInvalidIndex = std::numeric_limits<IndexType>::max()
 
static constexpr GenerationType kInvalidGeneration = 0
 

Detailed Description

Unique identifier for entities with generation counter to handle recycling.

Entity uses a combination of index and generation to provide stable references even when entities are destroyed and their indices are recycled. The generation counter ensures that old entity references become invalid when the index is reused.

Memory layout: 32-bit index + 32-bit generation = 64-bit total

Note
This class is thread-safe for all operations.

Definition at line 21 of file entity.hpp.

Member Typedef Documentation

◆ GenerationType

◆ IndexType

Constructor & Destructor Documentation

◆ Entity() [1/4]

constexpr helios::ecs::Entity::Entity ( )
constexprdefaultnoexcept

Constructs an invalid entity.

Creates an entity with invalid index and generation values.

◆ Entity() [2/4]

constexpr helios::ecs::Entity::Entity ( IndexType  index,
GenerationType  generation 
)
inlineconstexprnoexcept

Constructs entity with specific index and generation.

Private constructor used by entity manager to create valid entities.

Parameters
indexThe entity index
generationThe entity generation

Definition at line 41 of file entity.hpp.

41: index_(index), generation_(generation) {}
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481

◆ Entity() [3/4]

constexpr helios::ecs::Entity::Entity ( const Entity )
constexprdefaultnoexcept

◆ Entity() [4/4]

constexpr helios::ecs::Entity::Entity ( Entity &&  )
constexprdefaultnoexcept

◆ ~Entity()

constexpr helios::ecs::Entity::~Entity ( )
constexprdefaultnoexcept

Member Function Documentation

◆ Generation()

constexpr GenerationType helios::ecs::Entity::Generation ( ) const
inlineconstexprnoexcept

Gets the generation component of the entity.

The generation counter prevents use of stale entity references after recycling.

Returns
Entity generation, or kInvalidGeneration if entity is invalid

Definition at line 82 of file entity.hpp.

82{ return generation_; }

◆ Hash()

constexpr size_t helios::ecs::Entity::Hash ( ) const
constexprnoexcept

Generates a hash value for this entity.

Combines index and generation into a 64-bit hash value. Invalid entities always return hash value of 0.

Returns
Hash combining generation (high bits) and index (low bits)

Definition at line 96 of file entity.hpp.

96 {
97 if (!Valid()) [[unlikely]] {
98 return 0;
99 }
100
101 return (static_cast<size_t>(generation_) << (sizeof(size_t) / 2)) | static_cast<size_t>(index_);
102}
constexpr bool Valid() const noexcept
Checks if the entity is valid.
Definition entity.hpp:58

◆ Index()

constexpr IndexType helios::ecs::Entity::Index ( ) const
inlineconstexprnoexcept

Gets the index component of the entity.

The index identifies the entity's storage location in sparse arrays.

Returns
Entity index, or kInvalidIndex if entity is invalid

Definition at line 75 of file entity.hpp.

75{ return index_; }

◆ operator!=()

constexpr bool helios::ecs::Entity::operator!= ( const Entity ) const
constexprdefaultnoexcept

◆ operator<()

constexpr bool helios::ecs::Entity::operator< ( const Entity other) const
constexprnoexcept

Definition at line 89 of file entity.hpp.

89 {
90 if (index_ != other.index_) {
91 return index_ < other.index_;
92 }
93 return generation_ < other.generation_;
94}

◆ operator=() [1/2]

constexpr Entity & helios::ecs::Entity::operator= ( const Entity )
constexprdefaultnoexcept

◆ operator=() [2/2]

constexpr Entity & helios::ecs::Entity::operator= ( Entity &&  )
constexprdefaultnoexcept

◆ operator==()

constexpr bool helios::ecs::Entity::operator== ( const Entity ) const
constexprdefaultnoexcept

◆ Valid()

constexpr bool helios::ecs::Entity::Valid ( ) const
inlineconstexprnoexcept

Checks if the entity is valid.

An entity is valid if both its index and generation are not the reserved invalid values.

Returns
True if entity has valid index and generation, false otherwise

Definition at line 58 of file entity.hpp.

58 {
59 return index_ != kInvalidIndex && generation_ != kInvalidGeneration;
60 }
static constexpr GenerationType kInvalidGeneration
Definition entity.hpp:27
static constexpr IndexType kInvalidIndex
Definition entity.hpp:26

Member Data Documentation

◆ kInvalidGeneration

constexpr GenerationType helios::ecs::Entity::kInvalidGeneration = 0
staticconstexpr

◆ kInvalidIndex

constexpr IndexType helios::ecs::Entity::kInvalidIndex = std::numeric_limits<IndexType>::max()
staticconstexpr