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