Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
aegis2d::ecs::SparseSet< Component > Class Template Referencefinal

Pool of one component type, stored in a sparse set. More...

#include <aegis2d/ecs/sparse_set.hpp>

Inheritance diagram for aegis2d::ecs::SparseSet< Component >:
aegis2d::ecs::SparseSetBase

Public Member Functions

template<typename... Args>
Component & emplace (Entity entity, Args &&... args)
 Gives an entity a component built in place.
 
Component & get (Entity entity)
 Component of an entity.
 
const Component & get (Entity entity) const
 Component of an entity.
 
std::optional< std::reference_wrapper< Component > > try_get (Entity entity)
 Component of an entity, if it has one.
 
std::optional< std::reference_wrapper< const Component > > try_get (Entity entity) const
 Component of an entity, if it has one.
 
std::span< Component > components () noexcept
 Components of the pool, packed, in the same order as entities().
 
std::span< const Component > components () const noexcept
 Components of the pool, packed, in the same order as entities().
 
void reserve (std::size_t count)
 Prepares room for a number of components without reallocating.
 
- Public Member Functions inherited from aegis2d::ecs::SparseSetBase
 SparseSetBase ()=default
 
 SparseSetBase (const SparseSetBase &)=delete
 
SparseSetBase & operator= (const SparseSetBase &)=delete
 
 SparseSetBase (SparseSetBase &&) noexcept=default
 
SparseSetBase & operator= (SparseSetBase &&) noexcept=default
 
virtual ~SparseSetBase ()=default
 
bool contains (Entity entity) const noexcept
 Tells whether an entity is in the pool.
 
bool remove (Entity entity)
 Removes an entity and its component; the last entry moves into its place.
 
void clear () noexcept
 Removes every entity and component.
 
std::size_t size () const noexcept
 Number of entities in the pool.
 
bool empty () const noexcept
 Tells whether the pool is empty.
 
std::span< const Entity > entities () const noexcept
 Entities of the pool, packed, in the same order as the components.
 

Additional Inherited Members

- Protected Types inherited from aegis2d::ecs::SparseSetBase
using Position = std::uint32_t
 Position of an entity in the dense array.
 
- Protected Member Functions inherited from aegis2d::ecs::SparseSetBase
void check_insertable (Entity entity) const
 Throws if an entity cannot be inserted.
 
void insert (Entity entity)
 Appends an entity at the end of the dense array.
 
void reserve_entities (std::size_t count)
 Prepares room in the dense array.
 
std::size_t position_of (Entity entity) const
 Position of an entity in the dense array.
 

Detailed Description

template<typename Component>
requires std::movable<Component> && std::is_nothrow_move_assignable_v<Component> && (!std::is_same_v<Component, bool>)
class aegis2d::ecs::SparseSet< Component >

Pool of one component type, stored in a sparse set.

Components are packed in the same order as entities(): iterate both spans together to visit every (entity, component) pair. References and spans returned by the pool are invalidated by any insertion or removal.

Template Parameters
ComponentMovable component type whose move assignment does not throw (removal cannot fail halfway); bool is not allowed (use a tag struct).

Member Function Documentation

◆ components() [1/2]

template<typename Component >
std::span< const Component > aegis2d::ecs::SparseSet< Component >::components ( ) const
inlinenoexcept

Components of the pool, packed, in the same order as entities().

Returns
A view invalidated by any insertion or removal.

◆ components() [2/2]

template<typename Component >
std::span< Component > aegis2d::ecs::SparseSet< Component >::components ( )
inlinenoexcept

Components of the pool, packed, in the same order as entities().

Returns
A view invalidated by any insertion or removal.

◆ emplace()

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

Gives an entity a component built in place.

Parameters
entityLiving handle, not in the pool yet.
argsArguments of the component's constructor (or of its aggregate initialization).
Returns
The new component.
Exceptions
std::invalid_argumentFor the null handle.
std::logic_errorIf the entity's index already has a component here.

◆ get() [1/2]

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

Component of an entity.

Parameters
entityHandle in the pool.
Returns
The component.
Exceptions
std::out_of_rangeIf the entity has no component here.

◆ get() [2/2]

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

Component of an entity.

Parameters
entityHandle in the pool.
Returns
The component.
Exceptions
std::out_of_rangeIf the entity has no component here.

◆ reserve()

template<typename Component >
void aegis2d::ecs::SparseSet< Component >::reserve ( std::size_t  count)
inline

Prepares room for a number of components without reallocating.

Parameters
countNumber of components.

◆ try_get() [1/2]

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

Component of an entity, if it has one.

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::SparseSet< Component >::try_get ( Entity  entity) const
inline

Component of an entity, if it has one.

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

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