Skip to content
Flecs v4.2
iterable.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/utils/iterable.hpp
3 * @brief Base class for iterable objects, like queries.
4 */
5
6namespace flecs {
7
8/** Forward declaration of iter_iterable. */
9template <typename ... Components>
10struct iter_iterable;
11
12namespace _ {
13 template <auto Create, ecs_iter_next_action_t Next, typename... Components>
14 struct chained_iterable;
15}
16
17template <typename... Components>
18using page_iterable = _::chained_iterable<ecs_page_iter, ecs_page_next, Components...>;
19
20template <typename... Components>
21using worker_iterable = _::chained_iterable<ecs_worker_iter, ecs_worker_next, Components...>;
22
23/** Base class for iterable query objects. */
24template <typename ... Components>
25struct iterable {
26
27 /** Each iterator.
28 * The "each" iterator accepts a function that is invoked for each matching
29 * entity. The following function signatures are valid:
30 * - func(flecs::entity e, Components& ...)
31 * - func(flecs::iter& it, size_t index, Components& ...)
32 * - func(Components& ...)
33 *
34 * @param func The callback function.
35 */
36 template <typename Func>
37 void each(Func&& func) const {
38 ecs_iter_t it = this->get_iter(nullptr);
39 ecs_iter_next_action_t next = this->next_action();
40 _::each_delegate<Func&, Components...> delegate(func);
41 while (next(&it)) {
42 delegate.invoke(&it);
43 }
44 }
45
46 /** Run the iterator.
47 * The "run" callback accepts a function that is invoked once for a query
48 * with a valid iterator. The following signature is valid:
49 * - func(flecs::iter&)
50 *
51 * @param func The callback function.
52 */
53 template <typename Func>
54 void run(Func&& func) const {
55 ecs_iter_t it = this->get_iter(nullptr);
56 _::run_delegate<Func>(func).invoke(&it);
57 }
58
59 /** Find the first entity matching a condition.
60 * Return the first entity for which the provided function returns true.
61 *
62 * @param func The predicate function.
63 * @return The first matching entity, or an empty entity if none found.
64 */
65 template <typename Func>
66 flecs::entity find(Func&& func) const {
67 ecs_iter_t it = this->get_iter(nullptr);
68 ecs_iter_next_action_t next = this->next_action();
69
70 _::find_delegate<Func&, Components...> delegate(func);
71 flecs::entity result;
72 while (!result && next(&it)) {
73 result = delegate.invoke(&it);
74 }
75
76 if (result) {
77 ecs_iter_fini(&it);
78 }
79
80 return result;
81 }
82
83 /** Create an iterator.
84 * Create an iterator object that can be modified before iterating.
85 */
86 iter_iterable<Components...> iter(flecs::world_t *world = nullptr) const;
87
88 /** Create an iterator.
89 * Create an iterator object that can be modified before iterating.
90 */
91 iter_iterable<Components...> iter(flecs::iter& iter) const;
92
93 /** Create an iterator.
94 * Create an iterator object that can be modified before iterating.
95 */
96 iter_iterable<Components...> iter(flecs::entity e) const;
97
98 /** Page iterator.
99 * Create an iterator that limits the returned entities with offset/limit.
100 *
101 * @param offset How many entities to skip.
102 * @param limit The maximum number of entities to return.
103 * @return Iterable that can be iterated with each/iter.
104 */
105 page_iterable<Components...> page(int32_t offset, int32_t limit);
106
107 /** Worker iterator.
108 * Create an iterator that divides the number of matched entities across
109 * a number of resources.
110 *
111 * @param index The index of the current resource.
112 * @param count The total number of resources to divide entities between.
113 * @return Iterable that can be iterated with each/iter.
114 */
115 worker_iterable<Components...> worker(int32_t index, int32_t count);
116
117 /** Return the number of entities matched by the iterable. */
118 int32_t count() const {
119 return this->iter().count();
120 }
121
122 /** Return whether the iterable has any matches. */
123 bool is_true() const {
124 return this->iter().is_true();
125 }
126
127 /** Return the first entity matched by the iterable. */
129 return this->iter().first();
130 }
131
132 template <typename Var, typename Value>
133 iter_iterable<Components...> set_var(Var var, Value value) const {
134 return this->iter().set_var(var, value);
135 }
136
137#ifdef FLECS_CACHED_QUERIES
138 /** Limit results to tables with the specified group ID (grouped queries only). */
139 iter_iterable<Components...> set_group(uint64_t group_id) const {
140 return this->iter().set_group(group_id);
141 }
142
143 /** Limit results to tables with the specified group type (grouped queries only). */
144 template <typename Group>
145 iter_iterable<Components...> set_group() const {
146 return this->iter().template set_group<Group>();
147 }
148#endif
149
150 /** Virtual destructor. */
151 virtual ~iterable() { }
152protected:
153 friend iter_iterable<Components...>;
154 friend page_iterable<Components...>;
155 friend worker_iterable<Components...>;
156
157 virtual ecs_iter_t get_iter(flecs::world_t *stage) const = 0;
158 virtual ecs_iter_next_action_t next_action() const = 0;
159};
160
161/** Iterable adapter for iterating with iter/each/run. */
162template <typename ... Components>
163struct iter_iterable final : iterable<Components...> {
164 /** Construct iter_iterable from an iterable and a world. */
165 template <typename Iterable>
167 {
168 it_ = it->get_iter(world);
169 next_ = it->next_action();
170 ecs_assert(next_ != nullptr, ECS_INTERNAL_ERROR, nullptr);
171 }
172
173 template <typename Var, typename Value>
174 iter_iterable& set_var(Var var, Value value) {
175 int var_id;
176#ifdef FLECS_QUERY_PLANS
177 if constexpr (std::is_convertible_v<Var, const char*>) {
178 var_id = ecs_query_find_var(it_.query, var);
179 } else
180#endif
181 {
182 var_id = var;
183 }
184 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, "query variable was not found");
185 if constexpr (std::is_convertible_v<Value, flecs::entity_t>) {
186 ecs_iter_set_var(&it_, var_id, value);
187 } else if constexpr (std::is_convertible_v<Value, flecs::table_t*> &&
188 !std::is_base_of_v<flecs::table_range, Value>)
189 {
190 ecs_iter_set_var_as_table(&it_, var_id, value);
191 } else {
192 ecs_table_range_t range;
193 if constexpr (std::is_same_v<Value, ecs_table_range_t>) {
194 range = value;
195 } else {
196 range = {value.get_table(), value.offset(), value.count()};
197 }
198 ecs_iter_set_var_as_range(&it_, var_id, &range);
199 }
200 return *this;
201 }
202
203# ifdef FLECS_JSON
205# endif
206
207 /** Return the total number of entities in the result. */
208 int32_t count() {
209 int32_t result = 0;
210 while (next_(&it_)) {
211 result += it_.count;
212 }
213 return result;
214 }
215
216 /** Return whether the iterator yields at least one result. */
217 bool is_true() {
218 bool result = next_(&it_);
219 if (result) {
220 ecs_iter_fini(&it_);
221 }
222 return result;
223 }
224
225 /** Return the first matching entity. */
227 while (next_(&it_)) {
228 if (it_.count) {
229 flecs::entity result(it_.stage, it_.entities[0]);
230 ecs_iter_fini(&it_);
231 return result;
232 }
233 }
234 return flecs::entity();
235 }
236
237#ifdef FLECS_CACHED_QUERIES
238 /** Limit results to tables with the specified group ID (grouped queries only). */
239 iter_iterable<Components...>& set_group(uint64_t group_id) {
240 ecs_iter_set_group(&it_, group_id);
241 return *this;
242 }
243
244 /** Limit results to tables with the specified group type (grouped queries only). */
245 template <typename Group>
246 iter_iterable<Components...>& set_group() {
247 ecs_iter_set_group(&it_, _::type<Group>().id(it_.world));
248 return *this;
249 }
250#endif
251
252protected:
253 ecs_iter_t get_iter(flecs::world_t *world) const override {
254 if (world) {
255 ecs_iter_t result = it_;
256 result.stage = world;
257 return result;
258 }
259 return it_;
260 }
261
262 ecs_iter_next_action_t next_action() const override {
263 return next_;
264 }
265
266private:
267 ecs_iter_t it_;
269};
270
271template <typename ... Components>
273{
274 return iter_iterable<Components...>(this, world);
275}
276
277template <typename ... Components>
279{
280 return iter_iterable<Components...>(this, it.stage());
281}
282
283template <typename ... Components>
285{
286 return iter_iterable<Components...>(this, e.world());
287}
288
289namespace _ {
290
291template <auto Create, ecs_iter_next_action_t Next, typename... Components>
292struct chained_iterable final : iterable<Components...> {
293 chained_iterable(int32_t first, int32_t second, const iterable<Components...> *source)
294 : chain_it_(source->get_iter(nullptr)), args_{first, second} { }
295
296protected:
297 ecs_iter_t get_iter(flecs::world_t*) const override {
298 return Create(&chain_it_, args_[0], args_[1]);
299 }
300
301 ecs_iter_next_action_t next_action() const override {
302 return Next;
303 }
304
305private:
306 ecs_iter_t chain_it_;
307 int32_t args_[2];
308};
309
310}
311
312template <typename... Components>
313page_iterable<Components...> iterable<Components...>::page(int32_t offset, int32_t limit) {
314 return page_iterable<Components...>(offset, limit, this);
315}
316
317template <typename... Components>
318worker_iterable<Components...> iterable<Components...>::worker(int32_t index, int32_t count) {
319 return worker_iterable<Components...>(index, count, this);
320}
321
322}
_::each_delegate< typename std::decay< Func >::type, Args... > delegate
Delegate type for each callbacks.
Definition delegate.hpp:549
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:479
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:704
#define ECS_INTERNAL_ERROR
Internal error code.
Definition log.h:714
struct ecs_iter_t ecs_iter_t
Type used for iterating iterable objects.
Definition flecs.h:482
ecs_world_t world_t
World type.
Definition c_types.hpp:18
bool(*) ecs_iter_next_action_t(ecs_iter_t *it)
Function prototype for iterating an iterator.
Definition flecs.h:602
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)
Clean up iterator resources.
ecs_iter_t ecs_page_iter(const ecs_iter_t *it, int32_t offset, int32_t limit)
Create a paged iterator.
void ecs_iter_set_var(ecs_iter_t *it, int32_t var_id, ecs_entity_t entity)
Set the value for an iterator variable.
bool ecs_worker_next(ecs_iter_t *it)
Progress a worker iterator.
void ecs_iter_set_var_as_table(ecs_iter_t *it, int32_t var_id, const ecs_table_t *table)
Same as ecs_iter_set_var(), but for a table.
bool ecs_page_next(ecs_iter_t *it)
Progress a paged iterator.
void ecs_iter_set_var_as_range(ecs_iter_t *it, int32_t var_id, const ecs_table_range_t *range)
Same as ecs_iter_set_var(), but for a range of entities.
void ecs_iter_set_group(ecs_iter_t *it, uint64_t group_id)
Set the group to iterate for a query iterator.
int32_t ecs_query_find_var(const ecs_query_t *query, const char *name)
Find a variable index.
JSON iterable mixin.
Int to enum.
Definition component.hpp:18
Iterator.
Definition flecs.h:1184
const ecs_query_t * query
Query being evaluated.
Definition flecs.h:1219
Entity.
Definition entity.hpp:30
flecs::world world() const
Get the world.
Definition impl.hpp:60
flecs::entity first() const
Get first element from a pair.
Definition impl.hpp:20
Forward declaration of iter_iterable.
Definition iterable.hpp:163
iter_iterable< Components... > & set_group()
Limit results to tables with the specified group type (grouped queries only).
Definition iterable.hpp:246
iter_iterable< Components... > & set_group(uint64_t group_id)
Limit results to tables with the specified group ID (grouped queries only).
Definition iterable.hpp:239
flecs::entity first()
Return the first matching entity.
Definition iterable.hpp:226
iter_iterable(Iterable *it, flecs::world_t *world)
Construct iter_iterable from an iterable and a world.
Definition iterable.hpp:166
bool is_true()
Return whether the iterator yields at least one result.
Definition iterable.hpp:217
int32_t count()
Return the total number of entities in the result.
Definition iterable.hpp:208
Class for iterating over query results.
Definition iter.hpp:68
iter(ecs_iter_t *it)
Construct iterator from C iterator object.
Definition iter.hpp:78
Base class for iterable query objects.
Definition iterable.hpp:25
bool is_true() const
Return whether the iterable has any matches.
Definition iterable.hpp:123
iter_iterable< Components... > set_group() const
Limit results to tables with the specified group type (grouped queries only).
Definition iterable.hpp:145
void run(Func &&func) const
Run the iterator.
Definition iterable.hpp:54
flecs::entity first() const
Return the first entity matched by the iterable.
Definition iterable.hpp:128
void each(Func &&func) const
Each iterator.
Definition iterable.hpp:37
virtual ~iterable()
Virtual destructor.
Definition iterable.hpp:151
page_iterable< Components... > page(int32_t offset, int32_t limit)
Page iterator.
Definition iterable.hpp:313
flecs::entity find(Func &&func) const
Find the first entity matching a condition.
Definition iterable.hpp:66
worker_iterable< Components... > worker(int32_t index, int32_t count)
Worker iterator.
Definition iterable.hpp:318
iter_iterable< Components... > iter(flecs::world_t *world=nullptr) const
Create an iterator.
Definition iterable.hpp:272
iter_iterable< Components... > set_group(uint64_t group_id) const
Limit results to tables with the specified group ID (grouped queries only).
Definition iterable.hpp:139
The world.
Definition world.hpp:109