Flecs: include/flecs/addons/cpp/ref.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.0 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
ref.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8namespace flecs
9{
10
18
22 struct untyped_ref {
23
24 untyped_ref () : world_(nullptr), ref_{} {}
25
26 untyped_ref(world_t *world, entity_t entity, flecs::id_t id)
27 : ref_() {
28 ecs_assert(id != 0, ECS_INVALID_PARAMETER,
29 "invalid id");
30 // the world we were called with may be a stage; convert it to a world
31 // here if that is the case
32 world_ = world ? const_cast<flecs::world_t *>(ecs_get_world(world))
33 : nullptr;
34
35#ifdef FLECS_DEBUG
36 flecs::entity_t type = ecs_get_typeid(world, id);
37 const flecs::type_info_t *ti = ecs_get_type_info(world, type);
38 ecs_assert(ti && ti->size != 0, ECS_INVALID_PARAMETER,
39 "cannot create ref to empty type");
40#endif
41 ref_ = ecs_ref_init_id(world_, entity, id);
42 }
43
44 untyped_ref(flecs::entity entity, flecs::id_t id);
45
47 flecs::entity entity() const;
48
50 flecs::id component() const {
51 return flecs::id(world_, ref_.id);
52 }
53
54 void* get() {
55 return ecs_ref_get_id(world_, &ref_, this->ref_.id);
56 }
57
58 bool has() {
59 return !!try_get();
60 }
61
64 operator bool() {
65 return has();
66 }
67
68 void* try_get() {
69 if (!world_ || !ref_.entity) {
70 return nullptr;
71 }
72
73 return get();
74 }
75
76private:
77 world_t *world_;
78 flecs::ref_t ref_;
79};
80
84template <typename T>
85 struct ref : public untyped_ref {
86 ref() : untyped_ref() { }
87
88 ref(world_t *world, entity_t entity, flecs::id_t id = 0)
89 : untyped_ref(world, entity, id ? id : _::type<T>::id(world))
90 { }
91
92 ref(flecs::entity entity, flecs::id_t id = 0);
93
94 T* operator->() {
95 T* result = static_cast<T*>(get());
96
97 ecs_assert(result != NULL, ECS_INVALID_PARAMETER,
98 "nullptr dereference by flecs::ref");
99
100 return result;
101 }
102
103 T* get() {
104 return static_cast<T*>(untyped_ref::get());
105 }
106
107 T* try_get() {
108 return static_cast<T*>(untyped_ref::try_get());
109 }
110};
111
113
114}
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
ecs_get_type_info
const ecs_type_info_t * ecs_get_type_info(const ecs_world_t *world, ecs_id_t id)
Get the type for an id.
ecs_ref_init_id
ecs_ref_t ecs_ref_init_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Create a component ref.
ecs_ref_get_id
void * ecs_ref_get_id(const ecs_world_t *world, ecs_ref_t *ref, ecs_id_t id)
Get component from ref.
ecs_get_typeid
ecs_entity_t ecs_get_typeid(const ecs_world_t *world, ecs_id_t id)
Get the type for an id.
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
ecs_type_info_t::size
ecs_size_t size
Size of type.
Definition flecs.h:990
flecs::_::type
Definition flecs.hpp:39
flecs::entity
Entity.
Definition entity.hpp:30
flecs::id
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::type
Type class.
Definition type.hpp:21
flecs::untyped_ref::component
flecs::id component() const
Return component associated with reference.
Definition ref.hpp:50
flecs::untyped_ref::entity
flecs::entity entity() const
Return entity associated with reference.
Definition impl.hpp:13
flecs::world
The world.
Definition world.hpp:137