co_ecs 0.9.0
Cobalt ECS
Loading...
Searching...
No Matches
view.hpp
Go to the documentation of this file.
1#pragma once
2
4#include <co_ecs/registry.hpp>
6
7#include <type_traits>
8
9namespace co_ecs {
10
35template<component_reference... Args>
36class view {
37private:
39 static constexpr bool is_const = detail::view_arguments<Args...>::is_const;
40
42 using value_type = std::tuple<Args...>;
43
45 using registry_type = std::conditional_t<is_const, const registry&, registry&>;
46
47public:
50 explicit view(registry_type registry) noexcept : _registry(registry) {
51 }
52
64 auto single() -> std::optional<std::tuple<Args...>>
65 requires(!is_const)
66 {
67 for (auto chunk : chunks(_registry.archetypes())) {
68 for (auto entry : chunk) {
69 return entry;
70 }
71 }
72 return {};
73 }
74
77 auto single() const -> std::optional<std::tuple<Args...>>
78 requires(is_const)
79 {
80 for (auto chunk : chunks(_registry.archetypes())) {
81 for (auto entry : chunk) {
82 return entry;
83 }
84 }
85 return {};
86 }
87
90 auto each() -> decltype(auto)
91 requires(!is_const)
92 {
93 return chunks(_registry.archetypes()) | detail::views::join; // join all chunks together
94 }
95
98 auto each() const -> decltype(auto)
99 requires(is_const)
100 {
101 return chunks(_registry.archetypes()) | detail::views::join; // join all chunks together
102 }
103
106 void each(auto&& func)
107 requires(!is_const)
108 {
109 for (auto chunk : chunks(_registry.archetypes())) {
110 for (auto entry : chunk) {
111 std::apply(func, entry);
112 }
113 }
114 }
115
120 void each(auto&& func) const
121 requires(is_const)
122 {
123 for (auto chunk : chunks(_registry.archetypes())) {
124 for (auto entry : chunk) {
125 std::apply(func, entry);
126 }
127 }
128 }
129
132 void par_each(auto&& func)
133 requires(!is_const)
134 {
136 [&func](auto chunk) { std::ranges::for_each(chunk, [&](auto&& elem) { std::apply(func, elem); }); });
137 }
138
143 void par_each(auto&& func) const
144 requires(is_const)
145 {
147 [&func](auto chunk) { std::ranges::for_each(chunk, [&](auto&& elem) { std::apply(func, elem); }); });
148 }
149
152 auto chunks() -> decltype(auto) {
153 return chunks(_registry.archetypes());
154 }
155
158 auto chunks() const -> decltype(auto) {
159 return chunks(_registry.archetypes());
160 }
161
162private:
163 template<component C>
164 constexpr static bool match(auto& archetype) {
165 if constexpr (std::is_same_v<C, entity>) {
166 return true;
167 } else {
168 return archetype->template contains<C>();
169 }
170 }
171
172 constexpr static auto chunks(auto&& archetypes) -> decltype(auto) {
173 auto filter_archetypes = [](auto& archetype) -> bool {
174 return (match<decay_component_t<Args>>(archetype) && ...);
175 };
176 auto into_chunks = [](auto& archetype) -> decltype(auto) { return archetype->chunks(); };
177 auto as_typed_chunk = [](auto& chunk) -> decltype(auto) { return chunk_view<Args...>(chunk); };
178
179 return archetypes // for each archetype entry in archetype map
180 | detail::views::values // for each value, a pointer to archetype
181 | detail::views::filter(filter_archetypes) // filter archetype by requested components
182 | detail::views::transform(into_chunks) // fetch chunks vector
183 | detail::views::join // join chunks together
184 | detail::views::transform(as_typed_chunk); // each chunk casted to a typed chunk view range-like type
185 }
186
187 registry_type _registry;
188};
189
190
191} // namespace co_ecs
Archetype groups entities that share the same types of components. Archetype has a list of fixed size...
Definition archetype.hpp:15
Chunk holds a 16 Kb block of memory that holds components in blocks: |A1|A2|A3|......
Definition chunk.hpp:37
Registry is a container for all our entities and components. Components are stored in continuously in...
Definition registry.hpp:13
A view lets you get a range over components of Args out of a registry.
Definition view.hpp:36
auto chunks() const -> decltype(auto)
Gets the const chunks range.
Definition view.hpp:158
auto each() -> decltype(auto) requires(!is_const)
Returns an iterator that yields a std::tuple<Args...>.
Definition view.hpp:90
auto each() const -> decltype(auto) requires(is_const)
Returns an iterator that yields a std::tuple<Args...> (const version).
Definition view.hpp:98
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).
Definition view.hpp:77
void par_each(auto &&func)
Runs a function on every entity that matches the Args requirement in parallel.
Definition view.hpp:132
void each(auto &&func)
Runs a function on every entity that matches the Args requirement.
Definition view.hpp:106
view(registry_type registry) noexcept
Constructs a new view object.
Definition view.hpp:50
void each(auto &&func) const
Runs a function on every entity that matches the Args requirement (const version).
Definition view.hpp:120
void par_each(auto &&func) const
Runs a function on every entity that matches the Args requirement in parallel (const version).
Definition view.hpp:143
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
auto chunks() -> decltype(auto)
Gets the chunks range.
Definition view.hpp:152
Definition archetype.hpp:11
void parallel_for(R &&range, auto &&func)
Parallelize func over elements in range.
Definition parallel_for.hpp:13
STL namespace.