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

| 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
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}
46
47template <typename T>
48 inline 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
59 inline 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
67 inline 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
77 inline flecs::entity world::set_scope(const flecs::entity_t s) const {
78 return flecs::entity(ecs_set_scope(world_, s));
79}
80
81 inline flecs::entity world::get_scope() const {
82 return flecs::entity(world_, ecs_get_scope(world_));
83}
84
85template <typename T>
86 inline flecs::entity world::set_scope() const {
87 return set_scope( _::type<T>::id(world_) );
88}
89
90 inline 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>
97 inline 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>
104 inline void world::modified() const {
105 flecs::entity e(world_, _::type<T>::id(world_));
106 e.modified<T>();
107}
108
109template <typename First, typename Second>
110 inline 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>
116 inline 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>
122 inline ref<T> world::get_ref() const {
123 flecs::entity e(world_, _::type<T>::id(world_));
124 return e.get_ref<T>();
125}
126
127 inline const void* world::try_get(flecs::id_t id) const {
128 flecs::entity e(world_, id);
129 return e.try_get(id);
130}
131
132 inline 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>
138 inline 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>
144 inline 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>
150 inline 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
155 inline const void* world::get(flecs::id_t id) const {
156 flecs::entity e(world_, id);
157 return e.get(id);
158}
159
160 inline 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>
172 inline 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>
178 const First& world::get(Second second) const {
179 flecs::entity e(world_, _::type<First>::id(world_));
180 return e.get<First>(second);
181}
182
183 inline 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
188 inline 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>
200 inline 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>
206 inline 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
211 inline void* world::get_mut(flecs::id_t id) const {
212 flecs::entity e(world_, id);
213 return e.get_mut(id);
214}
215
216 inline 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>
228 inline 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>
234 inline 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>
240 inline 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>
246 inline bool world::has() const {
247 flecs::entity e(world_, _::type<First>::id(world_));
248 return e.has<First, Second>();
249}
250
251template <typename First>
252 inline 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
257 inline 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>
263 inline void world::add() const {
264 flecs::entity e(world_, _::type<T>::id(world_));
265 e.add<T>();
266}
267
268template <typename First, typename Second>
269 inline void world::add() const {
270 flecs::entity e(world_, _::type<First>::id(world_));
271 e.add<First, Second>();
272}
273
274template <typename First>
275 inline void world::add(flecs::entity_t second) const {
276 flecs::entity e(world_, _::type<First>::id(world_));
277 e.add<First>(second);
278}
279
280 inline 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>
286 inline void world::remove() const {
287 flecs::entity e(world_, _::type<T>::id(world_));
288 e.remove<T>();
289}
290
291template <typename First, typename Second>
292 inline void world::remove() const {
293 flecs::entity e(world_, _::type<First>::id(world_));
294 e.remove<First, Second>();
295}
296
297template <typename First>
298 inline void world::remove(flecs::entity_t second) const {
299 flecs::entity e(world_, _::type<First>::id(world_));
300 e.remove<First>(second);
301}
302
303 inline 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>
309 inline void world::children(Func&& f) const {
310 this->entity(0).children(FLECS_FWD(f));
311}
312
313template <typename T>
314 inline flecs::entity world::singleton() const {
315 return flecs::entity(world_, _::type<T>::id(world_));
316}
317
318template <typename First>
319 inline 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>
326 inline flecs::entity world::target(
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
334 inline flecs::entity world::target(
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 > >
343 inline void world::get(const Func& func) const {
344 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
345 _::entity_with_delegate<Func>::invoke_get(
346 this->world_, this->singleton<first_arg_t<Func>>(), func);
347}
348
349template <typename Func, if_t< is_callable<Func>::value > >
350 inline void world::set(const Func& func) const {
351 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
352 _::entity_with_delegate<Func>::invoke_ensure(
353 this->world_, this->singleton<first_arg_t<Func>>(), func);
354}
355
356 inline 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
361 inline 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
403 inline 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
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:914
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:694
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:701
flecs::entity_view::get
const T & get() const
Get component value.
Definition entity_view.hpp:397
flecs::entity_view::has
bool has(flecs::id_t e) const
Check if entity has the provided entity.
Definition entity_view.hpp:917
flecs::entity_view::try_get_mut
T * try_get_mut() const
Get mutable component value.
Definition entity_view.hpp:573
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:269
flecs::entity_view::children
void children(flecs::entity_t rel, Func &&func) const
Iterate children for entity.
Definition entity_view.hpp:204
flecs::entity
Entity.
Definition entity.hpp:30
flecs::entity::get_ref
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:294
flecs::entity::modified
void modified() const
Signal that component was modified.
Definition entity.hpp:209
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:409
flecs::ref
Component reference.
Definition ref.hpp:85
flecs::scoped_world
Scoped world.
Definition world.hpp:1366
flecs::world
The world.
Definition world.hpp:172
flecs::world::get_scope
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:81
flecs::world::remove
void remove() const
Remove singleton component.
Definition world.hpp:286
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:90
flecs::world::get_alive
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
Definition world.hpp:356
flecs::world::make_alive
flecs::entity make_alive(flecs::entity_t e) const
Definition world.hpp:361
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:319
flecs::world::get
const A & get() const
Get singleton pair.
Definition world.hpp:172
flecs::world::try_get
const T * try_get() const
Get singleton component.
Definition world.hpp:138
flecs::world::set_scope
flecs::entity set_scope() const
Same as set_scope but with type.
Definition world.hpp:86
flecs::world::try_get_mut
A * try_get_mut() const
Get mutable singleton pair.
Definition world.hpp:200
flecs::world::children
void children(Func &&f) const
Iterate entities in root of world Accepts a callback with the following signature:
Definition world.hpp:309
flecs::world::scope
void scope(id_t parent, const Func &func) const
All entities created in function are created in scope.
Definition world.hpp:1101
flecs::world::modified
void modified() const
Mark singleton component as modified.
Definition world.hpp:104
flecs::world::use
flecs::entity use(const char *alias=nullptr) const
Create alias for component.
Definition world.hpp:48
flecs::world::add
void add() const
Add singleton component.
Definition world.hpp:263
flecs::world::ensure
T & ensure() const
Ensure singleton component.
Definition world.hpp:97
flecs::world::get_mut
A & get_mut() const
Get mutable singleton pair.
Definition world.hpp:228
flecs::world::set
void set(const T &value) const
Set singleton component.
Definition world.hpp:684
flecs::world::has
bool has() const
Test if world has singleton component.
Definition world.hpp:240
flecs::world::singleton
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:314
flecs::world::get_ref
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:122
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.