Flecs v4.1
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
ref.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace flecs
9{
10
23
25 untyped_ref () : world_(nullptr), ref_{} {}
26
34 : ref_() {
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
57
59 flecs::entity entity() const;
60
63 return flecs::id(world_, ref_.id);
64 }
65
67 void* get() {
68 return ecs_ref_get_id(world_, &ref_, this->ref_.id);
69 }
70
72 bool has() {
73 return !!try_get();
74 }
75
78 return flecs::world(world_);
79 }
80
84 operator bool() {
85 return has();
86 }
87
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};
103
107template <typename T>
108struct ref : public untyped_ref {
110 ref() : untyped_ref() { }
111
119 : untyped_ref(world, entity, id ? id : _::type<T>::id(world))
120 { }
121
128
131 T* result = static_cast<T*>(get());
132
133 ecs_assert(result != NULL, ECS_INVALID_PARAMETER,
134 "nullptr dereference by flecs::ref");
135
136 return result;
137 }
138
140 T* get() {
141 return static_cast<T*>(untyped_ref::get());
142 }
143
147 T* try_get() {
148 return static_cast<T*>(untyped_ref::try_get());
149 }
150};
151
154}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:661
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
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.
Type that contains component information (passed to ctors/dtors/...).
Definition flecs.h:1019
ecs_size_t size
Size of the type.
Definition flecs.h:1020
Entity.
Definition entity.hpp:30
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
Component reference.
Definition ref.hpp:108
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:118
T * try_get()
Try to get a typed pointer to the component value.
Definition ref.hpp:147
ref()
Default constructor.
Definition ref.hpp:110
T * operator->()
Dereference operator.
Definition ref.hpp:130
T * get()
Get a typed pointer to the component value.
Definition ref.hpp:140
Type class.
Definition type.hpp:21
Untyped component reference.
Definition ref.hpp:22
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:246