Flecs v4.0
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
37 explicit entity(world_t *world)
39 {
40 world_ = world;
41 if (!ecs_get_scope(world_) && !ecs_get_with(world_)) {
42 id_ = ecs_new(world);
43 } else {
44 ecs_entity_desc_t desc = {};
45 id_ = ecs_entity_init(world_, &desc);
46 }
47 }
48
54 explicit entity(const flecs::world_t *world, flecs::entity_t id) {
55 world_ = const_cast<flecs::world_t*>(world);
56 id_ = id;
57 }
58
68 explicit entity(world_t *world, const char *name)
70 {
71 world_ = world;
72
73 ecs_entity_desc_t desc = {};
74 desc.name = name;
75 desc.sep = "::";
76 desc.root_sep = "::";
77 id_ = ecs_entity_init(world, &desc);
78 }
79
91 explicit entity(world_t *world, const char *name, const char *sep, const char *root_sep)
93 {
94 world_ = world;
95
96 ecs_entity_desc_t desc = {};
97 desc.name = name;
98 desc.sep = sep;
99 desc.root_sep = root_sep;
100 id_ = ecs_entity_init(world, &desc);
101 }
102
107 explicit entity(entity_t id)
108 : entity_builder( nullptr, id ) { }
109
110 #ifndef ensure
111
121 template <typename T>
122 T& ensure() const {
123 auto comp_id = _::type<T>::id(world_);
124 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
125 "operation invalid for empty type");
126 return *static_cast<T*>(ecs_ensure_id(world_, id_, comp_id, sizeof(T)));
127 }
128
138 void* ensure(entity_t comp) const {
139 const flecs::type_info_t *ti = ecs_get_type_info(world_, comp);
140 ecs_assert(ti != nullptr && ti->size != 0, ECS_INVALID_PARAMETER,
141 "provided component is not a type or has size 0");
142 return ecs_ensure_id(world_, id_, comp, static_cast<size_t>(ti->size));
143 }
144
151 template <typename First, typename Second, typename P = pair<First, Second>,
152 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
153 A& ensure() const {
154 return *static_cast<A*>(ecs_ensure_id(world_, id_, ecs_pair(
155 _::type<First>::id(world_),
156 _::type<Second>::id(world_)), sizeof(A)));
157 }
158
165 template <typename First>
166 First& ensure(entity_t second) const {
167 auto first = _::type<First>::id(world_);
168 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
169 "operation invalid for empty type");
170 return *static_cast<First*>(
171 ecs_ensure_id(world_, id_, ecs_pair(first, second), sizeof(First)));
172 }
173
182 void* ensure(entity_t first, entity_t second) const {
183 return ensure(ecs_pair(first, second));
184 }
185
192 template <typename Second>
193 Second& ensure_second(entity_t first) const {
194 auto second = _::type<Second>::id(world_);
195 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
196 ECS_INVALID_PARAMETER, "pair is not a component");
197 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
198 ECS_INVALID_PARAMETER, "type of pair is not Second");
199 ecs_assert(_::type<Second>::size() != 0, ECS_INVALID_PARAMETER,
200 "operation invalid for empty type");
201 return *static_cast<Second*>(
202 ecs_ensure_id(world_, id_, ecs_pair(first, second), sizeof(Second)));
203 }
204
205 #endif
206
211 template <typename T>
212 void modified() const {
213 auto comp_id = _::type<T>::id(world_);
214 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
215 "operation invalid for empty type");
216 this->modified(comp_id);
217 }
218
224 template <typename First, typename Second, typename A = actual_type_t<flecs::pair<First, Second>>>
225 void modified() const {
226 auto first = _::type<First>::id(world_);
227 auto second = _::type<Second>::id(world_);
228 ecs_assert(_::type<A>::size() != 0, ECS_INVALID_PARAMETER,
229 "operation invalid for empty type");
230 this->modified(first, second);
231 }
232
238 template <typename First>
239 void modified(entity_t second) const {
240 auto first = _::type<First>::id(world_);
241 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
242 "operation invalid for empty type");
243 this->modified(first, second);
244 }
245
253 void modified(entity_t first, entity_t second) const {
254 this->modified(ecs_pair(first, second));
255 }
256
261 void modified(entity_t comp) const {
262 ecs_modified_id(world_, id_, comp);
263 }
264
283 template <typename T, if_t< is_actual<T>::value > = 0>
284 ref<T> get_ref_w_id(flecs::id_t component) const {
285 _::type<T>::id(world_); // ensure type is registered
286 return ref<T>(world_, id_, component);
287 }
288
296 template <typename T, if_t< is_actual<T>::value > = 0>
297 ref<T> get_ref() const {
298 return ref<T>(world_, id_, _::type<T>::id(world_));
299 }
300
310 template <typename T, typename A = actual_type_t<T>, if_t< flecs::is_pair<T>::value > = 0>
311 ref<A> get_ref() const {
312 return ref<A>(world_, id_,
313 ecs_pair(_::type<typename T::first>::id(world_),
315 }
316
317
318 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
319 typename A = actual_type_t<P>>
320 ref<A> get_ref() const {
321 return ref<A>(world_, id_,
322 ecs_pair(_::type<First>::id(world_), _::type<Second>::id(world_)));
323 }
324
325 template <typename First>
326 ref<First> get_ref(flecs::entity_t second) const {
327 auto first = _::type<First>::id(world_);
328 return ref<First>(world_, id_, ecs_pair(first, second));
329 }
330
331 untyped_ref get_ref(flecs::id_t component) const {
332 return untyped_ref(world_, id_, component);
333 }
334
335 untyped_ref get_ref(flecs::id_t first, flecs::id_t second) const {
336 return untyped_ref(world_, id_, ecs_pair(first, second));
337 }
338
339 template <typename Second>
340 ref<Second> get_ref_second(flecs::entity_t first) const {
341 auto second = _::type<Second>::id(world_);
342 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second)) != NULL,
343 ECS_INVALID_PARAMETER, "pair is not a component");
344 ecs_assert( ecs_get_type_info(world_, ecs_pair(first, second))->component == second,
345 ECS_INVALID_PARAMETER, "type of pair is not Second");
346 return ref<Second>(world_, id_, ecs_pair(first, second));
347 }
348
355 void clear() const {
356 ecs_clear(world_, id_);
357 }
358
365 void destruct() const {
366 ecs_delete(world_, id_);
367 }
368
370 template <typename ... Args>
371 flecs::entity child(flecs::entity_t r = flecs::ChildOf, Args&&... args) {
372 flecs::world world(world_);
373 return world.entity(FLECS_FWD(args)...).add(r, id_);
374 }
375
376 template <typename R, typename ... Args>
377 flecs::entity child(Args&&... args) {
378 flecs::world world(world_);
379 return world.entity(FLECS_FWD(args)...).add(_::type<R>::id(world_), id_);
380 }
381
388 void set_child_order(flecs::entity_t *children, int32_t child_count) const {
389 ecs_set_child_order(world_, id_, children, child_count);
390 }
391
401 return flecs::entity_view(
402 const_cast<flecs::world_t*>(ecs_get_world(world_)), id_);
403 }
404
411 static
412 flecs::entity null(const flecs::world_t *world) {
413 flecs::entity result;
414 result.world_ = const_cast<flecs::world_t*>(world);
415 return result;
416 }
417
418 static
419 flecs::entity null() {
420 return flecs::entity();
421 }
422
423# ifdef FLECS_JSON
424# include "mixins/json/entity.inl"
425# endif
426};
427
428} // namespace flecs
429
Entity class with only readonly operations.
void ecs_clear(ecs_world_t *world, ecs_entity_t entity)
Clear all components.
ecs_id_t ecs_get_with(const ecs_world_t *world)
Get component set with ecs_set_with().
#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_new(ecs_world_t *world)
Create new entity id.
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.
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.
ecs_entity_t ecs_get_scope(const ecs_world_t *world)
Get the current scope.
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:1023
const char * sep
Optional custom separator for hierarchical names.
Definition flecs.h:1035
const char * root_sep
Optional, used for identifiers relative to root.
Definition flecs.h:1039
const char * name
Name of the entity.
Definition flecs.h:1030
Type that contains component information (passed to ctors/dtors/...)
Definition flecs.h:1000
ecs_size_t size
Size of type.
Definition flecs.h:1001
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.
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:365
ref< T > get_ref() const
Get reference to component.
Definition entity.hpp:297
void modified(entity_t second) const
Signal that the first part of a pair was modified.
Definition entity.hpp:239
void modified() const
Signal that the first element of a pair was modified.
Definition entity.hpp:225
First & ensure(entity_t second) const
Get mutable pointer for the first element of a pair.
Definition entity.hpp:166
void modified(entity_t comp) const
Signal that component was modified.
Definition entity.hpp:261
void clear() const
Clear an entity.
Definition entity.hpp:355
ref< T > get_ref_w_id(flecs::id_t component) const
Get reference to component specified by id.
Definition entity.hpp:284
void modified(entity_t first, entity_t second) const
Signal that a pair has modified (untyped).
Definition entity.hpp:253
entity(world_t *world, const char *name)
Create a named entity.
Definition entity.hpp:68
void * ensure(entity_t first, entity_t second) const
Get mutable pointer for a pair (untyped).
Definition entity.hpp:182
entity(const flecs::world_t *world, flecs::entity_t id)
Wrap an existing entity id.
Definition entity.hpp:54
void modified() const
Signal that component was modified.
Definition entity.hpp:212
T & ensure() const
Get mutable component value.
Definition entity.hpp:122
static flecs::entity null(const flecs::world_t *world)
Entity id 0.
Definition entity.hpp:412
Second & ensure_second(entity_t first) const
Get mutable pointer for the second element of a pair.
Definition entity.hpp:193
void * ensure(entity_t comp) const
Get mutable component value (untyped).
Definition entity.hpp:138
entity(entity_t id)
Conversion from flecs::entity_t to flecs::entity.
Definition entity.hpp:107
entity(world_t *world, const char *name, const char *sep, const char *root_sep)
Create a named entity.
Definition entity.hpp:91
entity(world_t *world)
Create entity.
Definition entity.hpp:37
flecs::entity_view view() const
Return entity as entity_view.
Definition entity.hpp:400
void set_child_order(flecs::entity_t *children, int32_t child_count) const
Set child order.
Definition entity.hpp:388
ref< A > get_ref() const
Get reference to component.
Definition entity.hpp:311
A & ensure() const
Get mutable pointer for a pair.
Definition entity.hpp:153
flecs::entity child(flecs::entity_t r=flecs::ChildOf, Args &&... args)
Create child.
Definition entity.hpp:371
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:85
The world.
Definition world.hpp:150