Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ecs::Entity Class Reference

Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation. More...

#include <aegis2d/ecs/entity.hpp>

Public Types

using Index = std::uint32_t
 Position of the entity in the component pools.
 
using Generation = std::uint8_t
 Reuse counter of an index.
 
using Id = std::uint32_t
 Packed value: generation in the 8 high bits, index in the 24 low bits.
 

Public Member Functions

constexpr Entity () noexcept=default
 Creates a null handle.
 
constexpr Entity (Index index, Generation generation) noexcept
 Creates the handle of an index at a given generation.
 
constexpr Index index () const noexcept
 Position of the entity in the component pools.
 
constexpr Generation generation () const noexcept
 Generation of the index when this handle was created.
 
constexpr Id id () const noexcept
 Packed value, unique per index and generation.
 
constexpr bool is_null () const noexcept
 Tells whether the handle designates no entity.
 

Static Public Member Functions

static constexpr Entity from_id (Id id) noexcept
 Rebuilds a handle from its packed value, e.g. received from the network.
 
static constexpr Entity null () noexcept
 The null handle, equal to a default-constructed one.
 

Static Public Attributes

static constexpr unsigned index_bits = 24
 Number of bits of the index.
 
static constexpr Index max_index = (Index{1} << index_bits) - 2
 Highest valid index: 16 777 214 (the last value is reserved for null).
 

Friends

constexpr bool operator== (Entity, Entity) noexcept=default
 Handles are equal when both index and generation are.
 
constexpr std::strong_ordering operator<=> (Entity, Entity) noexcept=default
 Orders handles by packed value (generation, then index).
 

Detailed Description

Handle to an entity: 32 bits made of a 24-bit index and an 8-bit generation.

The index locates the entity's components in the pools; it is recycled when the entity is destroyed. The generation changes at each reuse, so a handle kept by a system after its entity was destroyed no longer matches the new occupant of the index and is detected as stale instead of silently pointing to another entity.

The generation wraps after 256 reuses of the same index. A default-constructed handle is null: it designates no entity. Handles are plain values: compare, hash, copy and send them freely (id() gives the packed 32-bit value).

Member Typedef Documentation

◆ Generation

using aegis2d::ecs::Entity::Generation = std::uint8_t

Reuse counter of an index.

◆ Id

using aegis2d::ecs::Entity::Id = std::uint32_t

Packed value: generation in the 8 high bits, index in the 24 low bits.

◆ Index

using aegis2d::ecs::Entity::Index = std::uint32_t

Position of the entity in the component pools.

Constructor & Destructor Documentation

◆ Entity() [1/2]

constexpr aegis2d::ecs::Entity::Entity ( )
constexprdefaultnoexcept

Creates a null handle.

◆ Entity() [2/2]

constexpr aegis2d::ecs::Entity::Entity ( Index  index,
Generation  generation 
)
inlineconstexprnoexcept

Creates the handle of an index at a given generation.

Parameters
indexPosition in the pools, at most max_index.
generationCurrent generation of that index.

Member Function Documentation

◆ from_id()

static constexpr Entity aegis2d::ecs::Entity::from_id ( Id  id)
inlinestaticconstexprnoexcept

Rebuilds a handle from its packed value, e.g. received from the network.

Parameters
idValue returned by id().
Returns
The handle.

◆ generation()

constexpr Generation aegis2d::ecs::Entity::generation ( ) const
inlineconstexprnoexcept

Generation of the index when this handle was created.

Returns
The reuse counter.

◆ id()

constexpr Id aegis2d::ecs::Entity::id ( ) const
inlineconstexprnoexcept

Packed value, unique per index and generation.

Returns
Generation in the 8 high bits, index in the 24 low bits.

◆ index()

constexpr Index aegis2d::ecs::Entity::index ( ) const
inlineconstexprnoexcept

Position of the entity in the component pools.

Returns
A value in [0, max_index] for a non-null handle.

◆ is_null()

constexpr bool aegis2d::ecs::Entity::is_null ( ) const
inlineconstexprnoexcept

Tells whether the handle designates no entity.

Returns
true for a default-constructed or null() handle.

◆ null()

static constexpr Entity aegis2d::ecs::Entity::null ( )
inlinestaticconstexprnoexcept

The null handle, equal to a default-constructed one.

Returns
A handle that designates no entity.

Friends And Related Symbol Documentation

◆ operator<=>

constexpr std::strong_ordering operator<=> ( Entity  ,
Entity   
)
friend

Orders handles by packed value (generation, then index).

◆ operator==

constexpr bool operator== ( Entity  ,
Entity   
)
friend

Handles are equal when both index and generation are.

Member Data Documentation

◆ index_bits

constexpr unsigned aegis2d::ecs::Entity::index_bits = 24
staticconstexpr

Number of bits of the index.

◆ max_index

constexpr Index aegis2d::ecs::Entity::max_index = (Index{1} << index_bits) - 2
staticconstexpr

Highest valid index: 16 777 214 (the last value is reserved for null).


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