Flecs v3.2
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 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(m_world, parent, m_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(_::cpp_type<Parent>::id(m_world), sep, init_sep);
114 }
115
116 bool enabled() const {
117 return !ecs_has_id(m_world, m_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(_::cpp_type<First>::id(m_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 (m_id == flecs::Wildcard || m_id == flecs::Any) {
209 /* This is correct, wildcard entities don't have children */
210 return;
211 }
212
213 ecs_iter_t it = flecs_children(m_world, rel, m_id);
214 while (ecs_children_next(&it)) {
215 _::each_delegate<Func>(FLECS_MOV(func)).invoke(&it);
216 }
217 }
218
229 template <typename Rel, typename Func>
230 void children(Func&& func) const {
231 children(_::cpp_type<Rel>::id(m_world), FLECS_MOV(func));
232 }
233
245 template <typename Func>
246 void children(Func&& func) const {
247 children(flecs::ChildOf, FLECS_MOV(func));
248 }
249
256 template <typename T, if_t< is_actual<T>::value > = 0>
257 const T* get() const {
258 auto comp_id = _::cpp_type<T>::id(m_world);
259 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
260 return static_cast<const T*>(ecs_get_id(m_world, m_id, comp_id));
261 }
262
271 template <typename T, typename A = actual_type_t<T>,
272 if_t< flecs::is_pair<T>::value > = 0>
273 const A* get() const {
274 auto comp_id = _::cpp_type<T>::id(m_world);
275 ecs_assert(_::cpp_type<A>::size() != 0, ECS_INVALID_PARAMETER, NULL);
276 return static_cast<const A*>(ecs_get_id(m_world, m_id, comp_id));
277 }
278
285 template <typename First, typename Second, typename P = pair<First, Second>,
286 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value > = 0>
287 const A* get() const {
288 return this->get<P>();
289 }
290
297 template<typename First, typename Second, if_not_t< is_enum<Second>::value> = 0>
298 const First* get(Second second) const {
299 auto comp_id = _::cpp_type<First>::id(m_world);
300 ecs_assert(_::cpp_type<First>::size() != 0, ECS_INVALID_PARAMETER, NULL);
301 return static_cast<const First*>(
302 ecs_get_id(m_world, m_id, ecs_pair(comp_id, second)));
303 }
304
311 template<typename First, typename Second, if_t<is_enum<Second>::value> = 0>
312 const First* get(Second constant) const {
313 const auto& et = enum_type<Second>(this->m_world);
314 flecs::entity_t target = et.entity(constant);
315 return get<First>(target);
316 }
317
324 const void* get(flecs::id_t comp) const {
325 return ecs_get_id(m_world, m_id, comp);
326 }
327
336 const void* get(flecs::entity_t first, flecs::entity_t second) const {
337 return ecs_get_id(m_world, m_id, ecs_pair(first, second));
338 }
339
374 template <typename Func, if_t< is_callable<Func>::value > = 0>
375 bool get(const Func& func) const;
376
382 template <typename T, if_t< is_enum<T>::value > = 0>
383 const T* get() const;
384
392 template<typename Second>
393 const Second* get_second(flecs::entity_t first) const {
394 auto second = _::cpp_type<Second>::id(m_world);
395 ecs_assert(_::cpp_type<Second>::size() != 0, ECS_INVALID_PARAMETER, NULL);
396 return static_cast<const Second*>(
397 ecs_get_id(m_world, m_id, ecs_pair(first, second)));
398 }
399
407 template<typename First, typename Second>
408 const Second* get_second() const {
409 return get<pair_object<First, Second>>();
410 }
411
418 template <typename T, if_t< is_actual<T>::value > = 0>
419 T* get_mut() const {
420 auto comp_id = _::cpp_type<T>::id(m_world);
421 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
422 return static_cast<T*>(ecs_get_mut_id(m_world, m_id, comp_id));
423 }
424
433 template <typename T, typename A = actual_type_t<T>,
434 if_t< flecs::is_pair<T>::value > = 0>
435 A* get_mut() const {
436 auto comp_id = _::cpp_type<T>::id(m_world);
437 ecs_assert(_::cpp_type<A>::size() != 0, ECS_INVALID_PARAMETER, NULL);
438 return static_cast<A*>(ecs_get_mut_id(m_world, m_id, comp_id));
439 }
440
447 template <typename First, typename Second, typename P = pair<First, Second>,
448 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value > = 0>
449 A* get_mut() const {
450 return this->get_mut<P>();
451 }
452
459 template<typename First, typename Second, if_not_t< is_enum<Second>::value> = 0>
460 First* get_mut(Second second) const {
461 auto comp_id = _::cpp_type<First>::id(m_world);
462 ecs_assert(_::cpp_type<First>::size() != 0, ECS_INVALID_PARAMETER, NULL);
463 return static_cast<First*>(
464 ecs_get_mut_id(m_world, m_id, ecs_pair(comp_id, second)));
465 }
466
473 template<typename First, typename Second, if_t<is_enum<Second>::value> = 0>
474 First* get_mut(Second constant) const {
475 const auto& et = enum_type<Second>(this->m_world);
476 flecs::entity_t target = et.entity(constant);
477 return get_mut<First>(target);
478 }
479
486 void* get_mut(flecs::id_t comp) const {
487 return ecs_get_mut_id(m_world, m_id, comp);
488 }
489
498 void* get_mut(flecs::entity_t first, flecs::entity_t second) const {
499 return ecs_get_mut_id(m_world, m_id, ecs_pair(first, second));
500 }
501
509 template<typename Second>
510 Second* get_mut_second(flecs::entity_t first) const {
511 auto second = _::cpp_type<Second>::id(m_world);
512 ecs_assert(_::cpp_type<Second>::size() != 0, ECS_INVALID_PARAMETER, NULL);
513 return static_cast<Second*>(
514 ecs_get_mut_id(m_world, m_id, ecs_pair(first, second)));
515 }
516
524 template<typename First, typename Second>
525 Second* get_mut_second() const {
526 return get_mut<pair_object<First, Second>>();
527 }
528
537 template<typename First>
538 flecs::entity target(int32_t index = 0) const;
539
548 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
549
568 flecs::entity target_for(flecs::entity_t relationship, flecs::id_t id) const;
569
570 template <typename T>
571 flecs::entity target_for(flecs::entity_t relationship) const;
572
573 template <typename First, typename Second>
574 flecs::entity target_for(flecs::entity_t relationship) const;
575
581 int32_t depth(flecs::entity_t rel) const {
582 return ecs_get_depth(m_world, m_id, rel);
583 }
584
590 template<typename Rel>
591 int32_t depth() const {
592 return this->depth(_::cpp_type<Rel>::id(m_world));
593 }
594
600 flecs::entity parent() const;
601
610 flecs::entity lookup(const char *path, bool search_path = false) const;
611
617 bool has(flecs::id_t e) const {
618 return ecs_has_id(m_world, m_id, e);
619 }
620
626 template <typename T>
627 bool has() const {
628 flecs::id_t cid = _::cpp_type<T>::id(m_world);
629 bool result = ecs_has_id(m_world, m_id, cid);
630 if (result) {
631 return result;
632 }
633
634 if (is_enum<T>::value) {
635 return ecs_has_pair(m_world, m_id, cid, flecs::Wildcard);
636 }
637
638 return false;
639 }
640
647 template <typename E, if_t< is_enum<E>::value > = 0>
648 bool has(E value) const {
649 auto r = _::cpp_type<E>::id(m_world);
650 auto o = enum_type<E>(m_world).entity(value);
651 ecs_assert(o, ECS_INVALID_PARAMETER,
652 "Constant was not found in Enum reflection data."
653 " Did you mean to use has<E>() instead of has(E)?");
654 return ecs_has_pair(m_world, m_id, r, o);
655 }
656
663 template <typename First, typename Second>
664 bool has() const {
665 return this->has<First>(_::cpp_type<Second>::id(m_world));
666 }
667
674 template<typename First, typename Second, if_not_t< is_enum<Second>::value > = 0>
675 bool has(Second second) const {
676 auto comp_id = _::cpp_type<First>::id(m_world);
677 return ecs_has_id(m_world, m_id, ecs_pair(comp_id, second));
678 }
679
686 template <typename Second>
687 bool has_second(flecs::entity_t first) const {
688 return this->has(first, _::cpp_type<Second>::id(m_world));
689 }
690
697 template<typename First, typename E, if_t< is_enum<E>::value > = 0>
698 bool has(E value) const {
699 const auto& et = enum_type<E>(this->m_world);
700 flecs::entity_t second = et.entity(value);
701 return has<First>(second);
702 }
703
710 bool has(flecs::id_t first, flecs::id_t second) const {
711 return ecs_has_id(m_world, m_id, ecs_pair(first, second));
712 }
713
720 bool owns(flecs::id_t e) const {
721 return ecs_owns_id(m_world, m_id, e);
722 }
723
730 template <typename First>
731 bool owns(flecs::id_t second) const {
732 auto comp_id = _::cpp_type<First>::id(m_world);
733 return owns(ecs_pair(comp_id, second));
734 }
735
742 bool owns(flecs::id_t first, flecs::id_t second) const {
743 return owns(ecs_pair(first, second));
744 }
745
752 template <typename T>
753 bool owns() const {
754 return owns(_::cpp_type<T>::id(m_world));
755 }
756
764 template <typename First, typename Second>
765 bool owns() const {
766 return owns(
767 _::cpp_type<First>::id(m_world),
768 _::cpp_type<Second>::id(m_world));
769 }
770
776 bool enabled(flecs::id_t id) const {
777 return ecs_is_enabled_id(m_world, m_id, id);
778 }
779
785 template<typename T>
786 bool enabled() const {
787 return this->enabled(_::cpp_type<T>::id(m_world));
788 }
789
796 bool enabled(flecs::id_t first, flecs::id_t second) const {
797 return this->enabled(ecs_pair(first, second));
798 }
799
806 template <typename First>
807 bool enabled(flecs::id_t second) const {
808 return this->enabled(_::cpp_type<First>::id(m_world), second);
809 }
810
817 template <typename First, typename Second>
818 bool enabled() const {
819 return this->enabled<First>(_::cpp_type<Second>::id(m_world));
820 }
821
822 flecs::entity clone(bool clone_value = true, flecs::entity_t dst_id = 0) const;
823
843 flecs::entity mut(const flecs::world& stage) const;
844
852 flecs::entity mut(const flecs::iter& it) const;
853
862 flecs::entity mut(const flecs::entity_view& e) const;
863
864# ifdef FLECS_JSON
866# endif
867# ifdef FLECS_DOC
869# endif
870# ifdef FLECS_ALERTS
872# endif
873
876
877private:
878 flecs::entity set_stage(world_t *stage);
879};
880
881}
882
Alerts entity mixin.
Doc entity view mixin.
Enum entity view mixin.
Event entity mixin.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:351
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.
bool ecs_children_next(ecs_iter_t *it)
Progress a children iterator.
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.
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:184
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:132