Flecs: include/flecs/addons/cpp/iter.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| 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>
30 struct range_iterator
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
68 struct iter {
69private:
70 using row_iterator = _::range_iterator<size_t>;
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 other_table() const;
121
122 flecs::table_range range() const;
123
127 void* ctx() {
128 return iter_->ctx;
129 }
130
134 template <typename T>
135 T* ctx() {
136 return static_cast<T*>(iter_->ctx);
137 }
138
142 void* param() {
143 return iter_->param;
144 }
145
149 template <typename T>
150 T* param() {
151 /* TODO: type check */
152 return static_cast<T*>(iter_->param);
153 }
154
159 flecs::entity entity(size_t row) const;
160
165 bool is_self(int8_t index) const {
166 return ecs_field_is_self(iter_, index);
167 }
168
173 bool is_set(int8_t index) const {
174 return ecs_field_is_set(iter_, index);
175 }
176
181 bool is_readonly(int8_t index) const {
182 return ecs_field_is_readonly(iter_, index);
183 }
184
187 int32_t field_count() const {
188 return iter_->field_count;
189 }
190
195 size_t size(int8_t index) const {
196 return ecs_field_size(iter_, index);
197 }
198
203 flecs::entity src(int8_t index) const;
204
209 flecs::id id(int8_t index) const;
210
216 flecs::id pair(int8_t index) const;
217
222 int32_t column_index(int8_t index) const {
223 return ecs_field_column(iter_, index);
224 }
225
228 int8_t term_index() const {
229 return iter_->term_index;
230 }
231
234 flecs::string str() const {
235 char *s = ecs_iter_str(iter_);
236 return flecs::string(s);
237 }
238
247 template <typename T, typename A = actual_type_t<T>,
248 typename std::enable_if<std::is_const<T>::value, void>::type* = nullptr>
249 flecs::field<A> field(int8_t index) const;
250
259 template <typename T, typename A = actual_type_t<T>,
260 typename std::enable_if<
261 std::is_const<T>::value == false, void>::type* = nullptr>
262 flecs::field<A> field(int8_t index) const;
263
270 flecs::untyped_field field(int8_t index) const {
271 ecs_assert(!(iter_->flags & EcsIterCppEach), ECS_INVALID_OPERATION,
272 "cannot .field from .each, use .field_at(%d, row) instead", index);
273 return get_unchecked_field(index);
274 }
275
277 void* field_at(int8_t index, size_t row) const {
278 if (iter_->row_fields & (1llu << index)) {
279 return get_unchecked_field_at(index, row)[0];
280 } else {
281 return get_unchecked_field(index)[row];
282 }
283 }
284
286 template <typename T, typename A = actual_type_t<T>,
287 typename std::enable_if<std::is_const<T>::value, void>::type* = nullptr>
288 const A& field_at(int8_t index, size_t row) const {
289 if (iter_->row_fields & (1llu << index)) {
290 return get_field_at<A>(index, row)[0];
291 } else {
292 return get_field<A>(index)[row];
293 }
294 }
295
297 template <typename T, typename A = actual_type_t<T>,
298 typename std::enable_if<
299 std::is_const<T>::value == false, void>::type* = nullptr>
300 A& field_at(int8_t index, size_t row) const {
301 ecs_assert(!ecs_field_is_readonly(iter_, index),
302 ECS_ACCESS_VIOLATION, NULL);
303 if (iter_->row_fields & (1llu << index)) {
304 return get_field_at<A>(index, row)[0];
305 } else {
306 return get_field<A>(index)[row];
307 }
308 }
309
314 flecs::field<const flecs::entity_t> entities() const {
315 return flecs::field<const flecs::entity_t>(
316 iter_->entities, static_cast<size_t>(iter_->count), false);
317 }
318
321 bool changed() {
322 return ecs_iter_changed(iter_);
323 }
324
332 void skip() {
333 ecs_iter_skip(iter_);
334 }
335
336 /* Return group id for current table (grouped queries only) */
337 uint64_t group_id() const {
338 return iter_->group_id;
339 }
340
344 flecs::entity get_var(int var_id) const;
345
349 flecs::entity get_var(const char *name) const;
350
357 bool next() {
358 if (iter_->flags & EcsIterIsValid && iter_->table) {
359 ECS_TABLE_UNLOCK(iter_->world, iter_->table);
360 }
361 bool result = iter_->next(iter_);
362 iter_->flags |= EcsIterIsValid;
363 if (result && iter_->table) {
364 ECS_TABLE_LOCK(iter_->world, iter_->table);
365 }
366 return result;
367 }
368
373 void each() {
374 iter_->callback(iter_);
375 }
376
382 template <typename Func>
383 void targets(int8_t index, const Func& func);
384
394 void fini() {
395 if (iter_->flags & EcsIterIsValid && iter_->table) {
396 ECS_TABLE_UNLOCK(iter_->world, iter_->table);
397 }
398 ecs_iter_fini(iter_);
399 }
400
401private:
402 /* Get field, check if correct type is used */
403 template <typename T, typename A = actual_type_t<T>>
404 flecs::field<T> get_field(int8_t index) const {
405
406#ifndef FLECS_NDEBUG
407 ecs_entity_t term_id = ecs_field_id(iter_, index);
408 ecs_assert(ECS_HAS_ID_FLAG(term_id, PAIR) ||
409 term_id == _::type<T>::id(iter_->world),
410 ECS_COLUMN_TYPE_MISMATCH, NULL);
411#endif
412
413 size_t count;
414 bool is_shared = !ecs_field_is_self(iter_, index);
415
416 /* If a shared column is retrieved with 'column', there will only be a
417 * single value. Ensure that the application does not accidentally read
418 * out of bounds. */
419 if (is_shared) {
420 count = 1;
421 } else {
422 /* If column is owned, there will be as many values as there are
423 * entities. */
424 count = static_cast<size_t>(iter_->count);
425 }
426
427 return flecs::field<A>(
428 static_cast<T*>(ecs_field_w_size(iter_, sizeof(A), index)),
429 count, is_shared);
430 }
431
432 /* Get field, check if correct type is used */
433 template <typename T, typename A = actual_type_t<T>>
434 flecs::field<T> get_field_at(int8_t index, int32_t row) const {
435
436#ifndef FLECS_NDEBUG
437 ecs_entity_t term_id = ecs_field_id(iter_, index);
438 ecs_assert(ECS_HAS_ID_FLAG(term_id, PAIR) ||
439 term_id == _::type<T>::id(iter_->world),
440 ECS_COLUMN_TYPE_MISMATCH, NULL);
441#endif
442
443 return flecs::field<A>(
444 static_cast<T*>(ecs_field_at_w_size(iter_, sizeof(A), index, row)),
445 1, false);
446 }
447
448 flecs::untyped_field get_unchecked_field(int8_t index) const {
449 size_t count;
450 size_t size = ecs_field_size(iter_, index);
451 bool is_shared = !ecs_field_is_self(iter_, index);
452
453 /* If a shared column is retrieved with 'column', there will only be a
454 * single value. Ensure that the application does not accidentally read
455 * out of bounds. */
456 if (is_shared) {
457 count = 1;
458 } else {
459 /* If column is owned, there will be as many values as there are
460 * entities. */
461 count = static_cast<size_t>(iter_->count);
462 }
463
464 return flecs::untyped_field(
465 ecs_field_w_size(iter_, 0, index), size, count, is_shared);
466 }
467
468 flecs::untyped_field get_unchecked_field_at(int8_t index, size_t row) const {
469 size_t size = ecs_field_size(iter_, index);
470 return flecs::untyped_field(
471 ecs_field_at_w_size(iter_, 0, index, static_cast<int32_t>(row)),
472 size, 1, false);
473 }
474
475 flecs::iter_t *iter_;
476};
477
478} // namespace flecs
479
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
ecs_check
#define ecs_check(condition, error_code,...)
Check.
Definition log.h:399
ecs_entity_t
ecs_id_t ecs_entity_t
An entity identifier.
Definition flecs.h:347
ecs_iter_changed
bool ecs_iter_changed(ecs_iter_t *it)
Returns whether current iterator result has changed.
ecs_field_is_readonly
bool ecs_field_is_readonly(const ecs_iter_t *it, int8_t index)
Test whether the field is readonly.
ecs_iter_fini
void ecs_iter_fini(ecs_iter_t *it)
Cleanup iterator resources.
ecs_iter_str
char * ecs_iter_str(const ecs_iter_t *it)
Convert iterator to string.
ecs_field_at_w_size
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_field_id
ecs_id_t ecs_field_id(const ecs_iter_t *it, int8_t index)
Return id matched for field.
ecs_field_is_set
bool ecs_field_is_set(const ecs_iter_t *it, int8_t index)
Test whether field is set.
ecs_field_is_self
bool ecs_field_is_self(const ecs_iter_t *it, int8_t index)
Test whether the field is matched on self.
ecs_field_column
int32_t ecs_field_column(const ecs_iter_t *it, int8_t index)
Return index of matched table column.
ecs_field_w_size
void * ecs_field_w_size(const ecs_iter_t *it, size_t size, int8_t index)
Get data for field.
ecs_field_size
size_t ecs_field_size(const ecs_iter_t *it, int8_t index)
Return field type size.
ecs_ftime_t
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition flecs.h:59
ecs_iter_skip
void ecs_iter_skip(ecs_iter_t *it)
Skip a table while iterating.
ecs_iter_t
Iterator.
Definition flecs.h:1062
ecs_iter_t::param
void * param
Param passed to ecs_run.
Definition flecs.h:1099
ecs_iter_t::flags
ecs_flags32_t flags
Iterator flags.
Definition flecs.h:1115
ecs_iter_t::ctx
void * ctx
System context.
Definition flecs.h:1100
ecs_iter_t::table
ecs_table_t * table
Current table.
Definition flecs.h:1070
ecs_iter_t::world
ecs_world_t * world
The world.
Definition flecs.h:1064
ecs_iter_t::row_fields
ecs_flags32_t row_fields
Fields that must be obtained with field_at.
Definition flecs.h:1080
ecs_iter_t::delta_system_time
float delta_system_time
Time elapsed since last system invocation.
Definition flecs.h:1107
ecs_iter_t::term_index
int8_t term_index
Index of term that emitted an event.
Definition flecs.h:1091
ecs_iter_t::delta_time
float delta_time
Time elapsed since last frame.
Definition flecs.h:1106
ecs_iter_t::callback
ecs_iter_action_t callback
Callback of system or observer.
Definition flecs.h:1121
ecs_iter_t::field_count
int8_t field_count
Number of fields in iterator.
Definition flecs.h:1090
ecs_iter_t::count
int32_t count
Number of entities to iterate.
Definition flecs.h:1112
ecs_iter_t::group_id
uint64_t group_id
Group id for table, if group_by is used.
Definition flecs.h:1077
ecs_iter_t::next
ecs_iter_next_action_t next
Function to progress iterator.
Definition flecs.h:1120
ecs_iter_t::entities
const ecs_entity_t * entities
Entity identifiers.
Definition flecs.h:1068
flecs::_::range_iterator
Iterate over an integer range (used to iterate over entity range).
Definition iter.hpp:31
flecs::_::type
Definition flecs.hpp:38
flecs::entity
Entity.
Definition entity.hpp:30
flecs::field
Wrapper class around a field.
Definition field.hpp:61
flecs::id
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::iter
Class for iterating over query results.
Definition iter.hpp:68
flecs::iter::size
size_t size(int8_t index) const
Size of field data type.
Definition iter.hpp:195
flecs::iter::field
flecs::field< A > field(int8_t index) const
Get readonly access to field data.
Definition iter.hpp:68
flecs::iter::field_at
const A & field_at(int8_t index, size_t row) const
Get reference to field at row.
Definition iter.hpp:288
flecs::iter::is_self
bool is_self(int8_t index) const
Returns whether field is matched on self.
Definition iter.hpp:165
flecs::iter::str
flecs::string str() const
Convert current iterator result to string.
Definition iter.hpp:234
flecs::iter::field_count
int32_t field_count() const
Number of fields in iterator.
Definition iter.hpp:187
flecs::iter::column_index
int32_t column_index(int8_t index) const
Obtain column index for field.
Definition iter.hpp:222
flecs::iter::param
void * param()
Access param.
Definition iter.hpp:142
flecs::iter::field_at
void * field_at(int8_t index, size_t row) const
Get pointer to field at row.
Definition iter.hpp:277
flecs::iter::is_readonly
bool is_readonly(int8_t index) const
Returns whether field is readonly.
Definition iter.hpp:181
flecs::iter::changed
bool changed()
Check if the current table has changed since the last iteration.
Definition iter.hpp:321
flecs::iter::term_index
int8_t term_index() const
Obtain term that triggered an observer.
Definition iter.hpp:228
flecs::iter::ctx
T * ctx()
Access ctx.
Definition iter.hpp:135
flecs::iter::fini
void fini()
Free iterator resources.
Definition iter.hpp:394
flecs::iter::is_set
bool is_set(int8_t index) const
Returns whether field is set.
Definition iter.hpp:173
flecs::iter::field_at
A & field_at(int8_t index, size_t row) const
Get reference to field at row.
Definition iter.hpp:300
flecs::iter::ctx
void * ctx()
Access ctx.
Definition iter.hpp:127
flecs::iter::entity
flecs::entity entity(size_t row) const
Obtain mutable handle to entity being iterated over.
Definition iter.hpp:27
flecs::iter::each
void each()
Forward to each.
Definition iter.hpp:373
flecs::iter::field
flecs::untyped_field field(int8_t index) const
Get unchecked access to field data.
Definition iter.hpp:270
flecs::iter::get_var
flecs::entity get_var(int var_id) const
Get value of variable by id.
Definition iter.hpp:87
flecs::iter::entities
flecs::field< const flecs::entity_t > entities() const
Get readonly access to entity ids.
Definition iter.hpp:314
flecs::iter::iter
iter(ecs_iter_t *it)
Construct iterator from C iterator object.
Definition iter.hpp:78
flecs::iter::param
T * param()
Access param.
Definition iter.hpp:150
flecs::iter::targets
void targets(int8_t index, const Func &func)
Iterate targets for pair field.
Definition iter.hpp:104
flecs::iter::src
flecs::entity src(int8_t index) const
Obtain field source (0 if This).
Definition iter.hpp:33
flecs::iter::skip
void skip()
Skip current table.
Definition iter.hpp:332
flecs::iter::next
bool next()
Progress iterator.
Definition iter.hpp:357
flecs::pair
Type that represents a pair.
Definition pair.hpp:36
flecs::string
Definition string.hpp:13
flecs::table_range
Definition table.hpp:317
flecs::table
Definition table.hpp:18
flecs::type
Type class.
Definition type.hpp:21
flecs::untyped_field
Unsafe wrapper class around a field.
Definition field.hpp:26
flecs::world
The world.
Definition world.hpp:137