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
10template <typename T>
11 flecs::entity ref<T>::entity() const {
12 return flecs::entity(world_, ref_.entity);
13}
14
15template <typename Self>
16template <typename Func>
17 inline const Self& entity_builder<Self>::insert(const Func& func) const {
18 _::entity_with_delegate<Func>::invoke_ensure(
19 this->world_, this->id_, func);
20 return to_base();
21}
22
23template <typename T, if_t< is_enum<T>::value > >
24const T* entity_view::get() const {
25 entity_t r = _::type<T>::id(world_);
26 entity_t c = ecs_get_target(world_, id_, r, 0);
27
28 if (c) {
29#ifdef FLECS_META
30 using U = typename std::underlying_type<T>::type;
31 const T* v = static_cast<const T*>(
32 ecs_get_id(world_, c, ecs_pair(flecs::Constant, _::type<U>::id(world_))));
33 ecs_assert(v != NULL, ECS_INTERNAL_ERROR, "missing enum constant value");
34 return v;
35#else
36 // Fallback if we don't have the reflection addon
37 return static_cast<const T*>(ecs_get_id(world_, id_, r));
38#endif
39 } else {
40 // If there is no matching pair for (r, *), try just r
41 return static_cast<const T*>(ecs_get_id(world_, id_, r));
42 }
43}
44
45template<typename First>
46 inline flecs::entity entity_view::target(int32_t index) const
47{
48 return flecs::entity(world_,
49 ecs_get_target(world_, id_, _::type<First>::id(world_), index));
50}
51
52 inline flecs::entity entity_view::target(
53 flecs::entity_t relationship,
54 int32_t index) const
55{
56 return flecs::entity(world_,
57 ecs_get_target(world_, id_, relationship, index));
58}
59
60 inline flecs::entity entity_view::target_for(
61 flecs::entity_t relationship,
62 flecs::id_t id) const
63{
64 return flecs::entity(world_,
65 ecs_get_target_for_id(world_, id_, relationship, id));
66}
67
68template <typename T>
69inline flecs::entity entity_view::target_for(flecs::entity_t relationship) const {
70 return target_for(relationship, _::type<T>::id(world_));
71}
72
73template <typename First, typename Second>
74inline flecs::entity entity_view::target_for(flecs::entity_t relationship) const {
75 return target_for(relationship, _::type<First, Second>::id(world_));
76}
77
78 inline flecs::entity entity_view::parent() const {
79 return target(flecs::ChildOf);
80}
81
82 inline flecs::entity entity_view::mut(const flecs::world& stage) const {
83 ecs_assert(!stage.is_readonly(), ECS_INVALID_PARAMETER,
84 "cannot use readonly world/stage to create mutable handle");
85 return flecs::entity(id_).set_stage(stage.c_ptr());
86}
87
88 inline flecs::entity entity_view::mut(const flecs::iter& it) const {
89 ecs_assert(!it.world().is_readonly(), ECS_INVALID_PARAMETER,
90 "cannot use iterator created for readonly world/stage to create mutable handle");
91 return flecs::entity(id_).set_stage(it.world().c_ptr());
92}
93
94 inline flecs::entity entity_view::mut(const flecs::entity_view& e) const {
95 ecs_assert(!e.world().is_readonly(), ECS_INVALID_PARAMETER,
96 "cannot use entity created for readonly world/stage to create mutable handle");
97 return flecs::entity(id_).set_stage(e.world_);
98}
99
100inline flecs::entity entity_view::set_stage(world_t *stage) {
101 return flecs::entity(stage, id_);
102}
103
104 inline flecs::type entity_view::type() const {
105 return flecs::type(world_, ecs_get_type(world_, id_));
106}
107
108 inline flecs::table entity_view::table() const {
109 return flecs::table(world_, ecs_get_table(world_, id_));
110}
111
112 inline flecs::table_range entity_view::range() const {
113 ecs_record_t *r = ecs_record_find(world_, id_);
114 if (r) {
115 return flecs::table_range(world_, r->table,
116 ECS_RECORD_TO_ROW(r->row), 1);
117 }
118 return flecs::table_range();
119}
120
121template <typename Func>
122 inline void entity_view::each(const Func& func) const {
123 const ecs_type_t *type = ecs_get_type(world_, id_);
124 if (!type) {
125 return;
126 }
127
128 const ecs_id_t *ids = type->array;
129 int32_t count = type->count;
130
131 for (int i = 0; i < count; i ++) {
132 ecs_id_t id = ids[i];
133 flecs::id ent(world_, id);
134 func(ent);
135 }
136}
137
138template <typename Func>
139 inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& func) const {
140 flecs::world_t *real_world = const_cast<flecs::world_t*>(
141 ecs_get_world(world_));
142
143 const ecs_table_t *table = ecs_get_table(world_, id_);
144 if (!table) {
145 return;
146 }
147
148 const ecs_type_t *type = ecs_table_get_type(table);
149 if (!type) {
150 return;
151 }
152
153 flecs::id_t pattern = pred;
154 if (obj) {
155 pattern = ecs_pair(pred, obj);
156 }
157
158 int32_t cur = 0;
159 id_t *ids = type->array;
160
161 while (-1 != (cur = ecs_search_offset(real_world, table, cur, pattern, 0)))
162 {
163 flecs::id ent(world_, ids[cur]);
164 func(ent);
165 cur ++;
166 }
167}
168
169template <typename Func>
170 inline void entity_view::each(const flecs::entity_view& rel, const Func& func) const {
171 return this->each(rel, flecs::Wildcard, [&](flecs::id id) {
172 flecs::entity obj = id.second();
173 func(obj);
174 });
175}
176
177template <typename Func, if_t< is_callable<Func>::value > >
178 inline bool entity_view::get(const Func& func) const {
179 return _::entity_with_delegate<Func>::invoke_get(world_, id_, func);
180}
181
182 inline flecs::entity entity_view::lookup(const char *path, bool search_path) const {
183 ecs_assert(id_ != 0, ECS_INVALID_PARAMETER, "invalid lookup from null handle");
184 auto id = ecs_lookup_path_w_sep(world_, id_, path, "::", "::", search_path);
185 return flecs::entity(world_, id);
186}
187
188inline flecs::entity entity_view::clone(bool copy_value, flecs::entity_t dst_id) const {
189 if (!dst_id) {
190 dst_id = ecs_new(world_);
191 }
192
193 flecs::entity dst = flecs::entity(world_, dst_id);
194 ecs_clone(world_, dst_id, id_, copy_value);
195 return dst;
196}
197
198// Entity mixin implementation
199template <typename... Args>
200 inline flecs::entity world::entity(Args &&... args) const {
201 return flecs::entity(world_, FLECS_FWD(args)...);
202}
203
204template <typename E, if_t< is_enum<E>::value >>
205 inline flecs::id world::id(E value) const {
206 flecs::entity_t constant = enum_type<E>(world_).entity(value);
207 return flecs::id(world_, constant);
208}
209
210template <typename E, if_t< is_enum<E>::value >>
211 inline flecs::entity world::entity(E value) const {
212 flecs::entity_t constant = enum_type<E>(world_).entity(value);
213 return flecs::entity(world_, constant);
214}
215
216template <typename T>
217 inline flecs::entity world::entity(const char *name) const {
218 return flecs::entity(world_, _::type<T>::register_id(world_, name, true) );
219}
220
221template <typename... Args>
222 inline flecs::entity world::prefab(Args &&... args) const {
223 flecs::entity result = flecs::entity(world_, FLECS_FWD(args)...);
224 result.add(flecs::Prefab);
225 return result;
226}
227
228template <typename T>
229 inline flecs::entity world::prefab(const char *name) const {
230 flecs::entity result = flecs::component<T>(world_, name, true);
231 result.add(flecs::Prefab);
232 return result;
233}
234
235}
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:339
ecs_table_t
struct ecs_table_t ecs_table_t
A table stores entities and components for a specific type.
Definition flecs.h:396
flecs::component::constant
untyped_component & constant(const char *name, int32_t value)
Add constant.
Definition untyped_component.inl:191
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:200
flecs::world::prefab
flecs::entity prefab(Args &&... args) const
Create a prefab.
Definition impl.hpp:222
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_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_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 id)
Get the target of a relationship for a given id.
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_get_id
const void * ecs_get_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get an immutable pointer to a component.
ecs_record_find
ecs_record_t * ecs_record_find(const ecs_world_t *world, ecs_entity_t entity)
Find record for 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_table_get_type
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get type for table.
ecs_search_offset
int32_t ecs_search_offset(const ecs_world_t *world, const ecs_table_t *table, int32_t offset, ecs_id_t id, ecs_id_t *id_out)
Search for component id in table type starting from an offset.
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
ecs_record_t
Record for entity index.
Definition flecs.h:493
ecs_record_t::table
ecs_table_t * table
Identifies a type (and table) in world.
Definition flecs.h:495
ecs_record_t::row
uint32_t row
Table row of the entity.
Definition flecs.h:496
ecs_type_t
A type is a list of (component) ids.
Definition flecs.h:363
flecs::_::entity_with_delegate
Definition delegate.hpp:876
flecs::_::type
Definition flecs.hpp:38
flecs::component
Component class.
Definition flecs.hpp:30
flecs::entity_builder::insert
const Self & insert(const Func &func) const
Set 1..N components.
Definition impl.hpp:17
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:104
flecs::entity_view::get
const T * get() const
Get component value.
Definition entity_view.hpp:259
flecs::entity_view::path
flecs::string path(const char *sep="::", const char *init_sep="::") const
Return the entity path.
Definition entity_view.hpp:94
flecs::entity_view::range
flecs::table_range range() const
Get table range for the entity.
Definition impl.hpp:112
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:60
flecs::entity_view::lookup
flecs::entity lookup(const char *path, bool search_path=false) const
Lookup an entity by name.
Definition impl.hpp:182
flecs::entity_view::table
flecs::table table() const
Get the entity's table.
Definition impl.hpp:108
flecs::entity_view::parent
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:78
flecs::entity_view::each
void each(const Func &func) const
Iterate (component) ids of an entity.
Definition impl.hpp:122
flecs::entity_view::target
flecs::entity target(int32_t index=0) const
Get target for a given pair.
Definition impl.hpp:46
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:82
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::table_range
Definition table.hpp:317
flecs::table
Definition table.hpp:18
flecs::type
Type class.
Definition type.hpp:21
flecs::world
The world.
Definition world.hpp:137
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:237
flecs::world::is_readonly
bool is_readonly() const
Test whether the current world object is readonly.
Definition world.hpp:505
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.