Skip to content
Flecs v4.1
builder.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/mixins/event/builder.hpp
3 * @brief Event builder.
4 */
5
6#pragma once
7
8#define ECS_EVENT_DESC_ID_COUNT_MAX (8)
9
10namespace flecs {
11
12/**
13 * @ingroup cpp_addons_event
14 * @{
15 */
16
17/** Event builder interface. */
18template <typename Base, typename E>
19struct event_builder_base {
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
29 template <typename... T, typename... Args>
30 Base& id(Args... args) {
31 ids_.array = ids_array_;
32 ids_.array[ids_.count ++] = _::make_id<T...>(world_, args...).id;
33 return *this;
34 }
35
36 /** Set the entity for which to emit the event. */
38 desc_.entity = e;
39 return *this;
40 }
41
42 /** Set the table for which to emit the event. */
43 Base& table(flecs::table_t *t, int32_t offset = 0, int32_t count = 0) {
44 desc_.table = t;
45 desc_.offset = offset;
46 desc_.count = count;
47 return *this;
48 }
49
50 /** Set event data (const). */
51 Base& ctx(const E* ptr) {
52 desc_.const_param = ptr;
53 return *this;
54 }
55
56 /** Set event data (mutable). */
57 Base& ctx(E* ptr) {
58 desc_.param = ptr;
59 return *this;
60 }
61
62 /** Emit the event. */
63 void emit() {
64 ids_.array = ids_array_;
65 desc_.ids = &ids_;
66 desc_.observable = const_cast<flecs::world_t*>(ecs_get_world(world_));
67 ecs_emit(world_, &desc_);
68 }
69
70 /** Enqueue the event. */
71 void enqueue() {
72 ids_.array = ids_array_;
73 desc_.ids = &ids_;
74 desc_.observable = const_cast<flecs::world_t*>(ecs_get_world(world_));
75 ecs_enqueue(world_, &desc_);
76 }
77
78protected:
79 flecs::world_t *world_;
80 ecs_event_desc_t desc_;
81 flecs::type_t ids_;
82 flecs::id_t ids_array_[ECS_EVENT_DESC_ID_COUNT_MAX];
83
84private:
85 operator Base&() {
86 return *static_cast<Base*>(this);
87 }
88};
89
90/** Untyped event builder. */
91struct event_builder : event_builder_base<event_builder, void> {
92 using event_builder_base::event_builder_base;
93};
94
95/** Typed event builder. */
96template <typename E>
97struct event_builder_typed : event_builder_base<event_builder_typed<E>, E> {
98private:
99 using Class = event_builder_typed<E>;
100
101public:
102 using event_builder_base<Class, E>::event_builder_base;
103
104 /** Set event data (const reference). */
105 Class& ctx(const E& ptr) {
106 this->desc_.const_param = &ptr;
107 return *this;
108 }
109
110 /** Set event data (rvalue reference). */
111 Class& ctx(E&& ptr) {
112 this->desc_.param = &ptr;
113 return *this;
114 }
115};
116
117/** @} */
118
119}
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_type_t type_t
Type type.
Definition c_types.hpp:22
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.
Used with ecs_emit().
Definition flecs.h:1460
void enqueue()
Enqueue the event.
Definition builder.hpp:71
Base & ctx(E *ptr)
Set event data (mutable).
Definition builder.hpp:57
Base & entity(flecs::entity_t e)
Set the entity for which to emit the event.
Definition builder.hpp:37
void emit()
Emit the event.
Definition builder.hpp:63
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:43
Base & ctx(const E *ptr)
Set event data (const).
Definition builder.hpp:51
Typed event builder.
Definition builder.hpp:97
Class & ctx(const E &ptr)
Set event data (const reference).
Definition builder.hpp:105
Class & ctx(E &&ptr)
Set event data (rvalue reference).
Definition builder.hpp:111
Untyped event builder.
Definition builder.hpp:91
The world.
Definition world.hpp:129
flecs::event_builder event(flecs::entity_t evt) const
Create a new event.