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 flecs::world world(m_world);
214
215 ecs_term_t terms[2];
217 f.terms = terms;
218 f.term_count = 2;
219
220 ecs_filter_desc_t desc = {};
221 desc.terms[0].first.id = rel;
222 desc.terms[0].second.id = m_id;
223 desc.terms[0].second.flags = EcsIsEntity;
224 desc.terms[1].id = flecs::Prefab;
225 desc.terms[1].oper = EcsOptional;
226 desc.storage = &f;
227 if (ecs_filter_init(m_world, &desc) != nullptr) {
228 ecs_iter_t it = ecs_filter_iter(m_world, &f);
229 while (ecs_filter_next(&it)) {
230 _::each_delegate<Func>(FLECS_MOV(func)).invoke(&it);
231 }
232
233 ecs_filter_fini(&f);
234 }
235 }
236
247 template <typename Rel, typename Func>
248 void children(Func&& func) const {
249 children(_::cpp_type<Rel>::id(m_world), FLECS_MOV(func));
250 }
251
263 template <typename Func>
264 void children(Func&& func) const {
265 children(flecs::ChildOf, FLECS_MOV(func));
266 }
267
274 template <typename T, if_t< is_actual<T>::value > = 0>
275 const T* get() const {
276 auto comp_id = _::cpp_type<T>::id(m_world);
277 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
278 return static_cast<const T*>(ecs_get_id(m_world, m_id, comp_id));
279 }
280
289 template <typename T, typename A = actual_type_t<T>,
290 if_t< flecs::is_pair<T>::value > = 0>
291 const A* get() const {
292 auto comp_id = _::cpp_type<T>::id(m_world);
293 ecs_assert(_::cpp_type<A>::size() != 0, ECS_INVALID_PARAMETER, NULL);
294 return static_cast<const A*>(ecs_get_id(m_world, m_id, comp_id));
295 }
296
303 template <typename First, typename Second, typename P = pair<First, Second>,
304 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value > = 0>
305 const A* get() const {
306 return this->get<P>();
307 }
308
315 template<typename First, typename Second, if_not_t< is_enum<Second>::value> = 0>
316 const First* get(Second second) const {
317 auto comp_id = _::cpp_type<First>::id(m_world);
318 ecs_assert(_::cpp_type<First>::size() != 0, ECS_INVALID_PARAMETER, NULL);
319 return static_cast<const First*>(
320 ecs_get_id(m_world, m_id, ecs_pair(comp_id, second)));
321 }
322
329 template<typename First, typename Second, if_t<is_enum<Second>::value> = 0>
330 const First* get(Second constant) const {
331 const auto& et = enum_type<Second>(this->m_world);
332 flecs::entity_t target = et.entity(constant);
333 return get<First>(target);
334 }
335
342 const void* get(flecs::id_t comp) const {
343 return ecs_get_id(m_world, m_id, comp);
344 }
345
354 const void* get(flecs::entity_t first, flecs::entity_t second) const {
355 return ecs_get_id(m_world, m_id, ecs_pair(first, second));
356 }
357
392 template <typename Func, if_t< is_callable<Func>::value > = 0>
393 bool get(const Func& func) const;
394
400 template <typename T, if_t< is_enum<T>::value > = 0>
401 const T* get() const;
402
410 template<typename Second>
411 const Second* get_second(flecs::entity_t first) const {
412 auto second = _::cpp_type<Second>::id(m_world);
413 ecs_assert(_::cpp_type<Second>::size() != 0, ECS_INVALID_PARAMETER, NULL);
414 return static_cast<const Second*>(
415 ecs_get_id(m_world, m_id, ecs_pair(first, second)));
416 }
417
425 template<typename First, typename Second>
426 const Second* get_second() const {
427 return get<pair_object<First, Second>>();
428 }
429
438 template<typename First>
439 flecs::entity target(int32_t index = 0) const;
440
449 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
450
469 flecs::entity target_for(flecs::entity_t relationship, flecs::id_t id) const;
470
471 template <typename T>
472 flecs::entity target_for(flecs::entity_t relationship) const;
473
474 template <typename First, typename Second>
475 flecs::entity target_for(flecs::entity_t relationship) const;
476
482 int32_t depth(flecs::entity_t rel) const {
483 return ecs_get_depth(m_world, m_id, rel);
484 }
485
491 template<typename Rel>
492 int32_t depth() const {
493 return this->depth(_::cpp_type<Rel>::id(m_world));
494 }
495
501 flecs::entity parent() const;
502
511 flecs::entity lookup(const char *path, bool search_path = false) const;
512
518 bool has(flecs::id_t e) const {
519 return ecs_has_id(m_world, m_id, e);
520 }
521
527 template <typename T>
528 bool has() const {
529 flecs::id_t cid = _::cpp_type<T>::id(m_world);
530 bool result = ecs_has_id(m_world, m_id, cid);
531 if (result) {
532 return result;
533 }
534
535 if (is_enum<T>::value) {
536 return ecs_has_pair(m_world, m_id, cid, flecs::Wildcard);
537 }
538
539 return false;
540 }
541
548 template <typename E, if_t< is_enum<E>::value > = 0>
549 bool has(E value) const {
550 auto r = _::cpp_type<E>::id(m_world);
551 auto o = enum_type<E>(m_world).entity(value);
552 return ecs_has_pair(m_world, m_id, r, o);
553 }
554
561 template <typename First, typename Second>
562 bool has() const {
563 return this->has<First>(_::cpp_type<Second>::id(m_world));
564 }
565
572 template<typename First, typename Second, if_not_t< is_enum<Second>::value > = 0>
573 bool has(Second second) const {
574 auto comp_id = _::cpp_type<First>::id(m_world);
575 return ecs_has_id(m_world, m_id, ecs_pair(comp_id, second));
576 }
577
584 template <typename Second>
585 bool has_second(flecs::entity_t first) const {
586 return this->has(first, _::cpp_type<Second>::id(m_world));
587 }
588
595 template<typename First, typename E, if_t< is_enum<E>::value > = 0>
596 bool has(E value) const {
597 const auto& et = enum_type<E>(this->m_world);
598 flecs::entity_t second = et.entity(value);
599 return has<First>(second);
600 }
601
608 bool has(flecs::id_t first, flecs::id_t second) const {
609 return ecs_has_id(m_world, m_id, ecs_pair(first, second));
610 }
611
618 bool owns(flecs::id_t e) const {
619 return ecs_owns_id(m_world, m_id, e);
620 }
621
628 template <typename First>
629 bool owns(flecs::id_t second) const {
630 auto comp_id = _::cpp_type<First>::id(m_world);
631 return owns(ecs_pair(comp_id, second));
632 }
633
640 bool owns(flecs::id_t first, flecs::id_t second) const {
641 return owns(ecs_pair(first, second));
642 }
643
650 template <typename T>
651 bool owns() const {
652 return owns(_::cpp_type<T>::id(m_world));
653 }
654
662 template <typename First, typename Second>
663 bool owns() const {
664 return owns(
665 _::cpp_type<First>::id(m_world),
666 _::cpp_type<Second>::id(m_world));
667 }
668
674 bool enabled(flecs::id_t id) const {
675 return ecs_is_enabled_id(m_world, m_id, id);
676 }
677
683 template<typename T>
684 bool enabled() const {
685 return this->enabled(_::cpp_type<T>::id(m_world));
686 }
687
694 bool enabled(flecs::id_t first, flecs::id_t second) const {
695 return this->enabled(ecs_pair(first, second));
696 }
697
704 template <typename First>
705 bool enabled(flecs::id_t second) const {
706 return this->enabled(_::cpp_type<First>::id(m_world), second);
707 }
708
715 template <typename First, typename Second>
716 bool enabled() const {
717 return this->enabled<First>(_::cpp_type<Second>::id(m_world));
718 }
719
720 flecs::entity clone(bool clone_value = true, flecs::entity_t dst_id = 0) const;
721
741 flecs::entity mut(const flecs::world& stage) const;
742
750 flecs::entity mut(const flecs::iter& it) const;
751
760 flecs::entity mut(const flecs::entity_view& e) const;
761
762# ifdef FLECS_JSON
764# endif
765# ifdef FLECS_DOC
767# endif
768# ifdef FLECS_ALERTS
770# endif
771
774
775private:
776 flecs::entity set_stage(world_t *stage);
777};
778
779}
780
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.
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.
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.
#define EcsIsEntity
Term id is an entity.
Definition flecs.h:702
ecs_filter_t ECS_FILTER_INIT
Use $this variable to initialize user-allocated filter object.
@ EcsOptional
The term may match.
Definition flecs.h:687
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:983
ecs_term_t terms[(16)]
Terms of the filter.
Definition flecs.h:988
ecs_filter_t * storage
External storage to prevent allocation of the filter object.
Definition flecs.h:997
Filters allow for ad-hoc quick filtering of entity tables.
Definition flecs.h:773
ecs_term_t * terms
Array containing terms for filter.
Definition flecs.h:781
int8_t term_count
Number of elements in terms array.
Definition flecs.h:776
ecs_flags32_t flags
Term flags.
Definition flecs.h:741
ecs_entity_t id
Entity id.
Definition flecs.h:725
Type that describes a term (single element in a query)
Definition flecs.h:745
ecs_term_id_t second
Second element of pair.
Definition flecs.h:753
ecs_id_t id
Component id to be matched by term.
Definition flecs.h:746
ecs_term_id_t first
Component or first element of pair.
Definition flecs.h:752
ecs_oper_kind_t oper
Operator of term.
Definition flecs.h:756
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.
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
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.
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.
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.
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.
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.
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.
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:132