Flecs v3.2
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
builder.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#define ECS_EVENT_DESC_ID_COUNT_MAX (8)
9
10namespace flecs {
11
18template <typename Base, typename E>
20 event_builder_base(flecs::world_t *world, flecs::entity_t event)
21 : m_world(world)
22 , m_desc{}
23 , m_ids{}
24 , m_ids_array{}
25 {
26 m_desc.event = event;
27 }
28
30 template <typename T>
31 Base& id() {
32 m_ids.array = m_ids_array;
33 m_ids.array[m_ids.count] = _::cpp_type<T>().id(m_world);
34 m_ids.count ++;
35 return *this;
36 }
37
43 template <typename First, typename Second>
44 Base& id() {
45 return id(
46 ecs_pair(_::cpp_type<First>::id(this->m_world),
47 _::cpp_type<Second>::id(this->m_world)));
48 }
49
55 template <typename First>
56 Base& id(entity_t second) {
57 return id(ecs_pair(_::cpp_type<First>::id(this->m_world), second));
58 }
59
65 Base& id(entity_t first, entity_t second) {
66 return id(ecs_pair(first, second));
67 }
68
70 Base& id(flecs::id_t id) {
71 m_ids.array = m_ids_array;
72 m_ids.array[m_ids.count] = id;
73 m_ids.count ++;
74 return *this;
75 }
76
78 Base& entity(flecs::entity_t e) {
79 ecs_record_t *r = ecs_record_find(m_world, e);
80
81 /* Can't emit for empty entity */
82 ecs_assert(r != nullptr, ECS_INVALID_PARAMETER, nullptr);
83 ecs_assert(r->table != nullptr, ECS_INVALID_PARAMETER, nullptr);
84
85 m_desc.table = r->table;
86 m_desc.offset = ECS_RECORD_TO_ROW(r->row);
87 m_desc.count = 1;
88 return *this;
89 }
90
91 /* Set table for which to emit event */
92 Base& table(flecs::table_t *t, int32_t offset = 0, int32_t count = 0) {
93 m_desc.table = t;
94 m_desc.offset = offset;
95 m_desc.count = count;
96 return *this;
97 }
98
99 /* Set event data */
100 Base& ctx(const E* ptr) {
101 m_desc.param = ptr;
102 return *this;
103 }
104
105 void emit() {
106 ecs_assert(m_ids.count != 0, ECS_INVALID_PARAMETER, NULL);
107 ecs_assert(m_desc.table != nullptr, ECS_INVALID_PARAMETER, NULL);
108 m_ids.array = m_ids_array;
109 m_desc.ids = &m_ids;
110 m_desc.observable = const_cast<flecs::world_t*>(ecs_get_world(m_world));
111 ecs_emit(m_world, &m_desc);
112 }
113
114protected:
115 flecs::world_t *m_world;
116 ecs_event_desc_t m_desc;
117 flecs::type_t m_ids;
118 flecs::id_t m_ids_array[ECS_EVENT_DESC_ID_COUNT_MAX];
119
120private:
121 operator Base&() {
122 return *static_cast<Base*>(this);
123 }
124};
125
126struct event_builder : event_builder_base<event_builder, void> {
127 using event_builder_base::event_builder_base;
128};
129
130template <typename E>
131struct event_builder_typed : event_builder_base<event_builder_typed<E>, E> {
132private:
134
135public:
136 using event_builder_base<Class, E>::event_builder_base;
137
138 /* Set event data */
139 Class& ctx(const E& ptr) {
140 this->m_desc.param = &ptr;
141 return *this;
142 }
143};
144
147}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
void ecs_emit(ecs_world_t *world, ecs_event_desc_t *desc)
Send event.
ecs_record_t * ecs_record_find(const ecs_world_t *world, ecs_entity_t entity)
Find record for entity.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
ecs_table_t * table
The table for which to notify.
Definition flecs.h:4303
int32_t count
Limit number of notified entities to count.
Definition flecs.h:4315
const void * param
Optional context.
Definition flecs.h:4321
int32_t offset
Limit notified entities to ones starting from offset (row) in table.
Definition flecs.h:4310
const ecs_type_t * ids
Component ids.
Definition flecs.h:4300
ecs_poly_t * observable
Observable (usually the world)
Definition flecs.h:4324
ecs_entity_t event
The event id.
Definition flecs.h:4295
An array with (component) ids.
Definition flecs.h:285
Event builder interface.
Definition builder.hpp:19
Base & id()
Add component to emit for.
Definition builder.hpp:31
Base & id(flecs::id_t id)
Add (component) id to emit for.
Definition builder.hpp:70
Base & entity(flecs::entity_t e)
Set entity for which to emit event.
Definition builder.hpp:78
Base & id(entity_t first, entity_t second)
Add pair to emit for.
Definition builder.hpp:65
Base & id(entity_t second)
Add pair to emit for.
Definition builder.hpp:56
Base & id()
Add pair to emit for.
Definition builder.hpp:44
The world.
Definition world.hpp:113