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
147 template <typename Func>
148 void each(const Func& func) const;
149
156 template <typename Func>
157 void each(flecs::id_t first, flecs::id_t second, const Func& func) const;
158
166 template <typename Func>
167 void each(const flecs::entity_view& rel, const Func& func) const;
168
176 template <typename First, typename Func>
177 void each(const Func& func) const {
178 return each(_::cpp_type<First>::id(m_world), func);
179 }
180
188 template <typename Func>
189 void children(flecs::entity_t rel, Func&& func) const {
190 /* When the entity is a wildcard, this would attempt to query for all
191 * entities with (ChildOf, *) or (ChildOf, _) instead of querying for
192 * the children of the wildcard entity. */
193 if (m_id == flecs::Wildcard || m_id == flecs::Any) {
194 /* This is correct, wildcard entities don't have children */
195 return;
196 }
197
198 flecs::world world(m_world);
199
200 ecs_term_t terms[2];
202 f.terms = terms;
203 f.term_count = 2;
204
205 ecs_filter_desc_t desc = {};
206 desc.terms[0].first.id = rel;
207 desc.terms[0].second.id = m_id;
208 desc.terms[0].second.flags = EcsIsEntity;
209 desc.terms[1].id = flecs::Prefab;
210 desc.terms[1].oper = EcsOptional;
211 desc.storage = &f;
212 if (ecs_filter_init(m_world, &desc) != nullptr) {
213 ecs_iter_t it = ecs_filter_iter(m_world, &f);
214 while (ecs_filter_next(&it)) {
215 _::each_invoker<Func>(FLECS_MOV(func)).invoke(&it);
216 }
217
218 ecs_filter_fini(&f);
219 }
220 }
221
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
242 template <typename Func>
243 void children(Func&& func) const {
244 children(flecs::ChildOf, FLECS_MOV(func));
245 }
246
253 template <typename T, if_t< is_actual<T>::value > = 0>
254 const T* get() const {
255 auto comp_id = _::cpp_type<T>::id(m_world);
256 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
257 return static_cast<const T*>(ecs_get_id(m_world, m_id, comp_id));
258 }
259
268 template <typename T, typename A = actual_type_t<T>,
269 if_t< flecs::is_pair<T>::value > = 0>
270 const A* get() const {
271 auto comp_id = _::cpp_type<T>::id(m_world);
272 ecs_assert(_::cpp_type<A>::size() != 0, ECS_INVALID_PARAMETER, NULL);
273 return static_cast<const A*>(ecs_get_id(m_world, m_id, comp_id));
274 }
275
282 template <typename First, typename Second, typename P = pair<First, Second>,
283 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value > = 0>
284 const A* get() const {
285 return this->get<P>();
286 }
287
294 template<typename First, typename Second, if_not_t< is_enum<Second>::value> = 0>
295 const First* get(Second second) const {
296 auto comp_id = _::cpp_type<First>::id(m_world);
297 ecs_assert(_::cpp_type<First>::size() != 0, ECS_INVALID_PARAMETER, NULL);
298 return static_cast<const First*>(
299 ecs_get_id(m_world, m_id, ecs_pair(comp_id, second)));
300 }
301
308 template<typename First, typename Second, if_t<is_enum<Second>::value> = 0>
309 const First* get(Second constant) const {
310 const auto& et = enum_type<Second>(this->m_world);
311 flecs::entity_t target = et.entity(constant);
312 return get<First>(target);
313 }
314
321 const void* get(flecs::id_t comp) const {
322 return ecs_get_id(m_world, m_id, comp);
323 }
324
333 const void* get(flecs::entity_t first, flecs::entity_t second) const {
334 return ecs_get_id(m_world, m_id, ecs_pair(first, second));
335 }
336
368 template <typename Func, if_t< is_callable<Func>::value > = 0>
369 bool get(const Func& func) const;
370
376 template <typename T, if_t< is_enum<T>::value > = 0>
377 const T* get() const;
378
386 template<typename Second>
387 const Second* get_second(flecs::entity_t first) const {
388 auto second = _::cpp_type<Second>::id(m_world);
389 ecs_assert(_::cpp_type<Second>::size() != 0, ECS_INVALID_PARAMETER, NULL);
390 return static_cast<const Second*>(
391 ecs_get_id(m_world, m_id, ecs_pair(first, second)));
392 }
393
401 template<typename First, typename Second>
402 const Second* get_second() const {
403 return get<pair_object<First, Second>>();
404 }
405
414 template<typename First>
415 flecs::entity target(int32_t index = 0) const;
416
425 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
426
443 flecs::entity target_for(flecs::entity_t relationship, flecs::id_t id) const;
444
445 template <typename T>
446 flecs::entity target_for(flecs::entity_t relationship) const;
447
448 template <typename First, typename Second>
449 flecs::entity target_for(flecs::entity_t relationship) const;
450
456 int32_t depth(flecs::entity_t rel) const {
457 return ecs_get_depth(m_world, m_id, rel);
458 }
459
465 template<typename Rel>
466 int32_t depth() const {
467 return this->depth(_::cpp_type<Rel>::id(m_world));
468 }
469
475 flecs::entity parent() const;
476
485 flecs::entity lookup(const char *path, bool search_path = false) const;
486
492 bool has(flecs::id_t e) const {
493 return ecs_has_id(m_world, m_id, e);
494 }
495
501 template <typename T>
502 bool has() const {
503 flecs::id_t cid = _::cpp_type<T>::id(m_world);
504 bool result = ecs_has_id(m_world, m_id, cid);
505 if (result) {
506 return result;
507 }
508
509 if (is_enum<T>::value) {
510 return ecs_has_pair(m_world, m_id, cid, flecs::Wildcard);
511 }
512
513 return false;
514 }
515
522 template <typename E, if_t< is_enum<E>::value > = 0>
523 bool has(E value) const {
524 auto r = _::cpp_type<E>::id(m_world);
525 auto o = enum_type<E>(m_world).entity(value);
526 return ecs_has_pair(m_world, m_id, r, o);
527 }
528
535 template <typename First, typename Second>
536 bool has() const {
537 return this->has<First>(_::cpp_type<Second>::id(m_world));
538 }
539
546 template<typename First, typename Second, if_not_t< is_enum<Second>::value > = 0>
547 bool has(Second second) const {
548 auto comp_id = _::cpp_type<First>::id(m_world);
549 return ecs_has_id(m_world, m_id, ecs_pair(comp_id, second));
550 }
551
558 template <typename Second>
559 bool has_second(flecs::entity_t first) const {
560 return this->has(first, _::cpp_type<Second>::id(m_world));
561 }
562
569 template<typename First, typename E, if_t< is_enum<E>::value > = 0>
570 bool has(E value) const {
571 const auto& et = enum_type<E>(this->m_world);
572 flecs::entity_t second = et.entity(value);
573 return has<First>(second);
574 }
575
582 bool has(flecs::id_t first, flecs::id_t second) const {
583 return ecs_has_id(m_world, m_id, ecs_pair(first, second));
584 }
585
592 bool owns(flecs::id_t e) const {
593 return ecs_owns_id(m_world, m_id, e);
594 }
595
602 template <typename First>
603 bool owns(flecs::id_t second) const {
604 auto comp_id = _::cpp_type<First>::id(m_world);
605 return owns(ecs_pair(comp_id, second));
606 }
607
614 bool owns(flecs::id_t first, flecs::id_t second) const {
615 return owns(ecs_pair(first, second));
616 }
617
624 template <typename T>
625 bool owns() const {
626 return owns(_::cpp_type<T>::id(m_world));
627 }
628
636 template <typename First, typename Second>
637 bool owns() const {
638 return owns(
639 _::cpp_type<First>::id(m_world),
640 _::cpp_type<Second>::id(m_world));
641 }
642
648 bool enabled(flecs::id_t id) const {
649 return ecs_is_enabled_id(m_world, m_id, id);
650 }
651
657 template<typename T>
658 bool enabled() const {
659 return this->enabled(_::cpp_type<T>::id(m_world));
660 }
661
668 bool enabled(flecs::id_t first, flecs::id_t second) const {
669 return this->enabled(ecs_pair(first, second));
670 }
671
678 template <typename First>
679 bool enabled(flecs::id_t second) const {
680 return this->enabled(_::cpp_type<First>::id(m_world), second);
681 }
682
689 template <typename First, typename Second>
690 bool enabled() const {
691 return this->enabled<First>(_::cpp_type<Second>::id(m_world));
692 }
693
694 flecs::entity clone(bool clone_value = true, flecs::entity_t dst_id = 0) const;
695
715 flecs::entity mut(const flecs::world& stage) const;
716
724 flecs::entity mut(const flecs::iter& it) const;
725
734 flecs::entity mut(const flecs::entity_view& e) const;
735
736# ifdef FLECS_JSON
738# endif
739# ifdef FLECS_DOC
741# endif
742# ifdef FLECS_ALERTS
744# endif
745
747
748private:
749 flecs::entity set_stage(world_t *stage);
750};
751
752}
753
Alerts entity mixin.
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 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:676
ecs_filter_t ECS_FILTER_INIT
Use $this variable to initialize user-allocated filter object.
@ EcsOptional
The term may match.
Definition: flecs.h:662
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:961
ecs_term_t terms[(16)]
Terms of the filter.
Definition: flecs.h:966
ecs_filter_t * storage
External storage to prevent allocation of the filter object.
Definition: flecs.h:975
Filters alllow for ad-hoc quick filtering of entity tables.
Definition: flecs.h:745
ecs_term_t * terms
Array containing terms for filter.
Definition: flecs.h:748
int32_t term_count
Number of elements in terms array.
Definition: flecs.h:749
ecs_flags32_t flags
Term flags.
Definition: flecs.h:713
ecs_entity_t id
Entity id.
Definition: flecs.h:697
Type that describes a term (single element in a query)
Definition: flecs.h:717
ecs_term_id_t second
Second element of pair.
Definition: flecs.h:725
ecs_id_t id
Component id to be matched by term.
Definition: flecs.h:718
ecs_term_id_t first
Component or first element of pair.
Definition: flecs.h:724
ecs_oper_kind_t oper
Operator of term.
Definition: flecs.h:728
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.
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 if 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.
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.
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.
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