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
6#pragma once
7
8namespace flecs
9{
10
22template <typename T>
23struct ref {
24 ref() : world_(nullptr), ref_{} { }
25
26 ref(world_t *world, entity_t entity, flecs::id_t id = 0)
27 : ref_()
28 {
29 // the world we were called with may be a stage; convert it to a world
30 // here if that is the case
31 world_ = world ? const_cast<flecs::world_t *>(ecs_get_world(world))
32 : nullptr;
33 if (!id) {
35 }
36
37 ecs_assert(_::type<T>::size() != 0, ECS_INVALID_PARAMETER,
38 "operation invalid for empty type");
39
40 ref_ = ecs_ref_init_id(world_, entity, id);
41 }
42
43 T* operator->() {
44 T* result = static_cast<T*>(ecs_ref_get_id(
45 world_, &ref_, this->ref_.id));
46
47 ecs_assert(result != NULL, ECS_INVALID_PARAMETER,
48 "nullptr dereference by flecs::ref");
49
50 return result;
51 }
52
53 T* get() {
54 return static_cast<T*>(ecs_ref_get_id(
55 world_, &ref_, this->ref_.id));
56 }
57
58 T* try_get() {
59 if (!world_ || !ref_.entity) {
60 return nullptr;
61 }
62
63 return get();
64 }
65
66 flecs::entity entity() const;
67
68private:
69 world_t *world_;
70 flecs::ref_t ref_;
71};
72
75}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
ecs_ref_t ecs_ref_init_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Create a component ref.
void * ecs_ref_get_id(const ecs_world_t *world, ecs_ref_t *ref, ecs_id_t id)
Get component from ref.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
Entity.
Definition entity.hpp:30
Component reference.
Definition ref.hpp:23
The world.
Definition world.hpp:137