R-Type  2
Doom but in better
Loading...
Searching...
No Matches
ComponentContainer< Component, Allocator > Class Template Reference

Manages a collection of components associated with entities in an ECS (Entity-Component-System) architecture. More...

#include <ComponentContainer.hpp>

Collaboration diagram for ComponentContainer< Component, Allocator >:
Collaboration graph

Public Types

using value_type = Component
 Type aliases for convenience.
 
using optional_t = std::optional<Component>
 
using sparse_storage_t = SparseArray<Component, Allocator>
 
using dense_storage_t = DenseArray<Component, Allocator>
 
using size_type = typename sparse_storage_t::size_type
 
using reference = optional_t&
 
using const_reference = const optional_t&
 
using iterator = typename std::vector<optional_t, Allocator>::iterator
 
using const_iterator = typename std::vector<optional_t, Allocator>::const_iterator
 

Public Member Functions

 ComponentContainer ()
 Default constructor.
 
reference operator[] (size_t idx)
 Accesses the component at a given index.
 
const_reference operator[] (size_t idx) const
 Accesses the component at a given index (const version).
 
iterator begin ()
 Returns an iterator to the beginning of the component container.
 
const_iterator begin () const
 Returns a const iterator to the beginning of the component container.
 
const_iterator cbegin () const
 Returns a const iterator to the beginning of the component container.
 
iterator end ()
 Returns an iterator to the end of the component container.
 
const_iterator end () const
 Returns a const iterator to the end of the component container.
 
const_iterator cend () const
 Returns a const iterator to the end of the component container.
 
size_type size () const
 Returns the number of components stored in the array.
 
void insert_at (size_type id, const Component &component)
 Inserts a component at a specific ID.
 
void insert_at (size_type id, Component &&component)
 Inserts a component at a specific ID (move version).
 
template<typename... Params>
void emplace_at (size_type id, Params &&... params)
 Constructs and inserts a component at a specific ID.
 
std::optional< Component > get (const value_type &id) const
 Retrieves a component by its id.
 
void erase (size_type id)
 Removes a component at a specific ID.
 
void resize (size_type new_size)
 Resizes the container to a new size.
 
void optimize_storage (size_type sparse_threshold, size_type dense_threshold)
 Optimizes the storage type based on thresholds for sparse and dense storage.
 

Detailed Description

template<typename Component, typename Allocator = std::allocator<Component>>
class ComponentContainer< Component, Allocator >

Manages a collection of components associated with entities in an ECS (Entity-Component-System) architecture.

The ComponentContainer class provides an interface for managing components, offering storage flexibility between sparse and dense arrays, and supports efficient insertion, retrieval, and optimization.

Template Parameters
ComponentThe type of component stored.
AllocatorThe allocator type used for memory management (defaults to std::allocator<Component>).

Definition at line 24 of file ComponentContainer.hpp.

Member Typedef Documentation

◆ const_iterator

template<typename Component , typename Allocator = std::allocator<Component>>
using ComponentContainer< Component, Allocator >::const_iterator = typename std::vector<optional_t, Allocator>::const_iterator

Const iterator type.

Definition at line 36 of file ComponentContainer.hpp.

◆ const_reference

template<typename Component , typename Allocator = std::allocator<Component>>
using ComponentContainer< Component, Allocator >::const_reference = const optional_t&

Const reference to a component.

Definition at line 34 of file ComponentContainer.hpp.

◆ dense_storage_t

template<typename Component , typename Allocator = std::allocator<Component>>
using ComponentContainer< Component, Allocator >::dense_storage_t = DenseArray<Component, Allocator>

Dense storage type.

Definition at line 30 of file ComponentContainer.hpp.

◆ iterator

template<typename Component , typename Allocator = std::allocator<Component>>
using ComponentContainer< Component, Allocator >::iterator = typename std::vector<optional_t, Allocator>::iterator

Iterator type.

Definition at line 35 of file ComponentContainer.hpp.

◆ optional_t

template<typename Component , typename Allocator = std::allocator<Component>>
using ComponentContainer< Component, Allocator >::optional_t = std::optional<Component>

Optional type for components.

Definition at line 28 of file ComponentContainer.hpp.

◆ reference

template<typename Component , typename Allocator = std::allocator<Component>>
using ComponentContainer< Component, Allocator >::reference = optional_t&

Reference to a component.

Definition at line 33 of file ComponentContainer.hpp.

◆ size_type

template<typename Component , typename Allocator = std::allocator<Component>>
using ComponentContainer< Component, Allocator >::size_type = typename sparse_storage_t::size_type

Size type.

Definition at line 31 of file ComponentContainer.hpp.

◆ sparse_storage_t

template<typename Component , typename Allocator = std::allocator<Component>>
using ComponentContainer< Component, Allocator >::sparse_storage_t = SparseArray<Component, Allocator>

Sparse storage type.

Definition at line 29 of file ComponentContainer.hpp.

◆ value_type

template<typename Component , typename Allocator = std::allocator<Component>>
using ComponentContainer< Component, Allocator >::value_type = Component

Type aliases for convenience.

The type of component stored.

Definition at line 27 of file ComponentContainer.hpp.

Constructor & Destructor Documentation

◆ ComponentContainer()

template<typename Component , typename Allocator = std::allocator<Component>>
ComponentContainer< Component, Allocator >::ComponentContainer ( )
inline

Default constructor.

Definition at line 41 of file ComponentContainer.hpp.

Member Function Documentation

◆ begin() [1/2]

template<typename Component , typename Allocator = std::allocator<Component>>
iterator ComponentContainer< Component, Allocator >::begin ( )
inline

Returns an iterator to the beginning of the component container.

Returns
An iterator to the first component.

Definition at line 68 of file ComponentContainer.hpp.

◆ begin() [2/2]

template<typename Component , typename Allocator = std::allocator<Component>>
const_iterator ComponentContainer< Component, Allocator >::begin ( ) const
inline

Returns a const iterator to the beginning of the component container.

Returns
A const iterator to the first component.

Definition at line 83 of file ComponentContainer.hpp.

◆ cbegin()

template<typename Component , typename Allocator = std::allocator<Component>>
const_iterator ComponentContainer< Component, Allocator >::cbegin ( ) const
inline

Returns a const iterator to the beginning of the component container.

Returns
A const iterator to the first component.

Definition at line 98 of file ComponentContainer.hpp.

◆ cend()

template<typename Component , typename Allocator = std::allocator<Component>>
const_iterator ComponentContainer< Component, Allocator >::cend ( ) const
inline

Returns a const iterator to the end of the component container.

Returns
A const iterator to one past the last component.

Definition at line 143 of file ComponentContainer.hpp.

◆ emplace_at()

template<typename Component , typename Allocator = std::allocator<Component>>
template<typename... Params>
void ComponentContainer< Component, Allocator >::emplace_at ( size_type id,
Params &&... params )
inline

Constructs and inserts a component at a specific ID.

Template Parameters
ParamsThe types of the parameters for the component constructor.
Parameters
idThe ID where the component should be inserted.
paramsThe parameters for constructing the component.

Definition at line 208 of file ComponentContainer.hpp.

◆ end() [1/2]

template<typename Component , typename Allocator = std::allocator<Component>>
iterator ComponentContainer< Component, Allocator >::end ( )
inline

Returns an iterator to the end of the component container.

Returns
An iterator to one past the last component.

Definition at line 113 of file ComponentContainer.hpp.

◆ end() [2/2]

template<typename Component , typename Allocator = std::allocator<Component>>
const_iterator ComponentContainer< Component, Allocator >::end ( ) const
inline

Returns a const iterator to the end of the component container.

Returns
A const iterator to one past the last component.

Definition at line 128 of file ComponentContainer.hpp.

◆ erase()

template<typename Component , typename Allocator = std::allocator<Component>>
void ComponentContainer< Component, Allocator >::erase ( size_type id)
inline

Removes a component at a specific ID.

Parameters
idThe ID of the component to erase.

Definition at line 249 of file ComponentContainer.hpp.

◆ get()

template<typename Component , typename Allocator = std::allocator<Component>>
std::optional< Component > ComponentContainer< Component, Allocator >::get ( const value_type & id) const
inline

Retrieves a component by its id.

Parameters
idThe id value to search for.
Returns
An optional containing the component if found, or std::nullopt if not.

Definition at line 229 of file ComponentContainer.hpp.

◆ insert_at() [1/2]

template<typename Component , typename Allocator = std::allocator<Component>>
void ComponentContainer< Component, Allocator >::insert_at ( size_type id,
Component && component )
inline

Inserts a component at a specific ID (move version).

Parameters
idThe ID where the component should be inserted.
componentThe component to insert (moved).

Definition at line 190 of file ComponentContainer.hpp.

◆ insert_at() [2/2]

template<typename Component , typename Allocator = std::allocator<Component>>
void ComponentContainer< Component, Allocator >::insert_at ( size_type id,
const Component & component )
inline

Inserts a component at a specific ID.

Parameters
idThe ID where the component should be inserted.
componentThe component to insert.

Definition at line 174 of file ComponentContainer.hpp.

◆ operator[]() [1/2]

template<typename Component , typename Allocator = std::allocator<Component>>
reference ComponentContainer< Component, Allocator >::operator[] ( size_t idx)
inline

Accesses the component at a given index.

Parameters
idxThe index of the component.
Returns
A reference to the component at the specified index.

Definition at line 49 of file ComponentContainer.hpp.

◆ operator[]() [2/2]

template<typename Component , typename Allocator = std::allocator<Component>>
const_reference ComponentContainer< Component, Allocator >::operator[] ( size_t idx) const
inline

Accesses the component at a given index (const version).

Parameters
idxThe index of the component.
Returns
A const reference to the component at the specified index.

Definition at line 59 of file ComponentContainer.hpp.

◆ optimize_storage()

template<typename Component , typename Allocator = std::allocator<Component>>
void ComponentContainer< Component, Allocator >::optimize_storage ( size_type sparse_threshold,
size_type dense_threshold )
inline

Optimizes the storage type based on thresholds for sparse and dense storage.

Parameters
sparse_thresholdThe threshold for switching to sparse storage.
dense_thresholdThe threshold for switching to dense storage.

Definition at line 281 of file ComponentContainer.hpp.

◆ resize()

template<typename Component , typename Allocator = std::allocator<Component>>
void ComponentContainer< Component, Allocator >::resize ( size_type new_size)
inline

Resizes the container to a new size.

Parameters
new_sizeThe new size of the container.

Definition at line 268 of file ComponentContainer.hpp.

◆ size()

template<typename Component , typename Allocator = std::allocator<Component>>
size_type ComponentContainer< Component, Allocator >::size ( ) const
inline

Returns the number of components stored in the array.

Returns
The number of components.

Definition at line 158 of file ComponentContainer.hpp.


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