Flecs v4.1
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
8namespace flecs
9{
10
13 return flecs::entity(iter_->world, iter_->system);
14}
15
17inline flecs::entity iter::event() const {
18 return flecs::entity(iter_->world, iter_->event);
19}
20
22inline flecs::id iter::event_id() const {
23 return flecs::id(iter_->world, iter_->event_id);
24}
25
27inline flecs::world iter::world() const {
28 return flecs::world(iter_->world);
29}
30
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
39inline flecs::entity iter::src(int8_t index) const {
40 return flecs::entity(iter_->world, ecs_field_src(iter_, index));
41}
42
44inline flecs::id iter::id(int8_t index) const {
45 return flecs::id(iter_->world, ecs_field_id(iter_, index));
46}
47
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, NULL);
52 return flecs::id(iter_->world, id);
53error:
54 return flecs::id();
55}
56
58inline flecs::type iter::type() const {
59 return flecs::type(iter_->world, ecs_table_get_type(iter_->table));
60}
61
63inline flecs::table iter::table() const {
64 return flecs::table(iter_->real_world, iter_->table);
65}
66
69 return flecs::table(iter_->real_world, iter_->other_table);
70}
71
74 return flecs::table_range(iter_->real_world, iter_->table,
75 iter_->offset, iter_->count);
76}
77
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
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),
97 return get_field<A>(index);
98}
99
101inline flecs::entity iter::get_var(int var_id) const {
102 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, 0);
103 return flecs::entity(iter_->world, ecs_iter_get_var(iter_, var_id));
104}
105
109inline flecs::entity iter::get_var(const char *name) const {
110 const flecs::query_t *q = iter_->query;
111
112 int var_id = ecs_query_find_var(q, name);
113 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, "%s", name);
114 return flecs::entity(iter_->world, ecs_iter_get_var(iter_, var_id));
115}
116
118template <typename Func>
119void iter::targets(int8_t index, const Func& func) {
120 ecs_assert(iter_->table != nullptr, ECS_INVALID_OPERATION, NULL);
121 ecs_assert(index < iter_->field_count, ECS_INVALID_PARAMETER, NULL);
123 const ecs_type_t *table_type = ecs_table_get_type(iter_->table);
124 const ecs_table_record_t *tr = iter_->trs[index];
125 int32_t i = tr->index, end = i + tr->count;
126 for (; i < end; i ++) {
127 ecs_id_t id = table_type->array[i];
128 ecs_assert(ECS_IS_PAIR(id), ECS_INVALID_PARAMETER,
129 "field does not match a pair");
130 flecs::entity tgt(iter_->world,
131 ecs_pair_second(iter_->real_world, id));
132 func(tgt);
133 }
134}
135
136} // namespace flecs
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_ACCESS_VIOLATION
Access violation error code.
Definition log.h:709
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:659
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:661
#define ECS_COLUMN_INDEX_OUT_OF_RANGE
Column index out of range error code.
Definition log.h:711
#define ecs_check(condition, error_code,...)
Check.
Definition log.h:517
struct ecs_table_record_t ecs_table_record_t
Opaque type for table record.
Definition flecs.h:527
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:374
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_world_t * real_world
Actual world.
Definition flecs.h:1169
ecs_entity_t event
The event (if applicable).
Definition flecs.h:1190
ecs_flags32_t flags
Iterator flags.
Definition flecs.h:1216
ecs_table_t * table
Current table.
Definition flecs.h:1178
int32_t offset
Offset relative to the current table.
Definition flecs.h:1172
ecs_id_t event_id
The (component) ID for the event.
Definition flecs.h:1191
ecs_world_t * world
The world.
Definition flecs.h:1168
const ecs_query_t * query
Query being evaluated.
Definition flecs.h:1199
ecs_entity_t system
The system (if applicable).
Definition flecs.h:1189
ecs_table_t * other_table
Previous or next table when adding or removing.
Definition flecs.h:1179
const ecs_table_record_t ** trs
Info on where to find the field in the table.
Definition flecs.h:1176
int32_t count
Number of entities to iterate.
Definition flecs.h:1173
const ecs_entity_t * entities
Entity identifiers.
Definition flecs.h:1174
Queries are lists of constraints (terms) that match entities.
Definition flecs.h:834
A type is a list of (component) IDs.
Definition flecs.h:398
ecs_id_t * array
Array with IDs.
Definition flecs.h:399
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:101
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:119
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:449
Table.
Definition table.hpp:23
Type class.
Definition type.hpp:21
The world.
Definition world.hpp:246