Flecs: include/flecs/addons/cpp/entity.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| 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
7
8#pragma once
9
10#include "entity_view.hpp"
11#include "mixins/entity/builder.hpp"
12
15
20
21namespace flecs
22{
23
29 struct entity : entity_builder<entity>
30{
32 entity() : entity_builder<entity>() { }
33
39 explicit entity(const flecs::world_t *world, flecs::entity_t id) {
40 world_ = const_cast<flecs::world_t*>(world);
41 id_ = id;
42 }
43
48 explicit entity(world_t *world)
49 : entity_builder()
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(
86 world_t *world,
87 flecs::entity_t parent,
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;
117 id_ = ecs_new_w_parent(world, parent.value, name);
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_);
141 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
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 {
156 const flecs::type_info_t *ti = ecs_get_type_info(world_, comp);
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(
173 _::type<First>::id(world_),
174 _::type<Second>::id(world_)), sizeof(A)));
175 }
176
184 template <typename First>
185 First& ensure(entity_t second) const {
186 auto first = _::type<First>::id(world_);
187 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
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
202 void* ensure(entity_t first, entity_t second) const {
203 return ensure(ecs_pair(first, second));
204 }
205
213 template <typename Second>
214 Second& ensure_second(entity_t first) const {
215 auto second = _::type<Second>::id(world_);
216 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
217 ECS_INVALID_PARAMETER, "pair is not a component");
218 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
219 ECS_INVALID_PARAMETER, "type of pair is not Second");
220 ecs_assert(_::type<Second>::size() != 0, ECS_INVALID_PARAMETER,
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_);
235 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
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 {
247 auto first = _::type<First>::id(world_);
248 auto second = _::type<Second>::id(world_);
249 ecs_assert(_::type<A>::size() != 0, ECS_INVALID_PARAMETER,
250 "operation invalid for empty type");
251 this->modified(first, second);
252 }
253
259 template <typename First>
260 void modified(entity_t second) const {
261 auto first = _::type<First>::id(world_);
262 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
263 "operation invalid for empty type");
264 this->modified(first, second);
265 }
266
274 void modified(entity_t first, entity_t second) const {
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>
306 ref<T> get_ref_w_id(flecs::id_t component) const {
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 {
320 return ref<T>(world_, id_, _::type<T>::id(world_));
321 }
322
332 template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
333 ref<A> get_ref() const {
334 return ref<A>(world_, id_,
335 ecs_pair(_::type<typename T::first>::id(world_),
336 _::type<typename T::second>::id(world_)));
337 }
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_,
350 ecs_pair(_::type<First>::id(world_), _::type<Second>::id(world_)));
351 }
352
359 template <typename First>
360 ref<First> get_ref(flecs::entity_t second) const {
361 auto first = _::type<First>::id(world_);
362 return ref<First>(world_, id_, ecs_pair(first, second));
363 }
364
370 untyped_ref get_ref(flecs::id_t component) const {
371 return untyped_ref(world_, id_, component);
372 }
373
380 untyped_ref get_ref(flecs::id_t first, flecs::id_t second) const {
381 return untyped_ref(world_, id_, ecs_pair(first, second));
382 }
383
390 template <typename Second>
391 ref<Second> get_ref_second(flecs::entity_t first) const {
392 auto second = _::type<Second>::id(world_);
393 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
394 ECS_INVALID_PARAMETER, "pair is not a component");
395 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
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 {
407 ecs_clear(world_, id_);
408 }
409
416 void destruct() const {
417 ecs_delete(world_, id_);
418 }
419
426 template <typename ... Args>
427 flecs::entity child(flecs::entity_t r = flecs::ChildOf, Args&&... args) {
428 flecs::world world(world_);
429 return world.entity(FLECS_FWD(args)...).add(r, id_);
430 }
431
438 template <typename R, typename ... Args>
439 flecs::entity child(Args&&... args) {
440 flecs::world world(world_);
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
467 flecs::entity_view view() const {
468 return flecs::entity_view(
469 const_cast<flecs::world_t*>(ecs_get_world(world_)), id_);
470 }
471
479 static
480 flecs::entity null(const flecs::world_t *world) {
481 flecs::entity result;
482 result.world_ = const_cast<flecs::world_t*>(world);
483 return result;
484 }
485
490 static
491 flecs::entity null() {
492 return flecs::entity();
493 }
494
495# ifdef FLECS_JSON
496# include "mixins/json/entity.inl"
497# endif
498};
499
500} // namespace flecs
501
entity.inl
JSON entity mixin.
entity_view.hpp
Entity class with only read-only operations.
ecs_clear
void ecs_clear(ecs_world_t *world, ecs_entity_t entity)
Clear all components.
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
ECS_INVALID_PARAMETER
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:661
ecs_get_type_info
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::world::entity
flecs::entity entity(Args &&... args) const
Create an entity.
Definition impl.hpp:190
flecs::id_t
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
flecs::Parent
EcsParent Parent
Built-in EcsParent type.
Definition c_types.hpp:85
flecs::world_t
ecs_world_t world_t
World type.
Definition c_types.hpp:18
flecs::type_info_t
ecs_type_info_t type_info_t
Type info type.
Definition c_types.hpp:33
ecs_set_child_order
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_init
ecs_entity_t ecs_entity_init(ecs_world_t *world, const ecs_entity_desc_t *desc)
Find or create an entity.
ecs_delete
void ecs_delete(ecs_world_t *world, ecs_entity_t entity)
Delete an entity.
ecs_new_w_parent
ecs_entity_t ecs_new_w_parent(ecs_world_t *world, ecs_entity_t parent, const char *name)
Create child with Parent component.
ecs_modified_id
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
ecs_ensure_id
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.
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
builder.hpp
Entity builder.
ecs_entity_desc_t
Used with ecs_entity_init().
Definition flecs.h:1042
ecs_entity_desc_t::sep
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1054
ecs_entity_desc_t::root_sep
const char * root_sep
Optional, used for identifiers relative to the root.
Definition flecs.h:1058
ecs_entity_desc_t::name
const char * name
Name of the entity.
Definition flecs.h:1049
ecs_entity_desc_t::parent
ecs_entity_t parent
Parent entity.
Definition flecs.h:1047
ecs_type_info_t::size
ecs_size_t size
Size of the type.
Definition flecs.h:1020
flecs::_::type
Definition flecs.hpp:39
flecs::component
Component class.
Definition component.hpp:394
flecs::entity_builder
Entity builder.
Definition builder.hpp:15
flecs::entity_builder::add
const Self & add() const
Add a component to an entity.
Definition builder.hpp:25
flecs::entity_view
Entity view.
Definition entity_view.hpp:30
flecs::entity_view::name
flecs::string_view name() const
Return the entity name.
Definition entity_view.hpp:88
flecs::entity_view::parent
flecs::entity parent() const
Get parent of entity.
Definition impl.hpp:68
flecs::entity_view::children
void children(flecs::entity_t rel, Func &&func) const
Iterate children for an entity.
Definition entity_view.hpp:228
flecs::entity
Entity.
Definition entity.hpp:30
flecs::entity::destruct
void destruct() const
Delete an entity.
Definition entity.hpp:416
flecs::entity::null
static flecs::entity null()
Entity ID 0 without a world.
Definition entity.hpp:491
flecs::entity::get_ref
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:319
flecs::entity::modified
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition entity.hpp:260
flecs::entity::modified
void modified() const
Signal that the first element of a pair was modified.
Definition entity.hpp:246
flecs::entity::ensure
First & ensure(entity_t second) const
Get mutable reference for the first element of a pair.
Definition entity.hpp:185
flecs::entity::modified
void modified(entity_t comp) const
Signal that component was modified.
Definition entity.hpp:282
flecs::entity::get_ref_second
ref< Second > get_ref_second(flecs::entity_t first) const
Get reference to the second element of a pair.
Definition entity.hpp:391
flecs::entity::clear
void clear() const
Clear an entity.
Definition entity.hpp:406
flecs::entity::get_ref
ref< First > get_ref(flecs::entity_t second) const
Get reference to the first element of a pair.
Definition entity.hpp:360
flecs::entity::get_ref
untyped_ref get_ref(flecs::id_t component) const
Get untyped reference to component by component ID.
Definition entity.hpp:370
flecs::entity::get_ref_w_id
ref< T > get_ref_w_id(flecs::id_t component) const
Get reference to component specified by component ID.
Definition entity.hpp:306
flecs::entity::modified
void modified(entity_t first, entity_t second) const
Signal that a pair has been modified (untyped).
Definition entity.hpp:274
flecs::entity::get_ref
ref< A > get_ref() const
Get reference to pair component.
Definition entity.hpp:348
flecs::entity::ensure
void * ensure(entity_t first, entity_t second) const
Get mutable pointer for a pair (untyped).
Definition entity.hpp:202
flecs::entity::entity
entity(const flecs::world_t *world, flecs::entity_t id)
Wrap an existing entity ID.
Definition entity.hpp:39
flecs::entity::modified
void modified() const
Signal that component was modified.
Definition entity.hpp:233
flecs::entity::ensure
T & ensure() const
Get mutable component value.
Definition entity.hpp:139
flecs::entity::null
static flecs::entity null(const flecs::world_t *world)
Entity ID 0.
Definition entity.hpp:480
flecs::entity::entity
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
flecs::entity child(Args &&... args)
Create a child entity with a typed relationship.
Definition entity.hpp:439
flecs::entity::ensure_second
Second & ensure_second(entity_t first) const
Get mutable reference for the second element of a pair.
Definition entity.hpp:214
flecs::entity::ensure
void * ensure(entity_t comp) const
Get mutable component value (untyped).
Definition entity.hpp:155
flecs::entity::entity
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition entity.hpp:124
flecs::entity::entity
entity(world_t *world, const char *name, const char *sep="::", const char *root_sep="::")
Create a named entity.
Definition entity.hpp:62
flecs::entity::entity
entity(world_t *world)
Create a new entity.
Definition entity.hpp:48
flecs::entity::view
flecs::entity_view view() const
Return the entity as an entity_view.
Definition entity.hpp:467
flecs::entity::set_child_order
void set_child_order(flecs::entity_t *children, int32_t child_count) const
Set child order.
Definition entity.hpp:453
flecs::entity::entity
entity()
Default constructor.
Definition entity.hpp:32
flecs::entity::get_ref
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
flecs::entity::get_ref
ref< A > get_ref() const
Get reference to component.
Definition entity.hpp:333
flecs::entity::ensure
A & ensure() const
Get mutable reference for a pair.
Definition entity.hpp:171
flecs::entity::entity
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 child(flecs::entity_t r=flecs::ChildOf, Args &&... args)
Create a child entity with a specified relationship.
Definition entity.hpp:427
flecs::id
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::id::world
flecs::world world() const
Get the world.
Definition impl.hpp:58
flecs::id::id_
flecs::id_t id_
The raw ID value.
Definition decl.hpp:149
flecs::id::second
flecs::entity second() const
Get second element from a pair.
Definition impl.hpp:31
flecs::id::world_
flecs::world_t * world_
World is optional, but guarantees that entity identifiers extracted from the ID are valid.
Definition decl.hpp:147
flecs::id::first
flecs::entity first() const
Get first element from a pair.
Definition impl.hpp:20
flecs::ref
Component reference.
Definition ref.hpp:108
flecs::untyped_ref
Untyped component reference.
Definition ref.hpp:22
flecs::world
The world.
Definition world.hpp:246