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

Entry point of the ECS: creates entities and stores their components. More...

#include <aegis2d/ecs/registry.hpp>

Public Member Functions

 Registry ()=default
 
 Registry (const Registry &)=delete
 
Registry & operator= (const Registry &)=delete
 
 Registry (Registry &&) noexcept=default
 
Registry & operator= (Registry &&) noexcept=default
 
 ~Registry ()=default
 
Entity create ()
 Creates an entity without components.
 
bool destroy (Entity entity)
 Destroys an entity and removes all its components.
 
bool alive (Entity entity) const noexcept
 Tells whether a handle designates a living entity.
 
std::size_t size () const noexcept
 Number of living entities.
 
void clear ()
 Destroys every entity and removes every component.
 
template<typename Component , typename... Args>
Component & emplace (Entity entity, Args &&... args)
 Gives an entity a component built in place.
 
template<typename Component , typename... Args>
Component & emplace_or_replace (Entity entity, Args &&... args)
 Gives an entity a component, or replaces the one it has.
 
template<typename Component >
bool remove (Entity entity)
 Removes a component from an entity.
 
template<typename... Components>
requires (sizeof...(Components) > 0)
bool has (Entity entity) const
 Tells whether an entity has every listed component.
 
template<typename Component >
Component & get (Entity entity)
 Component of an entity.
 
template<typename Component >
const Component & get (Entity entity) const
 Component of an entity.
 
template<typename Component >
std::optional< std::reference_wrapper< Component > > try_get (Entity entity)
 Component of an entity, if it has one.
 
template<typename Component >
std::optional< std::reference_wrapper< const Component > > try_get (Entity entity) const
 Component of an entity, if it has one.
 
template<typename Component >
std::size_t count () const
 Number of entities that have a component.
 
template<typename Component >
SparseSet< Component > & pool ()
 Pool of a component type, created empty on first use.
 
template<typename Component >
std::optional< std::reference_wrapper< SparseSet< Component > > > find_pool ()
 Pool of a component type, if it was ever used.
 
template<typename Component >
std::optional< std::reference_wrapper< const SparseSet< Component > > > find_pool () const
 Pool of a component type, if it was ever used.
 
template<typename... Components>
View< Components... > view ()
 View on the entities that own all the listed components.
 

Detailed Description

Entry point of the ECS: creates entities and stores their components.

The registry owns an EntityAllocator and one SparseSet per component type, created the first time the type is used. Destroying an entity removes all its components, whatever their types, then recycles its index with a new generation: a handle kept after destroy() is stale, alive() rejects it and it reaches no component.

Any movable type with a noexcept move assignment can be a component, usually a plain struct: registry.emplace<Velocity>(player, Vec2{120.0F, 0.0F});.

References and spans returned by the registry or its pools are invalidated when a component of the same type is added or removed, as with std::vector. Not thread-safe: use the registry from the game thread only.

Constructor & Destructor Documentation

◆ Registry() [1/3]

aegis2d::ecs::Registry::Registry ( )
default

◆ Registry() [2/3]

aegis2d::ecs::Registry::Registry ( const Registry &  )
delete

◆ Registry() [3/3]

aegis2d::ecs::Registry::Registry ( Registry &&  )
defaultnoexcept

◆ ~Registry()

aegis2d::ecs::Registry::~Registry ( )
default

Member Function Documentation

◆ alive()

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

Tells whether a handle designates a living entity.

Parameters
entityHandle to check.
Returns
false for a null, destroyed or stale handle.

◆ clear()

void aegis2d::ecs::Registry::clear ( )

Destroys every entity and removes every component.

The pools are kept (empty) and the generations are incremented, so handles created before the call stay stale.

◆ count()

template<typename Component >
std::size_t aegis2d::ecs::Registry::count ( ) const
inline

Number of entities that have a component.

Template Parameters
ComponentComponent type.
Returns
Size of its pool, 0 if the type was never used.

◆ create()

Entity aegis2d::ecs::Registry::create ( )

Creates an entity without components.

Returns
Its handle.
Exceptions
std::length_errorIf all Entity::max_index + 1 indices are alive.

◆ destroy()

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

Destroys an entity and removes all its components.

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

◆ emplace()

template<typename Component , typename... Args>
Component & aegis2d::ecs::Registry::emplace ( Entity  entity,
Args &&...  args 
)
inline

Gives an entity a component built in place.

Template Parameters
ComponentComponent type.
Parameters
entityLiving handle.
argsArguments of the component's constructor (or aggregate initialization).
Returns
The new component.
Exceptions
std::invalid_argumentIf the entity is not alive.
std::logic_errorIf the entity already has a Component.

◆ emplace_or_replace()

template<typename Component , typename... Args>
Component & aegis2d::ecs::Registry::emplace_or_replace ( Entity  entity,
Args &&...  args 
)
inline

Gives an entity a component, or replaces the one it has.

Template Parameters
ComponentComponent type.
Parameters
entityLiving handle.
argsArguments of the component's constructor (or aggregate initialization).
Returns
The new or replaced component.
Exceptions
std::invalid_argumentIf the entity is not alive.

◆ find_pool() [1/2]

template<typename Component >
std::optional< std::reference_wrapper< SparseSet< Component > > > aegis2d::ecs::Registry::find_pool ( )
inline

Pool of a component type, if it was ever used.

Template Parameters
ComponentComponent type.
Returns
The pool, or std::nullopt.

◆ find_pool() [2/2]

template<typename Component >
std::optional< std::reference_wrapper< const SparseSet< Component > > > aegis2d::ecs::Registry::find_pool ( ) const
inline

Pool of a component type, if it was ever used.

Template Parameters
ComponentComponent type.
Returns
The pool, or std::nullopt.

◆ get() [1/2]

template<typename Component >
Component & aegis2d::ecs::Registry::get ( Entity  entity)
inline

Component of an entity.

Template Parameters
ComponentComponent type.
Parameters
entityHandle that has a Component.
Returns
The component.
Exceptions
std::out_of_rangeIf the entity has no Component (or is not alive).

◆ get() [2/2]

template<typename Component >
const Component & aegis2d::ecs::Registry::get ( Entity  entity) const
inline

Component of an entity.

Template Parameters
ComponentComponent type.
Parameters
entityHandle that has a Component.
Returns
The component.
Exceptions
std::out_of_rangeIf the entity has no Component (or is not alive).

◆ has()

template<typename... Components>
requires (sizeof...(Components) > 0)
bool aegis2d::ecs::Registry::has ( Entity  entity) const
inline

Tells whether an entity has every listed component.

Template Parameters
ComponentsComponent types, at least one.
Parameters
entityHandle; a dead or stale handle has no component.
Returns
true if the entity has all of them.

◆ operator=() [1/2]

Registry & aegis2d::ecs::Registry::operator= ( const Registry &  )
delete

◆ operator=() [2/2]

Registry & aegis2d::ecs::Registry::operator= ( Registry &&  )
defaultnoexcept

◆ pool()

template<typename Component >
SparseSet< Component > & aegis2d::ecs::Registry::pool ( )
inline

Pool of a component type, created empty on first use.

Iterate pool.entities() and pool.components() together to visit every entity that has the component; view() combines several types.

Template Parameters
ComponentComponent type.
Returns
The pool, owned by the registry.

◆ remove()

template<typename Component >
bool aegis2d::ecs::Registry::remove ( Entity  entity)
inline

Removes a component from an entity.

Template Parameters
ComponentComponent type.
Parameters
entityHandle.
Returns
true if the entity had a Component; false (nothing changes) otherwise.

◆ size()

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

Number of living entities.

Returns
Created minus destroyed entities.

◆ try_get() [1/2]

template<typename Component >
std::optional< std::reference_wrapper< Component > > aegis2d::ecs::Registry::try_get ( Entity  entity)
inline

Component of an entity, if it has one.

Template Parameters
ComponentComponent type.
Parameters
entityHandle to look up.
Returns
The component, or std::nullopt.

◆ try_get() [2/2]

template<typename Component >
std::optional< std::reference_wrapper< const Component > > aegis2d::ecs::Registry::try_get ( Entity  entity) const
inline

Component of an entity, if it has one.

Template Parameters
ComponentComponent type.
Parameters
entityHandle to look up.
Returns
The component, or std::nullopt.

◆ view()

template<typename... Components>
View< Components... > aegis2d::ecs::Registry::view ( )
inline

View on the entities that own all the listed components.

Creates the pools that do not exist yet, so the view stays valid when the first components of a type are added later. Typical system: registry.view<Transform, Velocity>().each([dt](Transform& t, const Velocity& v) { t.position += v.value * dt; });

Template Parameters
ComponentsComponent types, at least one, all different.
Returns
A view that refers to the registry's pools; it must not outlive the registry.

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