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
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# ifdef FLECS_SYSTEM
17 _::system_init(*this);
18# endif
19# ifdef FLECS_TIMER
20 _::timer_init(*this);
21# endif
22# ifdef FLECS_DOC
23 doc::_::init(*this);
24# endif
25# ifdef FLECS_REST
26 rest::_::init(*this);
27# endif
28# ifdef FLECS_META
29 meta::_::init(*this);
30# endif
31}
32
33template <typename T>
34 inline flecs::entity world::use(const char *alias) const {
35 entity_t e = _::type<T>::id(world_);
36 const char *name = alias;
37 if (!name) {
38 // If no name is defined, use the entity name without the scope
39 name = ecs_get_name(world_, e);
40 }
41 ecs_set_alias(world_, e, name);
42 return flecs::entity(world_, e);
43}
44
45 inline flecs::entity world::use(const char *name, const char *alias) const {
46 entity_t e = ecs_lookup_path_w_sep(world_, 0, name, "::", "::", true);
47 ecs_assert(e != 0, ECS_INVALID_PARAMETER, NULL);
48
49 ecs_set_alias(world_, e, alias);
50 return flecs::entity(world_, e);
51}
52
53 inline void world::use(flecs::entity e, const char *alias) const {
54 entity_t eid = e.id();
55 const char *name = alias;
56 if (!name) {
57 // If no name is defined, use the entity name without the scope
58 name = ecs_get_name(world_, eid);
59 }
60 ecs_set_alias(world_, eid, name);
61}
62
63 inline flecs::entity world::set_scope(const flecs::entity_t s) const {
64 return flecs::entity(ecs_set_scope(world_, s));
65}
66
67 inline flecs::entity world::get_scope() const {
68 return flecs::entity(world_, ecs_get_scope(world_));
69}
70
71template <typename T>
72 inline flecs::entity world::set_scope() const {
73 return set_scope( _::type<T>::id(world_) );
74}
75
76 inline entity world::lookup(const char *name, const char *sep, const char *root_sep, bool recursive) const {
77 auto e = ecs_lookup_path_w_sep(world_, 0, name, sep, root_sep, recursive);
78 return flecs::entity(*this, e);
79}
80
81#ifndef ensure
82template <typename T>
83 inline T& world::ensure() const {
84 flecs::entity e(world_, _::type<T>::id(world_));
85 return e.ensure<T>();
86}
87#endif
88
89template <typename T>
90 inline void world::modified() const {
91 flecs::entity e(world_, _::type<T>::id(world_));
92 e.modified<T>();
93}
94
95template <typename First, typename Second>
96 inline void world::set(Second second, const First& value) const {
97 flecs::entity e(world_, _::type<First>::id(world_));
98 e.set<First>(second, value);
99}
100
101template <typename First, typename Second>
102 inline void world::set(Second second, First&& value) const {
103 flecs::entity e(world_, _::type<First>::id(world_));
104 e.set<First>(second, value);
105}
106
107template <typename T>
108 inline ref<T> world::get_ref() const {
109 flecs::entity e(world_, _::type<T>::id(world_));
110 return e.get_ref<T>();
111}
112
113template <typename T>
114 inline const T* world::get() const {
115 flecs::entity e(world_, _::type<T>::id(world_));
116 return e.get<T>();
117}
118
119template <typename First, typename Second, typename P, typename A>
120 const A* world::get() const {
121 flecs::entity e(world_, _::type<First>::id(world_));
122 return e.get<First, Second>();
123}
124
125template <typename First, typename Second>
126 const First* world::get(Second second) const {
127 flecs::entity e(world_, _::type<First>::id(world_));
128 return e.get<First>(second);
129}
130
131template <typename T>
132T* world::get_mut() const {
133 flecs::entity e(world_, _::type<T>::id(world_));
134 return e.get_mut<T>();
135}
136
137template <typename First, typename Second, typename P, typename A>
138A* world::get_mut() const {
139 flecs::entity e(world_, _::type<First>::id(world_));
140 return e.get_mut<First, Second>();
141}
142
143template <typename First, typename Second>
144First* world::get_mut(Second second) const {
145 flecs::entity e(world_, _::type<First>::id(world_));
146 return e.get_mut<First>(second);
147}
148
149template <typename T>
150 inline bool world::has() const {
151 flecs::entity e(world_, _::type<T>::id(world_));
152 return e.has<T>();
153}
154
155template <typename First, typename Second>
156 inline bool world::has() const {
157 flecs::entity e(world_, _::type<First>::id(world_));
158 return e.has<First, Second>();
159}
160
161template <typename First>
162 inline bool world::has(flecs::id_t second) const {
163 flecs::entity e(world_, _::type<First>::id(world_));
164 return e.has<First>(second);
165}
166
167 inline bool world::has(flecs::id_t first, flecs::id_t second) const {
168 flecs::entity e(world_, first);
169 return e.has(first, second);
170}
171
172template <typename T>
173 inline void world::add() const {
174 flecs::entity e(world_, _::type<T>::id(world_));
175 e.add<T>();
176}
177
178template <typename First, typename Second>
179 inline void world::add() const {
180 flecs::entity e(world_, _::type<First>::id(world_));
181 e.add<First, Second>();
182}
183
184template <typename First>
185 inline void world::add(flecs::entity_t second) const {
186 flecs::entity e(world_, _::type<First>::id(world_));
187 e.add<First>(second);
188}
189
190 inline void world::add(flecs::entity_t first, flecs::entity_t second) const {
191 flecs::entity e(world_, first);
192 e.add(first, second);
193}
194
195template <typename T>
196 inline void world::remove() const {
197 flecs::entity e(world_, _::type<T>::id(world_));
198 e.remove<T>();
199}
200
201template <typename First, typename Second>
202 inline void world::remove() const {
203 flecs::entity e(world_, _::type<First>::id(world_));
204 e.remove<First, Second>();
205}
206
207template <typename First>
208 inline void world::remove(flecs::entity_t second) const {
209 flecs::entity e(world_, _::type<First>::id(world_));
210 e.remove<First>(second);
211}
212
213 inline void world::remove(flecs::entity_t first, flecs::entity_t second) const {
214 flecs::entity e(world_, first);
215 e.remove(first, second);
216}
217
218template <typename Func>
219 inline void world::children(Func&& f) const {
220 this->entity(0).children(FLECS_FWD(f));
221}
222
223template <typename T>
224 inline flecs::entity world::singleton() const {
225 return flecs::entity(world_, _::type<T>::id(world_));
226}
227
228template <typename First>
229 inline flecs::entity world::target(int32_t index) const
230{
231 return flecs::entity(world_,
232 ecs_get_target(world_, _::type<First>::id(world_), _::type<First>::id(world_), index));
233}
234
235template <typename T>
236 inline flecs::entity world::target(
237 flecs::entity_t relationship,
238 int32_t index) const
239{
240 return flecs::entity(world_,
241 ecs_get_target(world_, _::type<T>::id(world_), relationship, index));
242}
243
244 inline flecs::entity world::target(
245 flecs::entity_t relationship,
246 int32_t index) const
247{
248 return flecs::entity(world_,
249 ecs_get_target(world_, relationship, relationship, index));
250}
251
252template <typename Func, if_t< is_callable<Func>::value > >
253 inline void world::get(const Func& func) const {
254 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
255 _::entity_with_delegate<Func>::invoke_get(
256 this->world_, this->singleton<first_arg_t<Func>>(), func);
257}
258
259template <typename Func, if_t< is_callable<Func>::value > >
260 inline void world::set(const Func& func) const {
261 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
262 _::entity_with_delegate<Func>::invoke_ensure(
263 this->world_, this->singleton<first_arg_t<Func>>(), func);
264}
265
266 inline flecs::entity world::get_alive(flecs::entity_t e) const {
267 e = ecs_get_alive(world_, e);
268 return flecs::entity(world_, e);
269}
270
271 inline flecs::entity world::make_alive(flecs::entity_t e) const {
272 ecs_make_alive(world_, e);
273 return flecs::entity(world_, e);
274}
275
276template <typename E>
277inline flecs::entity enum_data<E>::entity() const {
278 return flecs::entity(world_, impl_.id);
279}
280
281template <typename E>
282inline flecs::entity enum_data<E>::entity(underlying_type_t<E> value) const {
283 int index = index_by_value(value);
284 if (index >= 0) {
285 return flecs::entity(world_, impl_.constants[index].id);
286 }
287#ifdef FLECS_META
288 // Reflection data lookup failed. Try value lookup amongst flecs::Constant relationships
289 flecs::world world = flecs::world(world_);
290 return world.query_builder()
291 .with(flecs::ChildOf, world.id<E>())
292 .with(flecs::Constant, world.id<int32_t>())
293 .build()
294 .find([value](flecs::entity constant) {
295 const int32_t *constant_value = constant.get_second<int32_t>(flecs::Constant);
296 ecs_assert(constant_value, ECS_INTERNAL_ERROR, NULL);
297 return value == static_cast<underlying_type_t<E>>(*constant_value);
298 });
299#else
300 return flecs::entity::null(world_);
301#endif
302}
303
304template <typename E>
305inline flecs::entity enum_data<E>::entity(E value) const {
306 return entity(static_cast<underlying_type_t<E>>(value));
307}
308
312 inline flecs::scoped_world world::scope(id_t parent) const {
313 return scoped_world(world_, parent);
314}
315
316template <typename T>
317inline flecs::scoped_world world::scope() const {
318 flecs::id_t parent = _::type<T>::id(world_);
319 return scoped_world(world_, parent);
320}
321
322inline flecs::scoped_world world::scope(const char* name) const {
323 return scope(entity(name));
324}
325
326} // namespace flecs
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
flecs::component::constant
untyped_component & constant(const char *name, int32_t value)
Add constant.
Definition untyped_component.inl:191
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:237
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:195
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:882
flecs::_::type
Definition flecs.hpp:38
flecs::arity
Definition function_traits.hpp:105
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_second
const Second * get_second(flecs::entity_t first) const
Get the second part for a pair.
Definition entity_view.hpp:398
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:247
flecs::entity::modified
void modified() const
Signal that component was modified.
Definition entity.hpp:186
flecs::entity::ensure
T & ensure() const
Get mutable component value.
Definition entity.hpp:99
flecs::entity::null
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:331
flecs::enum_data
Convenience type with enum reflection data.
Definition enum.hpp:396
flecs::ref
Component reference.
Definition ref.hpp:23
flecs::scoped_world
Scoped world.
Definition world.hpp:1226
flecs::world
The world.
Definition world.hpp:137
flecs::world::get_scope
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:67
flecs::world::get_mut
T * get_mut() const
Get mutable singleton component.
Definition world.hpp:132
flecs::world::remove
void remove() const
Remove singleton component.
Definition world.hpp:196
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:76
flecs::world::get_alive
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for id.
Definition world.hpp:266
flecs::world::make_alive
flecs::entity make_alive(flecs::entity_t e) const
Definition world.hpp:271
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:229
flecs::world::get
const T * get() const
Get singleton component.
Definition world.hpp:114
flecs::world::set_scope
flecs::entity set_scope() const
Same as set_scope but with type.
Definition world.hpp:72
flecs::world::children
void children(Func &&f) const
Iterate entities in root of world Accepts a callback with the following signature:
Definition world.hpp:219
flecs::world::modified
void modified() const
Mark singleton component as modified.
Definition world.hpp:90
flecs::world::use
flecs::entity use(const char *alias=nullptr) const
Create alias for component.
Definition world.hpp:34
flecs::world::add
void add() const
Add singleton component.
Definition world.hpp:173
flecs::world::ensure
T & ensure() const
Ensure singleton component.
Definition world.hpp:83
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:150
flecs::world::singleton
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:224
flecs::world::get_ref
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:108
flecs::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.