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