co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
registry.hpp
Go to the documentation of this file.
1#pragma once
2
6
7
8namespace co_ecs {
9
13class registry : public base_registry {
14public:
38 template<component... Components>
39 constexpr auto create(Components&&... args) -> entity_ref {
40 return get_entity(create_impl(std::forward<Components>(args)...));
41 }
42
50 [[nodiscard]]
51 constexpr auto get_entity(entity ent) noexcept -> entity_ref {
52 return entity_ref{ *this, ent };
53 }
54
62 [[nodiscard]]
63 constexpr auto get_entity(entity ent) const noexcept -> const_entity_ref {
64 return const_entity_ref{ *this, ent };
65 }
66
75 [[nodiscard]]
76 constexpr auto get_entity_const(entity ent) const noexcept -> const_entity_ref {
77 return get_entity(ent);
78 }
79
84 template<component_reference... Args>
85 constexpr auto view() -> co_ecs::view<Args...>
86 requires(!const_component_references_v<Args...>)
87 {
88
89 return co_ecs::view<Args...>{ *this };
90 }
91
96 template<component_reference... Args>
97 constexpr auto view() const -> co_ecs::view<Args...>
98 requires const_component_references_v<Args...>
99 {
100 return co_ecs::view<Args...>{ *this };
101 }
102
106 template<component_reference... Args>
107 constexpr auto single() -> std::optional<std::tuple<Args...>>
108 requires(!const_component_references_v<Args...>)
109 {
110 return view<Args...>().single();
111 }
112
119 template<component_reference... Args>
120 constexpr auto single() const -> std::optional<std::tuple<Args...>>
121 requires const_component_references_v<Args...>
122 {
123 return view<Args...>().single();
124 }
125
146 template<typename F>
147 constexpr void each(F&& func)
148 requires(!detail::func_decomposer<F>::is_const)
149 {
150 using view_t = typename detail::func_decomposer<F>::view_t;
151 view_t{ *this }.each(std::forward<F>(func));
152 }
153
163 template<typename F>
164 constexpr void each(F&& func) const
165 requires(detail::func_decomposer<F>::is_const)
166 {
167 using view_t = typename detail::func_decomposer<F>::view_t;
168 view_t{ *this }.each(std::forward<F>(func));
169 }
170
173 [[nodiscard]] constexpr std::size_t size() const noexcept {
174 return _entity_archetype_map.size();
175 }
176
179 [[nodiscard]] constexpr bool empty() const noexcept {
180 return _entity_archetype_map.empty();
181 }
182};
183
184} // namespace co_ecs
Definition base_registry.hpp:35
constexpr auto create_impl(Components &&... args) -> entity
Definition base_registry.hpp:141
detail::sparse_map< typename entity::id_t, entity_location > _entity_archetype_map
Definition base_registry.hpp:295
Represents a reference to an entity within a registry.
Definition entity_ref.hpp:355
Represents a reference to an entity within a registry.
Definition entity_ref.hpp:13
Registry is a container for all our entities and components. Components are stored in continuously in...
Definition registry.hpp:13
constexpr std::size_t size() const noexcept
Returns the number of enitites in the registry.
Definition registry.hpp:173
constexpr auto create(Components &&... args) -> entity_ref
Creates a new entity and attaches the specified components to it.
Definition registry.hpp:39
constexpr auto single() const -> std::optional< std::tuple< Args... > >
Returns a single tuple of components matching Args, if available in the view.
Definition registry.hpp:120
constexpr void each(F &&func) const
Executes a given function on each entity that matches the specified component requirements,...
Definition registry.hpp:164
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.
Definition registry.hpp:107
constexpr bool empty() const noexcept
Checks if the registry currently manages any entities.
Definition registry.hpp:179
constexpr auto get_entity(entity ent) const noexcept -> const_entity_ref
Retrieves a constant reference to an entity.
Definition registry.hpp:63
constexpr auto get_entity(entity ent) noexcept -> entity_ref
Retrieves a mutable reference to an entity.
Definition registry.hpp:51
constexpr auto get_entity_const(entity ent) const noexcept -> const_entity_ref
Retrieves a constant reference to an entity (alias).
Definition registry.hpp:76
constexpr void each(F &&func)
Executes a given function on each entity that matches the specified component requirements.
Definition registry.hpp:147
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.
Definition registry.hpp:85
constexpr auto view() const -> co_ecs::view< Args... >
Create a const view based on component query in parameter pack.
Definition registry.hpp:97
A view lets you get a range over components of Args out of a registry.
Definition view.hpp:36
Component reference concept. It should be a reference or const reference to C, where C satisfies comp...
Definition component.hpp:94
Component concept. The component must be a struct/class that can be move constructed and move assigna...
Definition component.hpp:86
Definition archetype.hpp:11
constexpr bool const_component_references_v
Returns true when all Args are const references.
Definition component.hpp:142
detail::handle< struct entity_tag_t > entity
Represents an entity, consisting of an ID and generation.
Definition entity.hpp:13
STL namespace.