Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
entity.hpp
Go to the documentation of this file.
1
8#pragma once
9
10#include "entity_view.hpp"
12
21namespace flecs
22{
23
29struct entity : entity_builder<entity>
30{
32
38 explicit entity(const flecs::world_t *world, flecs::entity_t id) {
39 world_ = const_cast<flecs::world_t*>(world);
40 id_ = id;
41 }
42
47 explicit entity(world_t *world)
49 {
50 world_ = world;
51 id_ = ecs_cpp_new(world, 0, nullptr, nullptr, nullptr);
52 }
53
61 explicit entity(
62 world_t *world,
63 const char *name,
64 const char *sep = "::",
65 const char *root_sep = "::") : entity_builder()
66 {
67 world_ = world;
68
69 ecs_entity_desc_t desc = {};
70 desc.name = name;
71 desc.sep = sep;
72 desc.root_sep = root_sep;
73 id_ = ecs_entity_init(world, &desc);
74 }
75
83 explicit entity(
84 world_t *world,
85 flecs::entity_t parent,
86 const char *name,
87 const char *sep = "::",
88 const char *root_sep = "::") : entity_builder()
89 {
90 world_ = world;
91
92 ecs_entity_desc_t desc = {};
93 desc.name = name;
94 desc.parent = parent;
95 desc.sep = sep;
96 desc.root_sep = root_sep;
97 id_ = ecs_entity_init(world, &desc);
98 }
99
108 explicit entity(
109 world_t *world,
110 const flecs::Parent& parent,
111 const char *name = nullptr) : entity_builder()
112 {
113 world_ = world;
114 id_ = ecs_new_w_parent(world, parent.value, name);
115 }
116
121 explicit entity(entity_t id)
122 : entity_builder( nullptr, id ) { }
123
124 #ifndef ensure
125
135 template <typename T>
136 T& ensure() const {
137 auto comp_id = _::type<T>::id(world_);
138 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
139 "operation invalid for empty type");
140 return *static_cast<T*>(ecs_ensure_id(world_, id_, comp_id, sizeof(T)));
141 }
142
152 void* ensure(entity_t comp) const {
153 const flecs::type_info_t *ti = ecs_get_type_info(world_, comp);
154 ecs_assert(ti != nullptr && ti->size != 0, ECS_INVALID_PARAMETER,
155 "provided component is not a type or has size 0");
156 return ecs_ensure_id(world_, id_, comp, static_cast<size_t>(ti->size));
157 }
158
165 template <typename First, typename Second, typename P = pair<First, Second>,
166 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
167 A& ensure() const {
168 return *static_cast<A*>(ecs_ensure_id(world_, id_, ecs_pair(
169 _::type<First>::id(world_),
170 _::type<Second>::id(world_)), sizeof(A)));
171 }
172
179 template <typename First>
180 First& ensure(entity_t second) const {
181 auto first = _::type<First>::id(world_);
182 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
183 "operation invalid for empty type");
184 return *static_cast<First*>(
185 ecs_ensure_id(world_, id_, ecs_pair(first, second), sizeof(First)));
186 }
187
196 void* ensure(entity_t first, entity_t second) const {
197 return ensure(ecs_pair(first, second));
198 }
199
206 template <typename Second>
207 Second& ensure_second(entity_t first) const {
208 auto second = _::type<Second>::id(world_);
209 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
210 ECS_INVALID_PARAMETER, "pair is not a component");
211 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
212 ECS_INVALID_PARAMETER, "type of pair is not Second");
213 ecs_assert(_::type<Second>::size() != 0, ECS_INVALID_PARAMETER,
214 "operation invalid for empty type");
215 return *static_cast<Second*>(
216 ecs_ensure_id(world_, id_, ecs_pair(first, second), sizeof(Second)));
217 }
218
219 #endif
220
225 template <typename T>
226 void modified() const {
227 auto comp_id = _::type<T>::id(world_);
228 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
229 "operation invalid for empty type");
230 this->modified(comp_id);
231 }
232
238 template <typename First, typename Second, typename A = actual_type_t<flecs::pair<First, Second>>>
239 void modified() const {
240 auto first = _::type<First>::id(world_);
241 auto second = _::type<Second>::id(world_);
242 ecs_assert(_::type<A>::size() != 0, ECS_INVALID_PARAMETER,
243 "operation invalid for empty type");
244 this->modified(first, second);
245 }
246
252 template <typename First>
253 void modified(entity_t second) const {
254 auto first = _::type<First>::id(world_);
255 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
256 "operation invalid for empty type");
257 this->modified(first, second);
258 }
259
267 void modified(entity_t first, entity_t second) const {
268 this->modified(ecs_pair(first, second));
269 }
270
275 void modified(entity_t comp) const {
276 ecs_modified_id(world_, id_, comp);
277 }
278
297 template <typename T, if_t< is_actual<T>::value > = 0>
298 ref<T> get_ref_w_id(flecs::id_t component) const {
299 _::type<T>::id(world_); // ensure type is registered
300 return ref<T>(world_, id_, component);
301 }
302
310 template <typename T, if_t< is_actual<T>::value > = 0>
311 ref<T> get_ref() const {
312 return ref<T>(world_, id_, _::type<T>::id(world_));
313 }
314
324 template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
325 ref<A> get_ref() const {
326 return ref<A>(world_, id_,
327 ecs_pair(_::type<typename T::first>::id(world_),
329 }
330
331
332 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
333 typename A = actual_type_t<P>>
334 ref<A> get_ref() const {
335 return ref<A>(world_, id_,
336 ecs_pair(_::type<First>::id(world_), _::type<Second>::id(world_)));
337 }
338
339 template <typename First>
340 ref<First> get_ref(flecs::entity_t second) const {
341 auto first = _::type<First>::id(world_);
342 return ref<First>(world_, id_, ecs_pair(first, second));
343 }
344
345 untyped_ref get_ref(flecs::id_t component) const {
346 return untyped_ref(world_, id_, component);
347 }
348
349 untyped_ref get_ref(flecs::id_t first, flecs::id_t second) const {
350 return untyped_ref(world_, id_, ecs_pair(first, second));
351 }
352
353 template <typename Second>
354 ref<Second> get_ref_second(flecs::entity_t first) const {
355 auto second = _::type<Second>::id(world_);
356 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
357 ECS_INVALID_PARAMETER, "pair is not a component");
358 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
359 ECS_INVALID_PARAMETER, "type of pair is not Second");
360 return ref<Second>(world_, id_, ecs_pair(first, second));
361 }
362
369 void clear() const {
370 ecs_clear(world_, id_);
371 }
372
379 void destruct() const {
380 ecs_delete(world_, id_);
381 }
382
384 template <typename ... Args>
385 flecs::entity child(flecs::entity_t r = flecs::ChildOf, Args&&... args) {
386 flecs::world world(world_);
387 return world.entity(FLECS_FWD(args)...).add(r, id_);
388 }
389
390 template <typename R, typename ... Args>
391 flecs::entity child(Args&&... args) {
392 flecs::world world(world_);
393 return world.entity(FLECS_FWD(args)...).add(_::type<R>::id(world_), id_);
394 }
395
402 void set_child_order(flecs::entity_t *children, int32_t child_count) const {
403 ecs_set_child_order(world_, id_, children, child_count);
404 }
405
415 return flecs::entity_view(
416 const_cast<flecs::world_t*>(ecs_get_world(world_)), id_);
417 }
418
425 static
426 flecs::entity null(const flecs::world_t *world) {
427 flecs::entity result;
428 result.world_ = const_cast<flecs::world_t*>(world);
429 return result;
430 }
431
432 static
433 flecs::entity null() {
434 return flecs::entity();
435 }
436
437# ifdef FLECS_JSON
438# include "mixins/json/entity.inl"
439# endif
440};
441
442} // namespace flecs
443
Entity class with only readonly operations.
void ecs_clear(ecs_world_t *world, ecs_entity_t entity)
Clear all components.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
const ecs_type_info_t * ecs_get_type_info(const ecs_world_t *world, ecs_id_t component)
Get the type info for an component.
flecs::entity entity(Args &&... args) const
Create an entity.
void ecs_set_child_order(ecs_world_t *world, ecs_entity_t parent, const ecs_entity_t *children, int32_t child_count)
Set child order for parent with OrderedChildren.
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
void ecs_delete(ecs_world_t *world, ecs_entity_t entity)
Delete an entity.
ecs_entity_t ecs_new_w_parent(ecs_world_t *world, ecs_entity_t parent, const char *name)
Create child with Parent component.
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
void * ecs_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size)
Ensure entity has component, return pointer.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
Entity builder.
Used with ecs_entity_init().
Definition flecs.h:1038
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1050
const char * root_sep
Optional, used for identifiers relative to root.
Definition flecs.h:1054
const char * name
Name of the entity.
Definition flecs.h:1045
ecs_entity_t parent
Parent entity.
Definition flecs.h:1043
Type that contains component information (passed to ctors/dtors/...)
Definition flecs.h:1015
ecs_size_t size
Size of type.
Definition flecs.h:1016
Component class.
Entity builder.
Definition builder.hpp:15
const Self & add() const
Add a component to an entity.
Definition builder.hpp:25
flecs::string_view name() const
Return the entity name.
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:68
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
void destruct() const
Delete an entity.
Definition entity.hpp:379
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:311
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition entity.hpp:253
void modified() const
Signal that the first element of a pair was modified.
Definition entity.hpp:239
First & ensure(entity_t second) const
Get mutable pointer for the first element of a pair.
Definition entity.hpp:180
void modified(entity_t comp) const
Signal that component was modified.
Definition entity.hpp:275
void clear() const
Clear an entity.
Definition entity.hpp:369
ref< T > get_ref_w_id(flecs::id_t component) const
Get reference to component specified by id.
Definition entity.hpp:298
void modified(entity_t first, entity_t second) const
Signal that a pair has modified (untyped).
Definition entity.hpp:267
void * ensure(entity_t first, entity_t second) const
Get mutable pointer for a pair (untyped).
Definition entity.hpp:196
entity(const flecs::world_t *world, flecs::entity_t id)
Wrap an existing entity id.
Definition entity.hpp:38
void modified() const
Signal that component was modified.
Definition entity.hpp:226
T & ensure() const
Get mutable component value.
Definition entity.hpp:136
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:426
entity(world_t *world, flecs::entity_t parent, const char *name, const char *sep="::", const char *root_sep="::")
Create a named entity for parent using ChildOf hierarchy storage.
Definition entity.hpp:83
Second & ensure_second(entity_t first) const
Get mutable pointer for the second element of a pair.
Definition entity.hpp:207
void * ensure(entity_t comp) const
Get mutable component value (untyped).
Definition entity.hpp:152
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition entity.hpp:121
entity(world_t *world, const char *name, const char *sep="::", const char *root_sep="::")
Create a named entity.
Definition entity.hpp:61
entity(world_t *world)
Create a new entity.
Definition entity.hpp:47
flecs::entity_view view() const
Return entity as entity_view.
Definition entity.hpp:414
void set_child_order(flecs::entity_t *children, int32_t child_count) const
Set child order.
Definition entity.hpp:402
ref< A > get_ref() const
Get reference to component.
Definition entity.hpp:325
A & ensure() const
Get mutable pointer for a pair.
Definition entity.hpp:167
entity(world_t *world, const flecs::Parent &parent, const char *name=nullptr)
Create a named entity for parent using Parent hierarchy storage.
Definition entity.hpp:108
flecs::entity child(flecs::entity_t r=flecs::ChildOf, Args &&... args)
Create child.
Definition entity.hpp:385
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::entity second() const
Get second element from a pair.
Definition impl.hpp:31
flecs::entity first() const
Get first element from a pair.
Definition impl.hpp:20
Component reference.
Definition ref.hpp:89
The world.
Definition world.hpp:174