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

Hands out entity handles and recycles the indices of destroyed entities. More...

#include <aegis2d/ecs/entity_allocator.hpp>

Public Member Functions

Entity create ()
 Creates an entity.
 
bool destroy (Entity entity)
 Destroys an entity: its index gets a new generation and goes to the free list.
 
bool alive (Entity entity) const noexcept
 Tells whether a handle designates a living entity.
 
void clear ()
 Destroys every living entity, as destroy() would.
 
void reserve (std::size_t slots)
 Prepares room for a number of indices without reallocating.
 
std::size_t size () const noexcept
 Number of living entities.
 
std::size_t slot_count () const noexcept
 Number of indices ever opened, living or free.
 
std::size_t free_count () const noexcept
 Number of indices waiting on the free list.
 

Detailed Description

Hands out entity handles and recycles the indices of destroyed entities.

Each index has a slot that stores its current generation and whether an entity lives there. create() reuses a free index when there is one, otherwise it opens a new slot; destroy() increments the generation of the index and puts it on the free list, so every handle created before is detected as stale by alive().

The free list is first in, first out: an index waits behind all the other free ones before it is reused. A game that spawns and kills bullets every tick thus spreads the reuses over many indices, and the 8-bit generation takes much longer to wrap back to a value an old handle could still hold.

Not thread-safe: the registry that owns the allocator serializes the calls.

Member Function Documentation

◆ alive()

bool aegis2d::ecs::EntityAllocator::alive ( Entity  entity) const
noexcept

Tells whether a handle designates a living entity.

Parameters
entityHandle to check.
Returns
false for a null handle, an index never created, a destroyed entity, or a handle whose generation is not the current one of its index.

◆ clear()

void aegis2d::ecs::EntityAllocator::clear ( )

Destroys every living entity, as destroy() would.

Generations are kept and incremented, not reset: handles created before the call stay stale instead of matching the next entities.

◆ create()

Entity aegis2d::ecs::EntityAllocator::create ( )

Creates an entity.

Returns
A handle to a free index (the oldest freed one first) at its current generation, or to a new index when none is free.
Exceptions
std::length_errorIf all Entity::max_index + 1 indices are alive.

◆ destroy()

bool aegis2d::ecs::EntityAllocator::destroy ( Entity  entity)

Destroys an entity: its index gets a new generation and goes to the free list.

Parameters
entityHandle to destroy.
Returns
true if the entity was alive; false for a null, stale or unknown handle, which is left untouched (destroying twice is harmless).

◆ free_count()

std::size_t aegis2d::ecs::EntityAllocator::free_count ( ) const
noexcept

Number of indices waiting on the free list.

Returns
slot_count() minus size().

◆ reserve()

void aegis2d::ecs::EntityAllocator::reserve ( std::size_t  slots)

Prepares room for a number of indices without reallocating.

Parameters
slotsNumber of indices to reserve.

◆ size()

std::size_t aegis2d::ecs::EntityAllocator::size ( ) const
noexcept

Number of living entities.

Returns
Created minus destroyed entities.

◆ slot_count()

std::size_t aegis2d::ecs::EntityAllocator::slot_count ( ) const
noexcept

Number of indices ever opened, living or free.

Returns
One past the highest index handed out; component pools are at most this big.

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