Flecs v3.2
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() : m_world(nullptr), m_ref{} { }
25
26 ref(world_t *world, entity_t entity, flecs::id_t id = 0)
27 : m_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 m_world = world ? const_cast<flecs::world_t *>(ecs_get_world(world))
32 : nullptr;
33 if (!id) {
35 }
36
37 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
38
39 m_ref = ecs_ref_init_id(m_world, entity, id);
40 }
41
42 T* operator->() {
43 T* result = static_cast<T*>(ecs_ref_get_id(
44 m_world, &m_ref, this->m_ref.id));
45
46 ecs_assert(result != NULL, ECS_INVALID_PARAMETER, NULL);
47
48 return result;
49 }
50
51 T* get() {
52 return static_cast<T*>(ecs_ref_get_id(
53 m_world, &m_ref, this->m_ref.id));
54 }
55
56 T* try_get() {
57 if (!m_world || !m_ref.entity) {
58 return nullptr;
59 }
60
61 return get();
62 }
63
64 flecs::entity entity() const;
65
66private:
67 world_t *m_world;
68 flecs::ref_t m_ref;
69};
70
73}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:351
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:132