Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
world.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace flecs
9{
10
11inline void world::init_builtin_components() {
12 this->component<Component>();
13 this->component<Identifier>();
14 this->component<Poly>();
15 this->component<Parent>();
16
17 /* If meta is not defined and we're using enum reflection, make sure that
18 * primitive types are registered. This makes sure we can set components of
19 * underlying_type_t<E> when registering constants. */
20# if !defined(FLECS_META) && !defined(FLECS_CPP_NO_ENUM_REFLECTION)
21 this->component<uint8_t>("flecs::meta::u8");
22 this->component<uint16_t>("flecs::meta::u16");
23 this->component<uint32_t>("flecs::meta::u32");
24 this->component<uint64_t>("flecs::meta::u64");
25 this->component<int8_t>("flecs::meta::i8");
26 this->component<int16_t>("flecs::meta::i16");
27 this->component<int32_t>("flecs::meta::i32");
28 this->component<int64_t>("flecs::meta::i64");
29# endif
30
31# ifdef FLECS_SYSTEM
32 _::system_init(*this);
33# endif
34# ifdef FLECS_TIMER
35 _::timer_init(*this);
36# endif
37# ifdef FLECS_DOC
38 doc::_::init(*this);
39# endif
40# ifdef FLECS_REST
41 rest::_::init(*this);
42# endif
43# ifdef FLECS_META
44 meta::_::init(*this);
45# endif
46# ifdef FLECS_SCRIPT
47 script::_::init(*this);
48# endif
49}
50
51template <typename T>
52inline flecs::entity world::use(const char *alias) const {
53 entity_t e = _::type<T>::id(world_);
54 const char *name = alias;
55 if (!name) {
56 // If no name is defined, use the entity name without the scope
57 name = ecs_get_name(world_, e);
58 }
59 ecs_set_alias(world_, e, name);
60 return flecs::entity(world_, e);
61}
62
63inline flecs::entity world::use(const char *name, const char *alias) const {
64 entity_t e = ecs_lookup_path_w_sep(world_, 0, name, "::", "::", true);
65 ecs_assert(e != 0, ECS_INVALID_PARAMETER, NULL);
66
67 ecs_set_alias(world_, e, alias);
68 return flecs::entity(world_, e);
69}
70
71inline void world::use(flecs::entity e, const char *alias) const {
72 entity_t eid = e.id();
73 const char *name = alias;
74 if (!name) {
75 // If no name is defined, use the entity name without the scope
76 name = ecs_get_name(world_, eid);
77 }
78 ecs_set_alias(world_, eid, name);
79}
80
81inline flecs::entity world::set_scope(const flecs::entity_t s) const {
82 return flecs::entity(ecs_set_scope(world_, s));
83}
84
86 return flecs::entity(world_, ecs_get_scope(world_));
87}
88
89template <typename T>
91 return set_scope( _::type<T>::id(world_) );
92}
93
94inline entity world::lookup(const char *name, const char *sep, const char *root_sep, bool recursive) const {
95 auto e = ecs_lookup_path_w_sep(world_, 0, name, sep, root_sep, recursive);
96 return flecs::entity(*this, e);
97}
98
99#ifndef ensure
100template <typename T>
101inline T& world::ensure() const {
102 flecs::entity e(world_, _::type<T>::id(world_));
103 return e.ensure<T>();
104}
105#endif
106
107template <typename T>
108inline void world::modified() const {
109 flecs::entity e(world_, _::type<T>::id(world_));
110 e.modified<T>();
111}
112
113template <typename First, typename Second>
114inline void world::set(Second second, const First& value) const {
115 flecs::entity e(world_, _::type<First>::id(world_));
116 e.set<First>(second, value);
117}
118
119template <typename First, typename Second>
120inline void world::set(Second second, First&& value) const {
121 flecs::entity e(world_, _::type<First>::id(world_));
122 e.set<First>(second, value);
123}
124
125template <typename T>
126inline ref<T> world::get_ref() const {
127 flecs::entity e(world_, _::type<T>::id(world_));
128 return e.get_ref<T>();
129}
130
131inline const void* world::try_get(flecs::id_t id) const {
132 flecs::entity e(world_, id);
133 return e.try_get(id);
134}
135
136inline const void* world::try_get(flecs::entity_t r, flecs::entity_t t) const {
137 flecs::entity e(world_, r);
138 return e.try_get(r, t);
139}
140
141template <typename T>
142inline const T* world::try_get() const {
143 flecs::entity e(world_, _::type<T>::id(world_));
144 return e.try_get<T>();
145}
146
147template <typename First, typename Second, typename P, typename A>
148inline const A* world::try_get() const {
149 flecs::entity e(world_, _::type<First>::id(world_));
150 return e.try_get<First, Second>();
151}
152
153template <typename First, typename Second>
154inline const First* world::try_get(Second second) const {
155 flecs::entity e(world_, _::type<First>::id(world_));
156 return e.try_get<First>(second);
157}
158
159inline const void* world::get(flecs::id_t id) const {
160 flecs::entity e(world_, id);
161 return e.get(id);
162}
163
164inline const void* world::get(flecs::entity_t r, flecs::entity_t t) const {
165 flecs::entity e(world_, r);
166 return e.get(r, t);
167}
168
169template <typename T>
170inline const T& world::get() const {
171 flecs::entity e(world_, _::type<T>::id(world_));
172 return e.get<T>();
173}
174
175template <typename First, typename Second, typename P, typename A>
176inline const A& world::get() const {
177 flecs::entity e(world_, _::type<First>::id(world_));
178 return e.get<First, Second>();
179}
180
181template <typename First, typename Second>
182const First& world::get(Second second) const {
183 flecs::entity e(world_, _::type<First>::id(world_));
184 return e.get<First>(second);
185}
186
187inline void* world::try_get_mut(flecs::id_t id) const {
188 flecs::entity e(world_, id);
189 return e.try_get_mut(id);
190}
191
192inline void* world::try_get_mut(flecs::entity_t r, flecs::entity_t t) const {
193 flecs::entity e(world_, r);
194 return e.try_get_mut(r, t);
195}
196
197template <typename T>
198inline T* world::try_get_mut() const {
199 flecs::entity e(world_, _::type<T>::id(world_));
200 return e.try_get_mut<T>();
201}
202
203template <typename First, typename Second, typename P, typename A>
204inline A* world::try_get_mut() const {
205 flecs::entity e(world_, _::type<First>::id(world_));
206 return e.try_get_mut<First, Second>();
207}
208
209template <typename First, typename Second>
210inline First* world::try_get_mut(Second second) const {
211 flecs::entity e(world_, _::type<First>::id(world_));
212 return e.try_get_mut<First>(second);
213}
214
215inline void* world::get_mut(flecs::id_t id) const {
216 flecs::entity e(world_, id);
217 return e.get_mut(id);
218}
219
220inline void* world::get_mut(flecs::entity_t r, flecs::entity_t t) const {
221 flecs::entity e(world_, r);
222 return e.get_mut(r, t);
223}
224
225template <typename T>
226inline T& world::get_mut() const {
227 flecs::entity e(world_, _::type<T>::id(world_));
228 return e.get_mut<T>();
229}
230
231template <typename First, typename Second, typename P, typename A>
232inline A& world::get_mut() const {
233 flecs::entity e(world_, _::type<First>::id(world_));
234 return e.get_mut<First, Second>();
235}
236
237template <typename First, typename Second>
238inline First& world::get_mut(Second second) const {
239 flecs::entity e(world_, _::type<First>::id(world_));
240 return e.get_mut<First>(second);
241}
242
243template <typename T>
244inline bool world::has() const {
245 flecs::entity e(world_, _::type<T>::id(world_));
246 return e.has<T>();
247}
248
249template <typename First, typename Second>
250inline bool world::has() const {
251 flecs::entity e(world_, _::type<First>::id(world_));
252 return e.has<First, Second>();
253}
254
255template <typename First>
256inline bool world::has(flecs::id_t second) const {
257 flecs::entity e(world_, _::type<First>::id(world_));
258 return e.has<First>(second);
259}
260
261inline bool world::has(flecs::id_t first, flecs::id_t second) const {
262 flecs::entity e(world_, first);
263 return e.has(first, second);
264}
265
266template <typename E, if_t< is_enum<E>::value > >
267inline bool world::has(E value) const {
268 flecs::entity e(world_, _::type<E>::id(world_));
269 return e.has(value);
270}
271
272template <typename T>
273inline void world::add() const {
274 flecs::entity e(world_, _::type<T>::id(world_));
275 e.add<T>();
276}
277
278template <typename First, typename Second>
279inline void world::add() const {
280 flecs::entity e(world_, _::type<First>::id(world_));
281 e.add<First, Second>();
282}
283
284template <typename First>
285inline void world::add(flecs::entity_t second) const {
286 flecs::entity e(world_, _::type<First>::id(world_));
287 e.add<First>(second);
288}
289
290inline void world::add(flecs::entity_t first, flecs::entity_t second) const {
291 flecs::entity e(world_, first);
292 e.add(first, second);
293}
294
295template <typename E, if_t< is_enum<E>::value > >
296inline void world::add(E value) const {
297 flecs::entity e(world_, _::type<E>::id(world_));
298 e.add(value);
299}
300
301template <typename T>
302inline void world::remove() const {
303 flecs::entity e(world_, _::type<T>::id(world_));
304 e.remove<T>();
305}
306
307template <typename First, typename Second>
308inline void world::remove() const {
309 flecs::entity e(world_, _::type<First>::id(world_));
310 e.remove<First, Second>();
311}
312
313template <typename First>
314inline void world::remove(flecs::entity_t second) const {
315 flecs::entity e(world_, _::type<First>::id(world_));
316 e.remove<First>(second);
317}
318
319inline void world::remove(flecs::entity_t first, flecs::entity_t second) const {
320 flecs::entity e(world_, first);
321 e.remove(first, second);
322}
323
324template <typename Func>
325inline void world::children(Func&& f) const {
326 this->entity(0).children(FLECS_FWD(f));
327}
328
329template <typename T>
331 return flecs::entity(world_, _::type<T>::id(world_));
332}
333
334template <typename First>
335inline flecs::entity world::target(int32_t index) const
336{
337 return flecs::entity(world_,
338 ecs_get_target(world_, _::type<First>::id(world_), _::type<First>::id(world_), index));
339}
340
341template <typename T>
343 flecs::entity_t relationship,
344 int32_t index) const
345{
346 return flecs::entity(world_,
347 ecs_get_target(world_, _::type<T>::id(world_), relationship, index));
348}
349
351 flecs::entity_t relationship,
352 int32_t index) const
353{
354 return flecs::entity(world_,
355 ecs_get_target(world_, relationship, relationship, index));
356}
357
358template <typename Func, if_t< is_callable<Func>::value > >
359inline void world::get(const Func& func) const {
360 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
362 this->world_, this->singleton<first_arg_t<Func>>(), func);
363}
364
365template <typename Func, if_t< is_callable<Func>::value > >
366inline void world::set(const Func& func) const {
367 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
369 this->world_, this->singleton<first_arg_t<Func>>(), func);
370}
371
372inline flecs::entity world::get_alive(flecs::entity_t e) const {
373 e = ecs_get_alive(world_, e);
374 return flecs::entity(world_, e);
375}
376
377inline flecs::entity world::make_alive(flecs::entity_t e) const {
378 ecs_make_alive(world_, e);
379 return flecs::entity(world_, e);
380}
381
382template <typename E>
383inline flecs::entity enum_data<E>::entity() const {
384 return flecs::entity(world_, _::type<E>::id(world_));
385}
386
387template <typename E>
388inline flecs::entity enum_data<E>::entity(underlying_type_t<E> value) const {
389 int index = index_by_value(value);
390 if (index >= 0) {
391 int32_t constant_i = impl_.constants[index].index;
392 flecs::entity_t entity = flecs_component_ids_get(world_, constant_i);
393 return flecs::entity(world_, entity);
394 }
395#ifdef FLECS_META
396 // Reflection data lookup failed. Try value lookup amongst flecs::Constant relationships
397 flecs::world world = flecs::world(world_);
398 return world.query_builder()
399 .with(flecs::ChildOf, world.id<E>())
400 .with(flecs::Constant, world.id<int32_t>())
401 .build()
402 .find([value](flecs::entity constant) {
403 const int32_t& constant_value = constant.get_second<int32_t>(flecs::Constant);
404 return value == static_cast<underlying_type_t<E>>(constant_value);
405 });
406#else
407 return flecs::entity::null(world_);
408#endif
409}
410
411template <typename E>
412inline flecs::entity enum_data<E>::entity(E value) const {
413 return entity(static_cast<underlying_type_t<E>>(value));
414}
415
419inline flecs::scoped_world world::scope(id_t parent) const {
420 return scoped_world(world_, parent);
421}
422
423template <typename T>
424inline flecs::scoped_world world::scope() const {
425 flecs::id_t parent = _::type<T>::id(world_);
426 return scoped_world(world_, parent);
427}
428
429inline flecs::scoped_world world::scope(const char* name) const {
430 return scope(entity(name));
431}
432
433} // namespace flecs
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
flecs::query_builder< Comps... > query_builder(Args &&... args) const
Create a query builder.
flecs::entity entity(Args &&... args) const
Create an entity.
flecs::id id(E value) const
Convert enum constant to 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.
void ecs_make_alive(ecs_world_t *world, ecs_entity_t entity)
Ensure id is alive.
ecs_entity_t ecs_get_alive(const ecs_world_t *world, ecs_entity_t e)
Get alive identifier.
void ecs_set_alias(ecs_world_t *world, ecs_entity_t entity, const char *alias)
Set alias for entity.
ecs_entity_t ecs_get_scope(const ecs_world_t *world)
Get the current scope.
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 char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
Get the name of an entity.
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
const Self & remove() const
Remove a component from an entity.
Definition builder.hpp:303
const Self & set(T &&value) const
Set a component for an entity.
Definition builder.hpp:705
const Self & add() const
Add a component to an entity.
Definition builder.hpp:25
T & get_mut() const
Get mutable component value.
const T & get() const
Get component value.
bool has(flecs::id_t e) const
Check if entity has the provided entity.
T * try_get_mut() const
Get mutable component value.
entity_t id() const
Get entity id.
const T * try_get() const
Get component value.
void children(flecs::entity_t rel, Func &&func) const
Iterate children for entity.
Entity.
Definition entity.hpp:30
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:311
void modified() const
Signal that component was modified.
Definition entity.hpp:226
T & ensure() const
Get mutable component value.
Definition entity.hpp:136
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:426
Convenience type with enum reflection data.
Definition enum.hpp:444
Component reference.
Definition ref.hpp:89
Scoped world.
Definition world.hpp:1462
The world.
Definition world.hpp:174
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:85
void remove() const
Remove singleton component.
Definition world.hpp:302
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:94
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
Definition world.hpp:372
flecs::entity make_alive(flecs::entity_t e) const
Definition world.hpp:377
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:335
const T * try_get() const
Get singleton component.
Definition world.hpp:142
flecs::entity set_scope() const
Same as set_scope but with type.
Definition world.hpp:90
void children(Func &&f) const
Iterate entities in root of world Accepts a callback with the following signature:
Definition world.hpp:325
void modified() const
Mark singleton component as modified.
Definition world.hpp:108
flecs::entity use(const char *alias=nullptr) const
Create alias for component.
Definition world.hpp:52
void add() const
Add singleton component.
Definition world.hpp:273
T & ensure() const
Ensure singleton component.
Definition world.hpp:101
void set(const T &value) const
Set singleton component.
Definition world.hpp:705
bool has() const
Test if world has singleton component.
Definition world.hpp:244
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:330
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:126