Skip to content
Flecs v4.1
world.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/impl/world.hpp
3 * @brief World implementation.
4 */
5
6#pragma once
7
8namespace flecs
9{
10
11/** Initialize built-in components. */
15 this->component<Poly>();
16 this->component<Parent>();
17
18 /* If meta is not defined and we're using enum reflection, make sure that
19 * primitive types are registered. This ensures we can set components of
20 * underlying_type_t<E> when registering constants. */
21# if !defined(FLECS_META) && !defined(FLECS_CPP_NO_ENUM_REFLECTION)
22 this->component<uint8_t>("flecs::meta::u8");
23 this->component<uint16_t>("flecs::meta::u16");
24 this->component<uint32_t>("flecs::meta::u32");
25 this->component<uint64_t>("flecs::meta::u64");
26 this->component<int8_t>("flecs::meta::i8");
27 this->component<int16_t>("flecs::meta::i16");
28 this->component<int32_t>("flecs::meta::i32");
29 this->component<int64_t>("flecs::meta::i64");
30# endif
31
32# ifdef FLECS_SYSTEM
33 _::system_init(*this);
34# endif
35# ifdef FLECS_TIMER
36 _::timer_init(*this);
37# endif
38# ifdef FLECS_DOC
39 doc::_::init(*this);
40# endif
41# ifdef FLECS_REST
42 rest::_::init(*this);
43# endif
44# ifdef FLECS_META
45 meta::_::init(*this);
46# endif
47# ifdef FLECS_SCRIPT
48 script::_::init(*this);
49# endif
50}
51
52/** Import a type (entity/component) as an alias. */
53template <typename T>
54inline flecs::entity world::use(const char *alias) const {
56 const char *name = alias;
57 if (!name) {
58 // If no name is defined, use the entity name without the scope.
59 name = ecs_get_name(world_, e);
60 }
61 ecs_set_alias(world_, e, name);
62 return flecs::entity(world_, e);
63}
64
65/** Import an entity by name as an alias. */
66inline flecs::entity world::use(const char *name, const char *alias) const {
67 entity_t e = ecs_lookup_path_w_sep(world_, 0, name, "::", "::", true);
68 ecs_assert(e != 0, ECS_INVALID_PARAMETER, nullptr);
69
70 ecs_set_alias(world_, e, alias);
71 return flecs::entity(world_, e);
72}
73
74/** Import an entity as an alias. */
75inline void world::use(flecs::entity e, const char *alias) const {
76 entity_t eid = e.id();
77 const char *name = alias;
78 if (!name) {
79 // If no name is defined, use the entity name without the scope.
80 name = ecs_get_name(world_, eid);
81 }
82 ecs_set_alias(world_, eid, name);
83}
84
85/** Set the current scope. */
88}
89
90/** Get the current scope. */
93}
94
95/** Set the current scope to a type. */
96template <typename T>
99}
100
101/** Look up an entity by name. */
102inline entity world::lookup(const char *name, const char *sep, const char *root_sep, bool recursive) const {
103 auto e = ecs_lookup_path_w_sep(world_, 0, name, sep, root_sep, recursive);
104 return flecs::entity(*this, e);
105}
106
107/** Get a ref for a singleton component. */
108template <typename T>
109inline ref<T> world::get_ref() const {
111 return e.get_ref<T>();
112}
113
114/** Try to get a singleton value by component ID (returns nullptr if not found). */
115template <typename Func>
116inline void world::children(Func&& f) const {
117 this->entity(0).children(FLECS_FWD(f));
118}
119
120/** Get the singleton entity for a component type. */
121template <typename T>
124}
125
126/** Get the target entity for a relationship on a singleton. */
127template <typename First>
128inline flecs::entity world::target(int32_t index) const
129{
130 return flecs::entity(world_,
132}
133
134/** Get the target entity for a relationship on a component entity. */
135template <typename T>
137 flecs::entity_t relationship,
138 int32_t index) const
139{
140 return flecs::entity(world_,
141 ecs_get_target(world_, _::type<T>::id(world_), relationship, index));
142}
143
144/** Get the target entity for a relationship. */
146 flecs::entity_t relationship,
147 int32_t index) const
148{
149 return flecs::entity(world_,
150 ecs_get_target(world_, relationship, relationship, index));
151}
152
153/** Get a singleton component using a callback. */
154template <typename Func, if_t< is_callable<Func>::value > >
155inline void world::get(const Func& func) const {
156 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
158 this->world_, this->singleton<first_arg_t<Func>>(), func);
159}
160
161/** Set a singleton component using a callback. */
162template <typename Func, if_t< is_callable<Func>::value > >
163inline void world::set(const Func& func) const {
164 static_assert(arity<Func>::value == 1, "singleton component must be the only argument");
166 this->world_, this->singleton<first_arg_t<Func>>(), func);
167}
168
169/** Get an alive entity from an ID. */
171 e = ecs_get_alive(world_, e);
172 return flecs::entity(world_, e);
173}
174
175/** Ensure an entity ID is alive. */
178 return flecs::entity(world_, e);
179}
180
181/** Get the entity for an enum type. */
182template <typename E>
185}
186
187/** Get the entity for an enum constant by underlying_type value. */
188template <typename E>
189inline flecs::entity enum_data<E>::entity(underlying_type_t<E> value) const {
190 int index = index_by_value(value);
191 if (index >= 0) {
192#ifdef FLECS_MULTI_WORLD
193 int32_t constant_i = impl_.constants[index].index;
194 flecs::entity_t entity = flecs_component_ids_get(world_, constant_i);
195#else
196 flecs::entity_t entity = impl_.constants[index].id;
197#endif
198 return flecs::entity(world_, entity);
199 }
200#ifdef FLECS_META
202 world_, _::type<E>::id(world_), static_cast<int64_t>(value)));
203#else
205#endif
206}
207
208/** Get the entity for an enum constant. */
209template <typename E>
210inline flecs::entity enum_data<E>::entity(E value) const {
211 return entity(static_cast<underlying_type_t<E>>(value));
212}
213
214} // namespace flecs
typename first_arg< Func >::type first_arg_t
Convenience alias for the first argument type of a callable.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
FLECS_API ecs_entity_t ecs_constant_to_entity_id(const ecs_world_t *world, ecs_entity_t type, int64_t value)
Find the entity for an enum constant with the provided value.
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:195
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
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 an ID is alive.
ecs_entity_t ecs_get_alive(const ecs_world_t *world, ecs_entity_t e)
Get an alive identifier.
void ecs_set_alias(ecs_world_t *world, ecs_entity_t entity, const char *alias)
Set an alias for an 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)
Look up 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.
component(flecs::world_t *world, const char *name=nullptr, bool allow_tag=true, flecs::id_t id=0)
Register a component.
entity_t id() const
Get entity ID.
Entity.
Definition entity.hpp:30
static flecs::entity null()
Entity ID 0 without a world.
Definition entity.hpp:259
entity()
Default constructor.
Definition entity.hpp:32
flecs::entity entity() const
Get entity for the enum type.
Definition world.hpp:183
flecs::world_t * world_
Manually register a constant for an enum.
Definition enum.hpp:451
int index_by_value(U value) const
Find the index into the constants array for a value, if one exists.
Definition enum.hpp:383
Component reference.
Definition ref.hpp:109
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1009
flecs::entity get_scope() const
Get current scope.
Definition world.hpp:91
flecs::entity lookup(const char *name, const char *sep="::", const char *root_sep="::", bool recursive=true) const
Lookup entity by name.
Definition world.hpp:102
flecs::entity get_alive(flecs::entity_t e) const
Get alive entity for ID.
Definition world.hpp:170
flecs::entity make_alive(flecs::entity_t e) const
Ensure an entity ID is alive.
Definition world.hpp:176
flecs::entity target(int32_t index=0) const
Get target for a given pair from a singleton entity.
Definition world.hpp:128
void init_builtin_components()
Initialize built-in components.
Definition world.hpp:12
flecs::entity set_scope() const
Same as set_scope(), but with type.
Definition world.hpp:97
void children(Func &&f) const
Iterate entities in root of world.
Definition world.hpp:116
flecs::entity set_scope(const flecs::entity_t scope) const
Set current scope.
Definition world.hpp:86
flecs::entity use(const char *alias=nullptr) const
Create an alias for a component.
Definition world.hpp:54
void get(const Func &func) const
Get a singleton component using a callback.
Definition world.hpp:155
flecs::entity singleton() const
Get singleton entity for type.
Definition world.hpp:122
ref< T > get_ref() const
Get ref singleton component.
Definition world.hpp:109