Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace flecs {
9
12
14 return flecs::entity(world_, ref_.entity);
15}
16
17template <typename T>
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>
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
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
97
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
111template <typename Func>
112inline void entity_view::each(const Func& func) const {
114 if (type) {
115 const ecs_id_t *ids = type->array;
116 int32_t count = type->count;
117
118 for (int i = 0; i < count; i ++) {
119 ecs_id_t id = ids[i];
120 flecs::id ent(world_, id);
121 func(ent);
122 }
123 }
124
125 /* Iterate components with the DontFragment trait, which are not stored in
126 * the entity's table. */
127 const flecs::world_t *real_world = ecs_get_world(world_);
128 const ecs_record_t *r = ecs_record_find(real_world, id_);
129 if (r && (r->row & EcsEntityHasDontFragment)) {
130 ecs_component_record_t *cur = flecs_component_dont_fragment_first(
131 real_world);
132 for (; cur; cur = flecs_component_dont_fragment_next(cur)) {
133 ecs_id_t id = flecs_component_get_id(cur);
134 if (ecs_id_is_wildcard(id)) {
135 continue;
136 }
137
138 ecs_sparse_t *sparse = flecs_component_get_sparse(cur);
139 if (sparse && flecs_sparse_has(sparse, id_)) {
140 flecs::id ent(world_, id);
141 func(ent);
142 }
143 }
144 }
145}
146
147template <typename Func>
148inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& func) const {
149 flecs::world_t *real_world = const_cast<flecs::world_t*>(
151
152 flecs::id_t pattern = pred;
153 if (obj) {
154 pattern = ecs_pair(pred, obj);
155 }
156
158 const ecs_type_t *type = table ? ecs_table_get_type(table) : nullptr;
159 if (type) {
160 int32_t cur = 0;
161 id_t *ids = type->array;
162
163 while (-1 != (cur = ecs_search_offset(real_world, table, cur, pattern, nullptr)))
164 {
165 flecs::id ent(world_, ids[cur]);
166 func(ent);
167 cur ++;
168 }
169 }
170
171 /* Iterate components with the DontFragment trait, which are not stored in
172 * the entity's table. */
173 const ecs_record_t *r = ecs_record_find(real_world, id_);
174 if (r && (r->row & EcsEntityHasDontFragment)) {
175 ecs_component_record_t *cr = flecs_component_dont_fragment_first(
176 real_world);
177 for (; cr; cr = flecs_component_dont_fragment_next(cr)) {
178 ecs_id_t id = flecs_component_get_id(cr);
179 if (ecs_id_is_wildcard(id) || !ecs_id_match(id, pattern)) {
180 continue;
181 }
182
183 ecs_sparse_t *sparse = flecs_component_get_sparse(cr);
184 if (sparse && flecs_sparse_has(sparse, id_)) {
185 flecs::id ent(world_, id);
186 func(ent);
187 }
188 }
189 }
190}
191
192template <typename Func>
193inline void entity_view::each(const flecs::entity_view& rel, const Func& func) const {
194 return this->each(rel, flecs::Wildcard, [&](flecs::id id) {
195 flecs::entity obj = id.second();
196 func(obj);
197 });
198}
199
200template <typename Func, if_t< is_callable<Func>::value > >
201inline bool entity_view::get(const Func& func) const {
203}
204
205inline flecs::entity entity_view::lookup(const char *path, bool search_path) const {
206 ecs_assert(id_ != 0, ECS_INVALID_PARAMETER, "invalid lookup from null handle");
207 auto id = ecs_lookup_path_w_sep(world_, id_, path, "::", "::", search_path);
208 return flecs::entity(world_, id);
209}
210
211inline flecs::entity entity_view::clone(bool copy_value, flecs::entity_t dst_id) const {
212 if (!dst_id) {
213 dst_id = ecs_new(world_);
214 }
215
216 flecs::entity dst = flecs::entity(world_, dst_id);
217 ecs_clone(world_, dst_id, id_, copy_value);
218 return dst;
219}
220
221// Entity mixin implementation
222template <typename... Args>
223inline flecs::entity world::entity(Args &&... args) const {
224 return flecs::entity(world_, FLECS_FWD(args)...);
225}
226
227template <typename E, if_t< is_enum<E>::value >>
228inline flecs::id world::id(E value) const {
229 flecs::entity_t constant = enum_type<E>(world_).entity(value);
230 return flecs::id(world_, constant);
231}
232
233template <typename E, if_t< is_enum<E>::value >>
234inline flecs::entity world::entity(E value) const {
235 flecs::entity_t constant = enum_type<E>(world_).entity(value);
237}
238
239template <typename T>
240inline flecs::entity world::entity(const char *name) const {
241 return flecs::entity(world_, _::type<T>::register_id(world_, name, true, false) );
242}
243
244}
component< T > & constant(const char *name, T value)
Add a constant.
Definition component.inl:38
#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_component_record_t ecs_component_record_t
Information about a (component) ID, such as type info and tables with the ID.
Definition flecs.h:505
uint64_t ecs_id_t
IDs are the things that can be added to an entity.
Definition flecs.h:386
struct ecs_record_t ecs_record_t
Information about an entity, like its table and row.
Definition flecs.h:502
struct ecs_table_t ecs_table_t
A table stores entities and components for a specific type.
Definition flecs.h:443
E to_constant() const
Convert an entity to an enum constant.
Definition impl.hpp:11
flecs::entity entity(Args &&... args) const
Create an entity.
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.
Component added to enum type entities.
Definition meta.h:289
A type is a list of (component) IDs.
Definition flecs.h:410
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
const T & get() const
Get component value.
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:211
flecs::entity lookup(const char *path, bool search_path=false) const
Lookup an entity by name.
Definition impl.hpp:205
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:112
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
entity()
Default constructor.
Definition entity.hpp:32
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::entity entity() const
Return ID as entity (only allowed when ID is a valid entity).
Definition impl.hpp:10
flecs::world world() const
Get the world.
Definition impl.hpp:58
flecs::id_t id_
The raw ID value.
Definition decl.hpp:149
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:147
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
Component reference.
Definition ref.hpp:109
ref()
Default constructor.
Definition ref.hpp:111
Trait that marks a component as Sparse at compile time.
Definition utils.hpp:187
Table range.
Definition table.hpp:449
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 component reference.
Definition ref.hpp:22
untyped_ref()
Default constructor.
Definition ref.hpp:25
flecs::entity entity() const
Return the entity associated with the reference.
Definition impl.hpp:13
The world.
Definition world.hpp:244
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1423
world_t * c_ptr() const
Obtain a pointer to the C world object.
Definition world.hpp:350
bool is_readonly() const
Test whether the current world object is readonly.
Definition world.hpp:586