Flecs v4.0
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 id_;
52 }
53
58 bool is_valid() const {
59 return world_ && ecs_is_valid(world_, id_);
60 }
61
62 explicit operator bool() const {
63 return is_valid();
64 }
65
70 bool is_alive() const {
71 return world_ && ecs_is_alive(world_, id_);
72 }
73
79 return flecs::string_view(ecs_get_name(world_, id_));
80 }
81
87 return flecs::string_view(ecs_get_symbol(world_, id_));
88 }
89
94 flecs::string path(const char *sep = "::", const char *init_sep = "::") const {
95 return path_from(0, sep, init_sep);
96 }
97
102 flecs::string path_from(flecs::entity_t parent, const char *sep = "::", const char *init_sep = "::") const {
103 char *path = ecs_get_path_w_sep(world_, parent, id_, sep, init_sep);
104 return flecs::string(path);
105 }
106
111 template <typename Parent>
112 flecs::string path_from(const char *sep = "::", const char *init_sep = "::") const {
113 return path_from(_::type<Parent>::id(world_), sep, init_sep);
114 }
115
116 bool enabled() const {
117 return !ecs_has_id(world_, id_, flecs::Disabled);
118 }
119
124 flecs::type type() const;
125
130 flecs::table table() const;
131
140
150 template <typename Func>
151 void each(const Func& func) const;
152
162 template <typename Func>
163 void each(flecs::id_t first, flecs::id_t second, const Func& func) const;
164
175 template <typename Func>
176 void each(const flecs::entity_view& rel, const Func& func) const;
177
188 template <typename First, typename Func>
189 void each(const Func& func) const {
190 return each(_::type<First>::id(world_), func);
191 }
192
203 template <typename Func>
204 void children(flecs::entity_t rel, Func&& func) const {
205 /* When the entity is a wildcard, this would attempt to query for all
206 * entities with (ChildOf, *) or (ChildOf, _) instead of querying for
207 * the children of the wildcard entity. */
208 if (id_ == flecs::Wildcard || id_ == flecs::Any) {
209 /* This is correct, wildcard entities don't have children */
210 return;
211 }
212
213 flecs::world world(world_);
214
215 ecs_iter_t it = ecs_each_id(world_, ecs_pair(rel, id_));
216 while (ecs_each_next(&it)) {
217 _::each_delegate<Func>(FLECS_MOV(func)).invoke(&it);
218 }
219 }
220
231 template <typename Rel, typename Func>
232 void children(Func&& func) const {
233 children(_::type<Rel>::id(world_), FLECS_MOV(func));
234 }
235
247 template <typename Func>
248 void children(Func&& func) const {
249 children(flecs::ChildOf, FLECS_MOV(func));
250 }
251
258 template <typename T, if_t< is_actual<T>::value > = 0>
259 const T* get() const {
260 auto comp_id = _::type<T>::id(world_);
261 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
262 "operation invalid for empty type");
263 return static_cast<const T*>(ecs_get_id(world_, id_, comp_id));
264 }
265
274 template <typename T, typename A = actual_type_t<T>,
275 if_t< flecs::is_pair<T>::value > = 0>
276 const A* get() const {
277 auto comp_id = _::type<T>::id(world_);
278 ecs_assert(_::type<A>::size() != 0, ECS_INVALID_PARAMETER,
279 "operation invalid for empty type");
280 return static_cast<const A*>(ecs_get_id(world_, id_, comp_id));
281 }
282
289 template <typename First, typename Second, typename P = pair<First, Second>,
290 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value > = 0>
291 const A* get() const {
292 return this->get<P>();
293 }
294
301 template<typename First, typename Second, if_not_t< is_enum<Second>::value> = 0>
302 const First* get(Second second) const {
303 auto comp_id = _::type<First>::id(world_);
304 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
305 "operation invalid for empty type");
306 return static_cast<const First*>(
307 ecs_get_id(world_, id_, ecs_pair(comp_id, second)));
308 }
309
316 template<typename First, typename Second, if_t<is_enum<Second>::value> = 0>
317 const First* get(Second constant) const {
318 const auto& et = enum_type<Second>(this->world_);
319 flecs::entity_t target = et.entity(constant);
320 return get<First>(target);
321 }
322
329 const void* get(flecs::id_t comp) const {
330 return ecs_get_id(world_, id_, comp);
331 }
332
341 const void* get(flecs::entity_t first, flecs::entity_t second) const {
342 return ecs_get_id(world_, id_, ecs_pair(first, second));
343 }
344
379 template <typename Func, if_t< is_callable<Func>::value > = 0>
380 bool get(const Func& func) const;
381
387 template <typename T, if_t< is_enum<T>::value > = 0>
388 const T* get() const;
389
397 template<typename Second>
398 const Second* get_second(flecs::entity_t first) const {
399 auto second = _::type<Second>::id(world_);
400 ecs_assert(_::type<Second>::size() != 0, ECS_INVALID_PARAMETER,
401 "operation invalid for empty type");
402 return static_cast<const Second*>(
403 ecs_get_id(world_, id_, ecs_pair(first, second)));
404 }
405
413 template<typename First, typename Second>
414 const Second* get_second() const {
415 return get<pair_object<First, Second>>();
416 }
417
424 template <typename T, if_t< is_actual<T>::value > = 0>
425 T* get_mut() const {
426 auto comp_id = _::type<T>::id(world_);
427 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
428 "operation invalid for empty type");
429 return static_cast<T*>(ecs_get_mut_id(world_, id_, comp_id));
430 }
431
440 template <typename T, typename A = actual_type_t<T>,
441 if_t< flecs::is_pair<T>::value > = 0>
442 A* get_mut() const {
443 auto comp_id = _::type<T>::id(world_);
444 ecs_assert(_::type<A>::size() != 0, ECS_INVALID_PARAMETER,
445 "operation invalid for empty type");
446 return static_cast<A*>(ecs_get_mut_id(world_, id_, comp_id));
447 }
448
455 template <typename First, typename Second, typename P = pair<First, Second>,
456 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value > = 0>
457 A* get_mut() const {
458 return this->get_mut<P>();
459 }
460
467 template<typename First, typename Second, if_not_t< is_enum<Second>::value> = 0>
468 First* get_mut(Second second) const {
469 auto comp_id = _::type<First>::id(world_);
470 ecs_assert(_::type<First>::size() != 0, ECS_INVALID_PARAMETER,
471 "operation invalid for empty type");
472 return static_cast<First*>(
473 ecs_get_mut_id(world_, id_, ecs_pair(comp_id, second)));
474 }
475
482 template<typename First, typename Second, if_t<is_enum<Second>::value> = 0>
483 First* get_mut(Second constant) const {
484 const auto& et = enum_type<Second>(this->world_);
485 flecs::entity_t target = et.entity(constant);
486 return get_mut<First>(target);
487 }
488
495 void* get_mut(flecs::id_t comp) const {
496 return ecs_get_mut_id(world_, id_, comp);
497 }
498
507 void* get_mut(flecs::entity_t first, flecs::entity_t second) const {
508 return ecs_get_mut_id(world_, id_, ecs_pair(first, second));
509 }
510
518 template<typename Second>
519 Second* get_mut_second(flecs::entity_t first) const {
520 auto second = _::type<Second>::id(world_);
521 ecs_assert(_::type<Second>::size() != 0, ECS_INVALID_PARAMETER,
522 "operation invalid for empty type");
523 return static_cast<Second*>(
524 ecs_get_mut_id(world_, id_, ecs_pair(first, second)));
525 }
526
534 template<typename First, typename Second>
535 Second* get_mut_second() const {
536 return get_mut<pair_object<First, Second>>();
537 }
538
547 template<typename First>
548 flecs::entity target(int32_t index = 0) const;
549
558 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
559
578 flecs::entity target_for(flecs::entity_t relationship, flecs::id_t id) const;
579
580 template <typename T>
581 flecs::entity target_for(flecs::entity_t relationship) const;
582
583 template <typename First, typename Second>
584 flecs::entity target_for(flecs::entity_t relationship) const;
585
591 int32_t depth(flecs::entity_t rel) const {
592 return ecs_get_depth(world_, id_, rel);
593 }
594
600 template<typename Rel>
601 int32_t depth() const {
602 return this->depth(_::type<Rel>::id(world_));
603 }
604
610 flecs::entity parent() const;
611
620 flecs::entity lookup(const char *path, bool search_path = false) const;
621
627 bool has(flecs::id_t e) const {
628 return ecs_has_id(world_, id_, e);
629 }
630
636 template <typename T>
637 bool has() const {
638 flecs::id_t cid = _::type<T>::id(world_);
639 bool result = ecs_has_id(world_, id_, cid);
640 if (result) {
641 return result;
642 }
643
644 if (is_enum<T>::value) {
645 return ecs_has_pair(world_, id_, cid, flecs::Wildcard);
646 }
647
648 return false;
649 }
650
657 template <typename E, if_t< is_enum<E>::value > = 0>
658 bool has(E value) const {
659 auto r = _::type<E>::id(world_);
660 auto o = enum_type<E>(world_).entity(value);
661 ecs_assert(o, ECS_INVALID_PARAMETER,
662 "Constant was not found in Enum reflection data."
663 " Did you mean to use has<E>() instead of has(E)?");
664 return ecs_has_pair(world_, id_, r, o);
665 }
666
673 template <typename First, typename Second>
674 bool has() const {
675 return this->has<First>(_::type<Second>::id(world_));
676 }
677
684 template<typename First, typename Second, if_not_t< is_enum<Second>::value > = 0>
685 bool has(Second second) const {
686 auto comp_id = _::type<First>::id(world_);
687 return ecs_has_id(world_, id_, ecs_pair(comp_id, second));
688 }
689
696 template <typename Second>
697 bool has_second(flecs::entity_t first) const {
698 return this->has(first, _::type<Second>::id(world_));
699 }
700
707 template<typename First, typename E, if_t< is_enum<E>::value > = 0>
708 bool has(E value) const {
709 const auto& et = enum_type<E>(this->world_);
710 flecs::entity_t second = et.entity(value);
711 return has<First>(second);
712 }
713
720 bool has(flecs::id_t first, flecs::id_t second) const {
721 return ecs_has_id(world_, id_, ecs_pair(first, second));
722 }
723
730 bool owns(flecs::id_t e) const {
731 return ecs_owns_id(world_, id_, e);
732 }
733
740 template <typename First>
741 bool owns(flecs::id_t second) const {
742 auto comp_id = _::type<First>::id(world_);
743 return owns(ecs_pair(comp_id, second));
744 }
745
752 bool owns(flecs::id_t first, flecs::id_t second) const {
753 return owns(ecs_pair(first, second));
754 }
755
762 template <typename T>
763 bool owns() const {
764 return owns(_::type<T>::id(world_));
765 }
766
774 template <typename First, typename Second>
775 bool owns() const {
776 return owns(
777 _::type<First>::id(world_),
778 _::type<Second>::id(world_));
779 }
780
786 bool enabled(flecs::id_t id) const {
787 return ecs_is_enabled_id(world_, id_, id);
788 }
789
795 template<typename T>
796 bool enabled() const {
797 return this->enabled(_::type<T>::id(world_));
798 }
799
806 bool enabled(flecs::id_t first, flecs::id_t second) const {
807 return this->enabled(ecs_pair(first, second));
808 }
809
816 template <typename First>
817 bool enabled(flecs::id_t second) const {
818 return this->enabled(_::type<First>::id(world_), second);
819 }
820
827 template <typename First, typename Second>
828 bool enabled() const {
829 return this->enabled<First>(_::type<Second>::id(world_));
830 }
831
832 flecs::entity clone(bool clone_value = true, flecs::entity_t dst_id = 0) const;
833
853 flecs::entity mut(const flecs::world& stage) const;
854
862 flecs::entity mut(const flecs::iter& it) const;
863
872 flecs::entity mut(const flecs::entity_view& e) const;
873
874# ifdef FLECS_JSON
876# endif
877# ifdef FLECS_DOC
879# endif
880# ifdef FLECS_ALERTS
882# endif
883
886
887private:
888 flecs::entity set_stage(world_t *stage);
889};
890
891}
892
Alerts entity mixin.
Doc entity view mixin.
Enum entity view mixin.
Event entity mixin.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
bool ecs_each_next(ecs_iter_t *it)
Progress an iterator created with ecs_each_id().
ecs_iter_t ecs_each_id(const ecs_world_t *world, ecs_id_t id)
Iterate all entities with specified (component id).
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 id.
bool ecs_owns_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Test if an entity owns an id.
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 the specified relationship.
void * ecs_get_mut_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get a mutable pointer to a component.
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.
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.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
JSON entity mixin.
Iterator.
Definition flecs.h:1025
flecs::type type() const
Get the entity's type.
Definition impl.hpp:99
A * get_mut() const
Get mutable component value.
const T * get() const
Get component value.
entity_view(entity_t id)
Implicit conversion from flecs::entity_t to flecs::entity_view.
int32_t depth(flecs::entity_t rel) const
Get depth for given relationship.
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.
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 if entity is valid.
flecs::string path(const char *sep="::", const char *init_sep="::") const
Return the entity path.
flecs::string_view symbol() const
Return the entity symbol.
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
void * get_mut(flecs::entity_t first, flecs::entity_t second) const
Get a mutable pair (untyped).
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.
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.
flecs::entity lookup(const char *path, bool search_path=false) const
Lookup an entity by name.
Definition impl.hpp:177
void children(Func &&func) const
Iterate children for entity.
bool owns(flecs::id_t e) const
Check if entity owns the provided entity.
T * get_mut() const
Get mutable component value.
flecs::table table() const
Get the entity's table.
Definition impl.hpp:103
flecs::string path_from(flecs::entity_t parent, const char *sep="::", const char *init_sep="::") const
Return the entity path relative to a parent.
Second * get_mut_second() const
Get the second part for a pair.
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.
void * get_mut(flecs::id_t comp) const
Get mutable component value (untyped).
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 if entity is alive.
First * get_mut(Second constant) const
Get a mutable pair.
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.
First * get_mut(Second second) const
Get a mutable pair.
void children(Func &&func) const
Iterate children for entity.
Second * get_mut_second(flecs::entity_t first) const
Get the second part for a pair.
entity_t id() const
Get entity id.
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.
flecs::string path_from(const char *sep="::", const char *init_sep="::") const
Return the entity path relative to a parent.
bool enabled() const
Test if component is enabled.
void children(flecs::entity_t rel, Func &&func) const
Iterate children for entity.
A * get_mut() const
Get a mutable pair.
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:68
Type class.
Definition type.hpp:21
The world.
Definition world.hpp:137