Flecs: include/flecs/addons/cpp/impl/world.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| 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
5
6#pragma once
7
8namespace flecs
9{
10
12 inline void world::init_builtin_components() {
13 this->component<Component>();
14 this->component<Identifier>();
15 this->component<Poly>();
16 this->component<Parent>();
17
18 /* If meta is not defined and we're using enum reflection, make sure that
19 * primitive types are registered. This ensures we can set components of
20 * underlying_type_t<E> when registering constants. */
21# if !defined(FLECS_META) && !defined(FLECS_CPP_NO_ENUM_REFLECTION)
22 this->component<uint8_t>("flecs::meta::u8");
23 this->component<uint16_t>("flecs::meta::u16");
24 this->component<uint32_t>("flecs::meta::u32");
25 this->component<uint64_t>("flecs::meta::u64");
26 this->component<int8_t>("flecs::meta::i8");
27 this->component<int16_t>("flecs::meta::i16");
28 this->component<int32_t>("flecs::meta::i32");
29 this->component<int64_t>("flecs::meta::i64");
30# endif
31
32# ifdef FLECS_SYSTEM
33 _::system_init(*this);
34# endif
35# ifdef FLECS_TIMER
36 _::timer_init(*this);
37# endif
38# ifdef FLECS_DOC
39 doc::_::init(*this);
40# endif
41# ifdef FLECS_REST
42 rest::_::init(*this);
43# endif
44# ifdef FLECS_META
45 meta::_::init(*this);
46# endif
47# ifdef FLECS_SCRIPT
48 script::_::init(*this);
49# endif
50}
51
53template <typename T>
54 inline flecs::entity world::use(const char *alias) const {
55 entity_t e = _::type<T>::id(world_);
56 const char *name = alias;
57 if (!name) {
58 // If no name is defined, use the entity name without the scope.
59 name = ecs_get_name(world_, e);
60 }
61 ecs_set_alias(world_, e, name);
62 return flecs::entity(world_, e);
63}
64
66 inline flecs::entity world::use(const char *name, const char *alias) const {
67 entity_t e = ecs_lookup_path_w_sep(world_, 0, name, "::", "::", true);
68 ecs_assert(e != 0, ECS_INVALID_PARAMETER, nullptr);
69
70 ecs_set_alias(world_, e, alias);
71 return flecs::entity(world_, e);
72}
73
75 inline void world::use(flecs::entity e, const char *alias) const {
76 entity_t eid = e.id();
77 const char *name = alias;
78 if (!name) {
79 // If no name is defined, use the entity name without the scope.
80 name = ecs_get_name(world_, eid);
81 }
82 ecs_set_alias(world_, eid, name);
83}
84
86 inline flecs::entity world::set_scope(const flecs::entity_t s) const {
87 return flecs::entity(ecs_set_scope(world_, s));
88}
89
91 inline flecs::entity world::get_scope() const {
92 return flecs::entity(world_, ecs_get_scope(world_));
93}
94
96template <typename T>
97 inline flecs::entity world::set_scope() const {
98 return set_scope( _::type<T>::id(world_) );
99}
100
102 inline entity world::lookup(const char *name, const char *sep, const char *root_sep, bool recursive) const {
103 auto e = ecs_lookup_path_w_sep(world_, 0, name, sep, root_sep, recursive);
104 return flecs::entity(*this, e);
105}
106
107#ifndef ensure
109template <typename T>
110 inline T& world::ensure() const {
111 flecs::entity e(world_, _::type<T>::id(world_));
112 return e.ensure<T>();
113}
114#endif
115
117template <typename T>
118 inline void world::modified() const {
119 flecs::entity e(world_, _::type<T>::id(world_));
120 e.modified<T>();
121}
122
124template <typename First, typename Second>
125 inline void world::set(Second second, const First& value) const {
126 flecs::entity e(world_, _::type<First>::id(world_));
127 e.set<First>(second, value);
128}
129
131template <typename First, typename Second>
132 inline void world::set(Second second, First&& value) const {
133 flecs::entity e(world_, _::type<First>::id(world_));
134 e.set<First>(second, value);
135}
136
138template <typename T>
139 inline ref<T> world::get_ref() const {
140 flecs::entity e(world_, _::type<T>::id(world_));
141 return e.get_ref<T>();
142}
143
145 inline const void* world::try_get(flecs::id_t id) const {
146 flecs::entity e(world_, id);
147 return e.try_get(id);
148}
149
151 inline const void* world::try_get(flecs::entity_t r, flecs::entity_t t) const {
152 flecs::entity e(world_, r);
153 return e.try_get(r, t);
154}
155
157template <typename T>
158 inline const T* world::try_get() const {
159 flecs::entity e(world_, _::type<T>::id(world_));
160 return e.try_get<T>();
161}
162
164template <typename First, typename Second, typename P, typename A>
165 inline const A* world::try_get() const {
166 flecs::entity e(world_, _::type<First>::id(world_));
167 return e.try_get<First, Second>();
168}
169
171template <typename First, typename Second>
172 inline const First* world::try_get(Second second) const {
173 flecs::entity e(world_, _::type<First>::id(world_));
174 return e.try_get<First>(second);
175}
176
178 inline const void* world::get(flecs::id_t id) const {
179 flecs::entity e(world_, id);
180 return e.get(id);
181}
182
184 inline const void* world::get(flecs::entity_t r, flecs::entity_t t) const {
185 flecs::entity e(world_, r);
186 return e.get(r, t);
187}
188
190template <typename T>
191 inline const T& world::get() const {
192 flecs::entity e(world_, _::type<T>::id(world_));
193 return e.get<T>();
194}
195
197template <typename First, typename Second, typename P, typename A>
198 inline const A& world::get() const {
199 flecs::entity e(world_, _::type<First>::id(world_));
200 return e.get<First, Second>();
201}
202
204template <typename First, typename Second>
205 const First& world::get(Second second) const {
206 flecs::entity e(world_, _::type<First>::id(world_));
207 return e.get<First>(second);
208}
209
211 inline void* world::try_get_mut(flecs::id_t id) const {
212 flecs::entity e(world_, id);
213 return e.try_get_mut(id);
214}
215
217 inline void* world::try_get_mut(flecs::entity_t r, flecs::entity_t t) const {
218 flecs::entity e(world_, r);
219 return e.try_get_mut(r, t);
220}
221
223template <typename T>
224 inline T* world::try_get_mut() const {
225 flecs::entity e(world_, _::type<T>::id(world_));
226 return e.try_get_mut<T>();
227}
228
230template <typename First, typename Second, typename P, typename A>
231 inline A* world::try_get_mut() const {
232 flecs::entity e(world_, _::type<First>::id(world_));
233 return e.try_get_mut<First, Second>();
234}
235
237template <typename First, typename Second>
238 inline First* world::try_get_mut(Second second) const {
239 flecs::entity e(world_, _::type<First>::id(world_));
240 return e.try_get_mut<First>(second);
241}
242
244 inline void* world::get_mut(flecs::id_t id) const {
245 flecs::entity e(world_, id);
246 return e.get_mut(id);
247}
248
250 inline void* world::get_mut(flecs::entity_t r, flecs::entity_t t) const {
251 flecs::entity e(world_, r);
252 return e.get_mut(r, t);
253}
254
256template <typename T>
257 inline T& world::get_mut() const {
258 flecs::entity e(world_, _::type<T>::id(world_));
259 return e.get_mut<T>();
260}
261
263template <typename First, typename Second, typename P, typename A>
264 inline A& world::get_mut() const {
265 flecs::entity e(world_, _::type<First>::id(world_));
266 return e.get_mut<First, Second>();
267}
268
270template <typename First, typename Second>
271 inline First& world::get_mut(Second second) const {
272 flecs::entity e(world_, _::type<First>::id(world_));
273 return e.get_mut<First>(second);
274}
275
277template <typename T>
278 inline bool world::has() const {
279 flecs::entity e(world_, _::type<T>::id(world_));
280 return e.has<T>();
281}
282
284template <typename First, typename Second>
285 inline bool world::has() const {
286 flecs::entity e(world_, _::type<First>::id(world_));
287 return e.has<First, Second>();
288}
289
291template <typename First>
292 inline bool world::has(flecs::id_t second) const {
293 flecs::entity e(world_, _::type<First>::id(world_));
294 return e.has<First>(second);
295}
296
298 inline bool world::has(flecs::id_t first, flecs::id_t second) const {
299 flecs::entity e(world_, first);
300 return e.has(first, second);
301}
302
304template <typename E, if_t< is_enum<E>::value > >
305 inline bool world::has(E value) const {
306 flecs::entity e(world_, _::type<E>::id(world_));
307 return e.has(value);
308}
309
311template <typename T>
312 inline void world::add() const {
313 flecs::entity e(world_, _::type<T>::id(world_));
314 e.add<T>();
315}
316
318template <typename First, typename Second>
319 inline void world::add() const {
320 flecs::entity e(world_, _::type<First>::id(world_));
321 e.add<First, Second>();
322}
323
325template <typename First>
326 inline void world::add(flecs::entity_t second) const {
327 flecs::entity e(world_, _::type<First>::id(world_));
328 e.add<First>(second);
329}
330
332 inline void world::add(flecs::entity_t first, flecs::entity_t second) const {
333 flecs::entity e(world_, first);
334 e.add(first, second);
335}
336
338template <typename E, if_t< is_enum<E>::value > >
339 inline void world::add(E value) const {
340 flecs::entity e(world_, _::type<E>::id(world_));
341 e.add(value);
342}
343
345template <typename T>
346 inline void world::remove() const {
347 flecs::entity e(world_, _::type<T>::id(world_));
348 e.remove<T>();
349}
350
352template <typename First, typename Second>
353 inline void world::remove() const {
354 flecs::entity e(world_, _::type<First>::id(world_));
355 e.remove<First, Second>();
356}
357
359template <typename First>
360 inline void world::remove(flecs::entity_t second) const {
361 flecs::entity e(world_, _::type<First>::id(world_));
362 e.remove<First>(second);
363}
364
366 inline void world::remove(flecs::entity_t first, flecs::entity_t second) const {
367 flecs::entity e(world_, first);
368 e.remove(first, second);
369}
370
372template <typename Func>
373 inline void world::children(Func&& f) const {
374 this->entity(0).children(FLECS_FWD(f));
375}
376
378template <typename T>
379 inline flecs::entity world::singleton() const {
380 return flecs::entity(world_, _::type<T>::id(world_));
381}
382
384template <typename First>
385 inline flecs::entity world::target(int32_t index) const
386{
387 return flecs::entity(world_,
388 ecs_get_target(world_, _::type<First>::id(world_), _::type<First>::id(world_), index));
389}
390
392template <typename T>
393 inline flecs::entity world::target(
394 flecs::entity_t relationship,
395 int32_t index) const
396{
397 return flecs::entity(world_,
398 ecs_get_target(world_, _::type<T>::id(world_), relationship, index));
399}
400
402 inline flecs::entity world::target(
403 flecs::entity_t relationship,
404 int32_t index) const
405{
406 return flecs::entity(world_,
407 ecs_get_target(world_, relationship, relationship, index));
408}
409
411template <typename Func, if_t< is_callable<Func>::value > >
412 inline void world::get(const Func& func) const {
413 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
414 _::entity_with_delegate<Func>::invoke_get(
415 this->world_, this->singleton<first_arg_t<Func>>(), func);
416}
417
419template <typename Func, if_t< is_callable<Func>::value > >
420 inline void world::set(const Func& func) const {
421 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
422 _::entity_with_delegate<Func>::invoke_ensure(
423 this->world_, this->singleton<first_arg_t<Func>>(), func);
424}
425
427 inline flecs::entity world::get_alive(flecs::entity_t e) const {
428 e = ecs_get_alive(world_, e);
429 return flecs::entity(world_, e);
430}
431
433 inline flecs::entity world::make_alive(flecs::entity_t e) const {
434 ecs_make_alive(world_, e);
435 return flecs::entity(world_, e);
436}
437
439template <typename E>
440 inline flecs::entity enum_data<E>::entity() const {
441 return flecs::entity(world_, _::type<E>::id(world_));
442}
443
445template <typename E>
446 inline flecs::entity enum_data<E>::entity(underlying_type_t<E> value) const {
447 int index = index_by_value(value);
448 if (index >= 0) {
449 int32_t constant_i = impl_.constants[index].index;
450 flecs::entity_t entity = flecs_component_ids_get(world_, constant_i);
451 return flecs::entity(world_, entity);
452 }
453#ifdef FLECS_META
454 // Reflection data lookup failed. Try a value lookup among flecs::Constant relationships.
455 flecs::world world = flecs::world(world_);
456 return world.query_builder()
457 .with(flecs::ChildOf, world.id<E>())
458 .with(flecs::Constant, world.id<int32_t>())
459 .build()
460 .find([value](flecs::entity constant) {
461 const int32_t& constant_value = constant.get_second<int32_t>(flecs::Constant);
462 return value == static_cast<underlying_type_t<E>>(constant_value);
463 });
464#else
465 return flecs::entity::null(world_);
466#endif
467}
468
470template <typename E>
471 inline flecs::entity enum_data<E>::entity(E value) const {
472 return entity(static_cast<underlying_type_t<E>>(value));
473}
474
478 inline flecs::scoped_world world::scope(id_t parent) const {
479 return scoped_world(world_, parent);
480}
481
483template <typename T>
484 inline flecs::scoped_world world::scope() const {
485 flecs::id_t parent = _::type<T>::id(world_);
486 return scoped_world(world_, parent);
487}
488
490 inline flecs::scoped_world world::scope(const char* name) const {
491 return scope(entity(name));
492}
493
494} // namespace flecs
flecs::first_arg_t
typename first_arg< Func >::type first_arg_t
Convenience alias for the first argument type of a callable.
Definition function_traits.hpp:149
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
ECS_INVALID_PARAMETER
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
flecs::component::constant
untyped_component & constant(const char *name, T value)
Add a constant.
Definition untyped_component.inl:186
flecs::world::component
flecs::component< T > component(Args &&... args) const
Find or register a component.
Definition impl.hpp:11
flecs::world::query_builder
flecs::query_builder< Comps... > query_builder(Args &&... args) const
Create a query builder.
Definition impl.hpp:325
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:190
flecs::id_t
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
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_make_alive
void ecs_make_alive(ecs_world_t *world, ecs_entity_t entity)
Ensure an ID is alive.
ecs_get_alive
ecs_entity_t ecs_get_alive(const ecs_world_t *world, ecs_entity_t e)
Get an alive identifier.
ecs_set_alias
void ecs_set_alias(ecs_world_t *world, ecs_entity_t entity, const char *alias)
Set an alias for an entity.
ecs_get_scope
ecs_entity_t ecs_get_scope(const ecs_world_t *world)
Get the current scope.
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)
Look up an entity from a path.
ecs_get_name
const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
Get the name of an entity.
ecs_set_scope
ecs_entity_t ecs_set_scope(ecs_world_t *world, ecs_entity_t scope)
Set the current scope.
flecs::_::entity_with_delegate
Definition delegate.hpp:908
flecs::_::type
Definition flecs.hpp:39
flecs::entity_builder::remove
const Self & remove() const
Remove a component from an entity.
Definition builder.hpp:303
flecs::entity_builder::set
const Self & set(T &&value) const
Set a component for an entity.
Definition builder.hpp:705
flecs::entity_builder::add
const Self & add() const
Add a component to an entity.
Definition builder.hpp:25
flecs::entity_view::get_mut
T & get_mut() const
Get mutable component value.
Definition entity_view.hpp:772
flecs::entity_view::get
const T & get() const
Get component value.
Definition entity_view.hpp:432
flecs::entity_view::has
bool has(flecs::id_t e) const
Check if entity has the provided entity.
Definition entity_view.hpp:1032
flecs::entity_view::try_get_mut
T * try_get_mut() const
Get mutable component value.
Definition entity_view.hpp:626
flecs::entity_view::id
entity_t id() const
Get entity ID.
Definition entity_view.hpp:53
flecs::entity_view::try_get
const T * try_get() const
Get component value.
Definition entity_view.hpp:286
flecs::entity_view::children
void children(flecs::entity_t rel, Func &&func) const
Iterate children for an entity.
Definition entity_view.hpp:228
flecs::entity
Entity.
Definition entity.hpp:30
flecs::entity::null
static flecs::entity null()
Entity ID 0 without a world.
Definition entity.hpp:491
flecs::entity::get_ref
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:319
flecs::entity::modified
void modified() const
Signal that component was modified.
Definition entity.hpp:233
flecs::entity::ensure
T & ensure() const
Get mutable component value.
Definition entity.hpp:139
flecs::enum_data::entity
flecs::entity entity() const
Get entity for the enum type.
Definition world.hpp:440
flecs::enum_data::world_
flecs::world_t * world_
Manually register a constant for an enum.
Definition enum.hpp:610
flecs::enum_data::index_by_value
int index_by_value(U value) const
Find the index into the constants array for a value, if one exists.
Definition enum.hpp:535
flecs::ref
Component reference.
Definition ref.hpp:109
flecs::scoped_world
Scoped world.
Definition world.hpp:1546
flecs::world
The world.
Definition world.hpp:246
flecs::world::get
const T & get() const
Get a singleton component.
Definition world.hpp:191
flecs::world::world_
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1540
flecs::world::get_scope
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:91
flecs::world::get_mut
T & get_mut() const
Get a mutable singleton component.
Definition world.hpp:257
flecs::world::remove
void remove() const
Remove singleton component.
Definition world.hpp:346
flecs::world::lookup
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:102
flecs::world::get_alive
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for ID.
Definition world.hpp:427
flecs::world::make_alive
flecs::entity make_alive(flecs::entity_t e) const
Ensure an entity ID is alive.
Definition world.hpp:433
flecs::world::id
flecs::id id() const
Get ID from a type.
Definition impl.hpp:70
flecs::world::target
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:385
flecs::world::try_get
const T * try_get() const
Get singleton component.
Definition world.hpp:158
flecs::world::init_builtin_components
void init_builtin_components()
Initialize built-in components.
Definition world.hpp:12
flecs::world::set_scope
flecs::entity set_scope() const
Same as set_scope(), but with type.
Definition world.hpp:97
flecs::world::scope
flecs::scoped_world scope() const
Use the provided scope (by type) for operations run on the returned world.
Definition world.hpp:484
flecs::world::children
void children(Func &&f) const
Iterate entities in root of world.
Definition world.hpp:373
flecs::world::try_get_mut
T * try_get_mut() const
Try to get a mutable singleton component (returns nullptr if not found).
Definition world.hpp:224
flecs::world::modified
void modified() const
Mark singleton component as modified.
Definition world.hpp:118
flecs::world::use
flecs::entity use(const char *alias=nullptr) const
Create an alias for a component.
Definition world.hpp:54
flecs::world::add
void add() const
Add singleton component.
Definition world.hpp:312
flecs::world::ensure
T & ensure() const
Ensure singleton component.
Definition world.hpp:110
flecs::world::set
void set(const T &value) const
Set singleton component.
Definition world.hpp:779
flecs::world::has
bool has() const
Test if world has singleton component.
Definition world.hpp:278
flecs::world::singleton
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:379
flecs::world::get_ref
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:139