Skip to content
Flecs v4.1
ref.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/ref.hpp
3 * @brief Class that caches data to speed up get operations.
4 */
5
6#pragma once
7
8namespace flecs
9{
10
11/**
12 * @defgroup cpp_ref Refs
13 * @ingroup cpp_core
14 * Refs are a fast mechanism for referring to a specific entity/component.
15 *
16 * @{
17 */
18
19/** Untyped component reference.
20 * Reference to a component from a specific entity.
21 */
23
24 /** Default constructor. Creates an empty reference. */
25 untyped_ref () : world_(nullptr), ref_{}, id_(0) {}
26
27 /** Construct a reference from a world, entity, and component ID.
28 *
29 * @param world The world.
30 * @param entity The entity.
31 * @param id The component ID.
32 */
34 : ref_(), id_(id) {
36 "invalid id");
37 // The world we were called with may be a stage; convert it to a world
38 // here if that is the case.
39 world_ = world ? const_cast<flecs::world_t *>(ecs_get_world(world))
40 : nullptr;
41
42#ifdef FLECS_DEBUG
46 "cannot create ref to empty type");
47#endif
48 ref_ = ecs_ref_init_id(world_, entity, id);
49 }
50
51 /** Construct a reference from an entity and component ID.
52 *
53 * @param entity The entity.
54 * @param id The component ID.
55 */
57
58 /** Return the entity associated with the reference. */
59 flecs::entity entity() const;
60
61 /** Return the component associated with the reference. */
63 return flecs::id(world_, id_);
64 }
65
66 /** Get a pointer to the component value. */
67 void* get() {
68 return ecs_ref_get_id(world_, &ref_, id_);
69 }
70
71 /** Check if the reference has a valid component value. */
72 bool has() {
73 return !!try_get();
74 }
75
76 /** Get the world associated with the reference. */
78 return flecs::world(world_);
79 }
80
81 /** Implicit conversion to bool.
82 * Return true if there is a valid component instance being referred to.
83 */
84 operator bool() {
85 return has();
86 }
87
88 /** Try to get a pointer to the component value.
89 * Return nullptr if the reference is invalid.
90 */
91 void* try_get() {
92 if (!world_ || !ref_.entity) {
93 return nullptr;
94 }
95
96 return get();
97 }
98
99private:
100 world_t *world_;
101 flecs::ref_t ref_;
102 flecs::id_t id_;
103};
104
105/** Component reference.
106 * Reference to a component from a specific entity.
107 */
108template <typename T>
109struct ref : public untyped_ref {
110 /** Default constructor. Creates an empty reference. */
111 ref() : untyped_ref() { }
112
113 /** Construct a reference from a world, entity, and optional component ID.
114 *
115 * @param world The world.
116 * @param entity The entity.
117 * @param id The component ID (defaults to type T's ID).
118 */
120 : untyped_ref(world, entity, id ? id : _::type<T>::id(world))
121 { }
122
123 /** Construct a reference from an entity and optional component ID.
124 *
125 * @param entity The entity.
126 * @param id The component ID (defaults to type T's ID).
127 */
129
130 /** Dereference operator. Return a pointer to the component value. */
132 T* result = static_cast<T*>(get());
133
134 ecs_assert(result != nullptr, ECS_INVALID_PARAMETER,
135 "nullptr dereference by flecs::ref");
136
137 return result;
138 }
139
140 /** Get a typed pointer to the component value. */
141 T* get() {
142 return static_cast<T*>(untyped_ref::get());
143 }
144
145 /** Try to get a typed pointer to the component value.
146 * Return nullptr if the reference is invalid.
147 */
148 T* try_get() {
149 return static_cast<T*>(untyped_ref::try_get());
150 }
151};
152
153/** @} */
154
155}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
const ecs_type_info_t * ecs_get_type_info(const ecs_world_t *world, ecs_id_t component)
Get the type info for a component.
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_ref_t ref_t
Ref type.
Definition c_types.hpp:29
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_type_info_t type_info_t
Type info type.
Definition c_types.hpp:33
void * ecs_ref_get_id(const ecs_world_t *world, ecs_ref_t *ref, ecs_id_t component)
Get a component from a ref.
ecs_ref_t ecs_ref_init_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Create a component ref.
ecs_entity_t ecs_get_typeid(const ecs_world_t *world, ecs_id_t component)
Get the type for a component.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
Int to enum.
Definition component.hpp:18
ecs_size_t size
Size of the type.
Definition flecs.h:1053
Entity.
Definition entity.hpp:30
entity()
Default constructor.
Definition entity.hpp:32
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
ref(world_t *world, entity_t entity, flecs::id_t id=0)
Construct a reference from a world, entity, and optional component ID.
Definition ref.hpp:119
T * try_get()
Try to get a typed pointer to the component value.
Definition ref.hpp:148
ref()
Default constructor.
Definition ref.hpp:111
T * operator->()
Dereference operator.
Definition ref.hpp:131
T * get()
Get a typed pointer to the component value.
Definition ref.hpp:141
Type class.
Definition type.hpp:21
untyped_ref(world_t *world, entity_t entity, flecs::id_t id)
Construct a reference from a world, entity, and component ID.
Definition ref.hpp:33
flecs::id component() const
Return the component associated with the reference.
Definition ref.hpp:62
untyped_ref()
Default constructor.
Definition ref.hpp:25
void * get()
Get a pointer to the component value.
Definition ref.hpp:67
void * try_get()
Try to get a pointer to the component value.
Definition ref.hpp:91
bool has()
Check if the reference has a valid component value.
Definition ref.hpp:72
flecs::world world() const
Get the world associated with the reference.
Definition ref.hpp:77
flecs::entity entity() const
Return the entity associated with the reference.
Definition impl.hpp:13
The world.
Definition world.hpp:129