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
127template <typename T>
128 inline const T* world::get() const {
129 flecs::entity e(world_, _::type<T>::id(world_));
130 return e.get<T>();
131}
132
133template <typename First, typename Second, typename P, typename A>
134 const A* world::get() const {
135 flecs::entity e(world_, _::type<First>::id(world_));
136 return e.get<First, Second>();
137}
138
139template <typename First, typename Second>
140 const First* world::get(Second second) const {
141 flecs::entity e(world_, _::type<First>::id(world_));
142 return e.get<First>(second);
143}
144
145template <typename T>
146T* world::get_mut() const {
147 flecs::entity e(world_, _::type<T>::id(world_));
148 return e.get_mut<T>();
149}
150
151template <typename First, typename Second, typename P, typename A>
152A* world::get_mut() const {
153 flecs::entity e(world_, _::type<First>::id(world_));
154 return e.get_mut<First, Second>();
155}
156
157template <typename First, typename Second>
158First* world::get_mut(Second second) const {
159 flecs::entity e(world_, _::type<First>::id(world_));
160 return e.get_mut<First>(second);
161}
162
163template <typename T>
164 inline bool world::has() const {
165 flecs::entity e(world_, _::type<T>::id(world_));
166 return e.has<T>();
167}
168
169template <typename First, typename Second>
170 inline bool world::has() const {
171 flecs::entity e(world_, _::type<First>::id(world_));
172 return e.has<First, Second>();
173}
174
175template <typename First>
176 inline bool world::has(flecs::id_t second) const {
177 flecs::entity e(world_, _::type<First>::id(world_));
178 return e.has<First>(second);
179}
180
181 inline bool world::has(flecs::id_t first, flecs::id_t second) const {
182 flecs::entity e(world_, first);
183 return e.has(first, second);
184}
185
186template <typename T>
187 inline void world::add() const {
188 flecs::entity e(world_, _::type<T>::id(world_));
189 e.add<T>();
190}
191
192template <typename First, typename Second>
193 inline void world::add() const {
194 flecs::entity e(world_, _::type<First>::id(world_));
195 e.add<First, Second>();
196}
197
198template <typename First>
199 inline void world::add(flecs::entity_t second) const {
200 flecs::entity e(world_, _::type<First>::id(world_));
201 e.add<First>(second);
202}
203
204 inline void world::add(flecs::entity_t first, flecs::entity_t second) const {
205 flecs::entity e(world_, first);
206 e.add(first, second);
207}
208
209template <typename T>
210 inline void world::remove() const {
211 flecs::entity e(world_, _::type<T>::id(world_));
212 e.remove<T>();
213}
214
215template <typename First, typename Second>
216 inline void world::remove() const {
217 flecs::entity e(world_, _::type<First>::id(world_));
218 e.remove<First, Second>();
219}
220
221template <typename First>
222 inline void world::remove(flecs::entity_t second) const {
223 flecs::entity e(world_, _::type<First>::id(world_));
224 e.remove<First>(second);
225}
226
227 inline void world::remove(flecs::entity_t first, flecs::entity_t second) const {
228 flecs::entity e(world_, first);
229 e.remove(first, second);
230}
231
232template <typename Func>
233 inline void world::children(Func&& f) const {
234 this->entity(0).children(FLECS_FWD(f));
235}
236
237template <typename T>
238 inline flecs::entity world::singleton() const {
239 return flecs::entity(world_, _::type<T>::id(world_));
240}
241
242template <typename First>
243 inline flecs::entity world::target(int32_t index) const
244{
245 return flecs::entity(world_,
246 ecs_get_target(world_, _::type<First>::id(world_), _::type<First>::id(world_), index));
247}
248
249template <typename T>
250 inline flecs::entity world::target(
251 flecs::entity_t relationship,
252 int32_t index) const
253{
254 return flecs::entity(world_,
255 ecs_get_target(world_, _::type<T>::id(world_), relationship, index));
256}
257
258 inline flecs::entity world::target(
259 flecs::entity_t relationship,
260 int32_t index) const
261{
262 return flecs::entity(world_,
263 ecs_get_target(world_, relationship, relationship, index));
264}
265
266template <typename Func, if_t< is_callable<Func>::value > >
267 inline void world::get(const Func& func) const {
268 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
269 _::entity_with_delegate<Func>::invoke_get(
270 this->world_, this->singleton<first_arg_t<Func>>(), func);
271}
272
273template <typename Func, if_t< is_callable<Func>::value > >
274 inline void world::set(const Func& func) const {
275 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
276 _::entity_with_delegate<Func>::invoke_ensure(
277 this->world_, this->singleton<first_arg_t<Func>>(), func);
278}
279
280 inline flecs::entity world::get_alive(flecs::entity_t e) const {
281 e = ecs_get_alive(world_, e);
282 return flecs::entity(world_, e);
283}
284
285 inline flecs::entity world::make_alive(flecs::entity_t e) const {
286 ecs_make_alive(world_, e);
287 return flecs::entity(world_, e);
288}
289
290template <typename E>
291inline flecs::entity enum_data<E>::entity() const {
292 return flecs::entity(world_, _::type<E>::id(world_));
293}
294
295template <typename E>
296inline flecs::entity enum_data<E>::entity(underlying_type_t<E> value) const {
297 int index = index_by_value(value);
298 if (index >= 0) {
299 int32_t constant_i = impl_.constants[index].index;
300 flecs::entity_t entity = flecs_component_ids_get(world_, constant_i);
301 return flecs::entity(world_, entity);
302 }
303#ifdef FLECS_META
304 // Reflection data lookup failed. Try value lookup amongst flecs::Constant relationships
305 flecs::world world = flecs::world(world_);
306 return world.query_builder()
307 .with(flecs::ChildOf, world.id<E>())
308 .with(flecs::Constant, world.id<int32_t>())
309 .build()
310 .find([value](flecs::entity constant) {
311 const int32_t *constant_value = constant.get_second<int32_t>(flecs::Constant);
312 ecs_assert(constant_value, ECS_INTERNAL_ERROR, NULL);
313 return value == static_cast<underlying_type_t<E>>(*constant_value);
314 });
315#else
316 return flecs::entity::null(world_);
317#endif
318}
319
320template <typename E>
321inline flecs::entity enum_data<E>::entity(E value) const {
322 return entity(static_cast<underlying_type_t<E>>(value));
323}
324
328 inline flecs::scoped_world world::scope(id_t parent) const {
329 return scoped_world(world_, parent);
330}
331
332template <typename T>
333inline flecs::scoped_world world::scope() const {
334 flecs::id_t parent = _::type<T>::id(world_);
335 return scoped_world(world_, parent);
336}
337
338inline flecs::scoped_world world::scope(const char* name) const {
339 return scope(entity(name));
340}
341
342} // 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:248
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:206
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:878
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::add
const Self & add() const
Add a component to an entity.
Definition builder.hpp:25
flecs::entity_view::get
const T * get() const
Get component value.
Definition entity_view.hpp:259
flecs::entity_view::get_mut
T * get_mut() const
Get mutable component value.
Definition entity_view.hpp:429
flecs::entity_view::has
bool has(flecs::id_t e) const
Check if entity has the provided entity.
Definition entity_view.hpp:635
flecs::entity_view::id
entity_t id() const
Get entity id.
Definition entity_view.hpp:50
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:386
flecs::ref
Component reference.
Definition ref.hpp:85
flecs::scoped_world
Scoped world.
Definition world.hpp:1242
flecs::world
The world.
Definition world.hpp:137
flecs::world::get_scope
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:81
flecs::world::get_mut
T * get_mut() const
Get mutable singleton component.
Definition world.hpp:146
flecs::world::remove
void remove() const
Remove singleton component.
Definition world.hpp:210
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:280
flecs::world::make_alive
flecs::entity make_alive(flecs::entity_t e) const
Definition world.hpp:285
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:243
flecs::world::get
const T * get() const
Get singleton component.
Definition world.hpp:128
flecs::world::set_scope
flecs::entity set_scope() const
Same as set_scope but with type.
Definition world.hpp:86
flecs::world::children
void children(Func &&f) const
Iterate entities in root of world Accepts a callback with the following signature:
Definition world.hpp:233
flecs::world::scope
void scope(id_t parent, const Func &func) const
All entities created in function are created in scope.
Definition world.hpp:995
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:187
flecs::world::ensure
T & ensure() const
Ensure singleton component.
Definition world.hpp:97
flecs::world::set
void set(const T &value) const
Set singleton component.
Definition world.hpp:649
flecs::world::has
bool has() const
Test if world has singleton component.
Definition world.hpp:164
flecs::world::singleton
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:238
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.