Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::ecs::details::ComponentStorage< T > Class Template Referencefinal

Type-specific component storage using sparse set. More...

#include <components_manager.hpp>

Inheritance diagram for helios::ecs::details::ComponentStorage< T >:
helios::ecs::details::ComponentStorageBase

Public Types

using SparseSetType = container::SparseSet< T, Entity::IndexType >
 
using Iterator = typename SparseSetType::iterator
 
using ConstIterator = typename SparseSetType::const_iterator
 

Public Member Functions

 ComponentStorage ()=default
 
 ComponentStorage (const ComponentStorage &)=default
 
 ComponentStorage (ComponentStorage &&) noexcept=default
 
 ~ComponentStorage () override=default
 
ComponentStorageoperator= (const ComponentStorage &)=default
 
ComponentStorageoperator= (ComponentStorage &&) noexcept=default
 
void Clear () noexcept override
 Clears all components from this storage.
 
template<typename... Args>
requires std::constructible_from<T, Args...>
void Emplace (Entity entity, Args &&... args)
 Constructs component in-place for the specified entity.
 
void Insert (Entity entity, const T &component)
 Inserts component for the specified entity (copy version).
 
void Insert (Entity entity, T &&component)
 Inserts component for the specified entity (move version).
 
void Remove (Entity entity) override
 Removes component from the specified entity.
 
bool TryRemove (Entity entity) override
 Attempts to remove component from the specified entity.
 
T & Get (Entity entity)
 Gets mutable reference to component for the specified entity.
 
const T & Get (Entity entity) const
 Gets const reference to component for the specified entity.
 
T * TryGet (Entity entity)
 Attempts to get mutable pointer to component for the specified entity.
 
const T * TryGet (Entity entity) const
 Attempts to get const pointer to component for the specified entity.
 
bool Contains (Entity entity) const override
 Checks if entity has a component in this storage.
 
size_t Size () const noexcept override
 Gets the number of components stored.
 
constexpr ComponentTypeInfo GetTypeInfo () const noexcept override
 Gets compile-time type information for component T.
 
std::span< T > Data () noexcept
 Gets mutable span over all stored components.
 
std::span< const T > Data () const noexcept
 Gets const span over all stored components.
 
Iterator begin () noexcept
 
ConstIterator begin () const noexcept
 
ConstIterator cbegin () const noexcept
 
Iterator end () noexcept
 
ConstIterator end () const noexcept
 
ConstIterator cend () const noexcept
 
- Public Member Functions inherited from helios::ecs::details::ComponentStorageBase
virtual ~ComponentStorageBase ()=default
 

Detailed Description

template<ComponentTrait T>
class helios::ecs::details::ComponentStorage< T >

Type-specific component storage using sparse set.

Manages components of type T using a sparse set data structure for O(1) insertion, removal, and lookup operations. Components are stored in dense arrays for cache-friendly iteration.

Memory layout:

  • Sparse array: Entity index -> Dense index mapping
  • Dense array: Packed component instances
  • Reverse mapping: Dense index -> Entity index
Template Parameters
TComponent type to store
Note
Not thread-safe. All operations should be performed from the main thread.

Definition at line 95 of file components_manager.hpp.

Member Typedef Documentation

◆ ConstIterator

template<ComponentTrait T>
using helios::ecs::details::ComponentStorage< T >::ConstIterator = typename SparseSetType::const_iterator

Definition at line 99 of file components_manager.hpp.

◆ Iterator

Definition at line 98 of file components_manager.hpp.

◆ SparseSetType

template<ComponentTrait T>
using helios::ecs::details::ComponentStorage< T >::SparseSetType = container::SparseSet<T, Entity::IndexType>

Definition at line 97 of file components_manager.hpp.

Constructor & Destructor Documentation

◆ ComponentStorage() [1/3]

template<ComponentTrait T>
helios::ecs::details::ComponentStorage< T >::ComponentStorage ( )
default

◆ ComponentStorage() [2/3]

template<ComponentTrait T>
helios::ecs::details::ComponentStorage< T >::ComponentStorage ( const ComponentStorage< T > &  )
default

◆ ComponentStorage() [3/3]

template<ComponentTrait T>
helios::ecs::details::ComponentStorage< T >::ComponentStorage ( ComponentStorage< T > &&  )
defaultnoexcept

◆ ~ComponentStorage()

template<ComponentTrait T>
helios::ecs::details::ComponentStorage< T >::~ComponentStorage ( )
overridedefault

Member Function Documentation

◆ begin() [1/2]

template<ComponentTrait T>
ConstIterator helios::ecs::details::ComponentStorage< T >::begin ( ) const
inlinenoexcept

Definition at line 255 of file components_manager.hpp.

255{ return storage_.begin(); }
constexpr iterator begin() noexcept

◆ begin() [2/2]

template<ComponentTrait T>
Iterator helios::ecs::details::ComponentStorage< T >::begin ( )
inlinenoexcept

Definition at line 254 of file components_manager.hpp.

254{ return storage_.begin(); }

◆ cbegin()

template<ComponentTrait T>
ConstIterator helios::ecs::details::ComponentStorage< T >::cbegin ( ) const
inlinenoexcept

Definition at line 256 of file components_manager.hpp.

256{ return storage_.cbegin(); }
constexpr const_iterator cbegin() const noexcept

◆ cend()

template<ComponentTrait T>
ConstIterator helios::ecs::details::ComponentStorage< T >::cend ( ) const
inlinenoexcept

Definition at line 260 of file components_manager.hpp.

260{ return storage_.cend(); }
constexpr const_iterator cend() const noexcept

◆ Clear()

template<ComponentTrait T>
void helios::ecs::details::ComponentStorage< T >::Clear ( )
inlineoverridevirtualnoexcept

Clears all components from this storage.

Removes all component instances and resets sparse set state. Time complexity: O(sparse_capacity).

Implements helios::ecs::details::ComponentStorageBase.

Definition at line 114 of file components_manager.hpp.

114{ storage_.Clear(); }
constexpr void Clear() noexcept
Clears the set, removing all elements.

◆ Contains()

template<ComponentTrait T>
bool helios::ecs::details::ComponentStorage< T >::Contains ( Entity  entity) const
inlineoverridevirtual

Checks if entity has a component in this storage.

Performs comprehensive validation including entity validity and component presence. Time complexity: O(1).

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

Implements helios::ecs::details::ComponentStorageBase.

Definition at line 312 of file components_manager.hpp.

312 {
313 HELIOS_ASSERT(entity.Valid(),
314 "Failed to check if '{}' component storage contains entity: Entity with index '{}' is invalid!",
315 ComponentNameOf<T>(), entity.Index());
316 return storage_.Contains(entity.Index());
317}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140
constexpr bool Contains(IndexType index) const noexcept
Checks if an index exists in the set.
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481

◆ Data() [1/2]

template<ComponentTrait T>
std::span< const T > helios::ecs::details::ComponentStorage< T >::Data ( ) const
inlinenoexcept

Gets const span over all stored components.

Provides direct read-only access to densely packed component array. Components are stored in insertion order (with removal gaps filled by later insertions). Time complexity: O(1).

Returns
Const span over all components in storage

Definition at line 252 of file components_manager.hpp.

252{ return storage_.Data(); }
constexpr std::span< T > Data() noexcept
Returns a writable span of the packed values.

◆ Data() [2/2]

template<ComponentTrait T>
std::span< T > helios::ecs::details::ComponentStorage< T >::Data ( )
inlinenoexcept

Gets mutable span over all stored components.

Provides direct access to densely packed component array for efficient iteration. Components are stored in insertion order (with removal gaps filled by later insertions). Time complexity: O(1).

Returns
Span over all components in storage

Definition at line 243 of file components_manager.hpp.

243{ return storage_.Data(); }

◆ Emplace()

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

Constructs component in-place for the specified entity.

Creates a new component by forwarding arguments to T's constructor. If entity already has this component, it will be replaced. Time complexity: O(1) amortized.

Warning
Triggers assertion if entity is invalid.
Template Parameters
ArgsArgument types to forward to T's constructor
Parameters
entityEntity to add component to
argsArguments to forward to component constructor

Definition at line 269 of file components_manager.hpp.

269 {
270 HELIOS_ASSERT(entity.Valid(), "Failed to emplace component '{}': Entity with index '{}' is invalid!",
271 ComponentNameOf<T>(), entity.Index());
272 storage_.Emplace(entity.Index(), std::forward<Args>(args)...);
273}
DenseIndexType Emplace(IndexType index, Args &&... args)
Constructs a value in-place at the specified index.

◆ end() [1/2]

template<ComponentTrait T>
ConstIterator helios::ecs::details::ComponentStorage< T >::end ( ) const
inlinenoexcept

Definition at line 259 of file components_manager.hpp.

259{ return storage_.end(); }
constexpr iterator end() noexcept

◆ end() [2/2]

template<ComponentTrait T>
Iterator helios::ecs::details::ComponentStorage< T >::end ( )
inlinenoexcept

Definition at line 258 of file components_manager.hpp.

258{ return storage_.end(); }

◆ Get() [1/2]

template<ComponentTrait T>
T & helios::ecs::details::ComponentStorage< T >::Get ( Entity  entity)
inline

Gets mutable reference to component for the specified entity.

Returns direct reference to the stored component. Time complexity: O(1).

Warning
Triggers assertion if entity is invalid or doesn't have the component.
Parameters
entityEntity to get component from
Returns
Mutable reference to the component

Definition at line 320 of file components_manager.hpp.

320 {
321 HELIOS_ASSERT(entity.Valid(), "Failed to get component '{}': Entity with index '{}' is invalid!",
322 ComponentNameOf<T>(), entity.Index());
323 return storage_.Get(entity.Index());
324}
constexpr T & Get(IndexType index) noexcept
Gets the value at the specified index.

◆ Get() [2/2]

template<ComponentTrait T>
const T & helios::ecs::details::ComponentStorage< T >::Get ( Entity  entity) const
inline

Gets const reference to component for the specified entity.

Returns direct const reference to the stored component. Time complexity: O(1).

Warning
Triggers assertion if entity is invalid or doesn't have the component.
Parameters
entityEntity to get component from
Returns
Const reference to the component

Definition at line 327 of file components_manager.hpp.

327 {
328 HELIOS_ASSERT(entity.Valid(), "Failed to get component '{}': Entity with index '{}' is invalid!",
329 ComponentNameOf<T>(), entity.Index());
330 return storage_.Get(entity.Index());
331}

◆ GetTypeInfo()

template<ComponentTrait T>
constexpr ComponentTypeInfo helios::ecs::details::ComponentStorage< T >::GetTypeInfo ( ) const
inlineconstexproverridevirtualnoexcept

Gets compile-time type information for component T.

Returns metadata including type ID, size, alignment, and optimization flags.

Returns
ComponentTypeInfo for type T

Implements helios::ecs::details::ComponentStorageBase.

Definition at line 232 of file components_manager.hpp.

232 {
233 return ComponentTypeInfo::Create<T>();
234 }

◆ Insert() [1/2]

template<ComponentTrait T>
void helios::ecs::details::ComponentStorage< T >::Insert ( Entity  entity,
const T &  component 
)
inline

Inserts component for the specified entity (copy version).

Adds a copy of the component to the entity. If entity already has this component, it will be replaced. Time complexity: O(1) amortized.

Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity to add component to
componentComponent to copy

Definition at line 276 of file components_manager.hpp.

276 {
277 HELIOS_ASSERT(entity.Valid(), "Failed to insert component '{}': Entity with index '{}' is invalid!",
278 ComponentNameOf<T>(), entity.Index());
279 storage_.Insert(entity.Index(), component);
280}
DenseIndexType Insert(IndexType index, const T &value)
Inserts a value at the specified index (copy version).

◆ Insert() [2/2]

template<ComponentTrait T>
void helios::ecs::details::ComponentStorage< T >::Insert ( Entity  entity,
T &&  component 
)
inline

Inserts component for the specified entity (move version).

Moves the component to the entity. If entity already has this component, it will be replaced. Time complexity: O(1) amortized.

Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity to add component to
componentComponent to move

Definition at line 283 of file components_manager.hpp.

283 {
284 HELIOS_ASSERT(entity.Valid(), "Failed to insert component '{}': Entity with index '{}' is invalid!",
285 ComponentNameOf<T>(), entity.Index());
286 storage_.Insert(entity.Index(), std::move(component));
287}

◆ operator=() [1/2]

template<ComponentTrait T>
ComponentStorage & helios::ecs::details::ComponentStorage< T >::operator= ( ComponentStorage< T > &&  )
defaultnoexcept

◆ operator=() [2/2]

template<ComponentTrait T>
ComponentStorage & helios::ecs::details::ComponentStorage< T >::operator= ( const ComponentStorage< T > &  )
default

◆ Remove()

template<ComponentTrait T>
void helios::ecs::details::ComponentStorage< T >::Remove ( Entity  entity)
inlineoverridevirtual

Removes component from the specified entity.

Removes the component using swap-and-pop for dense packing. Time complexity: O(1).

Warning
Triggers assertion if entity is invalid or doesn't have the component.
Parameters
entityEntity to remove component from

Implements helios::ecs::details::ComponentStorageBase.

Definition at line 290 of file components_manager.hpp.

290 {
291 HELIOS_ASSERT(entity.Valid(), "Failed to remove component '{}': Entity with index '{}' is invalid!",
292 ComponentNameOf<T>(), entity.Index());
293 HELIOS_ASSERT(storage_.Contains(entity.Index()),
294 "Failed to remove component '{}': Entity with index '{}' does not have this component!",
295 ComponentNameOf<T>(), entity.Index());
296 storage_.Remove(entity.Index());
297}
void Remove(IndexType index) noexcept
Removes an index from the set.

◆ Size()

template<ComponentTrait T>
size_t helios::ecs::details::ComponentStorage< T >::Size ( ) const
inlineoverridevirtualnoexcept

Gets the number of components stored.

Returns count of component instances currently in storage. Time complexity: O(1).

Returns
Number of components in storage

Implements helios::ecs::details::ComponentStorageBase.

Definition at line 225 of file components_manager.hpp.

225{ return storage_.Size(); }
constexpr size_type Size() const noexcept
Returns the number of values in the set.

◆ TryGet() [1/2]

template<ComponentTrait T>
T * helios::ecs::details::ComponentStorage< T >::TryGet ( Entity  entity)
inline

Attempts to get mutable pointer to component for the specified entity.

Returns pointer to component if it exists, nullptr otherwise. Time complexity: O(1).

Parameters
entityEntity to get component from
Returns
Pointer to component, or nullptr if entity doesn't have the component

Definition at line 334 of file components_manager.hpp.

334 {
335 if (!entity.Valid()) {
336 return nullptr;
337 }
338 return storage_.TryGet(entity.Index());
339}
constexpr T * TryGet(IndexType index) noexcept
Tries to get the value at the specified index.

◆ TryGet() [2/2]

template<ComponentTrait T>
const T * helios::ecs::details::ComponentStorage< T >::TryGet ( Entity  entity) const
inline

Attempts to get const pointer to component for the specified entity.

Returns const pointer to component if it exists, nullptr otherwise. Time complexity: O(1).

Parameters
entityEntity to get component from
Returns
Const pointer to component, or nullptr if entity doesn't have the component

Definition at line 342 of file components_manager.hpp.

342 {
343 if (!entity.Valid()) {
344 return nullptr;
345 }
346 return storage_.TryGet(entity.Index());
347}

◆ TryRemove()

template<ComponentTrait T>
bool helios::ecs::details::ComponentStorage< T >::TryRemove ( Entity  entity)
inlineoverridevirtual

Attempts to remove component from the specified entity.

Removes the component if it exists, otherwise does nothing. Time complexity: O(1).

Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity to remove component from
Returns
True if component was removed, false if entity didn't have the component

Implements helios::ecs::details::ComponentStorageBase.

Definition at line 300 of file components_manager.hpp.

300 {
301 HELIOS_ASSERT(entity.Valid(), "Failed to try remove component '{}': Entity with index '{}' is invalid!",
302 ComponentNameOf<T>(), entity.Index());
303 if (!storage_.Contains(entity.Index())) {
304 return false;
305 }
306
307 storage_.Remove(entity.Index());
308 return true;
309}