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

| Flecs v4.0 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8namespace flecs {
9
10inline untyped_ref::untyped_ref(flecs::entity entity, flecs::id_t id)
11 : untyped_ref(entity.world(), entity.id(), id) { }
12
13 inline flecs::entity untyped_ref::entity() const {
14 return flecs::entity(world_, ref_.entity);
15}
16
17template <typename T>
18flecs::ref<T>::ref(flecs::entity entity, flecs::id_t id)
19 : ref(entity.world(), entity.id(), id) { }
20
21template <typename Self>
22template <typename Func>
23 inline const Self& entity_builder<Self>::insert(const Func& func) const {
24 _::entity_with_delegate<Func>::invoke_ensure(
25 this->world_, this->id_, func);
26 return to_base();
27}
28
29template<typename Enum>
30 inline Enum entity_view::get_constant() const {
31 flecs::entity tgt = this->target<Enum>();
32 return tgt.to_constant<Enum>();
33}
34
35template<typename First>
36 inline flecs::entity entity_view::target(int32_t index) const
37{
38 return flecs::entity(world_,
39 ecs_get_target(world_, id_, _::type<First>::id(world_), index));
40}
41
42 inline flecs::entity entity_view::target(
43 flecs::entity_t relationship,
44 int32_t index) const
45{
46 return flecs::entity(world_,
47 ecs_get_target(world_, id_, relationship, index));
48}
49
50 inline flecs::entity entity_view::target_for(
51 flecs::entity_t relationship,
52 flecs::id_t id) const
53{
54 return flecs::entity(world_,
55 ecs_get_target_for_id(world_, id_, relationship, id));
56}
57
58template <typename T>
59inline flecs::entity entity_view::target_for(flecs::entity_t relationship) const {
60 return target_for(relationship, _::type<T>::id(world_));
61}
62
63template <typename First, typename Second>
64inline flecs::entity entity_view::target_for(flecs::entity_t relationship) const {
65 return target_for(relationship, _::type<First, Second>::id(world_));
66}
67
68 inline flecs::entity entity_view::parent() const {
69 return flecs::entity(world_, ecs_get_parent(world_, id_));
70}
71
72 inline flecs::entity entity_view::mut(const flecs::world& stage) const {
73 ecs_assert(!stage.is_readonly(), ECS_INVALID_PARAMETER,
74 "cannot use readonly world/stage to create mutable handle");
75 return flecs::entity(id_).set_stage(stage.c_ptr());
76}
77
78 inline flecs::entity entity_view::mut(const flecs::iter& it) const {
79 ecs_assert(!it.world().is_readonly(), ECS_INVALID_PARAMETER,
80 "cannot use iterator created for readonly world/stage to create mutable handle");
81 return flecs::entity(id_).set_stage(it.world().c_ptr());
82}
83
84 inline flecs::entity entity_view::mut(const flecs::entity_view& e) const {
85 ecs_assert(!e.world().is_readonly(), ECS_INVALID_PARAMETER,
86 "cannot use entity created for readonly world/stage to create mutable handle");
87 return flecs::entity(id_).set_stage(e.world_);
88}
89
90inline flecs::entity entity_view::set_stage(world_t *stage) {
91 return flecs::entity(stage, id_);
92}
93
94 inline flecs::type entity_view::type() const {
95 return flecs::type(world_, ecs_get_type(world_, id_));
96}
97
98 inline flecs::table entity_view::table() const {
99 return flecs::table(world_, ecs_get_table(world_, id_));
100}
101
102 inline flecs::table_range entity_view::range() const {
103 ecs_record_t *r = ecs_record_find(world_, id_);
104 if (r) {
105 return flecs::table_range(world_, r->table,
106 ECS_RECORD_TO_ROW(r->row), 1);
107 }
108 return flecs::table_range();
109}
110
111template <typename Func>
112 inline void entity_view::each(const Func& func) const {
113 const ecs_type_t *type = ecs_get_type(world_, id_);
114 if (!type) {
115 return;
116 }
117
118 const ecs_id_t *ids = type->array;
119 int32_t count = type->count;
120
121 for (int i = 0; i < count; i ++) {
122 ecs_id_t id = ids[i];
123 flecs::id ent(world_, id);
124 func(ent);
125 }
126}
127
128template <typename Func>
129 inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& func) const {
130 flecs::world_t *real_world = const_cast<flecs::world_t*>(
131 ecs_get_world(world_));
132
133 const ecs_table_t *table = ecs_get_table(world_, id_);
134 if (!table) {
135 return;
136 }
137
138 const ecs_type_t *type = ecs_table_get_type(table);
139 if (!type) {
140 return;
141 }
142
143 flecs::id_t pattern = pred;
144 if (obj) {
145 pattern = ecs_pair(pred, obj);
146 }
147
148 int32_t cur = 0;
149 id_t *ids = type->array;
150
151 while (-1 != (cur = ecs_search_offset(real_world, table, cur, pattern, 0)))
152 {
153 flecs::id ent(world_, ids[cur]);
154 func(ent);
155 cur ++;
156 }
157}
158
159template <typename Func>
160 inline void entity_view::each(const flecs::entity_view& rel, const Func& func) const {
161 return this->each(rel, flecs::Wildcard, [&](flecs::id id) {
162 flecs::entity obj = id.second();
163 func(obj);
164 });
165}
166
167template <typename Func, if_t< is_callable<Func>::value > >
168 inline bool entity_view::get(const Func& func) const {
169 return _::entity_with_delegate<Func>::invoke_get(world_, id_, func);
170}
171
172 inline flecs::entity entity_view::lookup(const char *path, bool search_path) const {
173 ecs_assert(id_ != 0, ECS_INVALID_PARAMETER, "invalid lookup from null handle");
174 auto id = ecs_lookup_path_w_sep(world_, id_, path, "::", "::", search_path);
175 return flecs::entity(world_, id);
176}
177
178inline flecs::entity entity_view::clone(bool copy_value, flecs::entity_t dst_id) const {
179 if (!dst_id) {
180 dst_id = ecs_new(world_);
181 }
182
183 flecs::entity dst = flecs::entity(world_, dst_id);
184 ecs_clone(world_, dst_id, id_, copy_value);
185 return dst;
186}
187
188// Entity mixin implementation
189template <typename... Args>
190 inline flecs::entity world::entity(Args &&... args) const {
191 return flecs::entity(world_, FLECS_FWD(args)...);
192}
193
194template <typename E, if_t< is_enum<E>::value >>
195 inline flecs::id world::id(E value) const {
196 flecs::entity_t constant = enum_type<E>(world_).entity(value);
197 return flecs::id(world_, constant);
198}
199
200template <typename E, if_t< is_enum<E>::value >>
201 inline flecs::entity world::entity(E value) const {
202 flecs::entity_t constant = enum_type<E>(world_).entity(value);
203 return flecs::entity(world_, constant);
204}
205
206template <typename T>
207 inline flecs::entity world::entity(const char *name) const {
208 return flecs::entity(world_, _::type<T>::register_id(world_, name, true, false) );
209}
210
211template <typename... Args>
212 inline flecs::entity world::prefab(Args &&... args) const {
213 flecs::entity result = flecs::entity(world_, FLECS_FWD(args)...);
214 result.add(flecs::Prefab);
215 return result;
216}
217
218template <typename T>
219 inline flecs::entity world::prefab(const char *name) const {
220 flecs::entity result = this->entity<T>(name);
221 result.add(flecs::Prefab);
222 return result;
223}
224
225}
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
ecs_id_t
uint64_t ecs_id_t
Ids are the things that can be added to an entity.
Definition flecs.h:354
ecs_record_t
struct ecs_record_t ecs_record_t
Information about an entity, like its table and row.
Definition flecs.h:470
ecs_table_t
struct ecs_table_t ecs_table_t
A table stores entities and components for a specific type.
Definition flecs.h:411
flecs::component::constant
untyped_component & constant(const char *name, T value)
Add constant.
Definition untyped_component.inl:192
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:190
flecs::entity_view::to_constant
E to_constant() const
Convert entity to enum constant.
Definition impl.hpp:11
flecs::world::prefab
flecs::entity prefab(Args &&... args) const
Create a prefab.
Definition impl.hpp:212
ecs_clone
ecs_entity_t ecs_clone(ecs_world_t *world, ecs_entity_t dst, ecs_entity_t src, bool copy_value)
Clone an entity This operation clones the components of one entity into another entity.
ecs_new
ecs_entity_t ecs_new(ecs_world_t *world)
Create new entity id.
ecs_get_target
ecs_entity_t ecs_get_target(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t rel, int32_t index)
Get the target of a relationship.
ecs_get_parent
ecs_entity_t ecs_get_parent(const ecs_world_t *world, ecs_entity_t entity)
Get parent (target of ChildOf relationship) for entity.
ecs_get_target_for_id
ecs_entity_t ecs_get_target_for_id(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t rel, ecs_id_t component)
Get the target of a relationship for a given component.
ecs_get_type
const ecs_type_t * ecs_get_type(const ecs_world_t *world, ecs_entity_t entity)
Get the type of an entity.
ecs_get_table
ecs_table_t * ecs_get_table(const ecs_world_t *world, ecs_entity_t entity)
Get the table of an entity.
ecs_lookup_path_w_sep
ecs_entity_t ecs_lookup_path_w_sep(const ecs_world_t *world, ecs_entity_t parent, const char *path, const char *sep, const char *prefix, bool recursive)
Lookup an entity from a path.
ecs_search_offset
int32_t ecs_search_offset(const ecs_world_t *world, const ecs_table_t *table, int32_t offset, ecs_id_t component, ecs_id_t *component_out)
Search for component in table type starting from an offset.
ecs_table_get_type
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get type for table.
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
ecs_type_t
A type is a list of (component) ids.
Definition flecs.h:378
flecs::_::entity_with_delegate
Definition delegate.hpp:927
flecs::_::type
Definition flecs.hpp:39
flecs::entity_builder::insert
const Self & insert(const Func &func) const
Set 1..N components.
Definition impl.hpp:23
flecs::entity_builder::add
const Self & add() const
Add a component to an entity.
Definition builder.hpp:25
flecs::entity_view
Entity view.
Definition entity_view.hpp:28
flecs::entity_view::type
flecs::type type() const
Get the entity's type.
Definition impl.hpp:94
flecs::entity_view::path
flecs::string path(const char *sep="::", const char *init_sep="::") const
Return the entity path.
Definition entity_view.hpp:100
flecs::entity_view::range
flecs::table_range range() const
Get table range for the entity.
Definition impl.hpp:102
flecs::entity_view::get
const T & get() const
Get component value.
Definition entity_view.hpp:396
flecs::entity_view::target_for
flecs::entity target_for(flecs::entity_t relationship, flecs::id_t id) const
Get the target of a pair for a given relationship id.
Definition impl.hpp:50
flecs::entity_view::lookup
flecs::entity lookup(const char *path, bool search_path=false) const
Lookup an entity by name.
Definition impl.hpp:172
flecs::entity_view::table
flecs::table table() const
Get the entity's table.
Definition impl.hpp:98
flecs::entity_view::parent
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:68
flecs::entity_view::each
void each(const Func &func) const
Iterate (component) ids of an entity.
Definition impl.hpp:112
flecs::entity_view::get_constant
Enum get_constant() const
Get enum constant for enum relationship.
Definition impl.hpp:30
flecs::entity_view::target
flecs::entity target(int32_t index=0) const
Get target for a given pair.
Definition impl.hpp:36
flecs::entity_view::mut
flecs::entity mut(const flecs::world &stage) const
Return mutable entity handle for current stage When an entity handle created from the world is used w...
Definition impl.hpp:72
flecs::entity
Entity.
Definition entity.hpp:30
flecs::id
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::id::second
flecs::entity second() const
Get second element from a pair.
Definition impl.hpp:31
flecs::iter
Class for iterating over query results.
Definition iter.hpp:68
flecs::ref
Component reference.
Definition ref.hpp:89
flecs::table_range
Definition table.hpp:412
flecs::table
Definition table.hpp:18
flecs::type
Type class.
Definition type.hpp:21
flecs::untyped_ref
Untyped component reference.
Definition ref.hpp:22
flecs::untyped_ref::entity
flecs::entity entity() const
Return entity associated with reference.
Definition impl.hpp:13
flecs::world
The world.
Definition world.hpp:158
flecs::world::id
flecs::id id() const
Get id from a type.
Definition impl.hpp:70
flecs::world::c_ptr
world_t * c_ptr() const
Obtain pointer to C world object.
Definition world.hpp:258
flecs::world::is_readonly
bool is_readonly() const
Test whether the current world object is readonly.
Definition world.hpp:526