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