Flecs v4.1
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>
21 : world_(world)
22 , desc_{}
23 , ids_{}
24 , ids_array_{}
25 {
26 desc_.event = event;
27 }
28
30 template <typename T>
31 Base& id() {
32 ids_.array = ids_array_;
33 ids_.array[ids_.count] = _::type<T>().id(world_);
34 ids_.count ++;
35 return *this;
36 }
37
43 template <typename First, typename Second>
44 Base& id() {
45 return id(
46 ecs_pair(_::type<First>::id(this->world_),
47 _::type<Second>::id(this->world_)));
48 }
49
55 template <typename First>
56 Base& id(entity_t second) {
57 return id(ecs_pair(_::type<First>::id(this->world_), second));
58 }
59
65 Base& id(entity_t first, entity_t second) {
66 return id(ecs_pair(first, second));
67 }
68
74 template <typename Enum, if_t<is_enum<Enum>::value> = 0>
75 Base& id(Enum value) {
76 const auto& et = enum_type<Enum>(this->world_);
77 flecs::entity_t target = et.entity(value);
78 return id(et.entity(), target);
79 }
80
82 Base& id(flecs::id_t id) {
83 ids_.array = ids_array_;
84 ids_.array[ids_.count] = id;
85 ids_.count ++;
86 return *this;
87 }
88
91 desc_.entity = e;
92 return *this;
93 }
94
96 Base& table(flecs::table_t *t, int32_t offset = 0, int32_t count = 0) {
97 desc_.table = t;
98 desc_.offset = offset;
99 desc_.count = count;
100 return *this;
101 }
102
104 Base& ctx(const E* ptr) {
105 desc_.const_param = ptr;
106 return *this;
107 }
108
110 Base& ctx(E* ptr) {
111 desc_.param = ptr;
112 return *this;
113 }
114
116 void emit() {
117 ids_.array = ids_array_;
118 desc_.ids = &ids_;
119 desc_.observable = const_cast<flecs::world_t*>(ecs_get_world(world_));
120 ecs_emit(world_, &desc_);
121 }
122
124 void enqueue() {
125 ids_.array = ids_array_;
126 desc_.ids = &ids_;
127 desc_.observable = const_cast<flecs::world_t*>(ecs_get_world(world_));
128 ecs_enqueue(world_, &desc_);
129 }
130
131protected:
132 flecs::world_t *world_;
133 ecs_event_desc_t desc_;
134 flecs::type_t ids_;
135 flecs::id_t ids_array_[ECS_EVENT_DESC_ID_COUNT_MAX];
136
137private:
138 operator Base&() {
139 return *static_cast<Base*>(this);
140 }
141};
142
144struct event_builder : event_builder_base<event_builder, void> {
145 using event_builder_base::event_builder_base;
146};
147
149template <typename E>
150struct event_builder_typed : event_builder_base<event_builder_typed<E>, E> {
151private:
153
154public:
155 using event_builder_base<Class, E>::event_builder_base;
156
158 Class& ctx(const E& ptr) {
159 this->desc_.const_param = &ptr;
160 return *this;
161 }
162
164 Class& ctx(E&& ptr) {
165 this->desc_.param = &ptr;
166 return *this;
167 }
168};
169
172}
ecs_table_t table_t
Table type.
Definition c_types.hpp:23
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
void ecs_emit(ecs_world_t *world, ecs_event_desc_t *desc)
Send an event.
void ecs_enqueue(ecs_world_t *world, ecs_event_desc_t *desc)
Enqueue an event.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
Component added to enum type entities.
Definition meta.h:285
Used with ecs_emit().
Definition flecs.h:1410
ecs_entity_t entity
Single-entity alternative to setting table / offset / count.
Definition flecs.h:1435
const void * const_param
Same as param, but with the guarantee that the value won't be modified.
Definition flecs.h:1446
ecs_table_t * table
The table for which to notify.
Definition flecs.h:1420
int32_t count
Limit number of notified entities to count.
Definition flecs.h:1432
int32_t offset
Limit notified entities to ones starting from offset (row) in table.
Definition flecs.h:1427
const ecs_type_t * ids
Component IDs.
Definition flecs.h:1417
ecs_poly_t * observable
Observable (usually the world).
Definition flecs.h:1449
ecs_entity_t event
The event ID.
Definition flecs.h:1412
void * param
Optional context.
Definition flecs.h:1441
A type is a list of (component) IDs.
Definition flecs.h:398
ecs_id_t * array
Array with IDs.
Definition flecs.h:399
int32_t count
Number of elements in array.
Definition flecs.h:400
Event builder interface.
Definition builder.hpp:19
void enqueue()
Enqueue the event.
Definition builder.hpp:124
Base & id(Enum value)
Add an enum constant to emit for.
Definition builder.hpp:75
Base & ctx(E *ptr)
Set event data (mutable).
Definition builder.hpp:110
Base & id()
Add a component to emit for.
Definition builder.hpp:31
Base & id(flecs::id_t id)
Add a (component) ID to emit for.
Definition builder.hpp:82
Base & entity(flecs::entity_t e)
Set the entity for which to emit the event.
Definition builder.hpp:90
Base & id(entity_t first, entity_t second)
Add a pair to emit for.
Definition builder.hpp:65
void emit()
Emit the event.
Definition builder.hpp:116
Base & id(entity_t second)
Add a pair to emit for.
Definition builder.hpp:56
Base & table(flecs::table_t *t, int32_t offset=0, int32_t count=0)
Set the table for which to emit the event.
Definition builder.hpp:96
Base & id()
Add a pair to emit for.
Definition builder.hpp:44
Base & ctx(const E *ptr)
Set event data (const).
Definition builder.hpp:104
Typed event builder.
Definition builder.hpp:150
Class & ctx(const E &ptr)
Set event data (const reference).
Definition builder.hpp:158
Class & ctx(E &&ptr)
Set event data (rvalue reference).
Definition builder.hpp:164
Untyped event builder.
Definition builder.hpp:144
The world.
Definition world.hpp:246