Skip to content
Flecs v4.1
impl.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/entity/impl.hpp
3 * @brief Entity implementation.
4 */
5
6#pragma once
7
8namespace flecs {
9
11 : untyped_ref(entity.world(), entity.id(), id) { }
12
14 return flecs::entity(world_, ref_.entity);
15}
16
17template <typename T>
19 : ref(entity.world(), entity.id(), id) { }
20
21template <typename Self>
22template <typename Func>
23inline const Self& entity_builder<Self>::insert(const Func& func) const {
25 this->world_, this->id_, func);
26 return to_base();
27}
28
29template<typename Enum>
30inline Enum entity_view::get_constant() const {
31 flecs::entity tgt = this->target<Enum>();
32 return tgt.to_constant<Enum>();
33}
34
35template<typename First>
36inline flecs::entity entity_view::target(int32_t index) const
37{
38 return flecs::entity(world_,
40}
41
43 flecs::entity_t relationship,
44 int32_t index) const
45{
46 return flecs::entity(world_,
47 ecs_get_target(world_, id_, relationship, index));
48}
49
51 flecs::entity_t relationship,
52 flecs::id_t id) const
53{
54 return flecs::entity(world_,
55 ecs_get_target_for_id(world_, id_, relationship, id));
56}
57
58template <typename T>
60 return target_for(relationship, _::type<T>::id(world_));
61}
62
63template <typename First, typename Second>
65 return target_for(relationship, _::type<First, Second>::id(world_));
66}
67
70}
71
72inline flecs::entity entity_view::mut(const flecs::world& stage) const {
74 "cannot use read-only world/stage to create mutable handle");
75 return flecs::entity(id_).set_stage(stage.c_ptr());
76}
77
80 "cannot use iterator created for read-only world/stage to create mutable handle");
81 return flecs::entity(id_).set_stage(it.world().c_ptr());
82}
83
86 "cannot use entity created for read-only world/stage to create mutable handle");
87 return flecs::entity(id_).set_stage(e.world_);
88}
89
90inline flecs::entity entity_view::set_stage(world_t *stage) {
91 return flecs::entity(stage, id_);
92}
93
96}
97
100}
101
103 ecs_record_t *r = ecs_record_find(world_, id_);
104 if (r) {
105 return flecs::table_range(world_, r->table,
106 ECS_RECORD_TO_ROW(r->row), 1);
107 }
108 return flecs::table_range();
109}
110
111namespace _ {
112
113template <bool Match, typename Func>
115 flecs::id_t pattern, const Func& func)
116{
117 auto real_world = const_cast<flecs::world_t*>(ecs_get_world(world));
119 const ecs_type_t *type = table ? ecs_table_get_type(table) : nullptr;
120 if (type) {
121 const auto ids = type->array;
122 const int32_t count = type->count;
123 for (int32_t cur = 0; cur < count; cur ++) {
124 if constexpr (Match) {
125 cur = ecs_search_offset(real_world, table, cur, pattern, nullptr);
126 if (cur == -1) {
127 break;
128 }
129 }
130 flecs::id id(world, ids[cur]);
131 func(id);
132 }
133 }
134 const ecs_record_t *r = ecs_record_find(real_world, entity);
135 if (r && (r->row & EcsEntityHasDontFragment)) {
136 for (auto cr = flecs_component_dont_fragment_first(real_world); cr;
137 cr = flecs_component_dont_fragment_next(cr))
138 {
139 auto id = flecs_component_get_id(cr);
140 if (ecs_id_is_wildcard(id) || (Match && !ecs_id_match(id, pattern))) {
141 continue;
142 }
143 auto sparse = flecs_component_get_sparse(cr);
144 if (sparse && flecs_sparse_has(sparse, entity)) {
145 flecs::id ent(world, id);
146 func(ent);
147 }
148 }
149 }
150}
151
152}
153
154template <typename Func>
155inline void entity_view::each(const Func& func) const {
156 _::each_id<false>(world_, id_, 0, func);
157}
158
159template <typename Func>
160inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& func) const {
161 _::each_id<true>(world_, id_, obj ? ecs_pair(pred, obj) : pred, func);
162}
163
164template <typename Func>
165inline void entity_view::each(const flecs::entity_view& rel, const Func& func) const {
166 return this->each(rel, flecs::Wildcard, [&](flecs::id id) {
167 flecs::entity obj = id.second();
168 func(obj);
169 });
170}
171
172template <typename Func, if_t< is_callable<Func>::value > >
173inline bool entity_view::get(const Func& func) const {
175}
176
177inline flecs::entity entity_view::lookup(const char *path, bool search_path) const {
178 ecs_assert(id_ != 0, ECS_INVALID_PARAMETER, "invalid lookup from null handle");
179 auto id = ecs_lookup_path_w_sep(world_, id_, path, "::", "::", search_path);
180 return flecs::entity(world_, id);
181}
182
183inline flecs::entity entity_view::clone(bool copy_value, flecs::entity_t dst_id) const {
184 if (!dst_id) {
185 dst_id = ecs_new(world_);
186 }
187
188 flecs::entity dst = flecs::entity(world_, dst_id);
189 ecs_clone(world_, dst_id, id_, copy_value);
190 return dst;
191}
192
193// Entity mixin implementation
194template <typename... Args>
195inline flecs::entity world::entity(Args &&... args) const {
196 return flecs::entity(world_, FLECS_FWD(args)...);
197}
198
199template <typename E, if_t< is_enum<E>::value >>
200inline flecs::id world::id(E value) const {
202 return flecs::id(world_, constant);
203}
204
205template <typename E, if_t< is_enum<E>::value >>
206inline flecs::entity world::entity(E value) const {
209}
210
211template <typename T>
212inline flecs::entity world::entity(const char *name) const {
213 return flecs::entity(world_, _::type<T>::register_id(world_, name, true, false) );
214}
215
216}
enum_data< E > enum_type(flecs::world_t *world)
Convenience function for getting enum reflection data.
Definition enum.hpp:457
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
struct ecs_record_t ecs_record_t
Information about an entity, like its table and row.
Definition flecs.h:504
struct ecs_table_t ecs_table_t
A table stores entities and components for a specific type.
Definition flecs.h:445
untyped_component & constant(const char *name, T value)
Add a constant.
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:195
E to_constant() const
Convert an entity to an enum constant.
Definition impl.hpp:11
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_entity_t ecs_clone(ecs_world_t *world, ecs_entity_t dst, ecs_entity_t src, bool copy_value)
Clone an entity.
ecs_entity_t ecs_new(ecs_world_t *world)
Create new entity ID.
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_entity_t ecs_get_parent(const ecs_world_t *world, ecs_entity_t entity)
Get the parent (target of the ChildOf relationship) for an entity.
ecs_entity_t ecs_get_target_for_id(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t rel, ecs_id_t component)
Get the target of a relationship for a given component.
const ecs_type_t * ecs_get_type(const ecs_world_t *world, ecs_entity_t entity)
Get the type of an entity.
ecs_table_t * ecs_get_table(const ecs_world_t *world, ecs_entity_t entity)
Get the table of an entity.
bool ecs_id_match(ecs_id_t component, ecs_id_t pattern)
Utility to match a component with a pattern.
bool ecs_id_is_wildcard(ecs_id_t component)
Utility to check if a component is a wildcard.
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.
int32_t ecs_search_offset(const ecs_world_t *world, const ecs_table_t *table, int32_t offset, ecs_id_t component, ecs_id_t *component_out)
Search for a component in a table type starting from an offset.
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get the type for a table.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
Int to enum.
Definition component.hpp:18
A type is a list of (component) IDs.
Definition flecs.h:412
const Self & insert(const Func &func) const
Set 1..N components.
Definition impl.hpp:23
flecs::type type() const
Get the entity's type.
Definition impl.hpp:94
flecs::string path(const char *sep="::", const char *init_sep="::") const
Return the entity path.
flecs::table_range range() const
Get table range for the entity.
Definition impl.hpp:102
flecs::entity target_for(flecs::entity_t relationship, flecs::id_t id) const
Get the target of a pair for a given relationship ID.
Definition impl.hpp:50
flecs::entity clone(bool clone_value=true, flecs::entity_t dst_id=0) const
Clone an entity.
Definition impl.hpp:183
flecs::entity lookup(const char *path, bool search_path=false) const
Lookup an entity by name.
Definition impl.hpp:177
flecs::table table() const
Get the entity's table.
Definition impl.hpp:98
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:68
void each(const Func &func) const
Iterate (component) IDs of an entity.
Definition impl.hpp:155
Enum get_constant() const
Get enum constant for enum relationship.
Definition impl.hpp:30
flecs::entity target(int32_t index=0) const
Get target for a given pair.
Definition impl.hpp:36
flecs::entity mut(const flecs::world &stage) const
Return a mutable entity handle for the current stage.
Definition impl.hpp:72
Entity.
Definition entity.hpp:30
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::world world() const
Get the world.
Definition impl.hpp:60
flecs::id_t id_
The raw ID value.
Definition decl.hpp:154
flecs::entity second() const
Get second element from a pair.
Definition impl.hpp:31
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:152
Class for iterating over query results.
Definition iter.hpp:68
flecs::world world() const
Get the world associated with the iterator.
Definition iter.hpp:27
ref()
Default constructor.
Definition ref.hpp:111
Table range.
Definition table.hpp:205
Table.
Definition table.hpp:23
Type class.
Definition type.hpp:21
flecs::id_t * array() const
Return a pointer to the ID array.
Definition type.hpp:57
int32_t count() const
Return the number of IDs in the type.
Definition type.hpp:46
untyped_ref()
Default constructor.
Definition ref.hpp:25
flecs::world world() const
Get the world associated with the reference.
Definition ref.hpp:77
flecs::entity entity() const
Return the entity associated with the reference.
Definition impl.hpp:13
The world.
Definition world.hpp:129
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1009
flecs::id id() const
Get ID from a type.
Definition impl.hpp:72
world_t * c_ptr() const
Obtain a pointer to the C world object.
Definition world.hpp:235
bool is_readonly() const
Test whether the current world object is readonly.
Definition world.hpp:471
flecs::id id(E value) const
Convert an enum constant to an entity.