Crimson Engine 1.0.0
A high-performance C++20 game engine prototype showcasing a custom ECS, Vulkan renderer, and ImGui editor.
Loading...
Searching...
No Matches
ComponentStorage< T > Class Template Reference

Template class storing components contiguously in memory for optimal cache usage. More...

#include <ComponentStorage.hpp>

Inheritance diagram for ComponentStorage< T >:
IStorage

Public Member Functions

void add (Entity e, T comp)
 Adds a component to an entity.
bool has (Entity e) const
 Checks if an entity possesses this component.
void remove (Entity e)
 Removes the component from an entity.
void removeEntity (Entity e) override
 Polymorphic interface to remove entity component.
T & get (Entity e)
 Retrieves the component associated with an entity.
const std::vector< Entity > & getEntities () const override
 Retrieves all entities with this component.
size_t size () const override
 Returns the size of the storage.
auto begin ()
 Gets start iterator of stored component data.
auto end ()
 Gets end iterator of stored component data.
auto entityBegin ()
 Gets start iterator of stored entity list.
auto entityEnd ()
 Gets end iterator of stored entity list.
Public Member Functions inherited from IStorage
virtual ~IStorage ()=default
 Virtual destructor.

Private Attributes

std::vector< T > data
 Contiguous array of component instances.
std::vector< Entity > entities
 Entity mapping matching data array order.
std::unordered_map< Entity, size_t > entityToIndex
 Mapping for quick entity component lookup.

Detailed Description

template<typename T>
class ComponentStorage< T >

Template class storing components contiguously in memory for optimal cache usage.

Template Parameters
TType of component being stored.

Member Function Documentation

◆ add()

template<typename T>
void ComponentStorage< T >::add ( Entity e,
T comp )
inline

Adds a component to an entity.

Parameters
eThe target entity.
compThe component instance.

◆ begin()

template<typename T>
auto ComponentStorage< T >::begin ( )
inline

Gets start iterator of stored component data.

Returns
Iterator.

◆ end()

template<typename T>
auto ComponentStorage< T >::end ( )
inline

Gets end iterator of stored component data.

Returns
Iterator.

◆ entityBegin()

template<typename T>
auto ComponentStorage< T >::entityBegin ( )
inline

Gets start iterator of stored entity list.

Returns
Iterator.

◆ entityEnd()

template<typename T>
auto ComponentStorage< T >::entityEnd ( )
inline

Gets end iterator of stored entity list.

Returns
Iterator.

◆ get()

template<typename T>
T & ComponentStorage< T >::get ( Entity e)
inline

Retrieves the component associated with an entity.

Parameters
eThe target entity.
Returns
Reference to the component instance.

◆ getEntities()

template<typename T>
const std::vector< Entity > & ComponentStorage< T >::getEntities ( ) const
inlineoverridevirtual

Retrieves all entities with this component.

Returns
Vector of entities.

Implements IStorage.

◆ has()

template<typename T>
bool ComponentStorage< T >::has ( Entity e) const
inline

Checks if an entity possesses this component.

Parameters
eThe entity to check.
Returns
True if the component exists, false otherwise.

◆ remove()

template<typename T>
void ComponentStorage< T >::remove ( Entity e)
inline

Removes the component from an entity.

Parameters
eThe entity to remove.

◆ removeEntity()

template<typename T>
void ComponentStorage< T >::removeEntity ( Entity e)
inlineoverridevirtual

Polymorphic interface to remove entity component.

Parameters
eThe entity to remove.

Implements IStorage.

◆ size()

template<typename T>
size_t ComponentStorage< T >::size ( ) const
inlineoverridevirtual

Returns the size of the storage.

Returns
Element count.

Implements IStorage.

Member Data Documentation

◆ data

template<typename T>
std::vector<T> ComponentStorage< T >::data
private

Contiguous array of component instances.

◆ entities

template<typename T>
std::vector<Entity> ComponentStorage< T >::entities
private

Entity mapping matching data array order.

◆ entityToIndex

template<typename T>
std::unordered_map<Entity, size_t> ComponentStorage< T >::entityToIndex
private

Mapping for quick entity component lookup.


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