Skip to content
Flecs v4.2
iter.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/impl/iter.hpp
3 * @brief Iterator implementation.
4 */
5
6#pragma once
7
8namespace flecs
9{
10
11/** Get the entity associated with the system currently being run. */
13 return flecs::entity(iter_->stage, iter_->system);
14}
15
16/** Get the entity associated with the event that triggered the observer. */
17inline flecs::entity iter::event() const {
18 return flecs::entity(iter_->stage, iter_->event);
19}
20
21/** Get the event ID (component or pair). */
22inline flecs::id iter::event_id() const {
23 return flecs::id(iter_->stage, iter_->event_id);
24}
25
26/** Get the iterator world. */
27inline flecs::world iter::world() const {
28 return flecs::world(iter_->world);
29}
30
31inline flecs::world iter::stage() const {
32 return flecs::world(iter_->stage);
33}
34
35/** Get the entity for a given row. */
36inline flecs::entity iter::entity(size_t row) const {
37 ecs_assert(row < static_cast<size_t>(iter_->count),
39 return flecs::entity(iter_->stage, iter_->entities[row]);
40}
41
42/** Get the source entity for a field. */
43inline flecs::entity iter::src(int8_t index) const {
44 return flecs::entity(iter_->stage, ecs_field_src(iter_, index));
45}
46
47/** Get the ID for a field. */
48inline flecs::id iter::id(int8_t index) const {
49 return flecs::id(iter_->stage, ecs_field_id(iter_, index));
50}
51
52/** Get the pair ID for a field. */
53inline flecs::id iter::pair(int8_t index) const {
54 flecs::id_t id = ecs_field_id(iter_, index);
55 ecs_check(ECS_HAS_ID_FLAG(id, PAIR), ECS_INVALID_PARAMETER, nullptr);
56 return flecs::id(iter_->stage, id);
57error:
58 return flecs::id();
59}
60
61/** Get the type of the current table. */
62inline flecs::type iter::type() const {
63 return flecs::type(iter_->stage, ecs_table_get_type(iter_->table));
64}
65
66/** Get the current table. */
67inline flecs::table iter::table() const {
68 return flecs::table(iter_->world, iter_->table);
69}
70
71/** Get the other table (used for on_add/on_remove observers). */
73 return flecs::table(iter_->world, iter_->other_table);
74}
75
76/** Get the table range for the current result. */
78 return flecs::table_range(iter_->world, iter_->table,
79 iter_->offset, iter_->count);
80}
81
82/** Get field data for a const component type. */
83template <typename T, typename A, if_t< is_const_v<T> >>
84inline flecs::field<A> iter::field(int8_t index) const {
85 ecs_assert(!(iter_->flags & EcsIterCppEach) ||
86 ecs_field_src(iter_, index) != 0, ECS_INVALID_OPERATION,
87 "cannot .field from .each, use .field_at<%s>(%d, row) instead",
88 _::type_name<T>(), index);
89 return get_field<A>(index);
90}
91
92/** Get field data for a mutable component type. */
93template <typename T, typename A, if_not_t< is_const_v<T> >>
94inline flecs::field<A> iter::field(int8_t index) const {
95 ecs_assert(!(iter_->flags & EcsIterCppEach) ||
96 ecs_field_src(iter_, index) != 0, ECS_INVALID_OPERATION,
97 "cannot .field from .each, use .field_at<%s>(%d, row) instead",
98 _::type_name<T>(), index);
99 ecs_assert(!ecs_field_is_readonly(iter_, index),
100 ECS_ACCESS_VIOLATION, nullptr);
101 return get_field<A>(index);
102}
103
104/** Get base field data for a const component type. */
105template <typename T, typename A, if_t< is_const_v<T> >>
106inline flecs::base_field<A> iter::base_field(int8_t index) const {
107 ecs_assert(!(iter_->flags & EcsIterCppEach) ||
108 ecs_field_src(iter_, index) != 0, ECS_INVALID_OPERATION,
109 "cannot .base_field from .each, use .field_at<%s>(%d, row) instead",
110 _::type_name<T>(), index);
111 return get_base_field<A>(index);
112}
113
114/** Get base field data for a mutable component type. */
115template <typename T, typename A, if_not_t< is_const_v<T> >>
116inline flecs::base_field<A> iter::base_field(int8_t index) const {
117 ecs_assert(!(iter_->flags & EcsIterCppEach) ||
118 ecs_field_src(iter_, index) != 0, ECS_INVALID_OPERATION,
119 "cannot .base_field from .each, use .field_at<%s>(%d, row) instead",
120 _::type_name<T>(), index);
121 ecs_assert(!ecs_field_is_readonly(iter_, index),
122 ECS_ACCESS_VIOLATION, nullptr);
123 return get_base_field<A>(index);
124}
125
126#ifdef FLECS_QUERY_PLANS
127/** Get the value of a variable by ID. */
128inline flecs::entity iter::get_var(int var_id) const {
129 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, nullptr);
130 return flecs::entity(iter_->stage, ecs_iter_get_var(iter_, var_id));
131}
132
133/** Get the value of a variable by name.
134 * Get the value of a query variable for the current result.
135 */
136inline flecs::entity iter::get_var(const char *name) const {
137 const flecs::query_t *q = iter_->query;
138
139 int var_id = ecs_query_find_var(q, name);
140 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, "%s", name);
141 return flecs::entity(iter_->stage, ecs_iter_get_var(iter_, var_id));
142}
143#endif
144
145/** Iterate over targets for a field. */
146template <typename Func>
147void iter::targets(int8_t index, const Func& func) {
148 ecs_assert(iter_->table != nullptr, ECS_INVALID_OPERATION, nullptr);
149 ecs_assert(index < iter_->field_count, ECS_INVALID_PARAMETER, nullptr);
150 ecs_assert(ecs_field_is_set(iter_, index), ECS_INVALID_PARAMETER, nullptr);
151 const ecs_type_t *table_type = ecs_table_get_type(iter_->table);
152 const ecs_table_record_t *tr = iter_->trs[index];
153 int32_t i = tr->index, end = i + tr->count;
154 for (; i < end; i ++) {
155 ecs_id_t id = table_type->array[i];
156 ecs_assert(ECS_IS_PAIR(id), ECS_INVALID_PARAMETER,
157 "field does not match a pair");
158 flecs::entity tgt(iter_->stage,
159 ecs_pair_second(iter_->world, id));
160 func(tgt);
161 }
162}
163
164} // namespace flecs
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_ACCESS_VIOLATION
Access violation error code.
Definition log.h:746
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:696
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:698
#define ECS_COLUMN_INDEX_OUT_OF_RANGE
Column index out of range error code.
Definition log.h:748
#define ecs_check(condition, error_code,...)
Check.
Definition log.h:527
struct ecs_table_record_t ecs_table_record_t
Opaque type for table record.
Definition flecs.h:542
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:389
ecs_query_t query_t
Query type.
Definition c_types.hpp:25
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t ecs_field_src(const ecs_iter_t *it, int8_t index)
Return the field source.
bool ecs_field_is_readonly(const ecs_iter_t *it, int8_t index)
Test whether the field is read-only.
ecs_id_t ecs_field_id(const ecs_iter_t *it, int8_t index)
Return the ID matched for a field.
bool ecs_field_is_set(const ecs_iter_t *it, int8_t index)
Test whether a field is set.
ecs_entity_t ecs_iter_get_var(ecs_iter_t *it, int32_t var_id)
Get the value of an iterator variable as an entity.
int32_t ecs_query_find_var(const ecs_query_t *query, const char *name)
Find a variable index.
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get the type for a table.
ecs_flags32_t flags
Iterator flags.
Definition flecs.h:1236
A type is a list of (component) IDs.
Definition flecs.h:413
ecs_id_t * array
Array with IDs.
Definition flecs.h:414
Wrapper class around a field matched through component inheritance.
Definition field.hpp:122
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
flecs::id id(int8_t index) const
Obtain the ID matched for the field.
Definition iter.hpp:48
flecs::field< A > field(int8_t index) const
Get read-only access to field data.
Definition iter.hpp:84
flecs::entity event() const
Get the event entity associated with the iterator.
Definition iter.hpp:17
flecs::table table() const
Get the table for the current iterator result.
Definition iter.hpp:67
flecs::type type() const
Get the type of the iterated table.
Definition iter.hpp:62
int32_t field_count() const
Number of fields in the iterator.
Definition iter.hpp:228
row_iterator end() const
Get an iterator to the end of the entity range.
Definition iter.hpp:86
flecs::base_field< A > base_field(int8_t index) const
Get access to a field matched through component inheritance.
Definition iter.hpp:106
flecs::table other_table() const
Get the other table for the current iterator result.
Definition iter.hpp:72
flecs::entity entity(size_t row) const
Obtain a mutable handle to the entity being iterated over.
Definition iter.hpp:36
flecs::id pair(int8_t index) const
Obtain the pair ID matched for the field.
Definition iter.hpp:53
flecs::id event_id() const
Get the event ID associated with the iterator.
Definition iter.hpp:22
flecs::entity get_var(int var_id) const
Get value of variable by ID.
Definition iter.hpp:128
flecs::table_range range() const
Get the table range for the current iterator result.
Definition iter.hpp:77
void targets(int8_t index, const Func &func)
Iterate targets for pair field.
Definition iter.hpp:147
flecs::entity src(int8_t index) const
Obtain the field source (0 if This).
Definition iter.hpp:43
flecs::world world() const
Get the world associated with the iterator.
Definition iter.hpp:27
flecs::entity system() const
Get the system entity associated with the iterator.
Definition iter.hpp:12
Table range.
Definition table.hpp:205
Table.
Definition table.hpp:23
Type class.
Definition type.hpp:21
The world.
Definition world.hpp:129