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(world_t *world, entity_t entity, flecs::id_t id = 0)
25 : m_ref()
26 {
27 // the world we were called with may be a stage; convert it to a world
28 // here if that is the case
29 m_world = world ? const_cast<flecs::world_t *>(ecs_get_world(world))
30 : nullptr;
31 if (!id) {
33 }
34
35 ecs_assert(_::cpp_type<T>::size() != 0, ECS_INVALID_PARAMETER, NULL);
36
37 m_ref = ecs_ref_init_id(m_world, entity, id);
38 }
39
40 T* operator->() {
41 T* result = static_cast<T*>(ecs_ref_get_id(
42 m_world, &m_ref, this->m_ref.id));
43
44 ecs_assert(result != NULL, ECS_INVALID_PARAMETER, NULL);
45
46 return result;
47 }
48
49 T* get() {
50 return static_cast<T*>(ecs_ref_get_id(
51 m_world, &m_ref, this->m_ref.id));
52 }
53
54 flecs::entity entity() const;
55
56private:
57 world_t *m_world;
58 flecs::ref_t m_ref;
59};
60
63}
#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:113