Template-based storage container for components of a specific type. More...
#include <ComponentStorage.hpp>
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< Entity > | m_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. | |
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.
| T | The component type to be stored. |
Definition at line 20 of file ComponentStorage.hpp.
|
inline |
Adds or replaces a component for an entity.
| Args | Parameter types forwarded to the component constructor. |
| entity | The entity to add/replace the component for. |
| args | Arguments forwarded to the component's constructor. |
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.
|
inlineoverridevirtual |
Called by the Registry when an entity is destroyed.
| entity | The 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.
|
inline |
Retrieves a reference to the component for an entity.
| entity | The entity whose component to retrieve. |
Definition at line 100 of file ComponentStorage.hpp.
|
inline |
Returns the list of all entities that have this component type.
Definition at line 124 of file ComponentStorage.hpp.
|
inline |
Checks if the given entity has a component of type T.
| entity | The entity to check. |
Definition at line 38 of file ComponentStorage.hpp.
|
inline |
Registers a callback to be invoked when a component is created.
| cb | The callback function to register. |
Callback is invoked with the entity and component reference.
Definition at line 115 of file ComponentStorage.hpp.
|
inline |
Registers a callback to be invoked when a component is destroyed.
| cb | The callback function to register. |
Callback is invoked with the entity and component reference.
Definition at line 120 of file ComponentStorage.hpp.
|
inline |
Removes the component from an entity.
| entity | The 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.
|
private |
Dense array of component instances, cache-optimized for iteration.
Definition at line 23 of file ComponentStorage.hpp.
|
private |
Maps entity ID to the component storage index, or -1 if not present.
Definition at line 27 of file ComponentStorage.hpp.
|
private |
Maps component storage index to the entity that owns the component.
Definition at line 25 of file ComponentStorage.hpp.
|
private |
Callbacks invoked when a component is created for an entity.
Definition at line 30 of file ComponentStorage.hpp.
|
private |
Callbacks invoked when a component is destroyed for an entity.
Definition at line 32 of file ComponentStorage.hpp.