Flecs v4.0
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 : 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
69 template <typename Enum, if_t<is_enum<Enum>::value> = 0>
70 Base& id(Enum value) {
71 const auto& et = enum_type<Enum>(this->world_);
72 flecs::entity_t target = et.entity(value);
73 return id(et.entity(), target);
74 }
75
77 Base& id(flecs::id_t id) {
78 ids_.array = ids_array_;
79 ids_.array[ids_.count] = id;
80 ids_.count ++;
81 return *this;
82 }
83
85 Base& entity(flecs::entity_t e) {
86 desc_.entity = e;
87 return *this;
88 }
89
90 /* Set table for which to emit event */
91 Base& table(flecs::table_t *t, int32_t offset = 0, int32_t count = 0) {
92 desc_.table = t;
93 desc_.offset = offset;
94 desc_.count = count;
95 return *this;
96 }
97
98 /* Set event data */
99 Base& ctx(const E* ptr) {
100 desc_.const_param = ptr;
101 return *this;
102 }
103
104 /* Set event data */
105 Base& ctx(E* ptr) {
106 desc_.param = ptr;
107 return *this;
108 }
109
110 void emit() {
111 ids_.array = ids_array_;
112 desc_.ids = &ids_;
113 desc_.observable = const_cast<flecs::world_t*>(ecs_get_world(world_));
114 ecs_emit(world_, &desc_);
115 }
116
117 void enqueue() {
118 ids_.array = ids_array_;
119 desc_.ids = &ids_;
120 desc_.observable = const_cast<flecs::world_t*>(ecs_get_world(world_));
121 ecs_enqueue(world_, &desc_);
122 }
123
124protected:
125 flecs::world_t *world_;
126 ecs_event_desc_t desc_;
127 flecs::type_t ids_;
128 flecs::id_t ids_array_[ECS_EVENT_DESC_ID_COUNT_MAX];
129
130private:
131 operator Base&() {
132 return *static_cast<Base*>(this);
133 }
134};
135
136struct event_builder : event_builder_base<event_builder, void> {
137 using event_builder_base::event_builder_base;
138};
139
140template <typename E>
141struct event_builder_typed : event_builder_base<event_builder_typed<E>, E> {
142private:
144
145public:
146 using event_builder_base<Class, E>::event_builder_base;
147
148 /* Set event data */
149 Class& ctx(const E& ptr) {
150 this->desc_.const_param = &ptr;
151 return *this;
152 }
153
154 /* Set event data */
155 Class& ctx(E&& ptr) {
156 this->desc_.param = &ptr;
157 return *this;
158 }
159};
160
163}
void ecs_emit(ecs_world_t *world, ecs_event_desc_t *desc)
Send event.
void ecs_enqueue(ecs_world_t *world, ecs_event_desc_t *desc)
Enqueue event.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
Component added to enum type entities.
Definition meta.h:277
Used with ecs_emit().
Definition flecs.h:1274
ecs_entity_t entity
Single-entity alternative to setting table / offset / count.
Definition flecs.h:1299
const void * const_param
Same as param, but with the guarantee that the value won't be modified.
Definition flecs.h:1310
ecs_table_t * table
The table for which to notify.
Definition flecs.h:1284
int32_t count
Limit number of notified entities to count.
Definition flecs.h:1296
int32_t offset
Limit notified entities to ones starting from offset (row) in table.
Definition flecs.h:1291
const ecs_type_t * ids
Component ids.
Definition flecs.h:1281
ecs_poly_t * observable
Observable (usually the world)
Definition flecs.h:1313
ecs_entity_t event
The event id.
Definition flecs.h:1276
void * param
Optional context.
Definition flecs.h:1305
A type is a list of (component) ids.
Definition flecs.h:356
ecs_id_t * array
Array with ids.
Definition flecs.h:357
int32_t count
Number of elements in array.
Definition flecs.h:358
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:77
Base & entity(flecs::entity_t e)
Set entity for which to emit event.
Definition builder.hpp:85
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:137