SolarSim
Loading...
Searching...
No Matches
solarsim::Registry Class Reference

Manages entities and their components in the ECS. More...

#include <registry.hpp>

Data Structures

struct  IComponentStorage
 Base interface for type-erased component storage. More...
struct  ComponentStorage
 Typed component storage for a specific component type. More...

Public Member Functions

Entity createEntity ()
 Create a new entity.
template<typename Component>
void addComponent (Entity e, Component c)
 Add a component to an entity.
template<typename Component>
Component & getComponent (Entity e)
 Get a component from an entity.
template<typename Component>
bool hasComponent (Entity e)
 Check if an entity has a specific component.
template<typename... Components>
std::vector< Entityview ()
 Get all entities that have all specified components.
const auto & getEntities () const

Private Member Functions

template<typename Component>
std::unordered_map< Entity, Component > & getComponentStorage ()
 Get or create the storage for a component type.

Private Attributes

std::unordered_set< Entityentities
uint32_t nextEntity = 0
std::unordered_map< std::type_index, std::unique_ptr< IComponentStorage > > componentStorages

Detailed Description

Manages entities and their components in the ECS.

Member Function Documentation

◆ addComponent()

template<typename Component>
void solarsim::Registry::addComponent ( Entity e,
Component c )
inline

Add a component to an entity.

Template Parameters
ComponentType of component to add
Parameters
eEntity to add the component to
cComponent instance to add
Note
Overwrites existing component of same type
Warning
Entity must exist (created via createEntity())

◆ createEntity()

Entity solarsim::Registry::createEntity ( )
inlinenodiscard

Create a new entity.

Returns
Entity The newly created entity ID

◆ getComponent()

template<typename Component>
Component & solarsim::Registry::getComponent ( Entity e)
inline

Get a component from an entity.

Template Parameters
ComponentType of component to retrieve
Parameters
eEntity to get the component from
Returns
Component& Reference to the component
Exceptions
std::out_of_rangeif entity doesn't have the component

◆ getComponentStorage()

template<typename Component>
std::unordered_map< Entity, Component > & solarsim::Registry::getComponentStorage ( )
inlineprivate

Get or create the storage for a component type.

Template Parameters
ComponentComponent type to get storage for
Returns
std::unordered_map<Entity, Component>& Reference to the component storage
Note
Creates storage on first access for a component type

◆ getEntities()

const auto & solarsim::Registry::getEntities ( ) const
inline

Get all entities currently in scene

Returns
std::unordered_seet<Entity> A set of all entities

◆ hasComponent()

template<typename Component>
bool solarsim::Registry::hasComponent ( Entity e)
inline

Check if an entity has a specific component.

Template Parameters
ComponentType of component to check for
Parameters
eEntity to check
Returns
bool True if entity has the component

◆ view()

template<typename... Components>
std::vector< Entity > solarsim::Registry::view ( )
inlinenodiscard

Get all entities that have all specified components.

Template Parameters
ComponentsComponent types to filter by
Returns
std::vector<Entity> Entities matching the component filter

Field Documentation

◆ componentStorages

std::unordered_map<std::type_index, std::unique_ptr<IComponentStorage> > solarsim::Registry::componentStorages
private

Type-indexed component storage

◆ entities

std::unordered_set<Entity> solarsim::Registry::entities
private

Set of all active entities

◆ nextEntity

uint32_t solarsim::Registry::nextEntity = 0
private

Next available entity ID


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