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
127template <typename T>
128inline 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>
134const 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>
140const 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>
164inline 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>
170inline bool world::has() const {
171 flecs::entity e(world_, _::type<First>::id(world_));
172 return e.has<First, Second>();
173}
174
175template <typename First>
176inline 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
181inline 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>
187inline void world::add() const {
188 flecs::entity e(world_, _::type<T>::id(world_));
189 e.add<T>();
190}
191
192template <typename First, typename Second>
193inline void world::add() const {
194 flecs::entity e(world_, _::type<First>::id(world_));
195 e.add<First, Second>();
196}
197
198template <typename First>
199inline void world::add(flecs::entity_t second) const {
200 flecs::entity e(world_, _::type<First>::id(world_));
201 e.add<First>(second);
202}
203
204inline 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>
210inline void world::remove() const {
211 flecs::entity e(world_, _::type<T>::id(world_));
212 e.remove<T>();
213}
214
215template <typename First, typename Second>
216inline void world::remove() const {
217 flecs::entity e(world_, _::type<First>::id(world_));
218 e.remove<First, Second>();
219}
220
221template <typename First>
222inline void world::remove(flecs::entity_t second) const {
223 flecs::entity e(world_, _::type<First>::id(world_));
224 e.remove<First>(second);
225}
226
227inline 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>
233inline void world::children(Func&& f) const {
234 this->entity(0).children(FLECS_FWD(f));
235}
236
237template <typename T>
239 return flecs::entity(world_, _::type<T>::id(world_));
240}
241
242template <typename First>
243inline 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>
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
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 > >
267inline void world::get(const Func& func) const {
268 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
270 this->world_, this->singleton<first_arg_t<Func>>(), func);
271}
272
273template <typename Func, if_t< is_callable<Func>::value > >
274inline void world::set(const Func& func) const {
275 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
277 this->world_, this->singleton<first_arg_t<Func>>(), func);
278}
279
280inline 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
285inline 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
328inline 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
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
const T * get() const
Get component value.
T * get_mut() const
Get mutable component value.
bool has(flecs::id_t e) const
Check if entity has the provided entity.
entity_t id() const
Get entity id.
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:386
Convenience type with enum reflection data.
Definition enum.hpp:442
Component reference.
Definition ref.hpp:85
Scoped world.
Definition world.hpp:1242
The world.
Definition world.hpp:137
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:81
T * get_mut() const
Get mutable singleton component.
Definition world.hpp:146
void remove() const
Remove singleton component.
Definition world.hpp:210
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:280
flecs::entity make_alive(flecs::entity_t e) const
Definition world.hpp:285
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:243
const T * get() const
Get singleton component.
Definition world.hpp:128
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:233
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:187
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:164
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:238
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:122