Flecs v3.1
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>
11flecs::entity ref<T>::entity() const {
12 return flecs::entity(m_world, m_ref.entity);
13}
14
15template <typename Self>
16template <typename Func, if_t< is_callable<Func>::value > >
17inline Self& entity_builder<Self>::set(const Func& func) {
19 this->m_world, this->m_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 = _::cpp_type<T>::id(m_world);
26 entity_t c = ecs_get_target(m_world, m_id, r, 0);
27
28 if (c) {
29 // Get constant value from constant entity
30 const T* v = static_cast<const T*>(ecs_get_id(m_world, c, r));
31 ecs_assert(v != NULL, ECS_INTERNAL_ERROR,
32 "missing enum constant value");
33 return v;
34 } else {
35 // If there is no matching pair for (r, *), try just r
36 return static_cast<const T*>(ecs_get_id(m_world, m_id, r));
37 }
38}
39
40template<typename First>
41inline flecs::entity entity_view::target(int32_t index) const
42{
43 return flecs::entity(m_world,
44 ecs_get_target(m_world, m_id, _::cpp_type<First>::id(m_world), index));
45}
46
48 flecs::entity_t relationship,
49 int32_t index) const
50{
51 return flecs::entity(m_world,
52 ecs_get_target(m_world, m_id, relationship, index));
53}
54
56 flecs::entity_t relationship,
57 flecs::id_t id) const
58{
59 return flecs::entity(m_world,
60 ecs_get_target_for_id(m_world, m_id, relationship, id));
61}
62
63template <typename T>
64inline flecs::entity entity_view::target_for(flecs::entity_t relationship) const {
65 return target_for(relationship, _::cpp_type<T>::id(m_world));
66}
67
68template <typename First, typename Second>
69inline flecs::entity entity_view::target_for(flecs::entity_t relationship) const {
70 return target_for(relationship, _::cpp_type<First, Second>::id(m_world));
71}
72
74 return target(flecs::ChildOf);
75}
76
77inline flecs::entity entity_view::mut(const flecs::world& stage) const {
78 ecs_assert(!stage.is_readonly(), ECS_INVALID_PARAMETER,
79 "cannot use readonly world/stage to create mutable handle");
80 return flecs::entity(m_id).set_stage(stage.c_ptr());
81}
82
84 ecs_assert(!it.world().is_readonly(), ECS_INVALID_PARAMETER,
85 "cannot use iterator created for readonly world/stage to create mutable handle");
86 return flecs::entity(m_id).set_stage(it.world().c_ptr());
87}
88
90 ecs_assert(!e.world().is_readonly(), ECS_INVALID_PARAMETER,
91 "cannot use entity created for readonly world/stage to create mutable handle");
92 return flecs::entity(m_id).set_stage(e.m_world);
93}
94
95inline flecs::entity entity_view::set_stage(world_t *stage) {
96 return flecs::entity(stage, m_id);
97}
98
100 return flecs::type(m_world, ecs_get_type(m_world, m_id));
101}
102
104 return flecs::table(m_world, ecs_get_table(m_world, m_id));
105}
106
108 ecs_record_t *r = ecs_record_find(m_world, m_id);
109 if (r) {
110 return flecs::table_range(m_world, r->table,
111 ECS_RECORD_TO_ROW(r->row), 1);
112 }
113 return flecs::table_range();
114}
115
116template <typename Func>
117inline void entity_view::each(const Func& func) const {
118 const ecs_type_t *type = ecs_get_type(m_world, m_id);
119 if (!type) {
120 return;
121 }
122
123 const ecs_id_t *ids = type->array;
124 int32_t count = type->count;
125
126 for (int i = 0; i < count; i ++) {
127 ecs_id_t id = ids[i];
128 flecs::id ent(m_world, id);
129 func(ent);
130
131 // Union object is not stored in type, so handle separately
132 if (ECS_PAIR_FIRST(id) == EcsUnion) {
133 ent = flecs::id(m_world, ECS_PAIR_SECOND(id),
134 ecs_get_target(m_world, m_id, ECS_PAIR_SECOND(id), 0));
135 func(ent);
136 }
137 }
138}
139
140template <typename Func>
141inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& func) const {
142 flecs::world_t *real_world = const_cast<flecs::world_t*>(
143 ecs_get_world(m_world));
144
145 const ecs_table_t *table = ecs_get_table(m_world, m_id);
146 if (!table) {
147 return;
148 }
149
151 if (!type) {
152 return;
153 }
154
155 flecs::id_t pattern = pred;
156 if (obj) {
157 pattern = ecs_pair(pred, obj);
158 }
159
160 int32_t cur = 0;
161 id_t *ids = type->array;
162
163 while (-1 != (cur = ecs_search_offset(real_world, table, cur, pattern, 0)))
164 {
165 flecs::id ent(m_world, ids[cur]);
166 func(ent);
167 cur ++;
168 }
169}
170
171template <typename Func>
172inline void entity_view::each(const flecs::entity_view& rel, const Func& func) const {
173 return this->each(rel, flecs::Wildcard, [&](flecs::id id) {
174 flecs::entity obj = id.second();
175 func(obj);
176 });
177}
178
179template <typename Func, if_t< is_callable<Func>::value > >
180inline bool entity_view::get(const Func& func) const {
181 return _::entity_with_invoker<Func>::invoke_get(m_world, m_id, func);
182}
183
184inline flecs::entity entity_view::lookup(const char *path) const {
185 ecs_assert(m_id != 0, ECS_INVALID_PARAMETER, "invalid lookup from null handle");
186 auto id = ecs_lookup_path_w_sep(m_world, m_id, path, "::", "::", false);
187 return flecs::entity(m_world, id);
188}
189
190inline flecs::entity entity_view::clone(bool copy_value, flecs::entity_t dst_id) const {
191 if (!dst_id) {
192 dst_id = ecs_new_id(m_world);
193 }
194
195 flecs::entity dst = flecs::entity(m_world, dst_id);
196 ecs_clone(m_world, dst_id, m_id, copy_value);
197 return dst;
198}
199
200// Entity mixin implementation
201template <typename... Args>
202inline flecs::entity world::entity(Args &&... args) const {
203 return flecs::entity(m_world, FLECS_FWD(args)...);
204}
205
206template <typename E, if_t< is_enum<E>::value >>
207inline flecs::entity world::id(E value) const {
208 flecs::entity_t constant = enum_type<E>(m_world).entity(value);
209 return flecs::entity(m_world, constant);
210}
211
212template <typename E, if_t< is_enum<E>::value >>
213inline flecs::entity world::entity(E value) const {
214 flecs::entity_t constant = enum_type<E>(m_world).entity(value);
215 return flecs::entity(m_world, constant);
216}
217
218template <typename T>
219inline flecs::entity world::entity(const char *name) const {
220 return flecs::entity(m_world,
221 _::cpp_type<T>::id_explicit(m_world, name, true, 0, false) );
222}
223
224template <typename... Args>
225inline flecs::entity world::prefab(Args &&... args) const {
226 flecs::entity result = flecs::entity(m_world, FLECS_FWD(args)...);
227 result.add(flecs::Prefab);
228 return result;
229}
230
231template <typename T>
232inline flecs::entity world::prefab(const char *name) const {
233 flecs::entity result = flecs::component<T>(m_world, name, true);
234 result.add(flecs::Prefab);
235 return result;
236}
237
238}
const ecs_entity_t EcsUnion
Tag to indicate that relationship is stored as union.
#define ecs_assert(condition, error_code,...)
Assert.
Definition: log.h:352
uint64_t ecs_id_t
An id.
Definition: flecs.h:217
struct ecs_table_t ecs_table_t
A table is where entities and components are stored.
Definition: flecs.h:232
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_new_id(ecs_world_t *world)
Create new entity id.
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_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.
ecs_record_t * ecs_record_find(const ecs_world_t *world, ecs_entity_t entity)
Find record for entity.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
An array with (component) ids.
Definition: flecs.h:223
Component class.
Definition: component.hpp:363
Entity builder.
Definition: builder.hpp:15
Self & add()
Add a component to an entity.
Definition: builder.hpp:25
Entity view.
Definition: entity_view.hpp:28
flecs::type type() const
Get the entity's type.
Definition: impl.hpp:99
const T * get() const
Get component value.
flecs::entity lookup(const char *path) const
Lookup an entity by name.
Definition: impl.hpp:184
flecs::string path(const char *sep="::", const char *init_sep="::") const
Return the entity path.
Definition: entity_view.hpp:94
flecs::table_range range() const
Get table range for the entity.
Definition: impl.hpp:107
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:55
flecs::table table() const
Get the entity's table.
Definition: impl.hpp:103
flecs::entity parent() const
Get parent of entity.
Definition: impl.hpp:73
void each(const Func &func) const
Iterate (component) ids of an entity.
Definition: impl.hpp:117
flecs::entity target(int32_t index=0) const
Get target for a given pair.
Definition: impl.hpp:41
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:77
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:169
Type class.
Definition: type.hpp:21
The world.
Definition: world.hpp:113
world_t * c_ptr() const
Obtain pointer to C world object.
Definition: world.hpp:173
bool is_readonly() const
Test whether the current world object is readonly.
Definition: world.hpp:447