Skip to content
Flecs v4.1
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_->world, 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_->world, iter_->event);
19}
20
21/** Get the event ID (component or pair). */
22inline flecs::id iter::event_id() const {
23 return flecs::id(iter_->world, iter_->event_id);
24}
25
26/** Get the iterator world. */
27inline flecs::world iter::world() const {
28 return flecs::world(iter_->world);
29}
30
31/** Get the entity for a given row. */
32inline flecs::entity iter::entity(size_t row) const {
33 ecs_assert(row < static_cast<size_t>(iter_->count),
35 return flecs::entity(iter_->world, iter_->entities[row]);
36}
37
38/** Get the source entity for a field. */
39inline flecs::entity iter::src(int8_t index) const {
40 return flecs::entity(iter_->world, ecs_field_src(iter_, index));
41}
42
43/** Get the ID for a field. */
44inline flecs::id iter::id(int8_t index) const {
45 return flecs::id(iter_->world, ecs_field_id(iter_, index));
46}
47
48/** Get the pair ID for a field. */
49inline flecs::id iter::pair(int8_t index) const {
50 flecs::id_t id = ecs_field_id(iter_, index);
51 ecs_check(ECS_HAS_ID_FLAG(id, PAIR), ECS_INVALID_PARAMETER, nullptr);
52 return flecs::id(iter_->world, id);
53error:
54 return flecs::id();
55}
56
57/** Get the type of the current table. */
58inline flecs::type iter::type() const {
59 return flecs::type(iter_->world, ecs_table_get_type(iter_->table));
60}
61
62/** Get the current table. */
63inline flecs::table iter::table() const {
64 return flecs::table(iter_->real_world, iter_->table);
65}
66
67/** Get the other table (used for on_add/on_remove observers). */
69 return flecs::table(iter_->real_world, iter_->other_table);
70}
71
72/** Get the table range for the current result. */
74 return flecs::table_range(iter_->real_world, iter_->table,
75 iter_->offset, iter_->count);
76}
77
78/** Get field data for a const component type. */
79template <typename T, typename A, if_t< is_const_v<T> >>
80inline flecs::field<A> iter::field(int8_t index) const {
81 ecs_assert(!(iter_->flags & EcsIterCppEach) ||
82 ecs_field_src(iter_, index) != 0, ECS_INVALID_OPERATION,
83 "cannot .field from .each, use .field_at<%s>(%d, row) instead",
84 _::type_name<T>(), index);
85 return get_field<A>(index);
86}
87
88/** Get field data for a mutable component type. */
89template <typename T, typename A, if_not_t< is_const_v<T> >>
90inline flecs::field<A> iter::field(int8_t index) const {
91 ecs_assert(!(iter_->flags & EcsIterCppEach) ||
92 ecs_field_src(iter_, index) != 0, ECS_INVALID_OPERATION,
93 "cannot .field from .each, use .field_at<%s>(%d, row) instead",
94 _::type_name<T>(), index);
95 ecs_assert(!ecs_field_is_readonly(iter_, index),
96 ECS_ACCESS_VIOLATION, nullptr);
97 return get_field<A>(index);
98}
99
100#ifdef FLECS_QUERY_PLANS
101/** Get the value of a variable by ID. */
102inline flecs::entity iter::get_var(int var_id) const {
103 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, nullptr);
104 return flecs::entity(iter_->world, ecs_iter_get_var(iter_, var_id));
105}
106
107/** Get the value of a variable by name.
108 * Get the value of a query variable for the current result.
109 */
110inline flecs::entity iter::get_var(const char *name) const {
111 const flecs::query_t *q = iter_->query;
112
113 int var_id = ecs_query_find_var(q, name);
114 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, "%s", name);
115 return flecs::entity(iter_->world, ecs_iter_get_var(iter_, var_id));
116}
117#endif
118
119/** Iterate over targets for a field. */
120template <typename Func>
121void iter::targets(int8_t index, const Func& func) {
122 ecs_assert(iter_->table != nullptr, ECS_INVALID_OPERATION, nullptr);
123 ecs_assert(index < iter_->field_count, ECS_INVALID_PARAMETER, nullptr);
124 ecs_assert(ecs_field_is_set(iter_, index), ECS_INVALID_PARAMETER, nullptr);
125 const ecs_type_t *table_type = ecs_table_get_type(iter_->table);
126 const ecs_table_record_t *tr = iter_->trs[index];
127 int32_t i = tr->index, end = i + tr->count;
128 for (; i < end; i ++) {
129 ecs_id_t id = table_type->array[i];
130 ecs_assert(ECS_IS_PAIR(id), ECS_INVALID_PARAMETER,
131 "field does not match a pair");
132 flecs::entity tgt(iter_->world,
133 ecs_pair_second(iter_->real_world, id));
134 func(tgt);
135 }
136}
137
138} // namespace flecs
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_ACCESS_VIOLATION
Access violation error code.
Definition log.h:719
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:669
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
#define ECS_COLUMN_INDEX_OUT_OF_RANGE
Column index out of range error code.
Definition log.h:721
#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:541
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:388
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:1245
A type is a list of (component) IDs.
Definition flecs.h:412
ecs_id_t * array
Array with IDs.
Definition flecs.h:413
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:44
flecs::field< A > field(int8_t index) const
Get read-only access to field data.
Definition iter.hpp:80
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:63
flecs::type type() const
Get the type of the iterated table.
Definition iter.hpp:58
int32_t field_count() const
Number of fields in the iterator.
Definition iter.hpp:219
row_iterator end() const
Get an iterator to the end of the entity range.
Definition iter.hpp:86
flecs::table other_table() const
Get the other table for the current iterator result.
Definition iter.hpp:68
flecs::entity entity(size_t row) const
Obtain a mutable handle to the entity being iterated over.
Definition iter.hpp:32
flecs::id pair(int8_t index) const
Obtain the pair ID matched for the field.
Definition iter.hpp:49
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:102
flecs::table_range range() const
Get the table range for the current iterator result.
Definition iter.hpp:73
void targets(int8_t index, const Func &func)
Iterate targets for pair field.
Definition iter.hpp:121
flecs::entity src(int8_t index) const
Obtain the field source (0 if This).
Definition iter.hpp:39
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