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
12 inline flecs::entity iter::system() const {
13 return flecs::entity(iter_->world, iter_->system);
14}
15
17 inline flecs::entity iter::event() const {
18 return flecs::entity(iter_->world, iter_->event);
19}
20
22 inline flecs::id iter::event_id() const {
23 return flecs::id(iter_->world, iter_->event_id);
24}
25
27 inline flecs::world iter::world() const {
28 return flecs::world(iter_->world);
29}
30
32 inline flecs::entity iter::entity(size_t row) const {
33 ecs_assert(row < static_cast<size_t>(iter_->count),
34 ECS_COLUMN_INDEX_OUT_OF_RANGE, NULL);
35 return flecs::entity(iter_->world, iter_->entities[row]);
36}
37
39 inline flecs::entity iter::src(int8_t index) const {
40 return flecs::entity(iter_->world, ecs_field_src(iter_, index));
41}
42
44 inline flecs::id iter::id(int8_t index) const {
45 return flecs::id(iter_->world, ecs_field_id(iter_, index));
46}
47
49 inline 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
58 inline flecs::type iter::type() const {
59 return flecs::type(iter_->world, ecs_table_get_type(iter_->table));
60}
61
63 inline flecs::table iter::table() const {
64 return flecs::table(iter_->real_world, iter_->table);
65}
66
68 inline flecs::table iter::other_table() const {
69 return flecs::table(iter_->real_world, iter_->other_table);
70}
71
73 inline flecs::table_range iter::range() const {
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> >>
80 inline 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),
96 ECS_ACCESS_VIOLATION, NULL);
97 return get_field<A>(index);
98}
99
101 inline 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
109 inline 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>
119 void 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);
122 ecs_assert(ecs_field_is_set(iter_, index), 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
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
ECS_ACCESS_VIOLATION
#define ECS_ACCESS_VIOLATION
Access violation error code.
Definition log.h:709
ECS_INVALID_OPERATION
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:659
ECS_INVALID_PARAMETER
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:661
ECS_COLUMN_INDEX_OUT_OF_RANGE
#define ECS_COLUMN_INDEX_OUT_OF_RANGE
Column index out of range error code.
Definition log.h:711
ecs_check
#define ecs_check(condition, error_code,...)
Check.
Definition log.h:517
ecs_table_record_t
struct ecs_table_record_t ecs_table_record_t
Opaque type for table record.
Definition flecs.h:527
ecs_id_t
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:374
flecs::query_t
ecs_query_t query_t
Query type.
Definition c_types.hpp:25
flecs::id_t
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_field_src
ecs_entity_t ecs_field_src(const ecs_iter_t *it, int8_t index)
Return the field source.
ecs_field_is_readonly
bool ecs_field_is_readonly(const ecs_iter_t *it, int8_t index)
Test whether the field is read-only.
ecs_field_id
ecs_id_t ecs_field_id(const ecs_iter_t *it, int8_t index)
Return the ID matched for a field.
ecs_field_is_set
bool ecs_field_is_set(const ecs_iter_t *it, int8_t index)
Test whether a field is set.
ecs_iter_get_var
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.
ecs_query_find_var
int32_t ecs_query_find_var(const ecs_query_t *query, const char *name)
Find a variable index.
ecs_table_get_type
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get the type for a table.
ecs_iter_t::flags
ecs_flags32_t flags
Iterator flags.
Definition flecs.h:1216
ecs_type_t
A type is a list of (component) IDs.
Definition flecs.h:398
ecs_type_t::array
ecs_id_t * array
Array with IDs.
Definition flecs.h:399
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 the ID matched for the field.
Definition iter.hpp:44
flecs::iter::field
flecs::field< A > field(int8_t index) const
Get read-only access to field data.
Definition iter.hpp:80
flecs::iter::event
flecs::entity event() const
Get the event entity associated with the iterator.
Definition iter.hpp:17
flecs::iter::table
flecs::table table() const
Get the table for the current iterator result.
Definition iter.hpp:63
flecs::iter::type
flecs::type type() const
Get the type of the iterated table.
Definition iter.hpp:58
flecs::iter::field_count
int32_t field_count() const
Number of fields in the iterator.
Definition iter.hpp:219
flecs::iter::end
row_iterator end() const
Get an iterator to the end of the entity range.
Definition iter.hpp:86
flecs::iter::other_table
flecs::table other_table() const
Get the other table for the current iterator result.
Definition iter.hpp:68
flecs::iter::entity
flecs::entity entity(size_t row) const
Obtain a mutable handle to the entity being iterated over.
Definition iter.hpp:32
flecs::iter::pair
flecs::id pair(int8_t index) const
Obtain the pair ID matched for the field.
Definition iter.hpp:49
flecs::iter::event_id
flecs::id event_id() const
Get the event ID associated with the iterator.
Definition iter.hpp:22
flecs::iter::get_var
flecs::entity get_var(int var_id) const
Get value of variable by ID.
Definition iter.hpp:101
flecs::iter::range
flecs::table_range range() const
Get the table range for the current iterator result.
Definition iter.hpp:73
flecs::iter::targets
void targets(int8_t index, const Func &func)
Iterate targets for pair field.
Definition iter.hpp:119
flecs::iter::src
flecs::entity src(int8_t index) const
Obtain the field source (0 if This).
Definition iter.hpp:39
flecs::iter::world
flecs::world world() const
Get the world associated with the iterator.
Definition iter.hpp:27
flecs::iter::system
flecs::entity system() const
Get the system entity associated with the iterator.
Definition iter.hpp:12
flecs::table_range
Table range.
Definition table.hpp:449
flecs::table
Table.
Definition table.hpp:23
flecs::type
Type class.
Definition type.hpp:21
flecs::world
The world.
Definition world.hpp:246