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

| 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
5
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
27 inline 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
33 inline flecs::entity iter::src(int8_t index) const {
34 return flecs::entity(iter_->world, ecs_field_src(iter_, index));
35}
36
37 inline flecs::id iter::id(int8_t index) const {
38 return flecs::id(iter_->world, ecs_field_id(iter_, index));
39}
40
41 inline 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*>
68 inline flecs::field<A> iter::field(int8_t index) const {
69 ecs_assert(!(iter_->flags & EcsIterCppEach) ||
70 ecs_field_src(iter_, index) != 0, ECS_INVALID_OPERATION,
71 "cannot .field from .each, use .field_at<%s>(%d, row) instead",
72 _::type_name<T>(), index);
73 return get_field<A>(index);
74}
75
76template <typename T, typename A,
77 typename std::enable_if<
78 std::is_const<T>::value == false, void>::type*>
79inline flecs::field<A> iter::field(int8_t index) const {
80 ecs_assert(!(iter_->flags & EcsIterCppEach) ||
81 ecs_field_src(iter_, index) != 0, ECS_INVALID_OPERATION,
82 "cannot .field from .each, use .field_at<%s>(%d, row) instead",
83 _::type_name<T>(), index);
84 ecs_assert(!ecs_field_is_readonly(iter_, index),
85 ECS_ACCESS_VIOLATION, NULL);
86 return get_field<A>(index);
87}
88
89 inline flecs::entity iter::get_var(int var_id) const {
90 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, 0);
91 return flecs::entity(iter_->world, ecs_iter_get_var(iter_, var_id));
92}
93
97 inline flecs::entity iter::get_var(const char *name) const {
98 const flecs::query_t *q = iter_->query;
99
100 int var_id = ecs_query_find_var(q, name);
101 ecs_assert(var_id != -1, ECS_INVALID_PARAMETER, name);
102 return flecs::entity(iter_->world, ecs_iter_get_var(iter_, var_id));
103}
104
105template <typename Func>
106 void iter::targets(int8_t index, const Func& func) {
107 ecs_assert(iter_->table != nullptr, ECS_INVALID_OPERATION, NULL);
108 ecs_assert(index < iter_->field_count, ECS_INVALID_PARAMETER, NULL);
109 ecs_assert(ecs_field_is_set(iter_, index), ECS_INVALID_PARAMETER, NULL);
110 const ecs_type_t *table_type = ecs_table_get_type(iter_->table);
111 const ecs_table_record_t *tr = iter_->trs[index];
112 int32_t i = tr->index, end = i + tr->count;
113 for (; i < end; i ++) {
114 ecs_id_t id = table_type->array[i];
115 ecs_assert(ECS_IS_PAIR(id), ECS_INVALID_PARAMETER,
116 "field does not match a pair");
117 flecs::entity tgt(iter_->world,
118 ecs_pair_second(iter_->real_world, id));
119 func(tgt);
120 }
121}
122
123} // namespace flecs
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
ecs_check
#define ecs_check(condition, error_code,...)
Check.
Definition log.h:412
ecs_id_t
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
Definition flecs.h:354
ecs_field_src
ecs_entity_t ecs_field_src(const ecs_iter_t *it, int8_t index)
Return field source.
ecs_field_is_readonly
bool ecs_field_is_readonly(const ecs_iter_t *it, int8_t index)
Test whether the field is readonly.
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_iter_get_var
ecs_entity_t ecs_iter_get_var(ecs_iter_t *it, int32_t var_id)
Get value of iterator variable as entity.
ecs_query_find_var
int32_t ecs_query_find_var(const ecs_query_t *query, const char *name)
Find variable index.
ecs_table_get_type
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get type for table.
ecs_iter_t::real_world
ecs_world_t * real_world
Actual world.
Definition flecs.h:1145
ecs_iter_t::flags
ecs_flags32_t flags
Iterator flags.
Definition flecs.h:1192
ecs_iter_t::table
ecs_table_t * table
Current table.
Definition flecs.h:1154
ecs_iter_t::world
ecs_world_t * world
The world.
Definition flecs.h:1144
ecs_type_t
A type is a list of (component) ids.
Definition flecs.h:378
ecs_type_t::array
ecs_id_t * array
Array with ids.
Definition flecs.h:379
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::id
flecs::id id(int8_t index) const
Obtain id matched for field.
Definition iter.hpp:37
flecs::iter::field
flecs::field< A > field(int8_t index) const
Get readonly access to field data.
Definition iter.hpp:68
flecs::iter::field_count
int32_t field_count() const
Number of fields in iterator.
Definition iter.hpp:187
flecs::iter::entity
flecs::entity entity(size_t row) const
Obtain mutable handle to entity being iterated over.
Definition iter.hpp:27
flecs::iter::pair
flecs::id pair(int8_t index) const
Obtain pair id matched for field.
Definition iter.hpp:41
flecs::iter::get_var
flecs::entity get_var(int var_id) const
Get value of variable by id.
Definition iter.hpp:89
flecs::iter::targets
void targets(int8_t index, const Func &func)
Iterate targets for pair field.
Definition iter.hpp:106
flecs::iter::src
flecs::entity src(int8_t index) const
Obtain field source (0 if This).
Definition iter.hpp:33
flecs::table
Definition table.hpp:18
flecs::type
Type class.
Definition type.hpp:21
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
flecs::world::id
flecs::id id(E value) const
Convert enum constant to entity.