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 m_desc.entity = e;
80 return *this;
81 }
82
83 /* Set table for which to emit event */
84 Base& table(flecs::table_t *t, int32_t offset = 0, int32_t count = 0) {
85 m_desc.table = t;
86 m_desc.offset = offset;
87 m_desc.count = count;
88 return *this;
89 }
90
91 /* Set event data */
92 Base& ctx(const E* ptr) {
93 m_desc.const_param = ptr;
94 return *this;
95 }
96
97 /* Set event data */
98 Base& ctx(E* ptr) {
99 m_desc.param = ptr;
100 return *this;
101 }
102
103 void emit() {
104 m_ids.array = m_ids_array;
105 m_desc.ids = &m_ids;
106 m_desc.observable = const_cast<flecs::world_t*>(ecs_get_world(m_world));
107 ecs_emit(m_world, &m_desc);
108 }
109
110 void enqueue() {
111 m_ids.array = m_ids_array;
112 m_desc.ids = &m_ids;
113 m_desc.observable = const_cast<flecs::world_t*>(ecs_get_world(m_world));
114 ecs_enqueue(m_world, &m_desc);
115 }
116
117protected:
118 flecs::world_t *m_world;
119 ecs_event_desc_t m_desc;
120 flecs::type_t m_ids;
121 flecs::id_t m_ids_array[ECS_EVENT_DESC_ID_COUNT_MAX];
122
123private:
124 operator Base&() {
125 return *static_cast<Base*>(this);
126 }
127};
128
129struct event_builder : event_builder_base<event_builder, void> {
130 using event_builder_base::event_builder_base;
131};
132
133template <typename E>
134struct event_builder_typed : event_builder_base<event_builder_typed<E>, E> {
135private:
137
138public:
139 using event_builder_base<Class, E>::event_builder_base;
140
141 /* Set event data */
142 Class& ctx(const E& ptr) {
143 this->m_desc.const_param = &ptr;
144 return *this;
145 }
146
147 /* Set event data */
148 Class& ctx(E&& ptr) {
149 this->m_desc.param = &ptr;
150 return *this;
151 }
152};
153
156}
void ecs_emit(ecs_world_t *world, ecs_event_desc_t *desc)
Send event.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
Used with ecs_emit().
Definition flecs.h:1159
ecs_entity_t entity
Single-entity alternative to setting table / offset / count.
Definition flecs.h:1184
ecs_table_t * table
The table for which to notify.
Definition flecs.h:1169
int32_t count
Limit number of notified entities to count.
Definition flecs.h:1181
int32_t offset
Limit notified entities to ones starting from offset (row) in table.
Definition flecs.h:1176
const ecs_type_t * ids
Component ids.
Definition flecs.h:1166
ecs_poly_t * observable
Observable (usually the world)
Definition flecs.h:1198
ecs_entity_t event
The event id.
Definition flecs.h:1161
void * param
Optional context.
Definition flecs.h:1190
A type is a list of (component) ids.
Definition flecs.h:335
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:132