Flecs v3.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
iterable.hpp
Go to the documentation of this file.
1
6namespace flecs {
7
8template <typename ... Components>
9struct iter_iterable;
10
11template <typename ... Components>
12struct page_iterable;
13
14template <typename ... Components>
15struct worker_iterable;
16
17template <typename ... Components>
18struct iterable {
19
29 template <typename Func>
30 void each(Func&& func) const {
31 each(nullptr, FLECS_FWD(func));
32 }
33
34 template <typename Func>
35 void each(flecs::world_t *world, Func&& func) const {
36 iterate<_::each_invoker>(world, FLECS_FWD(func),
37 this->next_each_action());
38 }
39
40 template <typename Func>
41 void each(flecs::iter& it, Func&& func) const {
42 iterate<_::each_invoker>(it.world(), FLECS_FWD(func),
43 this->next_each_action());
44 }
45
46 template <typename Func>
47 void each(flecs::entity e, Func&& func) const {
48 iterate<_::each_invoker>(e.world(), FLECS_FWD(func),
49 this->next_each_action());
50 }
51
63 template <typename Func>
64 void iter(Func&& func) const {
65 iterate<_::iter_invoker>(nullptr, FLECS_FWD(func),
66 this->next_action());
67 }
68
69 template <typename Func>
70 void iter(flecs::world_t *world, Func&& func) const {
71 iterate<_::iter_invoker>(world, FLECS_FWD(func),
72 this->next_action());
73 }
74
75 template <typename Func>
76 void iter(flecs::iter& it, Func&& func) const {
77 iterate<_::iter_invoker>(it.world(), FLECS_FWD(func),
78 this->next_action());
79 }
80
81 template <typename Func>
82 void iter(flecs::entity e, Func&& func) const {
83 iterate<_::iter_invoker>(e.world(), FLECS_FWD(func),
84 this->next_action());
85 }
86
90 iter_iterable<Components...> iter(flecs::world_t *world = nullptr) const;
91
99 page_iterable<Components...> page(int32_t offset, int32_t limit);
100
109 worker_iterable<Components...> worker(int32_t index, int32_t count);
110
112 int32_t count() const {
113 return this->iter().count();
114 }
115
117 bool is_true() const {
118 return this->iter().is_true();
119 }
120
123 return this->iter().first();
124 }
125
126 virtual ~iterable() { }
127protected:
128 friend iter_iterable<Components...>;
129 friend page_iterable<Components...>;
130 friend worker_iterable<Components...>;
131
132 virtual ecs_iter_t get_iter(flecs::world_t *stage) const = 0;
133 virtual ecs_iter_next_action_t next_action() const = 0;
134 virtual ecs_iter_next_action_t next_each_action() const = 0;
135
136 template < template<typename Func, typename ... Comps> class Invoker, typename Func, typename NextFunc, typename ... Args>
137 void iterate(flecs::world_t *stage, Func&& func, NextFunc next, Args &&... args) const {
138 ecs_iter_t it = this->get_iter(stage);
139 if (Invoker<Func, Components...>::instanced()) {
140 ECS_BIT_SET(it.flags, EcsIterIsInstanced);
141 }
142
143 while (next(&it, FLECS_FWD(args)...)) {
144 Invoker<Func, Components...>(func).invoke(&it);
145 }
146 }
147};
148
149template <typename ... Components>
150struct iter_iterable final : iterable<Components...> {
151 template <typename Iterable>
152 iter_iterable(Iterable *it, flecs::world_t *world)
153 {
154 m_it = it->get_iter(world);
155 m_next = it->next_action();
156 m_next_each = it->next_action();
157 }
158
159# ifdef FLECS_RULES
160# include "../mixins/rule/iterable.inl"
161# endif
162# ifdef FLECS_JSON
163# include "../mixins/json/iterable.inl"
164# endif
165
166 // Return total number of entities in result.
167 int32_t count() {
168 int32_t result = 0;
169 while (m_next_each(&m_it)) {
170 result += m_it.count;
171 }
172 return result;
173 }
174
175 // Returns true if iterator yields at least once result.
176 bool is_true() {
177 bool result = m_next_each(&m_it);
178 if (result) {
179 ecs_iter_fini(&m_it);
180 }
181 return result;
182 }
183
184 // Return first matching entity.
185 flecs::entity first() {
186 flecs::entity result;
187 if (m_next_each(&m_it) && m_it.count) {
188 result = flecs::entity(m_it.world, m_it.entities[0]);
189 ecs_iter_fini(&m_it);
190 }
191 return result;
192 }
193
194 // Limit results to tables with specified group id (grouped queries only)
195 iter_iterable<Components...>& set_group(uint64_t group_id) {
196 ecs_query_set_group(&m_it, group_id);
197 return *this;
198 }
199
200 // Limit results to tables with specified group id (grouped queries only)
201 template <typename Group>
202 iter_iterable<Components...>& set_group() {
203 ecs_query_set_group(&m_it, _::cpp_type<Group>().id(m_it.real_world));
204 return *this;
205 }
206
207protected:
208 ecs_iter_t get_iter(flecs::world_t *world) const {
209 if (world) {
210 ecs_iter_t result = m_it;
211 result.world = world;
212 return result;
213 }
214 return m_it;
215 }
216
217 ecs_iter_next_action_t next_action() const {
218 return m_next;
219 }
220
221 ecs_iter_next_action_t next_each_action() const {
222 return m_next_each;
223 }
224
225private:
226 ecs_iter_t m_it;
228 ecs_iter_next_action_t m_next_each;
229};
230
231template <typename ... Components>
232iter_iterable<Components...> iterable<Components...>::iter(flecs::world_t *world) const
233{
234 return iter_iterable<Components...>(this, world);
235}
236
237template <typename ... Components>
238struct page_iterable final : iterable<Components...> {
239 template <typename Iterable>
240 page_iterable(int32_t offset, int32_t limit, Iterable *it)
241 : m_offset(offset)
242 , m_limit(limit)
243 {
244 m_chain_it = it->get_iter(nullptr);
245 }
246
247protected:
248 ecs_iter_t get_iter(flecs::world_t*) const {
249 return ecs_page_iter(&m_chain_it, m_offset, m_limit);
250 }
251
252 ecs_iter_next_action_t next_action() const {
253 return ecs_page_next;
254 }
255
256 ecs_iter_next_action_t next_each_action() const {
257 return ecs_page_next;
258 }
259
260private:
261 ecs_iter_t m_chain_it;
262 int32_t m_offset;
263 int32_t m_limit;
264};
265
266template <typename ... Components>
268 int32_t offset,
269 int32_t limit)
270{
271 return page_iterable<Components...>(offset, limit, this);
272}
273
274template <typename ... Components>
275struct worker_iterable final : iterable<Components...> {
276 worker_iterable(int32_t offset, int32_t limit, iterable<Components...> *it)
277 : m_offset(offset)
278 , m_limit(limit)
279 {
280 m_chain_it = it->get_iter(nullptr);
281 }
282
283protected:
284 ecs_iter_t get_iter(flecs::world_t*) const {
285 return ecs_worker_iter(&m_chain_it, m_offset, m_limit);
286 }
287
288 ecs_iter_next_action_t next_action() const {
289 return ecs_worker_next;
290 }
291
292 ecs_iter_next_action_t next_each_action() const {
293 return ecs_worker_next;
294 }
295
296private:
297 ecs_iter_t m_chain_it;
298 int32_t m_offset;
299 int32_t m_limit;
300};
301
302template <typename ... Components>
304 int32_t index,
305 int32_t count)
306{
307 return worker_iterable<Components...>(index, count, this);
308}
309
310}
bool(* ecs_iter_next_action_t)(ecs_iter_t *it)
Function prototype for iterating an iterator.
Definition: flecs.h:356
ecs_iter_t ecs_worker_iter(const ecs_iter_t *it, int32_t index, int32_t count)
Create a worker iterator.
void ecs_iter_fini(ecs_iter_t *it)
Cleanup iterator resources.
ecs_iter_t ecs_page_iter(const ecs_iter_t *it, int32_t offset, int32_t limit)
Create a paged iterator.
bool ecs_worker_next(ecs_iter_t *it)
Progress a worker iterator.
bool ecs_page_next(ecs_iter_t *it)
Progress a paged iterator.
void ecs_query_set_group(ecs_iter_t *it, uint64_t group_id)
Set group to iterate for query iterator.
Entity.
Definition: entity.hpp:30
Class for iterating over query results.
Definition: iter.hpp:169
bool is_true() const
Return whether iterable has any matches.
Definition: iterable.hpp:117
flecs::entity first() const
Return first entity matched by iterable.
Definition: iterable.hpp:122
void each(Func &&func) const
Each iterator.
Definition: iterable.hpp:30
int32_t count() const
Return number of entities matched by iterable.
Definition: iterable.hpp:112
void iter(Func &&func) const
Iter iterator.
Definition: iterable.hpp:64
page_iterable< Components... > page(int32_t offset, int32_t limit)
Page iterator.
Definition: iterable.hpp:267
worker_iterable< Components... > worker(int32_t index, int32_t count)
Worker iterator.
Definition: iterable.hpp:303
iter_iterable< Components... > iter(flecs::world_t *world=nullptr) const
Create iterator.
Definition: iterable.hpp:232
The world.
Definition: world.hpp:113