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

| 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
5
6#pragma once
7
8namespace flecs
9{
10
18
22 struct untyped_ref {
23
25 untyped_ref () : world_(nullptr), ref_{}, id_(0) {}
26
33 untyped_ref(world_t *world, entity_t entity, flecs::id_t id)
34 : ref_(), id_(id) {
35 ecs_assert(id != 0, ECS_INVALID_PARAMETER,
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
43 flecs::entity_t type = ecs_get_typeid(world, id);
44 const flecs::type_info_t *ti = ecs_get_type_info(world, type);
45 ecs_assert(ti && ti->size != 0, ECS_INVALID_PARAMETER,
46 "cannot create ref to empty type");
47#endif
48 ref_ = ecs_ref_init_id(world_, entity, id);
49 }
50
56 untyped_ref(flecs::entity entity, flecs::id_t id);
57
59 flecs::entity entity() const;
60
62 flecs::id component() const {
63 return flecs::id(world_, id_);
64 }
65
67 void* get() {
68 return ecs_ref_get_id(world_, &ref_, id_);
69 }
70
72 bool has() {
73 return !!try_get();
74 }
75
77 flecs::world world() const {
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 flecs::id_t id_;
103};
104
108template <typename T>
109 struct ref : public untyped_ref {
111 ref() : untyped_ref() { }
112
119 ref(world_t *world, entity_t entity, flecs::id_t id = 0)
120 : untyped_ref(world, entity, id ? id : _::type<T>::id(world))
121 { }
122
128 ref(flecs::entity entity, flecs::id_t id = 0);
129
131 T* operator->() {
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
141 T* get() {
142 return static_cast<T*>(untyped_ref::get());
143 }
144
148 T* try_get() {
149 return static_cast<T*>(untyped_ref::try_get());
150 }
151};
152
154
155}
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
ECS_INVALID_PARAMETER
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
ecs_get_type_info
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.
flecs::id_t
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
flecs::ref_t
ecs_ref_t ref_t
Ref type.
Definition c_types.hpp:29
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
flecs::world_t
ecs_world_t world_t
World type.
Definition c_types.hpp:18
flecs::type_info_t
ecs_type_info_t type_info_t
Type info type.
Definition c_types.hpp:33
ecs_ref_get_id
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_init_id
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_get_typeid
ecs_entity_t ecs_get_typeid(const ecs_world_t *world, ecs_id_t component)
Get the type for a component.
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
flecs::_
Int to enum.
Definition component.hpp:18
ecs_type_info_t::size
ecs_size_t size
Size of the type.
Definition flecs.h:1024
flecs::entity
Entity.
Definition entity.hpp:30
flecs::id
Class that wraps around a flecs::id_t.
Definition decl.hpp:27
flecs::ref::ref
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
flecs::ref::try_get
T * try_get()
Try to get a typed pointer to the component value.
Definition ref.hpp:148
flecs::ref::ref
ref()
Default constructor.
Definition ref.hpp:111
flecs::ref::operator->
T * operator->()
Dereference operator.
Definition ref.hpp:131
flecs::ref::get
T * get()
Get a typed pointer to the component value.
Definition ref.hpp:141
flecs::type
Type class.
Definition type.hpp:21
flecs::untyped_ref::untyped_ref
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::untyped_ref::component
flecs::id component() const
Return the component associated with the reference.
Definition ref.hpp:62
flecs::untyped_ref::untyped_ref
untyped_ref()
Default constructor.
Definition ref.hpp:25
flecs::untyped_ref::get
void * get()
Get a pointer to the component value.
Definition ref.hpp:67
flecs::untyped_ref::try_get
void * try_get()
Try to get a pointer to the component value.
Definition ref.hpp:91
flecs::untyped_ref::has
bool has()
Check if the reference has a valid component value.
Definition ref.hpp:72
flecs::untyped_ref::world
flecs::world world() const
Get the world associated with the reference.
Definition ref.hpp:77
flecs::untyped_ref::entity
flecs::entity entity() const
Return the entity associated with the reference.
Definition impl.hpp:13
flecs::world
The world.
Definition world.hpp:246