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
8namespace flecs
9{
10
11inline flecs::entity iter::system() const {
12 return flecs::entity(iter_->world, iter_->system);
13}
14
15inline flecs::entity iter::event() const {
16 return flecs::entity(iter_->world, iter_->event);
17}
18
19inline flecs::id iter::event_id() const {
20 return flecs::id(iter_->world, iter_->event_id);
21}
22
23inline flecs::world iter::world() const {
24 return flecs::world(iter_->world);
25}
26
27inline flecs::entity iter::entity(size_t row) const {
28 ecs_assert(row < static_cast<size_t>(iter_->count),
29 ECS_COLUMN_INDEX_OUT_OF_RANGE, NULL);
30 return flecs::entity(iter_->world, iter_->entities[row]);
31}
32
33inline flecs::entity iter::src(int8_t index) const {
34 return flecs::entity(iter_->world, ecs_field_src(iter_, index));
35}
36
37inline flecs::id iter::id(int8_t index) const {
38 return flecs::id(iter_->world, ecs_field_id(iter_, index));
39}
40
41inline flecs::id iter::pair(int8_t index) const {
42 flecs::id_t id = ecs_field_id(iter_, index);
43 ecs_check(ECS_HAS_ID_FLAG(id, PAIR), ECS_INVALID_PARAMETER, NULL);
44 return flecs::id(iter_->world, id);
45error:
46 return flecs::id();
47}
48
49inline flecs::type iter::type() const {
50 return flecs::type(iter_->world, ecs_table_get_type(iter_->table));
51}
52
53inline flecs::table iter::table() const {
54 return flecs::table(iter_->real_world, iter_->table);
55}
56
57inline flecs::table iter::other_table() const {
58 return flecs::table(iter_->real_world, iter_->other_table);
59}
60
61inline flecs::table_range iter::range() const {
62 return flecs::table_range(iter_->real_world, iter_->table,
63 iter_->offset, iter_->count);
64}
65
66template <typename T, typename A,
67 typename std::enable_if<std::is_const<T>::value, void>::type*>
68inline flecs::field<A> iter::field(int8_t index) const {
69 ecs_assert(!(iter_->flags & EcsIterCppEach), ECS_INVALID_OPERATION,
70 "cannot .field from .each, use .field_at<const %s>(%d, row) instead",
71 _::type_name<T>(), index);
72 return get_field<A>(index);
73}
74
75template <typename T, typename A,
76 typename std::enable_if<
77 std::is_const<T>::value == false, void>::type*>
78inline flecs::field<A> iter::field(int8_t index) const {
79 ecs_assert(!(iter_->flags & EcsIterCppEach), ECS_INVALID_OPERATION,
80 "cannot .field from .each, use .field_at<%s>(%d, row) instead",
81 _::type_name<T>(), index);
82 ecs_assert(!ecs_field_is_readonly(iter_, index),
83 ECS_ACCESS_VIOLATION, NULL);
84 return get_field<A>(index);
85}
86
87inline flecs::entity iter::get_var(int var_id) const {
88 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, 0);
89 return flecs::entity(iter_->world, ecs_iter_get_var(iter_, var_id));
90}
91
95inline flecs::entity iter::get_var(const char *name) const {
96 ecs_query_iter_t *qit = &iter_->priv_.iter.query;
97 const flecs::query_t *q = qit->query;
98 int var_id = ecs_query_find_var(q, name);
99 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, name);
100 return flecs::entity(iter_->world, ecs_iter_get_var(iter_, var_id));
101}
102
103template <typename Func>
104void iter::targets(int8_t index, const Func& func) {
105 ecs_assert(iter_->table != nullptr, ECS_INVALID_OPERATION, NULL);
106 ecs_assert(index < iter_->field_count, ECS_INVALID_PARAMETER, NULL);
107 ecs_assert(ecs_field_is_set(iter_, index), ECS_INVALID_PARAMETER, NULL);
108 const ecs_type_t *table_type = ecs_table_get_type(iter_->table);
109 const ecs_table_record_t *tr = iter_->trs[index];
110 int32_t i = tr->index, end = i + tr->count;
111 for (; i < end; i ++) {
112 ecs_id_t id = table_type->array[i];
113 ecs_assert(ECS_IS_PAIR(id), ECS_INVALID_PARAMETER,
114 "field does not match a pair");
115 flecs::entity tgt(iter_->world,
116 ecs_pair_second(iter_->real_world, id));
117 func(tgt);
118 }
119}
120
121} // namespace flecs
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
#define ecs_check(condition, error_code,...)
Check.
Definition log.h:399
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
Definition flecs.h:339
ecs_entity_t ecs_field_src(const ecs_iter_t *it, int8_t index)
Return field source.
bool ecs_field_is_readonly(const ecs_iter_t *it, int8_t index)
Test whether the field is readonly.
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.
ecs_entity_t ecs_iter_get_var(ecs_iter_t *it, int32_t var_id)
Get value of iterator variable as entity.
int32_t ecs_query_find_var(const ecs_query_t *query, const char *name)
Find variable index.
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get type for table.
ecs_world_t * real_world
Actual world.
Definition flecs.h:1103
ecs_entity_t event
The event (if applicable)
Definition flecs.h:1123
ecs_flags32_t flags
Iterator flags.
Definition flecs.h:1153
ecs_table_t * table
Current table.
Definition flecs.h:1108
int32_t offset
Offset relative to current table.
Definition flecs.h:1149
ecs_id_t event_id
The (component) id for the event.
Definition flecs.h:1124
ecs_iter_private_t priv_
Private data.
Definition flecs.h:1155
ecs_world_t * world
The world.
Definition flecs.h:1102
ecs_entity_t system
The system (if applicable)
Definition flecs.h:1122
ecs_table_t * other_table
Prev or next table when adding/removing.
Definition flecs.h:1109
const ecs_table_record_t ** trs
Info on where to find field in table.
Definition flecs.h:1112
int32_t count
Number of entities to iterate.
Definition flecs.h:1150
const ecs_entity_t * entities
Entity identifiers.
Definition flecs.h:1106
Queries are lists of constraints (terms) that match entities.
Definition flecs.h:791
Metadata describing where a component id is stored in a table.
Definition flecs.h:512
int16_t index
First type index where id occurs in table.
Definition flecs.h:514
int16_t count
Number of times id occurs in table.
Definition flecs.h:515
A type is a list of (component) ids.
Definition flecs.h:363
ecs_id_t * array
Array with ids.
Definition flecs.h:364
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 id matched for field.
Definition iter.hpp:37
flecs::field< A > field(int8_t index) const
Get readonly access to field data.
Definition iter.hpp:68
int32_t field_count() const
Number of fields in iterator.
Definition iter.hpp:187
flecs::entity entity(size_t row) const
Obtain mutable handle to entity being iterated over.
Definition iter.hpp:27
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:87
void targets(int8_t index, const Func &func)
Iterate targets for pair field.
Definition iter.hpp:104
flecs::entity src(int8_t index) const
Obtain field source (0 if This).
Definition iter.hpp:33
Type class.
Definition type.hpp:21
The world.
Definition world.hpp:137