co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
co_ecs::registry Class Reference

Registry is a container for all our entities and components. Components are stored in continuously in memory allowing for very fast iterations, a so called SoA approach. A set of unique components form an archetype, where every entity is mapped to an archetype. More...

#include <registry.hpp>

Inheritance diagram for co_ecs::registry:
co_ecs::base_registry

Public Member Functions

template<component... Components>
constexpr auto create (Components &&... args) -> entity_ref
 Creates a new entity and attaches the specified components to it.
 
constexpr auto get_entity (entity ent) noexcept -> entity_ref
 Retrieves a mutable reference to an entity.
 
constexpr auto get_entity (entity ent) const noexcept -> const_entity_ref
 Retrieves a constant reference to an entity.
 
constexpr auto get_entity_const (entity ent) const noexcept -> const_entity_ref
 Retrieves a constant reference to an entity (alias).
 
template<component_reference... Args>
constexpr auto view () -> co_ecs::view< Args... > requires(!const_component_references_v< Args... >)
 Create a non-const view based on component query in parameter pack.
 
template<component_reference... Args>
requires const_component_references_v<Args...>
constexpr auto view () const -> co_ecs::view< Args... >
 Create a const view based on component query in parameter pack.
 
template<component_reference... Args>
constexpr auto single () -> std::optional< std::tuple< Args... > > requires(!const_component_references_v< Args... >)
 Returns a single tuple of components matching Args, if available in the view.
 
template<component_reference... Args>
requires const_component_references_v<Args...>
constexpr auto single () const -> std::optional< std::tuple< Args... > >
 Returns a single tuple of components matching Args, if available in the view.
 
template<typename F >
requires (!detail::func_decomposer<F>::is_const)
constexpr void each (F &&func)
 Executes a given function on each entity that matches the specified component requirements.
 
template<typename F >
requires (detail::func_decomposer<F>::is_const)
constexpr void each (F &&func) const
 Executes a given function on each entity that matches the specified component requirements, without modifying the entities.
 
constexpr std::size_t size () const noexcept
 Returns the number of enitites in the registry.
 
constexpr bool empty () const noexcept
 Checks if the registry currently manages any entities.
 
- Public Member Functions inherited from co_ecs::base_registry
constexpr auto alive (entity ent) const noexcept -> bool
 Checks if the specified entity is currently active within the registry.
 
void destroy (entity ent)
 Destroys the given entity.
 
constexpr auto archetypes () noexcept -> class archetypes &
 Provides access to the modifiable list of archetypes in the registry.
 
constexpr auto archetypes () const noexcept -> const class archetypes &
 Provides access to the immutable list of archetypes in the registry.
 
constexpr auto reserve () -> placeholder_entity
 Reserves an entity in a thread-safe manner.
 
constexpr void sync ()
 Synchronizes concurently reserved entities.
 
template<component... C>
constexpr auto has (entity ent) const -> bool
 Checks if the specified entity has all the given components.
 
constexpr void visit (entity ent, auto &&func)
 Visit all components of an entity.
 
constexpr void visit (entity ent, auto &&func) const
 Visit all components of an entity (const variant).
 

Additional Inherited Members

- Protected Member Functions inherited from co_ecs::base_registry
template<component... Components>
constexpr auto create_impl (Components &&... args) -> entity
 
template<component C>
constexpr auto set_impl (entity ent) -> std::pair< bool, C * >
 
template<component C>
constexpr void remove (entity ent)
 
constexpr auto allocate () -> entity
 
entity move (entity ent, base_registry &dest)
 
entity copy (entity ent, base_registry &dest) const
 
entity move (entity ent, base_registry &dest, placeholder_entity placeholder)
 
entity copy (entity ent, base_registry &dest, placeholder_entity placeholder) const
 
entity clone (entity ent)
 
entity clone (entity ent, placeholder_entity placeholder)
 
- Static Protected Member Functions inherited from co_ecs::base_registry
template<component... Args>
static constexpr auto get_impl (auto &&self, entity ent) -> decltype(auto)
 
- Protected Attributes inherited from co_ecs::base_registry
entity_pool _entity_pool
 
class archetypes _archetypes
 
detail::sparse_map< typename entity::id_t, entity_location_entity_archetype_map
 

Detailed Description

Registry is a container for all our entities and components. Components are stored in continuously in memory allowing for very fast iterations, a so called SoA approach. A set of unique components form an archetype, where every entity is mapped to an archetype.

Member Function Documentation

◆ create()

template<component... Components>
constexpr auto co_ecs::registry::create ( Components &&... args) -> entity_ref
inlineconstexpr

Creates a new entity and attaches the specified components to it.

This method instantiates a new entity and assigns it the components provided as template arguments. It ensures all component types are unique, sets the components to the entity, and returns a reference to the newly created entity.

Example usage:

struct position {
int x;
int y;
};
int main() {
ecs::registry registry;
auto entity = registry.create<position>({1, 2});
return 0;
}
Registry is a container for all our entities and components. Components are stored in continuously in...
Definition registry.hpp:13
constexpr auto create(Components &&... args) -> entity_ref
Creates a new entity and attaches the specified components to it.
Definition registry.hpp:39
detail::handle< struct entity_tag_t > entity
Represents an entity, consisting of an ID and generation.
Definition entity.hpp:13
Template Parameters
ComponentComponent types to attach to the new entity.
Parameters
argsInstances of components to attach.
Returns
entity_ref A reference to the newly created entity, allowing further operations.

◆ each() [1/2]

template<typename F >
requires (!detail::func_decomposer<F>::is_const)
constexpr void co_ecs::registry::each ( F && func)
inlineconstexpr

Executes a given function on each entity that matches the specified component requirements.

This method iterates over each entity that has the required components and applies the function func.

Template Parameters
FThe type of the callable function to run on the entity components.
Parameters
funcA callable to run on entity components.

Example

struct position { double x, y; };
struct velocity { double vx, vy; };
void update_position(position& p, const velocity& v) {
p.x += v.vx;
p.y += v.vy;
}
ecs::registry registry;
registry.each(update_position);

◆ each() [2/2]

template<typename F >
requires (detail::func_decomposer<F>::is_const)
constexpr void co_ecs::registry::each ( F && func) const
inlineconstexpr

Executes a given function on each entity that matches the specified component requirements, without modifying the entities.

This constant version of each allows for iteration over entities in a read-only fashion. It is suitable for operations that only need to read component data without modifying it, ensuring that the entity data remains unchanged. This method shares the same optimization benefits as its non-constant counterpart.

Template Parameters
FThe type of the callable function to run on the entity components.
Parameters
funcA callable to run on entity components, must not modify the entities.

◆ empty()

constexpr bool co_ecs::registry::empty ( ) const
inlineconstexprnoexcept

Checks if the registry currently manages any entities.

Returns
bool Returns true if the registry has no entities, false otherwise.

◆ get_entity() [1/2]

constexpr auto co_ecs::registry::get_entity ( entity ent) const -> const_entity_ref
inlineconstexprnoexcept

Retrieves a constant reference to an entity.

This method returns a constant reference to the specified entity from the registry.

Parameters
entThe entity to retrieve.
Returns
A constant reference to the specified entity.
Note
This method is marked as [[nodiscard]] and constexpr.

◆ get_entity() [2/2]

constexpr auto co_ecs::registry::get_entity ( entity ent) -> entity_ref
inlineconstexprnoexcept

Retrieves a mutable reference to an entity.

This method returns a mutable reference to the specified entity from the registry.

Parameters
entThe entity to retrieve.
Returns
A mutable reference to the specified entity.
Note
This method is marked as [[nodiscard]] and constexpr.

◆ get_entity_const()

constexpr auto co_ecs::registry::get_entity_const ( entity ent) const -> const_entity_ref
inlineconstexprnoexcept

Retrieves a constant reference to an entity (alias).

This method is an alias for get_entity to return a constant reference to the specified entity from the registry.

Parameters
entThe entity to retrieve.
Returns
A constant reference to the specified entity.
Note
This method is marked as [[nodiscard]] and constexpr.

◆ single() [1/2]

template<component_reference... Args>
constexpr auto co_ecs::registry::single ( ) -> std::optional<std::tuple<Args...>> requires(!const_component_references_v<Args...>)
inlineconstexpr

Returns a single tuple of components matching Args, if available in the view.

Returns
Optional tuple of components if found, otherwise empty optional

◆ single() [2/2]

template<component_reference... Args>
requires const_component_references_v<Args...>
constexpr auto co_ecs::registry::single ( ) const -> std::optional<std::tuple<Args...>>
inlineconstexpr

Returns a single tuple of components matching Args, if available in the view.

This method is available in const registry and allows accessing a single tuple of components matching Args. It returns an optional tuple, which is empty if no entities in the view match the component requirements.

Returns
Optional tuple of components if found, otherwise empty optional

◆ size()

constexpr std::size_t co_ecs::registry::size ( ) const
inlineconstexprnoexcept

Returns the number of enitites in the registry.

Returns
Number of entities present in the registry

◆ view() [1/2]

template<component_reference... Args>
constexpr auto co_ecs::registry::view ( ) -> co_ecs::view<Args...> requires(!const_component_references_v<Args...>)
inlineconstexpr

Create a non-const view based on component query in parameter pack.

Template Parameters
ArgsComponent references
Returns
view<Args...> A view

◆ view() [2/2]

template<component_reference... Args>
requires const_component_references_v<Args...>
constexpr auto co_ecs::registry::view ( ) const -> co_ecs::view<Args...>
inlineconstexpr

Create a const view based on component query in parameter pack.

Template Parameters
ArgsComponent references
Returns
view<Args...> A view

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