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 flecs::world world(m_world);
191
192 ecs_term_t terms[2];
194 f.terms = terms;
195 f.term_count = 2;
196
197 ecs_filter_desc_t desc = {};
198 desc.terms[0].id = ecs_pair(rel, m_id);
199 desc.terms[1].id = flecs::Prefab;
200 desc.terms[1].oper = EcsOptional;
201 desc.storage = &f;
202 ecs_filter_init(m_world, &desc);
203
204 ecs_iter_t it = ecs_filter_iter(m_world, &f);
205 while (ecs_filter_next(&it)) {
206 _::each_invoker<Func>(FLECS_MOV(func)).invoke(&it);
207 }
208
209 ecs_filter_fini(&f);
210 }
211
219 template <typename Rel, typename Func>
220 void children(Func&& func) const {
221 children(_::cpp_type<Rel>::id(m_world), FLECS_MOV(func));
222 }
223
232 template <typename Func>
233 void children(Func&& func) const {
234 children(flecs::ChildOf, FLECS_MOV(func));
235 }
236
243 template <typename T, if_t< is_actual<T>::value > = 0>
244 const T* get() const {
245 auto comp_id = _::cpp_type<T>::id(m_world);
246 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
247 return static_cast<const T*>(ecs_get_id(m_world, m_id, comp_id));
248 }
249
258 template <typename T, typename A = actual_type_t<T>,
259 if_t< flecs::is_pair<T>::value > = 0>
260 const A* get() const {
261 auto comp_id = _::cpp_type<T>::id(m_world);
262 ecs_assert(_::cpp_type<A>::size() != 0, ECS_INVALID_PARAMETER, NULL);
263 return static_cast<const A*>(ecs_get_id(m_world, m_id, comp_id));
264 }
265
272 template <typename First, typename Second, typename P = pair<First, Second>,
273 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value > = 0>
274 const A* get() const {
275 return this->get<P>();
276 }
277
284 template<typename First, typename Second, if_not_t< is_enum<Second>::value> = 0>
285 const First* get(Second second) const {
286 auto comp_id = _::cpp_type<First>::id(m_world);
287 ecs_assert(_::cpp_type<First>::size() != 0, ECS_INVALID_PARAMETER, NULL);
288 return static_cast<const First*>(
289 ecs_get_id(m_world, m_id, ecs_pair(comp_id, second)));
290 }
291
298 template<typename First, typename Second, if_t<is_enum<Second>::value> = 0>
299 const First* get(Second constant) const {
300 const auto& et = enum_type<Second>(this->m_world);
301 flecs::entity_t target = et.entity(constant);
302 return get<First>(target);
303 }
304
311 const void* get(flecs::id_t comp) const {
312 return ecs_get_id(m_world, m_id, comp);
313 }
314
323 const void* get(flecs::entity_t first, flecs::entity_t second) const {
324 return ecs_get_id(m_world, m_id, ecs_pair(first, second));
325 }
326
358 template <typename Func, if_t< is_callable<Func>::value > = 0>
359 bool get(const Func& func) const;
360
366 template <typename T, if_t< is_enum<T>::value > = 0>
367 const T* get() const;
368
376 template<typename Second>
377 const Second* get_second(flecs::entity_t first) const {
378 auto second = _::cpp_type<Second>::id(m_world);
379 ecs_assert(_::cpp_type<Second>::size() != 0, ECS_INVALID_PARAMETER, NULL);
380 return static_cast<const Second*>(
381 ecs_get_id(m_world, m_id, ecs_pair(first, second)));
382 }
383
391 template<typename First, typename Second>
392 const Second* get_second() const {
393 return get<pair_object<First, Second>>();
394 }
395
404 template<typename First>
405 flecs::entity target(int32_t index = 0) const;
406
415 flecs::entity target(flecs::entity_t first, int32_t index = 0) const;
416
433 flecs::entity target_for(flecs::entity_t relationship, flecs::id_t id) const;
434
435 template <typename T>
436 flecs::entity target_for(flecs::entity_t relationship) const;
437
438 template <typename First, typename Second>
439 flecs::entity target_for(flecs::entity_t relationship) const;
440
446 int32_t depth(flecs::entity_t rel) const {
447 return ecs_get_depth(m_world, m_id, rel);
448 }
449
455 template<typename Rel>
456 int32_t depth() const {
457 return this->depth(_::cpp_type<Rel>::id(m_world));
458 }
459
465 flecs::entity parent() const;
466
474 flecs::entity lookup(const char *path) const;
475
481 bool has(flecs::id_t e) const {
482 return ecs_has_id(m_world, m_id, e);
483 }
484
490 template <typename T>
491 bool has() const {
492 flecs::id_t cid = _::cpp_type<T>::id(m_world);
493 bool result = ecs_has_id(m_world, m_id, cid);
494 if (result) {
495 return result;
496 }
497
498 if (is_enum<T>::value) {
499 return ecs_has_pair(m_world, m_id, cid, flecs::Wildcard);
500 }
501
502 return false;
503 }
504
511 template <typename E, if_t< is_enum<E>::value > = 0>
512 bool has(E value) const {
513 auto r = _::cpp_type<E>::id(m_world);
514 auto o = enum_type<E>(m_world).entity(value);
515 return ecs_has_pair(m_world, m_id, r, o);
516 }
517
524 template <typename First, typename Second>
525 bool has() const {
526 return this->has<First>(_::cpp_type<Second>::id(m_world));
527 }
528
535 template<typename First, typename Second, if_not_t< is_enum<Second>::value > = 0>
536 bool has(Second second) const {
537 auto comp_id = _::cpp_type<First>::id(m_world);
538 return ecs_has_id(m_world, m_id, ecs_pair(comp_id, second));
539 }
540
547 template <typename Second>
548 bool has_second(flecs::entity_t first) const {
549 return this->has(first, _::cpp_type<Second>::id(m_world));
550 }
551
558 template<typename First, typename E, if_t< is_enum<E>::value > = 0>
559 bool has(E value) const {
560 const auto& et = enum_type<E>(this->m_world);
561 flecs::entity_t second = et.entity(value);
562 return has<First>(second);
563 }
564
571 bool has(flecs::id_t first, flecs::id_t second) const {
572 return ecs_has_id(m_world, m_id, ecs_pair(first, second));
573 }
574
581 bool owns(flecs::id_t e) const {
582 return ecs_owns_id(m_world, m_id, e);
583 }
584
591 template <typename First>
592 bool owns(flecs::id_t second) const {
593 auto comp_id = _::cpp_type<First>::id(m_world);
594 return owns(ecs_pair(comp_id, second));
595 }
596
603 bool owns(flecs::id_t first, flecs::id_t second) const {
604 return owns(ecs_pair(first, second));
605 }
606
613 template <typename T>
614 bool owns() const {
615 return owns(_::cpp_type<T>::id(m_world));
616 }
617
625 template <typename First, typename Second>
626 bool owns() const {
627 return owns(
628 _::cpp_type<First>::id(m_world),
629 _::cpp_type<Second>::id(m_world));
630 }
631
637 bool enabled(flecs::id_t id) const {
638 return ecs_is_enabled_id(m_world, m_id, id);
639 }
640
646 template<typename T>
647 bool enabled() const {
648 return this->enabled(_::cpp_type<T>::id(m_world));
649 }
650
657 bool enabled(flecs::id_t first, flecs::id_t second) const {
658 return this->enabled(ecs_pair(first, second));
659 }
660
667 template <typename First>
668 bool enabled(flecs::id_t second) const {
669 return this->enabled(_::cpp_type<First>::id(m_world), second);
670 }
671
678 template <typename First, typename Second>
679 bool enabled() const {
680 return this->enabled<First>(_::cpp_type<Second>::id(m_world));
681 }
682
683 flecs::entity clone(bool clone_value = true, flecs::entity_t dst_id = 0) const;
684
704 flecs::entity mut(const flecs::world& stage) const;
705
713 flecs::entity mut(const flecs::iter& it) const;
714
723 flecs::entity mut(const flecs::entity_view& e) const;
724
725# ifdef FLECS_JSON
727# endif
728# ifdef FLECS_DOC
730# endif
731
733
734private:
735 flecs::entity set_stage(world_t *stage);
736};
737
738}
739
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 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.
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:545
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:840
ecs_term_t terms[(16)]
Terms of the filter.
Definition: flecs.h:845
ecs_filter_t * storage
External storage to prevent allocation of the filter object.
Definition: flecs.h:854
Filters alllow for ad-hoc quick filtering of entity tables.
Definition: flecs.h:624
ecs_term_t * terms
Array containing terms for filter.
Definition: flecs.h:627
int32_t term_count
Number of elements in terms array.
Definition: flecs.h:628
Type that describes a term (single element in a query)
Definition: flecs.h:596
ecs_id_t id
Component id to be matched by term.
Definition: flecs.h:597
ecs_oper_kind_t oper
Operator of term.
Definition: flecs.h:607
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
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 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.
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:113