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(int8_t index) const {
164 return ecs_field_is_self(iter_, index);
165 }
166
171 bool is_set(int8_t index) const {
172 return ecs_field_is_set(iter_, index);
173 }
174
179 bool is_readonly(int8_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(int8_t index) const {
194 return ecs_field_size(iter_, index);
195 }
196
201 flecs::entity src(int8_t index) const;
202
207 flecs::id id(int8_t index) const;
208
214 flecs::id pair(int8_t index) const;
215
220 int32_t column_index(int8_t index) const {
221 return ecs_field_column(iter_, index);
222 }
223
226 int8_t term_index() const {
227 return iter_->term_index;
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(int8_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(int8_t index) const;
261
268 flecs::untyped_field field(int8_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(int8_t index, size_t row) const {
276 if (iter_->row_fields & (1llu << index)) {
277 return get_unchecked_field_at(index, row)[0];
278 } else {
279 return get_unchecked_field(index)[row];
280 }
281 }
282
284 template <typename T, typename A = actual_type_t<T>,
285 typename std::enable_if<std::is_const<T>::value, void>::type* = nullptr>
286 const A& field_at(int8_t index, size_t row) const {
287 if (iter_->row_fields & (1llu << index)) {
288 return get_field_at<A>(index, row)[0];
289 } else {
290 return get_field<A>(index)[row];
291 }
292 }
293
295 template <typename T, typename A = actual_type_t<T>,
296 typename std::enable_if<
297 std::is_const<T>::value == false, void>::type* = nullptr>
298 A& field_at(int8_t index, size_t row) const {
299 ecs_assert(!ecs_field_is_readonly(iter_, index),
300 ECS_ACCESS_VIOLATION, NULL);
301 if (iter_->row_fields & (1llu << index)) {
302 return get_field_at<A>(index, row)[0];
303 } else {
304 return get_field<A>(index)[row];
305 }
306 }
307
314 iter_->entities, static_cast<size_t>(iter_->count), false);
315 }
316
319 bool changed() {
320 return ecs_iter_changed(iter_);
321 }
322
330 void skip() {
331 ecs_iter_skip(iter_);
332 }
333
334 /* Return group id for current table (grouped queries only) */
335 uint64_t group_id() const {
336 return iter_->group_id;
337 }
338
342 flecs::entity get_var(int var_id) const;
343
347 flecs::entity get_var(const char *name) const;
348
355 bool next() {
356 if (iter_->flags & EcsIterIsValid && iter_->table) {
357 ECS_TABLE_UNLOCK(iter_->world, iter_->table);
358 }
359 bool result = iter_->next(iter_);
360 iter_->flags |= EcsIterIsValid;
361 if (result && iter_->table) {
362 ECS_TABLE_LOCK(iter_->world, iter_->table);
363 }
364 return result;
365 }
366
371 void each() {
372 iter_->callback(iter_);
373 }
374
384 void fini() {
385 if (iter_->flags & EcsIterIsValid && iter_->table) {
386 ECS_TABLE_UNLOCK(iter_->world, iter_->table);
387 }
388 ecs_iter_fini(iter_);
389 }
390
391private:
392 /* Get field, check if correct type is used */
393 template <typename T, typename A = actual_type_t<T>>
394 flecs::field<T> get_field(int8_t index) const {
395
396#ifndef FLECS_NDEBUG
397 ecs_entity_t term_id = ecs_field_id(iter_, index);
398 ecs_assert(ECS_HAS_ID_FLAG(term_id, PAIR) ||
399 term_id == _::type<T>::id(iter_->world),
400 ECS_COLUMN_TYPE_MISMATCH, NULL);
401#endif
402
403 size_t count;
404 bool is_shared = !ecs_field_is_self(iter_, index);
405
406 /* If a shared column is retrieved with 'column', there will only be a
407 * single value. Ensure that the application does not accidentally read
408 * out of bounds. */
409 if (is_shared) {
410 count = 1;
411 } else {
412 /* If column is owned, there will be as many values as there are
413 * entities. */
414 count = static_cast<size_t>(iter_->count);
415 }
416
417 return flecs::field<A>(
418 static_cast<T*>(ecs_field_w_size(iter_, sizeof(A), index)),
419 count, is_shared);
420 }
421
422 /* Get field, check if correct type is used */
423 template <typename T, typename A = actual_type_t<T>>
424 flecs::field<T> get_field_at(int8_t index, int32_t row) const {
425
426#ifndef FLECS_NDEBUG
427 ecs_entity_t term_id = ecs_field_id(iter_, index);
428 ecs_assert(ECS_HAS_ID_FLAG(term_id, PAIR) ||
429 term_id == _::type<T>::id(iter_->world),
430 ECS_COLUMN_TYPE_MISMATCH, NULL);
431#endif
432
433 return flecs::field<A>(
434 static_cast<T*>(ecs_field_at_w_size(iter_, sizeof(A), index, row)),
435 1, false);
436 }
437
438 flecs::untyped_field get_unchecked_field(int8_t index) const {
439 size_t count;
440 size_t size = ecs_field_size(iter_, index);
441 bool is_shared = !ecs_field_is_self(iter_, index);
442
443 /* If a shared column is retrieved with 'column', there will only be a
444 * single value. Ensure that the application does not accidentally read
445 * out of bounds. */
446 if (is_shared) {
447 count = 1;
448 } else {
449 /* If column is owned, there will be as many values as there are
450 * entities. */
451 count = static_cast<size_t>(iter_->count);
452 }
453
455 ecs_field_w_size(iter_, 0, index), size, count, is_shared);
456 }
457
458 flecs::untyped_field get_unchecked_field_at(int8_t index, size_t row) const {
459 size_t size = ecs_field_size(iter_, index);
461 ecs_field_at_w_size(iter_, 0, index, static_cast<int32_t>(row)),
462 size, 1, false);
463 }
464
465 flecs::iter_t *iter_;
466};
467
468} // namespace flecs
469
#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_iter_changed(ecs_iter_t *it)
Returns whether current iterator result has changed.
bool ecs_field_is_readonly(const ecs_iter_t *it, int8_t index)
Test whether the field is readonly.
void ecs_iter_fini(ecs_iter_t *it)
Cleanup iterator resources.
char * ecs_iter_str(const ecs_iter_t *it)
Convert iterator to string.
void * ecs_field_at_w_size(const ecs_iter_t *it, size_t size, int8_t index, int32_t row)
Get data for field at specified row.
ecs_id_t ecs_field_id(const ecs_iter_t *it, int8_t index)
Return id matched for field.
bool ecs_field_is_set(const ecs_iter_t *it, int8_t index)
Test whether field is set.
bool ecs_field_is_self(const ecs_iter_t *it, int8_t index)
Test whether the field is matched on self.
int32_t ecs_field_column(const ecs_iter_t *it, int8_t index)
Return index of matched table column.
void * ecs_field_w_size(const ecs_iter_t *it, size_t size, int8_t index)
Get data for field.
size_t ecs_field_size(const ecs_iter_t *it, int8_t index)
Return field type size.
#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:1050
void * param
Param passed to ecs_run.
Definition flecs.h:1087
ecs_flags32_t flags
Iterator flags.
Definition flecs.h:1103
void * ctx
System context.
Definition flecs.h:1088
ecs_table_t * table
Current table.
Definition flecs.h:1058
ecs_world_t * world
The world.
Definition flecs.h:1052
ecs_flags32_t row_fields
Fields that must be obtained with field_at.
Definition flecs.h:1068
float delta_system_time
Time elapsed since last system invocation.
Definition flecs.h:1095
int8_t term_index
Index of term that emitted an event.
Definition flecs.h:1079
float delta_time
Time elapsed since last frame.
Definition flecs.h:1094
ecs_iter_action_t callback
Callback of system or observer.
Definition flecs.h:1109
int8_t field_count
Number of fields in iterator.
Definition flecs.h:1078
int32_t count
Number of entities to iterate.
Definition flecs.h:1100
uint64_t group_id
Group id for table, if group_by is used.
Definition flecs.h:1065
ecs_iter_next_action_t next
Function to progress iterator.
Definition flecs.h:1108
const ecs_entity_t * entities
Entity identifiers.
Definition flecs.h:1056
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(int8_t index) const
Obtain id matched for field.
Definition iter.hpp:37
size_t size(int8_t index) const
Size of field data type.
Definition iter.hpp:193
flecs::field< A > field(int8_t index) const
Get readonly access to field data.
Definition iter.hpp:64
const A & field_at(int8_t index, size_t row) const
Get reference to field at row.
Definition iter.hpp:286
bool is_self(int8_t index) const
Returns whether field is matched on self.
Definition iter.hpp:163
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
int32_t column_index(int8_t index) const
Obtain column index for field.
Definition iter.hpp:220
void * param()
Access param.
Definition iter.hpp:140
void * field_at(int8_t index, size_t row) const
Get pointer to field at row.
Definition iter.hpp:275
bool is_readonly(int8_t index) const
Returns whether field is readonly.
Definition iter.hpp:179
bool changed()
Check if the current table has changed since the last iteration.
Definition iter.hpp:319
int8_t term_index() const
Obtain term that triggered an observer.
Definition iter.hpp:226
T * ctx()
Access ctx.
Definition iter.hpp:133
void fini()
Free iterator resources.
Definition iter.hpp:384
bool is_set(int8_t index) const
Returns whether field is set.
Definition iter.hpp:171
A & field_at(int8_t index, size_t row) const
Get reference to field at row.
Definition iter.hpp:298
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
void each()
Forward to each.
Definition iter.hpp:371
flecs::untyped_field field(int8_t index) const
Get unchecked access to field data.
Definition iter.hpp:268
flecs::id pair(int8_t index) const
Obtain pair id matched for field.
Definition iter.hpp:41
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:312
iter(ecs_iter_t *it)
Construct iterator from C iterator object.
Definition iter.hpp:78
T * param()
Access param.
Definition iter.hpp:148
flecs::entity src(int8_t index) const
Obtain field source (0 if This).
Definition iter.hpp:33
void skip()
Skip current table.
Definition iter.hpp:330
bool next()
Progress iterator.
Definition iter.hpp:355
Type class.
Definition type.hpp:21
Unsafe wrapper class around a field.
Definition field.hpp:26
The world.
Definition world.hpp:137