co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
co_ecs::view< Args > Class Template Reference

A view lets you get a range over components of Args out of a registry. More...

#include <view.hpp>

Public Member Functions

 view (registry_type registry) noexcept
 Constructs a new view object.
 
auto single () -> std::optional< std::tuple< Args... > > requires(!is_const)
 Returns a single tuple of components matching Args, if available in the view.
 
auto single () const -> std::optional< std::tuple< Args... > > requires(is_const)
 Returns a single tuple of components matching Args, if available in the view (const version).
 
auto each () -> decltype(auto) requires(!is_const)
 Returns an iterator that yields a std::tuple<Args...>.
 
auto each () const -> decltype(auto) requires(is_const)
 Returns an iterator that yields a std::tuple<Args...> (const version).
 
void each (auto &&func)
 Runs a function on every entity that matches the Args requirement.
 
void each (auto &&func) const
 Runs a function on every entity that matches the Args requirement (const version).
 
void par_each (auto &&func)
 Runs a function on every entity that matches the Args requirement in parallel.
 
void par_each (auto &&func) const
 Runs a function on every entity that matches the Args requirement in parallel (const version).
 
auto chunks () -> decltype(auto)
 Gets the chunks range.
 
auto chunks () const -> decltype(auto)
 Gets the const chunks range.
 

Detailed Description

template<component_reference... Args>
class co_ecs::view< Args >

A view lets you get a range over components of Args out of a registry.

Views represent a slice of a registry restricting the access to a particular components.

// Iterate using iterators:
for (auto [pos, vel] : view.each()) {
pos.x += vel.x;
pos.y += vel.y;
}
// Alternativelly, iterate using a callback
view.each([](auto& pos, const auto& vel) {
pos.x += vel.x;
pos.y += vel.y;
});
Registry is a container for all our entities and components. Components are stored in continuously in...
Definition registry.hpp:13
constexpr void each(F &&func)
Executes a given function on each entity that matches the specified component requirements.
Definition registry.hpp:147
A view lets you get a range over components of Args out of a registry.
Definition view.hpp:36
auto each() -> decltype(auto) requires(!is_const)
Returns an iterator that yields a std::tuple<Args...>.
Definition view.hpp:90
view(registry_type registry) noexcept
Constructs a new view object.
Definition view.hpp:50
Note
A view isn't invalidated when there are changes made to the registry, which allows creating one and re-using it over time.
Template Parameters
ArgsComponent reference types

Constructor & Destructor Documentation

◆ view()

template<component_reference... Args>
co_ecs::view< Args >::view ( registry_type registry)
inlineexplicitnoexcept

Constructs a new view object.

Parameters
registryReference to the registry.

Member Function Documentation

◆ chunks() [1/2]

template<component_reference... Args>
auto co_ecs::view< Args >::chunks ( ) -> decltype(auto)
inline

Gets the chunks range.

Returns
Chunks.

◆ chunks() [2/2]

template<component_reference... Args>
auto co_ecs::view< Args >::chunks ( ) const -> decltype(auto)
inline

Gets the const chunks range.

Returns
Chunks.

◆ each() [1/4]

template<component_reference... Args>
auto co_ecs::view< Args >::each ( ) -> decltype(auto) requires(!is_const)
inline

Returns an iterator that yields a std::tuple<Args...>.

Returns
decltype(auto) Iterator.

◆ each() [2/4]

template<component_reference... Args>
auto co_ecs::view< Args >::each ( ) const -> decltype(auto) requires(is_const)
inline

Returns an iterator that yields a std::tuple<Args...> (const version).

Returns
decltype(auto) Iterator.

◆ each() [3/4]

template<component_reference... Args>
void co_ecs::view< Args >::each ( auto && func)
inline

Runs a function on every entity that matches the Args requirement.

Parameters
funcA callable to run on entity components.

◆ each() [4/4]

template<component_reference... Args>
void co_ecs::view< Args >::each ( auto && func) const
inline

Runs a function on every entity that matches the Args requirement (const version).

Parameters
funcA callable to run on entity components.
Note
This method is similar to the non-const each() but is available in const views.

◆ par_each() [1/2]

template<component_reference... Args>
void co_ecs::view< Args >::par_each ( auto && func)
inline

Runs a function on every entity that matches the Args requirement in parallel.

Parameters
funcA callable to run on entity components.

◆ par_each() [2/2]

template<component_reference... Args>
void co_ecs::view< Args >::par_each ( auto && func) const
inline

Runs a function on every entity that matches the Args requirement in parallel (const version).

Parameters
funcA callable to run on entity components.
Note
This method is similar to the non-const par_each() but is available in const views.

◆ single() [1/2]

template<component_reference... Args>
auto co_ecs::view< Args >::single ( ) -> std::optional<std::tuple<Args...>> requires(!is_const)
inline

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

This method is available in const views 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.

// Only one instance with directional_light component is supported
auto [dir_light] = *view.single();
auto single() -> std::optional< std::tuple< Args... > > requires(!is_const)
Returns a single tuple of components matching Args, if available in the view.
Definition view.hpp:64
Returns
Optional tuple of components if found, otherwise empty optional.

◆ single() [2/2]

template<component_reference... Args>
auto co_ecs::view< Args >::single ( ) const -> std::optional<std::tuple<Args...>> requires(is_const)
inline

Returns a single tuple of components matching Args, if available in the view (const version).

Returns
Optional tuple of components if found, otherwise empty optional.

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