Flecs v3.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
entity_view.hpp
Go to the documentation of this file.
1
13#pragma once
14
20namespace flecs
21{
22
28struct entity_view : public id {
29
30 entity_view() : flecs::id() { }
31
37 explicit entity_view(flecs::world_t *world, flecs::id_t id)
38 : flecs::id(world
39 ? const_cast<flecs::world_t*>(ecs_get_world(world))
40 : nullptr
41 , id ) { }
42
44 entity_view(entity_t id)
45 : flecs::id( nullptr, id ) { }
46
50 entity_t id() const {
51 return m_id;
52 }
53
58 bool is_valid() const {
59 return m_world && ecs_is_valid(m_world, m_id);
60 }
61
62 explicit operator bool() const {
63 return is_valid();
64 }
65
70 bool is_alive() const {
71 return m_world && ecs_is_alive(m_world, m_id);
72 }
73
79 return flecs::string_view(ecs_get_name(m_world, m_id));
80 }
81
87 return flecs::string_view(ecs_get_symbol(m_world, m_id));
88 }
89
94 flecs::string path(const char *sep = "::", const char *init_sep = "::") const {
95 char *path = ecs_get_path_w_sep(m_world, 0, m_id, sep, init_sep);
96 return flecs::string(path);
97 }
98
99 bool enabled() const {
100 return !ecs_has_id(m_world, m_id, flecs::Disabled);
101 }
102
107 flecs::type type() const;
108
113 flecs::table table() const;
114
123
130 template <typename Func>
131 void each(const Func& func) const;
132
139 template <typename Func>
140 void each(flecs::id_t first, flecs::id_t second, const Func& func) const;
141
149 template <typename Func>
150 void each(const flecs::entity_view& rel, const Func& func) const;
151
159 template <typename First, typename Func>
160 void each(const Func& func) const {
161 return each(_::cpp_type<First>::id(m_world), func);
162 }
163
171 template <typename Func>
172 void children(flecs::entity_t rel, Func&& func) const {
173 flecs::world world(m_world);
174
175 ecs_term_t terms[2];
177 f.terms = terms;
178 f.term_count = 2;
179
180 ecs_filter_desc_t desc = {};
181 desc.terms[0].id = ecs_pair(rel, m_id);
182 desc.terms[1].id = flecs::Prefab;
183 desc.terms[1].oper = EcsOptional;
184 desc.storage = &f;
185 ecs_filter_init(m_world, &desc);
186
187 ecs_iter_t it = ecs_filter_iter(m_world, &f);
188 while (ecs_filter_next(&it)) {
189 _::each_invoker<Func>(FLECS_MOV(func)).invoke(&it);
190 }
191
192 ecs_filter_fini(&f);
193 }
194
202 template <typename Rel, typename Func>
203 void children(Func&& func) const {
204 children(_::cpp_type<Rel>::id(m_world), FLECS_MOV(func));
205 }
206
215 template <typename Func>
216 void children(Func&& func) const {
217 children(flecs::ChildOf, FLECS_MOV(func));
218 }
219
226 template <typename T, if_t< is_actual<T>::value > = 0>
227 const T* get() const {
228 auto comp_id = _::cpp_type<T>::id(m_world);
229 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
230 return static_cast<const T*>(ecs_get_id(m_world, m_id, comp_id));
231 }
232
241 template <typename T, typename A = actual_type_t<T>,
242 if_t< flecs::is_pair<T>::value > = 0>
243 const A* get() const {
244 auto comp_id = _::cpp_type<T>::id(m_world);
245 ecs_assert(_::cpp_type<A>::size() != 0, ECS_INVALID_PARAMETER, NULL);
246 return static_cast<const A*>(ecs_get_id(m_world, m_id, comp_id));
247 }
248
255 template <typename First, typename Second, typename P = pair<First, Second>,
256 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value > = 0>
257 const A* get() const {
258 return this->get<P>();
259 }
260
267 template<typename First, typename Second, if_not_t< is_enum<Second>::value> = 0>
268 const First* get(Second second) const {
269 auto comp_id = _::cpp_type<First>::id(m_world);
270 ecs_assert(_::cpp_type<First>::size() != 0, ECS_INVALID_PARAMETER, NULL);
271 return static_cast<const First*>(
272 ecs_get_id(m_world, m_id, ecs_pair(comp_id, second)));
273 }
274
281 template<typename First, typename Second, if_t<is_enum<Second>::value> = 0>
282 const First* get(Second constant) const {
283 const auto& et = enum_type<Second>(this->m_world);
284 flecs::entity_t target = et.entity(constant);
285 return get<First>(target);
286 }
287
294 const void* get(flecs::id_t comp) const {
295 return ecs_get_id(m_world, m_id, comp);
296 }
297
306 const void* get(flecs::entity_t first, flecs::entity_t second) const {
307 return ecs_get_id(m_world, m_id, ecs_pair(first, second));
308 }
309
341 template <typename Func, if_t< is_callable<Func>::value > = 0>
342 bool get(const Func& func) const;
343
349 template <typename T, if_t< is_enum<T>::value > = 0>
350 const T* get() const;
351
359 template<typename Second>
360 const Second* get_second(flecs::entity_t first) const {
361 auto second = _::cpp_type<Second>::id(m_world);
362 ecs_assert(_::cpp_type<Second>::size() != 0, ECS_INVALID_PARAMETER, NULL);
363 return static_cast<const Second*>(
364 ecs_get_id(m_world, m_id, ecs_pair(first, second)));
365 }
366
374 template<typename First, typename Second>
375 const Second* get_second() const {
376 return get<pair_object<First, Second>>();
377 }
378
387 template<typename First>
388 flecs::entity target(int32_t index = 0) const;
389
398 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
399
416 flecs::entity target_for(flecs::entity_t relationship, flecs::id_t id) const;
417
418 template <typename T>
419 flecs::entity target_for(flecs::entity_t relationship) const;
420
421 template <typename First, typename Second>
422 flecs::entity target_for(flecs::entity_t relationship) const;
423
429 int32_t depth(flecs::entity_t rel) const {
430 return ecs_get_depth(m_world, m_id, rel);
431 }
432
438 template<typename Rel>
439 int32_t depth() const {
440 return this->depth(_::cpp_type<Rel>::id(m_world));
441 }
442
448 flecs::entity parent() const;
449
457 flecs::entity lookup(const char *path) const;
458
464 bool has(flecs::id_t e) const {
465 return ecs_has_id(m_world, m_id, e);
466 }
467
473 template <typename T>
474 bool has() const {
475 flecs::id_t cid = _::cpp_type<T>::id(m_world);
476 bool result = ecs_has_id(m_world, m_id, cid);
477 if (result) {
478 return result;
479 }
480
481 if (is_enum<T>::value) {
482 return ecs_has_pair(m_world, m_id, cid, flecs::Wildcard);
483 }
484
485 return false;
486 }
487
494 template <typename E, if_t< is_enum<E>::value > = 0>
495 bool has(E value) const {
496 auto r = _::cpp_type<E>::id(m_world);
497 auto o = enum_type<E>(m_world).entity(value);
498 return ecs_has_pair(m_world, m_id, r, o);
499 }
500
507 template <typename First, typename Second>
508 bool has() const {
509 return this->has<First>(_::cpp_type<Second>::id(m_world));
510 }
511
518 template<typename First, typename Second, if_not_t< is_enum<Second>::value > = 0>
519 bool has(Second second) const {
520 auto comp_id = _::cpp_type<First>::id(m_world);
521 return ecs_has_id(m_world, m_id, ecs_pair(comp_id, second));
522 }
523
530 template <typename Second>
531 bool has_second(flecs::entity_t first) const {
532 return this->has(first, _::cpp_type<Second>::id(m_world));
533 }
534
541 template<typename First, typename E, if_t< is_enum<E>::value > = 0>
542 bool has(E value) const {
543 const auto& et = enum_type<E>(this->m_world);
544 flecs::entity_t second = et.entity(value);
545 return has<First>(second);
546 }
547
554 bool has(flecs::id_t first, flecs::id_t second) const {
555 return ecs_has_id(m_world, m_id, ecs_pair(first, second));
556 }
557
564 bool owns(flecs::id_t e) const {
565 return ecs_owns_id(m_world, m_id, e);
566 }
567
574 template <typename First>
575 bool owns(flecs::id_t second) const {
576 auto comp_id = _::cpp_type<First>::id(m_world);
577 return owns(ecs_pair(comp_id, second));
578 }
579
586 bool owns(flecs::id_t first, flecs::id_t second) const {
587 return owns(ecs_pair(first, second));
588 }
589
596 template <typename T>
597 bool owns() const {
598 return owns(_::cpp_type<T>::id(m_world));
599 }
600
608 template <typename First, typename Second>
609 bool owns() const {
610 return owns(
611 _::cpp_type<First>::id(m_world),
612 _::cpp_type<Second>::id(m_world));
613 }
614
620 bool enabled(flecs::id_t id) const {
621 return ecs_is_enabled_id(m_world, m_id, id);
622 }
623
629 template<typename T>
630 bool enabled() const {
631 return this->enabled(_::cpp_type<T>::id(m_world));
632 }
633
640 bool enabled(flecs::id_t first, flecs::id_t second) const {
641 return this->enabled(ecs_pair(first, second));
642 }
643
650 template <typename First>
651 bool enabled(flecs::id_t second) const {
652 return this->enabled(_::cpp_type<First>::id(m_world), second);
653 }
654
661 template <typename First, typename Second>
662 bool enabled() const {
663 return this->enabled<First>(_::cpp_type<Second>::id(m_world));
664 }
665
673 const ecs_world_info_t *stats = ecs_get_world_info(m_world);
674 return stats->delta_time;
675 }
676
677 flecs::entity clone(bool clone_value = true, flecs::entity_t dst_id = 0) const;
678
698 flecs::entity mut(const flecs::world& stage) const;
699
707 flecs::entity mut(const flecs::iter& it) const;
708
717 flecs::entity mut(const flecs::entity_view& e) const;
718
719# ifdef FLECS_JSON
721# endif
722# ifdef FLECS_DOC
724# endif
725
727
728private:
729 flecs::entity set_stage(world_t *stage);
730};
731
732}
733
Doc entity view mixin.
Enum entity view mixin.
#define ecs_assert(condition, error_code,...)
Assert.
Definition: log.h:352
bool ecs_is_enabled_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Test if component is enabled.
bool ecs_has_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Test if an entity has an entity.
int32_t ecs_get_depth(const ecs_world_t *world, ecs_entity_t entity, ecs_entity_t rel)
Return depth for entity in tree for relationship rel.
ecs_filter_t * ecs_filter_init(ecs_world_t *world, const ecs_filter_desc_t *desc)
Initialize filter A filter is a lightweight object that can be used to query for entities in a world.
bool ecs_filter_next(ecs_iter_t *it)
Iterate tables matched by filter.
ecs_iter_t ecs_filter_iter(const ecs_world_t *world, const ecs_filter_t *filter)
Return a filter iterator.
void ecs_filter_fini(ecs_filter_t *filter)
Deinitialize filter.
const void * ecs_get_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get an immutable pointer to a component.
bool ecs_is_valid(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is valid.
bool ecs_is_alive(const ecs_world_t *world, ecs_entity_t e)
Test whether an entity is alive.
#define ecs_ftime_t
Customizable precision for scalar time values.
Definition: flecs.h:42
char * ecs_get_path_w_sep(const ecs_world_t *world, ecs_entity_t parent, ecs_entity_t child, const char *sep, const char *prefix)
Get a path identifier for an entity.
const char * ecs_get_symbol(const ecs_world_t *world, ecs_entity_t entity)
Get the symbol of an entity.
const char * ecs_get_name(const ecs_world_t *world, ecs_entity_t entity)
Get the name of an entity.
ecs_filter_t ECS_FILTER_INIT
Use this variable to initialize user-allocated filter object.
@ EcsOptional
The term may match.
Definition: flecs.h:488
const ecs_world_info_t * ecs_get_world_info(const ecs_world_t *world)
Get world info.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
JSON entity mixin.
Used with ecs_filter_init.
Definition: flecs.h:770
ecs_term_t terms[(16)]
Terms of the filter.
Definition: flecs.h:775
ecs_filter_t * storage
External storage to prevent allocation of the filter object.
Definition: flecs.h:784
Filters alllow for ad-hoc quick filtering of entity tables.
Definition: flecs.h:555
ecs_term_t * terms
Array containing terms for filter.
Definition: flecs.h:558
int32_t term_count
Number of elements in terms array.
Definition: flecs.h:559
Type that describes a term (single element in a query)
Definition: flecs.h:529
ecs_id_t id
Component id to be matched by term.
Definition: flecs.h:530
ecs_oper_kind_t oper
Operator of term.
Definition: flecs.h:540
Type that contains information about the world.
Definition: flecs.h:928
float delta_time
Time passed to or computed by ecs_progress.
Definition: flecs.h:935
Entity view.
Definition: entity_view.hpp:28
flecs::type type() const
Get the entity's type.
Definition: impl.hpp:99
const T * get() const
Get component value.
entity_view(entity_t id)
Implicit conversion from flecs::entity_t to flecs::entity_view.
Definition: entity_view.hpp:44
int32_t depth(flecs::entity_t rel) const
Get depth for given relationship.
flecs::entity lookup(const char *path) const
Lookup an entity by name.
Definition: impl.hpp:184
bool enabled() const
Test if pair is enabled.
bool has(flecs::id_t first, flecs::id_t second) const
Check if entity has the provided pair.
const First * get(Second second) const
Get a pair.
flecs::string_view name() const
Return the entity name.
Definition: entity_view.hpp:78
bool owns(flecs::id_t second) const
Check if entity owns the provided pair.
const Second * get_second(flecs::entity_t first) const
Get the second part for a pair.
const Second * get_second() const
Get the second part for a pair.
bool is_valid() const
Check is entity is valid.
Definition: entity_view.hpp:58
flecs::string path(const char *sep="::", const char *init_sep="::") const
Return the entity path.
Definition: entity_view.hpp:94
flecs::string_view symbol() const
Return the entity symbol.
Definition: entity_view.hpp:86
void each(const Func &func) const
Iterate targets for a given relationship.
flecs::table_range range() const
Get table range for the entity.
Definition: impl.hpp:107
bool owns() const
Check if entity owns the provided pair.
bool has(E value) const
Check if entity has the provided pair.
int32_t depth() const
Get depth for given relationship.
entity_view(flecs::world_t *world, flecs::id_t id)
Wrap an existing entity id.
Definition: entity_view.hpp:37
bool enabled(flecs::id_t id) const
Test if id is enabled.
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:55
bool has_second(flecs::entity_t first) const
Check if entity has the provided pair.
const A * get() const
Get component value.
void children(Func &&func) const
Iterate children for entity.
bool owns(flecs::id_t e) const
Check if entity owns the provided entity.
flecs::table table() const
Get the entity's table.
Definition: impl.hpp:103
bool enabled(flecs::id_t second) const
Test if pair is enabled.
bool has(Second second) const
Check if entity has the provided pair.
bool has(flecs::id_t e) const
Check if entity has the provided entity.
bool owns() const
Check if entity owns the provided component.
flecs::entity parent() const
Get parent of entity.
Definition: impl.hpp:73
const First * get(Second constant) const
Get a pair.
ecs_ftime_t delta_time() const
Get current delta time.
const A * get() const
Get a pair.
void each(const Func &func) const
Iterate (component) ids of an entity.
Definition: impl.hpp:117
const void * get(flecs::entity_t first, flecs::entity_t second) const
Get a pair (untyped).
bool is_alive() const
Check is entity is alive.
Definition: entity_view.hpp:70
bool has() const
Check if entity has the provided component.
bool owns(flecs::id_t first, flecs::id_t second) const
Check if entity owns the provided pair.
void children(Func &&func) const
Iterate children for entity.
entity_t id() const
Get entity id.
Definition: entity_view.hpp:50
flecs::entity target(int32_t index=0) const
Get target for a given pair.
Definition: impl.hpp:41
const void * get(flecs::id_t comp) const
Get component value (untyped).
bool has() const
Check if entity has the provided pair.
bool enabled() const
Test if component is enabled.
void children(flecs::entity_t rel, Func &&func) const
Iterate children for entity.
flecs::entity mut(const flecs::world &stage) const
Return mutable entity handle for current stage When an entity handle created from the world is used w...
Definition: impl.hpp:77
bool has(E value) const
Check if entity has the provided enum constant.
bool enabled(flecs::id_t first, flecs::id_t second) const
Test if pair is enabled.
Entity.
Definition: entity.hpp:30
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
Class for iterating over query results.
Definition: iter.hpp:169
Type class.
Definition: type.hpp:21
The world.
Definition: world.hpp:113