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