Helios Engine 0.1.0
A modular ECS based data-oriented C++23 game engine
 
Loading...
Searching...
No Matches
helios::ecs::details::Components Class Reference

Manager for all component storages in the ECS world. More...

#include <components_manager.hpp>

Public Member Functions

 Components ()=default
 
 Components (const Components &)=delete
 
 Components (Components &&)=default
 
 ~Components ()=default
 
Componentsoperator= (const Components &)=delete
 
Componentsoperator= (Components &&)=default
 
void Clear () noexcept
 Clears all component storages.
 
void RemoveAllComponents (Entity entity)
 Removes all components from the specified entity.
 
template<ComponentTrait T>
void AddComponent (Entity entity, T &&component)
 Adds component to entity (move version).
 
template<ComponentTrait T, typename... Args>
requires std::constructible_from<T, Args...>
void EmplaceComponent (Entity entity, Args &&... args)
 Constructs component in-place for entity.
 
template<ComponentTrait T>
void RemoveComponent (Entity entity)
 Removes component from entity.
 
template<ComponentTrait T>
T & GetComponent (Entity entity)
 Gets mutable reference to entity's component.
 
template<ComponentTrait T>
const T & GetComponent (Entity entity) const
 Gets const reference to entity's component.
 
template<ComponentTrait T>
T * TryGetComponent (Entity entity)
 Attempts to get mutable pointer to entity's component.
 
template<ComponentTrait T>
const T * TryGetComponent (Entity entity) const
 Attempts to get const pointer to entity's component.
 
template<ComponentTrait T>
bool HasComponent (Entity entity) const
 Checks if entity has the specified component type.
 
template<ComponentTrait T>
ComponentStorage< T > & GetStorage ()
 Gets typed storage for component type T.
 
template<ComponentTrait T>
const ComponentStorage< T > & GetStorage () const
 Gets const typed storage for component type T.
 
std::vector< ComponentTypeInfoGetComponentTypes (Entity entity) const
 Gets all component types for the specified entity.
 

Detailed Description

Manager for all component storages in the ECS world.

Maintains a registry of type-erased component storages, providing a unified interface for component operations across all types. Each component type gets its own ComponentStorage<T> instance for type-safe operations.

Key features:

  • Type-erased storage management for heterogeneous component types
  • Lazy storage creation - storages are created only when first component is added
  • Efficient component lookup and manipulation
  • Bulk operations for entity lifecycle management
Note
Not thread-safe. All operations should be performed from the main thread.

Definition at line 363 of file components_manager.hpp.

Constructor & Destructor Documentation

◆ Components() [1/3]

helios::ecs::details::Components::Components ( )
default

◆ Components() [2/3]

helios::ecs::details::Components::Components ( const Components )
delete

◆ Components() [3/3]

helios::ecs::details::Components::Components ( Components &&  )
default

◆ ~Components()

helios::ecs::details::Components::~Components ( )
default

Member Function Documentation

◆ AddComponent()

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

Adds component to entity (move version).

Moves the component into storage. Creates storage if needed. If entity already has this component type, it will be replaced. Time complexity: O(1) amortized.

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

Definition at line 549 of file components_manager.hpp.

549 {
550 using ComponentType = std::decay_t<T>;
551 HELIOS_ASSERT(entity.Valid(), "Failed to add component '{}': Entity with index '{}' is invalid!",
552 ComponentNameOf<ComponentType>(), entity.Index());
553 GetOrCreateStorage<ComponentType>().Insert(entity, std::forward<T>(component));
554}
#define HELIOS_ASSERT(condition,...)
Assertion macro that aborts execution in debug builds.
Definition assert.hpp:140
BasicQuery< World, Allocator, Components... > Query
Type alias for query with mutable world access.
Definition query.hpp:2481

◆ Clear()

void helios::ecs::details::Components::Clear ( )
inlinenoexcept

Clears all component storages.

Removes all components of all types and resets the manager state. Time complexity: O(1).

Definition at line 378 of file components_manager.hpp.

378{ storages_.clear(); }

◆ EmplaceComponent()

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

Constructs component in-place for entity.

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

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

Definition at line 558 of file components_manager.hpp.

558 {
559 HELIOS_ASSERT(entity.Valid(), "Failed to emplace component '{}': Entity with index '{}' is invalid!",
560 ComponentNameOf<T>(), entity.Index());
561 GetOrCreateStorage<T>().Emplace(entity, std::forward<Args>(args)...);
562}

◆ GetComponent() [1/2]

template<ComponentTrait T>
T & helios::ecs::details::Components::GetComponent ( Entity  entity)
inline

Gets mutable reference to entity's component.

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

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

Definition at line 439 of file components_manager.hpp.

439 {
440 return GetStorage<T>().Get(entity);
441 }

◆ GetComponent() [2/2]

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

Gets const reference to entity's component.

Returns direct const reference to the component for read-only access. Time complexity: O(1).

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

Definition at line 453 of file components_manager.hpp.

453 {
454 return GetStorage<T>().Get(entity);
455 }

◆ GetComponentTypes()

std::vector< ComponentTypeInfo > helios::ecs::details::Components::GetComponentTypes ( Entity  entity) const
inline

Gets all component types for the specified entity.

Scans all storages to build list of component types present on entity. Used for archetype management and debugging. Time complexity: O(S) where S is the number of component types.

Parameters
entityEntity to get component types for
Returns
Vector of ComponentTypeInfo for all components on the entity

Definition at line 628 of file components_manager.hpp.

628 {
629 std::vector<ComponentTypeInfo> types;
630 types.reserve(storages_.size()); // Reserve worst-case size
631
632 for (const auto& [_, storage] : storages_) {
633 if (storage->Contains(entity)) {
634 types.push_back(storage->GetTypeInfo());
635 }
636 }
637
638 types.shrink_to_fit(); // Reduce to actual size
639 return types;
640}

◆ GetStorage() [1/2]

template<ComponentTrait T>
ComponentStorage< T > & helios::ecs::details::Components::GetStorage ( )
inline

Gets typed storage for component type T.

Returns reference to the ComponentStorage<T> for direct access. Time complexity: O(1) average case.

Warning
Triggers assertion if storage doesn't exist for type T.
Template Parameters
TComponent type
Returns
Reference to ComponentStorage<T>

Definition at line 611 of file components_manager.hpp.

611 {
612 const ComponentTypeId type_id = ComponentTypeIdOf<T>();
613 const auto it = storages_.find(type_id);
614 HELIOS_ASSERT(it != storages_.end(), "Failed to get storage: Component '{}' storage does not exist!",
616 return static_cast<ComponentStorage<T>&>(*it->second);
617}
size_t ComponentTypeId
Type ID for components.

◆ GetStorage() [2/2]

template<ComponentTrait T>
const ComponentStorage< T > & helios::ecs::details::Components::GetStorage ( ) const
inline

Gets const typed storage for component type T.

Returns const reference to the ComponentStorage<T> for read-only access. Time complexity: O(1) average case.

Warning
Triggers assertion if storage doesn't exist for type T.
Template Parameters
TComponent type
Returns
Const reference to ComponentStorage<T>

Definition at line 620 of file components_manager.hpp.

620 {
621 const ComponentTypeId type_id = ComponentTypeIdOf<T>();
622 const auto it = storages_.find(type_id);
623 HELIOS_ASSERT(it != storages_.end(), "Failed to get storage: Component '{}' storage does not exist!",
625 return static_cast<const ComponentStorage<T>&>(*it->second);
626}

◆ HasComponent()

template<ComponentTrait T>
bool helios::ecs::details::Components::HasComponent ( Entity  entity) const
inline

Checks if entity has the specified component type.

Performs fast lookup to determine if entity has a component of type T. Time complexity: O(1) average case.

Warning
Triggers assertion if entity is invalid.
Template Parameters
TComponent type to check
Parameters
entityEntity to check
Returns
True if entity has component of type T, false otherwise

Definition at line 600 of file components_manager.hpp.

600 {
601 HELIOS_ASSERT(entity.Valid(), "Failed to check if entity has component '{}': Entity with index '{}' is invalid!",
602 ComponentNameOf<T>(), entity.Index());
603 const ComponentTypeId type_id = ComponentTypeIdOf<T>();
604 if (const auto it = storages_.find(type_id); it != storages_.end()) {
605 return static_cast<const ComponentStorage<T>&>(*it->second).Contains(entity);
606 }
607 return false;
608}

◆ operator=() [1/2]

Components & helios::ecs::details::Components::operator= ( Components &&  )
default

◆ operator=() [2/2]

Components & helios::ecs::details::Components::operator= ( const Components )
delete

◆ RemoveAllComponents()

void helios::ecs::details::Components::RemoveAllComponents ( Entity  entity)
inline

Removes all components from the specified entity.

Iterates through all component storages and removes entity's components. Used when destroying entities or clearing their component sets. Time complexity: O(S) where S is the number of component types.

Warning
Triggers assertion if entity is invalid.
Parameters
entityEntity to remove all components from

Definition at line 540 of file components_manager.hpp.

540 {
541 HELIOS_ASSERT(entity.Valid(), "Failed to remove all components from entity: Entity with index '{}' is invalid!",
542 entity.Index());
543 for (auto& [_, storage] : storages_) {
544 storage->TryRemove(entity);
545 }
546}

◆ RemoveComponent()

template<ComponentTrait T>
void helios::ecs::details::Components::RemoveComponent ( Entity  entity)
inline

Removes component from entity.

Removes the specified component type from the entity. Time complexity: O(1).

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

Definition at line 565 of file components_manager.hpp.

565 {
566 HELIOS_ASSERT(entity.Valid(), "Failed to remove component '{}': Entity with index '{}' is invalid!",
567 ComponentNameOf<T>(), entity.Index());
568 const ComponentTypeId type_id = ComponentTypeIdOf<T>();
569 if (const auto it = storages_.find(type_id); it != storages_.end()) {
570 static_cast<ComponentStorage<T>&>(*it->second).Remove(entity);
571 } else {
572 HELIOS_ASSERT(false, "Failed to remove component '{}': Entity with index '{}' does not have this component!",
573 ComponentNameOf<T>(), entity.Index());
574 }
575}

◆ TryGetComponent() [1/2]

template<ComponentTrait T>
T * helios::ecs::details::Components::TryGetComponent ( Entity  entity)
inline

Attempts to get mutable pointer to entity's component.

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

Warning
Triggers assertion if entity is invalid.
Template Parameters
TComponent type to get
Parameters
entityEntity to get component from
Returns
Pointer to component, or nullptr if entity doesn't have the component

Definition at line 578 of file components_manager.hpp.

578 {
579 HELIOS_ASSERT(entity.Valid(), "Failed to try get component '{}': Entity with index '{}' is invalid!",
580 ComponentNameOf<T>(), entity.Index());
581 const ComponentTypeId type_id = ComponentTypeIdOf<T>();
582 if (const auto it = storages_.find(type_id); it != storages_.end()) {
583 return static_cast<ComponentStorage<T>&>(*it->second).TryGet(entity);
584 }
585 return nullptr;
586}

◆ TryGetComponent() [2/2]

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

Attempts to get const pointer to entity's component.

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

Warning
Triggers assertion if entity is invalid.
Template Parameters
TComponent type to get
Parameters
entityEntity to get component from
Returns
Const pointer to component, or nullptr if entity doesn't have the component

Definition at line 589 of file components_manager.hpp.

589 {
590 HELIOS_ASSERT(entity.Valid(), "Failed to try get component '{}': Entity with index '{}' is invalid!",
591 ComponentNameOf<T>(), entity.Index());
592 const ComponentTypeId type_id = ComponentTypeIdOf<T>();
593 if (const auto it = storages_.find(type_id); it != storages_.end()) {
594 return static_cast<const ComponentStorage<T>&>(*it->second).TryGet(entity);
595 }
596 return nullptr;
597}