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{
33
40 world_ = const_cast<flecs::world_t*>(world);
41 id_ = id;
42 }
43
48 explicit entity(world_t *world)
50 {
51 world_ = world;
52 id_ = ecs_cpp_new(world, 0, nullptr, nullptr, nullptr);
53 }
54
62 explicit entity(
63 world_t *world,
64 const char *name,
65 const char *sep = "::",
66 const char *root_sep = "::") : entity_builder()
67 {
68 world_ = world;
69
70 ecs_entity_desc_t desc = {};
71 desc.name = name;
72 desc.sep = sep;
73 desc.root_sep = root_sep;
74 id_ = ecs_entity_init(world, &desc);
75 }
76
85 explicit entity(
88 const char *name,
89 const char *sep = "::",
90 const char *root_sep = "::") : entity_builder()
91 {
92 world_ = world;
93
94 ecs_entity_desc_t desc = {};
95 desc.name = name;
96 desc.parent = parent;
97 desc.sep = sep;
98 desc.root_sep = root_sep;
99 id_ = ecs_entity_init(world, &desc);
100 }
101
111 explicit entity(
112 world_t *world,
113 const flecs::Parent& parent,
114 const char *name = nullptr) : entity_builder()
115 {
116 world_ = world;
118 }
119
124 explicit entity(entity_t id)
125 : entity_builder( nullptr, id ) { }
126
127 #ifndef ensure
128
138 template <typename T>
139 T& ensure() const {
140 auto comp_id = _::type<T>::id(world_);
142 "operation invalid for empty type");
143 return *static_cast<T*>(ecs_ensure_id(world_, id_, comp_id, sizeof(T)));
144 }
145
155 void* ensure(entity_t comp) const {
157 ecs_assert(ti != nullptr && ti->size != 0, ECS_INVALID_PARAMETER,
158 "provided component is not a type or has size 0");
159 return ecs_ensure_id(world_, id_, comp, static_cast<size_t>(ti->size));
160 }
161
169 template <typename First, typename Second, typename P = pair<First, Second>,
170 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
171 A& ensure() const {
172 return *static_cast<A*>(ecs_ensure_id(world_, id_, ecs_pair(
174 _::type<Second>::id(world_)), sizeof(A)));
175 }
176
184 template <typename First>
185 First& ensure(entity_t second) const {
188 "operation invalid for empty type");
189 return *static_cast<First*>(
190 ecs_ensure_id(world_, id_, ecs_pair(first, second), sizeof(First)));
191 }
192
203 return ensure(ecs_pair(first, second));
204 }
205
213 template <typename Second>
214 Second& ensure_second(entity_t first) const {
216 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
217 ECS_INVALID_PARAMETER, "pair is not a component");
219 ECS_INVALID_PARAMETER, "type of pair is not Second");
221 "operation invalid for empty type");
222 return *static_cast<Second*>(
223 ecs_ensure_id(world_, id_, ecs_pair(first, second), sizeof(Second)));
224 }
225
226 #endif
227
232 template <typename T>
233 void modified() const {
234 auto comp_id = _::type<T>::id(world_);
236 "operation invalid for empty type");
237 this->modified(comp_id);
238 }
239
245 template <typename First, typename Second, typename A = actual_type_t<flecs::pair<First, Second>>>
246 void modified() const {
250 "operation invalid for empty type");
251 this->modified(first, second);
252 }
253
259 template <typename First>
263 "operation invalid for empty type");
264 this->modified(first, second);
265 }
266
275 this->modified(ecs_pair(first, second));
276 }
277
282 void modified(entity_t comp) const {
283 ecs_modified_id(world_, id_, comp);
284 }
285
305 template <typename T, if_t< is_actual<T>::value > = 0>
307 _::type<T>::id(world_); // Ensure type is registered.
308 return ref<T>(world_, id_, component);
309 }
310
318 template <typename T, if_t< is_actual<T>::value > = 0>
319 ref<T> get_ref() const {
321 }
322
332 template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
338
339
346 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
347 typename A = actual_type_t<P>>
348 ref<A> get_ref() const {
349 return ref<A>(world_, id_,
351 }
352
359 template <typename First>
364
373
383
390 template <typename Second>
393 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
394 ECS_INVALID_PARAMETER, "pair is not a component");
396 ECS_INVALID_PARAMETER, "type of pair is not Second");
397 return ref<Second>(world_, id_, ecs_pair(first, second));
398 }
399
406 void clear() const {
408 }
409
416 void destruct() const {
418 }
419
426 template <typename ... Args>
427 flecs::entity child(flecs::entity_t r = flecs::ChildOf, Args&&... args) {
429 return world.entity(FLECS_FWD(args)...).add(r, id_);
430 }
431
438 template <typename R, typename ... Args>
439 flecs::entity child(Args&&... args) {
441 return world.entity(FLECS_FWD(args)...).add(_::type<R>::id(world_), id_);
442 }
443
453 void set_child_order(flecs::entity_t *children, int32_t child_count) const {
454 ecs_set_child_order(world_, id_, children, child_count);
455 }
456
468 return flecs::entity_view(
469 const_cast<flecs::world_t*>(ecs_get_world(world_)), id_);
470 }
471
479 static
481 flecs::entity result;
482 result.world_ = const_cast<flecs::world_t*>(world);
483 return result;
484 }
485
490 static
492 return flecs::entity();
493 }
494
495# ifdef FLECS_JSON
496# include "mixins/json/entity.inl"
497# endif
498};
499
500} // namespace flecs
501
JSON entity mixin.
Entity class with only read-only 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:473
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:661
const ecs_type_info_t * ecs_get_type_info(const ecs_world_t *world, ecs_id_t component)
Get the type info for a component.
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
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 an entity has a component and return a pointer.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
Entity builder.
Non-fragmenting ChildOf relationship.
Definition flecs.h:1586
Used with ecs_entity_init().
Definition flecs.h:1042
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1054
const char * root_sep
Optional, used for identifiers relative to the root.
Definition flecs.h:1058
const char * name
Name of the entity.
Definition flecs.h:1049
ecs_entity_t parent
Parent entity.
Definition flecs.h:1047
Type that contains component information (passed to ctors/dtors/...).
Definition flecs.h:1019
ecs_size_t size
Size of the type.
Definition flecs.h:1020
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 an entity.
Entity.
Definition entity.hpp:30
void destruct() const
Delete an entity.
Definition entity.hpp:416
static flecs::entity null()
Entity ID 0 without a world.
Definition entity.hpp:491
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:319
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition entity.hpp:260
void modified() const
Signal that the first element of a pair was modified.
Definition entity.hpp:246
First & ensure(entity_t second) const
Get mutable reference for the first element of a pair.
Definition entity.hpp:185
void modified(entity_t comp) const
Signal that component was modified.
Definition entity.hpp:282
ref< Second > get_ref_second(flecs::entity_t first) const
Get reference to the second element of a pair.
Definition entity.hpp:391
void clear() const
Clear an entity.
Definition entity.hpp:406
ref< First > get_ref(flecs::entity_t second) const
Get reference to the first element of a pair.
Definition entity.hpp:360
untyped_ref get_ref(flecs::id_t component) const
Get untyped reference to component by component ID.
Definition entity.hpp:370
ref< T > get_ref_w_id(flecs::id_t component) const
Get reference to component specified by component ID.
Definition entity.hpp:306
void modified(entity_t first, entity_t second) const
Signal that a pair has been modified (untyped).
Definition entity.hpp:274
ref< A > get_ref() const
Get reference to pair component.
Definition entity.hpp:348
void * ensure(entity_t first, entity_t second) const
Get mutable pointer for a pair (untyped).
Definition entity.hpp:202
entity(const flecs::world_t *world, flecs::entity_t id)
Wrap an existing entity ID.
Definition entity.hpp:39
void modified() const
Signal that component was modified.
Definition entity.hpp:233
T & ensure() const
Get mutable component value.
Definition entity.hpp:139
static flecs::entity null(const flecs::world_t *world)
Entity ID 0.
Definition entity.hpp:480
entity(world_t *world, flecs::entity_t parent, const char *name, const char *sep="::", const char *root_sep="::")
Create a named entity for a parent using ChildOf hierarchy storage.
Definition entity.hpp:85
flecs::entity child(Args &&... args)
Create a child entity with a typed relationship.
Definition entity.hpp:439
Second & ensure_second(entity_t first) const
Get mutable reference for the second element of a pair.
Definition entity.hpp:214
void * ensure(entity_t comp) const
Get mutable component value (untyped).
Definition entity.hpp:155
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition entity.hpp:124
entity(world_t *world, const char *name, const char *sep="::", const char *root_sep="::")
Create a named entity.
Definition entity.hpp:62
entity(world_t *world)
Create a new entity.
Definition entity.hpp:48
flecs::entity_view view() const
Return the entity as an entity_view.
Definition entity.hpp:467
void set_child_order(flecs::entity_t *children, int32_t child_count) const
Set child order.
Definition entity.hpp:453
entity()
Default constructor.
Definition entity.hpp:32
untyped_ref get_ref(flecs::id_t first, flecs::id_t second) const
Get untyped reference to pair by first and second entity IDs.
Definition entity.hpp:380
ref< A > get_ref() const
Get reference to component.
Definition entity.hpp:333
A & ensure() const
Get mutable reference for a pair.
Definition entity.hpp:171
entity(world_t *world, const flecs::Parent &parent, const char *name=nullptr)
Create a named entity for a parent using Parent hierarchy storage.
Definition entity.hpp:111
flecs::entity child(flecs::entity_t r=flecs::ChildOf, Args &&... args)
Create a child entity with a specified relationship.
Definition entity.hpp:427
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
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
flecs::entity first() const
Get first element from a pair.
Definition impl.hpp:20
Component reference.
Definition ref.hpp:108
Untyped component reference.
Definition ref.hpp:22
The world.
Definition world.hpp:246