Helios Engine
A modular ECS based data-oriented C++23 game engine framework
Loading...
Searching...
No Matches
helios::ecs::SparseComponentStorage< T > Class Template Reference

Concrete sparse-set storage for a single component type (non-polymorphic). More...

#include <sparse_storage.hpp>

Public Member Functions

constexpr SparseComponentStorage ()=default
constexpr SparseComponentStorage (const SparseComponentStorage &)=default
constexpr SparseComponentStorage (SparseComponentStorage &&) noexcept=default
constexpr ~SparseComponentStorage ()=default
constexpr SparseComponentStorageoperator= (const SparseComponentStorage &)=default
constexpr SparseComponentStorageoperator= (SparseComponentStorage &&) noexcept=default
constexpr void Clear () noexcept
 Removes all component instances.
constexpr void Remove (Entity entity)
 Removes the component for the given entity.
constexpr bool TryRemove (Entity entity)
 Tries to remove the component for the given entity.
template<ComponentTrait U = T>
requires std::same_as<std::remove_cvref_t<U>, T>
constexpr void Set (Entity entity, U &&component)
 Inserts or replaces a component for the given entity.
template<ComponentTrait U = T>
requires std::same_as<std::remove_cvref_t<U>, T>
constexpr bool TrySet (Entity entity, U &&component)
 Tries to insert a component. Returns false if entity already has it.
template<typename... Args>
requires std::constructible_from<T, Args...>
constexpr void Emplace (Entity entity, Args &&... args)
 Constructs a component in-place for the given entity.
template<typename... Args>
requires std::constructible_from<T, Args...>
constexpr bool TryEmplace (Entity entity, Args &&... args)
 Tries to emplace a component. Returns false if entity already has it.
constexpr T & Get (Entity entity) noexcept
 Gets a mutable reference to the component for the entity.
constexpr const T & Get (Entity entity) const noexcept
 Gets a const reference to the component for the entity.
constexpr T * TryGet (Entity entity) noexcept
 Tries to get a mutable pointer to the component for the entity.
constexpr const T * TryGet (Entity entity) const noexcept
 Tries to get a const pointer to the component for the entity.
constexpr bool Contains (Entity entity) const noexcept
 Checks if the entity has the component.
constexpr size_t Size () const noexcept
 Gets the number of stored components.
constexpr auto Data () noexcept -> std::span< T >
 Gets a span over all dense component data.
constexpr auto Data () const noexcept -> std::span< const T >
 Gets a span over all dense component data (const).

Detailed Description

template<ComponentTrait T>
class helios::ecs::SparseComponentStorage< T >

Concrete sparse-set storage for a single component type (non-polymorphic).

Wraps a SparseSet<T, Entity::IndexType> and provides entity-oriented API. Not designed to be a base class.

Template Parameters
TComponent type

Definition at line 25 of file sparse_storage.hpp.

Constructor & Destructor Documentation

◆ SparseComponentStorage() [1/3]

template<ComponentTrait T>
helios::ecs::SparseComponentStorage< T >::SparseComponentStorage ( )
constexprdefault

◆ SparseComponentStorage() [2/3]

template<ComponentTrait T>
helios::ecs::SparseComponentStorage< T >::SparseComponentStorage ( const SparseComponentStorage< T > & )
constexprdefault

◆ SparseComponentStorage() [3/3]

template<ComponentTrait T>
helios::ecs::SparseComponentStorage< T >::SparseComponentStorage ( SparseComponentStorage< T > && )
constexprdefaultnoexcept

◆ ~SparseComponentStorage()

template<ComponentTrait T>
helios::ecs::SparseComponentStorage< T >::~SparseComponentStorage ( )
constexprdefault

Member Function Documentation

◆ Clear()

template<ComponentTrait T>
void helios::ecs::SparseComponentStorage< T >::Clear ( )
inlineconstexprnoexcept

Removes all component instances.

Definition at line 38 of file sparse_storage.hpp.

◆ Contains()

template<ComponentTrait T>
bool helios::ecs::SparseComponentStorage< T >::Contains ( Entity entity) const
nodiscardconstexprnoexcept

Checks if the entity has the component.

Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity
Returns
True if entity has the component, false otherwise

Definition at line 284 of file sparse_storage.hpp.

◆ Data() [1/2]

template<ComponentTrait T>
auto helios::ecs::SparseComponentStorage< T >::Data ( ) const -> std::span< const T >
inlinenodiscardconstexprnoexcept

Gets a span over all dense component data (const).

Returns
Span of const components

Definition at line 170 of file sparse_storage.hpp.

◆ Data() [2/2]

template<ComponentTrait T>
auto helios::ecs::SparseComponentStorage< T >::Data ( ) -> std::span< T >
inlinenodiscardconstexprnoexcept

Gets a span over all dense component data.

Returns
Span of components

Definition at line 162 of file sparse_storage.hpp.

◆ Emplace()

template<ComponentTrait T>
requires std::constructible_from<T, Args...>
template<typename... Args>
requires std::constructible_from<T, Args...>
void helios::ecs::SparseComponentStorage< T >::Emplace ( Entity entity,
Args &&... args )
constexpr

Constructs a component in-place for the given entity.

Warning
Triggers assertion if entity is invalid.
Template Parameters
ArgsConstructor argument types
Parameters
entityEntity
argsArguments

Definition at line 226 of file sparse_storage.hpp.

◆ Get() [1/2]

template<ComponentTrait T>
const T & helios::ecs::SparseComponentStorage< T >::Get ( Entity entity) const
nodiscardconstexprnoexcept

Gets a const reference to the component for the entity.

Warning
Triggers assertion in next cases:
Parameters
entityEntity
Returns
Const reference to component

Definition at line 261 of file sparse_storage.hpp.

◆ Get() [2/2]

template<ComponentTrait T>
T & helios::ecs::SparseComponentStorage< T >::Get ( Entity entity)
nodiscardconstexprnoexcept

Gets a mutable reference to the component for the entity.

Warning
Triggers assertion in next cases:
Parameters
entityEntity
Returns
Mutable reference to component

Definition at line 252 of file sparse_storage.hpp.

◆ operator=() [1/2]

template<ComponentTrait T>
SparseComponentStorage & helios::ecs::SparseComponentStorage< T >::operator= ( const SparseComponentStorage< T > & )
constexprdefault

◆ operator=() [2/2]

template<ComponentTrait T>
SparseComponentStorage & helios::ecs::SparseComponentStorage< T >::operator= ( SparseComponentStorage< T > && )
constexprdefaultnoexcept

◆ Remove()

template<ComponentTrait T>
void helios::ecs::SparseComponentStorage< T >::Remove ( Entity entity)
constexpr

Removes the component for the given entity.

Warning
Triggers assertion in next cases:
Parameters
entityEntity

Definition at line 181 of file sparse_storage.hpp.

◆ Set()

template<ComponentTrait T>
requires std::same_as<std::remove_cvref_t<U>, T>
template<ComponentTrait U>
requires std::same_as<std::remove_cvref_t<U>, T>
void helios::ecs::SparseComponentStorage< T >::Set ( Entity entity,
U && component )
constexpr

Inserts or replaces a component for the given entity.

Warning
Triggers assertion if entity is invalid.
Template Parameters
UComponent type, must be the same as T
Parameters
entityEntity
componentComponent value

Definition at line 202 of file sparse_storage.hpp.

◆ Size()

template<ComponentTrait T>
size_t helios::ecs::SparseComponentStorage< T >::Size ( ) const
inlinenodiscardconstexprnoexcept

Gets the number of stored components.

Returns
Number of components

Definition at line 154 of file sparse_storage.hpp.

◆ TryEmplace()

template<ComponentTrait T>
requires std::constructible_from<T, Args...>
template<typename... Args>
requires std::constructible_from<T, Args...>
bool helios::ecs::SparseComponentStorage< T >::TryEmplace ( Entity entity,
Args &&... args )
constexpr

Tries to emplace a component. Returns false if entity already has it.

Warning
Triggers assertion if entity is invalid.
Template Parameters
ArgsConstructor argument types
Parameters
entityEntity
argsArguments
Returns
True if emplaced

Definition at line 241 of file sparse_storage.hpp.

◆ TryGet() [1/2]

template<ComponentTrait T>
const T * helios::ecs::SparseComponentStorage< T >::TryGet ( Entity entity) const
nodiscardconstexprnoexcept

Tries to get a const pointer to the component for the entity.

Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity
Returns
Const pointer to component or nullptr if entity does not have the component

Definition at line 277 of file sparse_storage.hpp.

◆ TryGet() [2/2]

template<ComponentTrait T>
T * helios::ecs::SparseComponentStorage< T >::TryGet ( Entity entity)
nodiscardconstexprnoexcept

Tries to get a mutable pointer to the component for the entity.

Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity
Returns
Pointer to component or nullptr if entity does not have the component

Definition at line 271 of file sparse_storage.hpp.

◆ TryRemove()

template<ComponentTrait T>
bool helios::ecs::SparseComponentStorage< T >::TryRemove ( Entity entity)
constexpr

Tries to remove the component for the given entity.

Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity
Returns
True if removed, false if entity did not have the component

Definition at line 190 of file sparse_storage.hpp.

◆ TrySet()

template<ComponentTrait T>
requires std::same_as<std::remove_cvref_t<U>, T>
template<ComponentTrait U>
requires std::same_as<std::remove_cvref_t<U>, T>
bool helios::ecs::SparseComponentStorage< T >::TrySet ( Entity entity,
U && component )
constexpr

Tries to insert a component. Returns false if entity already has it.

Warning
Triggers assertion if entity is invalid. tparam U Component type, must be the same as T
Parameters
entityEntity
componentComponent value
Returns
True if inserted

Definition at line 214 of file sparse_storage.hpp.