VEX Engine
Retro looking game engine made in vulkan mostly because i wanted to understand it better.
Loading...
Searching...
No Matches
vex::ComponentStorage< T > Class Template Reference

Template-based storage container for components of a specific type. More...

#include <ComponentStorage.hpp>

Inheritance diagram for vex::ComponentStorage< T >:
vex::IComponentStorage

Public Member Functions

bool has (Entity entity) const
 Checks if the given entity has a component of type T.
template<typename... Args>
T & add_or_replace (Entity entity, Args &&... args)
 Adds or replaces a component for an entity.
void remove (Entity entity)
 Removes the component from an entity.
T & get (Entity entity)
 Retrieves a reference to the component for an entity.
void entity_destroyed (Entity entity) override
 Called by the Registry when an entity is destroyed.
void on_create (std::function< void(Entity, T &)> cb)
 Registers a callback to be invoked when a component is created.
void on_destroy (std::function< void(Entity, T &)> cb)
 Registers a callback to be invoked when a component is destroyed.
const std::vector< Entity > & get_entities () const
 Returns the list of all entities that have this component type.
Public Member Functions inherited from vex::IComponentStorage
virtual ~IComponentStorage ()=default
 Virtual destructor for proper cleanup of derived classes.

Private Attributes

std::vector< T > m_components
 Dense array of component instances, cache-optimized for iteration.
std::vector< Entitym_indexToEntity
 Maps component storage index to the entity that owns the component.
std::vector< size_t > m_entityToIndex
 Maps entity ID to the component storage index, or -1 if not present.
std::vector< std::function< void(Entity, T &)> > m_onCreate
 Callbacks invoked when a component is created for an entity.
std::vector< std::function< void(Entity, T &)> > m_onDestroy
 Callbacks invoked when a component is destroyed for an entity.

Detailed Description

template<typename T>
class vex::ComponentStorage< T >

Template-based storage container for components of a specific type.

ComponentStorage manages all instances of a particular component type across all entities that possess it. It uses a dense packing strategy (packed array) to maintain cache-friendly memory layout while supporting efficient entity lookups.

Template Parameters
TThe component type to be stored.
Note
This class maintains three parallel data structures:
  • m_components: Dense array of actual component instances
  • m_indexToEntity: Mapping from component index to entity ID
  • m_entityToIndex: Mapping from entity ID to component index

Definition at line 20 of file ComponentStorage.hpp.

Member Function Documentation

◆ add_or_replace()

template<typename T>
template<typename... Args>
T & vex::ComponentStorage< T >::add_or_replace ( Entity entity,
Args &&... args )
inline

Adds or replaces a component for an entity.

Template Parameters
ArgsParameter types forwarded to the component constructor.
Parameters
entityThe entity to add/replace the component for.
argsArguments forwarded to the component's constructor.
Returns
Reference to the newly added or replaced component.

If the entity already has the component, it is replaced in-place. If not, a new component is created and all onCreate callbacks are invoked.

Definition at line 50 of file ComponentStorage.hpp.

◆ entity_destroyed()

template<typename T>
void vex::ComponentStorage< T >::entity_destroyed ( Entity entity)
inlineoverridevirtual

Called by the Registry when an entity is destroyed.

Parameters
entityThe entity being destroyed.

Invokes the remove() method to clean up the component and invoke callbacks.

Implements vex::IComponentStorage.

Definition at line 108 of file ComponentStorage.hpp.

◆ get()

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

Retrieves a reference to the component for an entity.

Parameters
entityThe entity whose component to retrieve.
Returns
Mutable reference to the component.
Precondition
The entity must have the component (checked via assert in debug builds).

Definition at line 100 of file ComponentStorage.hpp.

◆ get_entities()

template<typename T>
const std::vector< Entity > & vex::ComponentStorage< T >::get_entities ( ) const
inline

Returns the list of all entities that have this component type.

Returns
Const reference to the vector of entities.

Definition at line 124 of file ComponentStorage.hpp.

◆ has()

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

Checks if the given entity has a component of type T.

Parameters
entityThe entity to check.
Returns
True if the entity has the component, false otherwise.

Definition at line 38 of file ComponentStorage.hpp.

◆ on_create()

template<typename T>
void vex::ComponentStorage< T >::on_create ( std::function< void(Entity, T &)> cb)
inline

Registers a callback to be invoked when a component is created.

Parameters
cbThe callback function to register.

Callback is invoked with the entity and component reference.

Definition at line 115 of file ComponentStorage.hpp.

◆ on_destroy()

template<typename T>
void vex::ComponentStorage< T >::on_destroy ( std::function< void(Entity, T &)> cb)
inline

Registers a callback to be invoked when a component is destroyed.

Parameters
cbThe callback function to register.

Callback is invoked with the entity and component reference.

Definition at line 120 of file ComponentStorage.hpp.

◆ remove()

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

Removes the component from an entity.

Parameters
entityThe entity to remove the component from.

If the entity does not have the component, this operation is a no-op. All onDestroy callbacks are invoked before removal. Uses swap-and-pop to maintain dense packing without gaps in the storage array.

Definition at line 76 of file ComponentStorage.hpp.

Member Data Documentation

◆ m_components

template<typename T>
std::vector<T> vex::ComponentStorage< T >::m_components
private

Dense array of component instances, cache-optimized for iteration.

Definition at line 23 of file ComponentStorage.hpp.

◆ m_entityToIndex

template<typename T>
std::vector<size_t> vex::ComponentStorage< T >::m_entityToIndex
private

Maps entity ID to the component storage index, or -1 if not present.

Definition at line 27 of file ComponentStorage.hpp.

◆ m_indexToEntity

template<typename T>
std::vector<Entity> vex::ComponentStorage< T >::m_indexToEntity
private

Maps component storage index to the entity that owns the component.

Definition at line 25 of file ComponentStorage.hpp.

◆ m_onCreate

template<typename T>
std::vector<std::function<void(Entity, T&)> > vex::ComponentStorage< T >::m_onCreate
private

Callbacks invoked when a component is created for an entity.

Definition at line 30 of file ComponentStorage.hpp.

◆ m_onDestroy

template<typename T>
std::vector<std::function<void(Entity, T&)> > vex::ComponentStorage< T >::m_onDestroy
private

Callbacks invoked when a component is destroyed for an entity.

Definition at line 32 of file ComponentStorage.hpp.


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