Flecs v4.0
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
iter.hpp
Go to the documentation of this file.
1
6#pragma once
7
16namespace flecs
17{
18
20
21namespace _ {
22
24
29template <typename T>
31{
32 explicit range_iterator(T value)
33 : value_(value){}
34
35 bool operator!=(range_iterator const& other) const
36 {
37 return value_ != other.value_;
38 }
39
40 T const& operator*() const
41 {
42 return value_;
43 }
44
45 range_iterator& operator++()
46 {
47 ++value_;
48 return *this;
49 }
50
51private:
52 T value_;
53};
54
55} // namespace _
56
57} // namespace flecs
58
59namespace flecs
60{
61
63
68struct iter {
69private:
71
72public:
78 iter(ecs_iter_t *it) : iter_(it) { }
79
80 row_iterator begin() const {
81 return row_iterator(0);
82 }
83
84 row_iterator end() const {
85 return row_iterator(static_cast<size_t>(iter_->count));
86 }
87
88 flecs::entity system() const;
89
90 flecs::entity event() const;
91
92 flecs::id event_id() const;
93
94 flecs::world world() const;
95
96 const flecs::iter_t* c_ptr() const {
97 return iter_;
98 }
99
100 size_t count() const {
101 ecs_check(iter_->flags & EcsIterIsValid, ECS_INVALID_PARAMETER,
102 "operation invalid before calling next()");
103 return static_cast<size_t>(iter_->count);
104 error:
105 return 0;
106 }
107
108 ecs_ftime_t delta_time() const {
109 return iter_->delta_time;
110 }
111
112 ecs_ftime_t delta_system_time() const {
113 return iter_->delta_system_time;
114 }
115
116 flecs::type type() const;
117
118 flecs::table table() const;
119
120 flecs::table_range range() const;
121
125 void* ctx() {
126 return iter_->ctx;
127 }
128
132 template <typename T>
133 T* ctx() {
134 return static_cast<T*>(iter_->ctx);
135 }
136
140 void* param() {
141 return iter_->param;
142 }
143
147 template <typename T>
148 T* param() {
149 /* TODO: type check */
150 return static_cast<T*>(iter_->param);
151 }
152
157 flecs::entity entity(size_t row) const;
158
163 bool is_self(int32_t index) const {
164 return ecs_field_is_self(iter_, index);
165 }
166
171 bool is_set(int32_t index) const {
172 return ecs_field_is_set(iter_, index);
173 }
174
179 bool is_readonly(int32_t index) const {
180 return ecs_field_is_readonly(iter_, index);
181 }
182
185 int32_t field_count() const {
186 return iter_->field_count;
187 }
188
193 size_t size(int32_t index) const {
194 return ecs_field_size(iter_, index);
195 }
196
201 flecs::entity src(int32_t index) const;
202
207 flecs::id id(int32_t index) const;
208
214 flecs::id pair(int32_t index) const;
215
220 int32_t column_index(int32_t index) const {
221 return ecs_field_column(iter_, index);
222 }
223
226 int32_t term_index() const {
227 return iter_->term_index + 1; // in iter_t, the term index is zero-based, so add 1.
228 }
229
233 char *s = ecs_iter_str(iter_);
234 return flecs::string(s);
235 }
236
245 template <typename T, typename A = actual_type_t<T>,
246 typename std::enable_if<std::is_const<T>::value, void>::type* = nullptr>
247 flecs::field<A> field(int32_t index) const;
248
257 template <typename T, typename A = actual_type_t<T>,
258 typename std::enable_if<
259 std::is_const<T>::value == false, void>::type* = nullptr>
260 flecs::field<A> field(int32_t index) const;
261
268 flecs::untyped_field field(int32_t index) const {
269 ecs_assert(!(iter_->flags & EcsIterCppEach), ECS_INVALID_OPERATION,
270 "cannot .field from .each, use .field_at(%d, row) instead", index);
271 return get_unchecked_field(index);
272 }
273
275 void* field_at(int32_t index, size_t row) const {
276 return get_unchecked_field(index)[row];
277 }
278
280 template <typename T, typename A = actual_type_t<T>,
281 typename std::enable_if<std::is_const<T>::value, void>::type* = nullptr>
282 const A& field_at(int32_t index, size_t row) const {
283 return get_field<A>(index)[row];
284 }
285
287 template <typename T, typename A = actual_type_t<T>,
288 typename std::enable_if<
289 std::is_const<T>::value == false, void>::type* = nullptr>
290 A& field_at(int32_t index, size_t row) const {
291 ecs_assert(!ecs_field_is_readonly(iter_, index),
292 ECS_ACCESS_VIOLATION, NULL);
293 return get_field<A>(index)[row];
294 }
295
302 iter_->entities, static_cast<size_t>(iter_->count), false);
303 }
304
307 bool changed() {
308 return ecs_iter_changed(iter_);
309 }
310
318 void skip() {
319 ecs_iter_skip(iter_);
320 }
321
322 /* Return group id for current table (grouped queries only) */
323 uint64_t group_id() const {
324 return iter_->group_id;
325 }
326
330 flecs::entity get_var(int var_id) const;
331
335 flecs::entity get_var(const char *name) const;
336
343 bool next() {
344 if (iter_->flags & EcsIterIsValid && iter_->table) {
345 ECS_TABLE_UNLOCK(iter_->world, iter_->table);
346 }
347 bool result = iter_->next(iter_);
348 iter_->flags |= EcsIterIsValid;
349 if (result && iter_->table) {
350 ECS_TABLE_LOCK(iter_->world, iter_->table);
351 }
352 return result;
353 }
354
359 void each() {
360 iter_->callback(iter_);
361 }
362
372 void fini() {
373 if (iter_->flags & EcsIterIsValid && iter_->table) {
374 ECS_TABLE_UNLOCK(iter_->world, iter_->table);
375 }
376 ecs_iter_fini(iter_);
377 }
378
379private:
380 /* Get field, check if correct type is used */
381 template <typename T, typename A = actual_type_t<T>>
382 flecs::field<T> get_field(int32_t index) const {
383
384#ifndef FLECS_NDEBUG
385 ecs_entity_t term_id = ecs_field_id(iter_, index);
386 ecs_assert(ECS_HAS_ID_FLAG(term_id, PAIR) ||
387 term_id == _::type<T>::id(iter_->world),
388 ECS_COLUMN_TYPE_MISMATCH, NULL);
389#endif
390
391 size_t count;
392 bool is_shared = !ecs_field_is_self(iter_, index);
393
394 /* If a shared column is retrieved with 'column', there will only be a
395 * single value. Ensure that the application does not accidentally read
396 * out of bounds. */
397 if (is_shared) {
398 count = 1;
399 } else {
400 /* If column is owned, there will be as many values as there are
401 * entities. */
402 count = static_cast<size_t>(iter_->count);
403 }
404
405 return flecs::field<A>(
406 static_cast<T*>(ecs_field_w_size(iter_, sizeof(A), index)),
407 count, is_shared);
408 }
409
410 flecs::untyped_field get_unchecked_field(int32_t index) const {
411 size_t count;
412 size_t size = ecs_field_size(iter_, index);
413 bool is_shared = !ecs_field_is_self(iter_, index);
414
415 /* If a shared column is retrieved with 'column', there will only be a
416 * single value. Ensure that the application does not accidentally read
417 * out of bounds. */
418 if (is_shared) {
419 count = 1;
420 } else {
421 /* If column is owned, there will be as many values as there are
422 * entities. */
423 count = static_cast<size_t>(iter_->count);
424 }
425
427 ecs_field_w_size(iter_, 0, index), size, count, is_shared);
428 }
429
430 flecs::iter_t *iter_;
431};
432
433} // namespace flecs
434
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
#define ecs_check(condition, error_code,...)
Check.
Definition log.h:399
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:339
bool ecs_field_is_set(const ecs_iter_t *it, int32_t index)
Test whether field is set.
bool ecs_iter_changed(ecs_iter_t *it)
Returns whether current iterator result has changed.
int32_t ecs_field_column(const ecs_iter_t *it, int32_t index)
Return index of matched table column.
bool ecs_field_is_self(const ecs_iter_t *it, int32_t index)
Test whether the field is matched on self.
void * ecs_field_w_size(const ecs_iter_t *it, size_t size, int32_t index)
Obtain data for a query field.
size_t ecs_field_size(const ecs_iter_t *it, int32_t index)
Return field type size.
void ecs_iter_fini(ecs_iter_t *it)
Cleanup iterator resources.
char * ecs_iter_str(const ecs_iter_t *it)
Convert iterator to string.
ecs_id_t ecs_field_id(const ecs_iter_t *it, int32_t index)
Return id matched for field.
bool ecs_field_is_readonly(const ecs_iter_t *it, int32_t index)
Test whether the field is readonly.
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
void ecs_iter_skip(ecs_iter_t *it)
Skip a table while iterating.
Iterator.
Definition flecs.h:1025
void * param
Param passed to ecs_run.
Definition flecs.h:1062
ecs_flags32_t flags
Iterator flags.
Definition flecs.h:1079
void * ctx
System context.
Definition flecs.h:1063
ecs_table_t * table
Current table.
Definition flecs.h:1034
ecs_world_t * world
The world.
Definition flecs.h:1027
float delta_system_time
Time elapsed since last system invocation.
Definition flecs.h:1070
int32_t field_count
Number of fields in iterator.
Definition flecs.h:1042
float delta_time
Time elapsed since last frame.
Definition flecs.h:1069
ecs_iter_action_t callback
Callback of system or observer.
Definition flecs.h:1085
int32_t term_index
Index of term that emitted an event.
Definition flecs.h:1055
ecs_entity_t * entities
Entity identifiers.
Definition flecs.h:1031
int32_t count
Number of entities to iterate.
Definition flecs.h:1075
uint64_t group_id
Group id for table, if group_by is used.
Definition flecs.h:1041
ecs_iter_next_action_t next
Function to progress iterator.
Definition flecs.h:1084
Iterate over an integer range (used to iterate over entity range).
Definition iter.hpp:31
Entity.
Definition entity.hpp:30
Wrapper class around a field.
Definition field.hpp:61
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
Class for iterating over query results.
Definition iter.hpp:68
flecs::id id(int32_t index) const
Obtain id matched for field.
Definition iter.hpp:37
flecs::string str() const
Convert current iterator result to string.
Definition iter.hpp:232
int32_t field_count() const
Number of fields in iterator.
Definition iter.hpp:185
void * param()
Access param.
Definition iter.hpp:140
bool changed()
Check if the current table has changed since the last iteration.
Definition iter.hpp:307
const A & field_at(int32_t index, size_t row) const
Get reference to field at row.
Definition iter.hpp:282
A & field_at(int32_t index, size_t row) const
Get reference to field at row.
Definition iter.hpp:290
T * ctx()
Access ctx.
Definition iter.hpp:133
void fini()
Free iterator resources.
Definition iter.hpp:372
flecs::entity src(int32_t index) const
Obtain field source (0 if This).
Definition iter.hpp:33
void * field_at(int32_t index, size_t row) const
Get pointer to field at row.
Definition iter.hpp:275
flecs::field< A > field(int32_t index) const
Get readonly access to field data.
Definition iter.hpp:64
void * ctx()
Access ctx.
Definition iter.hpp:125
flecs::entity entity(size_t row) const
Obtain mutable handle to entity being iterated over.
Definition iter.hpp:27
int32_t term_index() const
Obtain term that triggered an observer.
Definition iter.hpp:226
size_t size(int32_t index) const
Size of field data type.
Definition iter.hpp:193
flecs::id pair(int32_t index) const
Obtain pair id matched for field.
Definition iter.hpp:41
void each()
Forward to each.
Definition iter.hpp:359
flecs::untyped_field field(int32_t index) const
Get unchecked access to field data.
Definition iter.hpp:268
flecs::entity get_var(int var_id) const
Get value of variable by id.
Definition iter.hpp:83
flecs::field< const flecs::entity_t > entities() const
Get readonly access to entity ids.
Definition iter.hpp:300
int32_t column_index(int32_t index) const
Obtain column index for field.
Definition iter.hpp:220
bool is_set(int32_t index) const
Returns whether field is set.
Definition iter.hpp:171
iter(ecs_iter_t *it)
Construct iterator from C iterator object.
Definition iter.hpp:78
T * param()
Access param.
Definition iter.hpp:148
bool is_self(int32_t index) const
Returns whether field is matched on self.
Definition iter.hpp:163
bool is_readonly(int32_t index) const
Returns whether field is readonly.
Definition iter.hpp:179
void skip()
Skip current table.
Definition iter.hpp:318
bool next()
Progress iterator.
Definition iter.hpp:343
Type class.
Definition type.hpp:21
Unsafe wrapper class around a field.
Definition field.hpp:26
The world.
Definition world.hpp:137