Flecs: include/flecs/addons/cpp/mixins/event/builder.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| 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
5
6#pragma once
7
8#define ECS_EVENT_DESC_ID_COUNT_MAX (8)
9
10namespace flecs {
11
16
18template <typename Base, typename E>
19 struct 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
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
136 struct event_builder : event_builder_base<event_builder, void> {
137 using event_builder_base::event_builder_base;
138};
139
140template <typename E>
141 struct event_builder_typed : event_builder_base<event_builder_typed<E>, E> {
142private:
143 using Class = event_builder_typed<E>;
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
162
163}
ecs_emit
void ecs_emit(ecs_world_t *world, ecs_event_desc_t *desc)
Send event.
ecs_enqueue
void ecs_enqueue(ecs_world_t *world, ecs_event_desc_t *desc)
Enqueue event.
ecs_event_desc_t
struct ecs_event_desc_t ecs_event_desc_t
Used with ecs_emit().
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
ecs_event_desc_t::const_param
const void * const_param
Same as param, but with the guarantee that the value won't be modified.
Definition flecs.h:1408
ecs_event_desc_t::table
ecs_table_t * table
The table for which to notify.
Definition flecs.h:1382
ecs_event_desc_t::count
int32_t count
Limit number of notified entities to count.
Definition flecs.h:1394
ecs_event_desc_t::offset
int32_t offset
Limit notified entities to ones starting from offset (row) in table.
Definition flecs.h:1389
flecs::_::type
Definition flecs.hpp:39
flecs::event_builder_base::id
Base & id()
Add component to emit for.
Definition builder.hpp:31
flecs::event_builder_base::id
Base & id(flecs::id_t id)
Add (component) id to emit for.
Definition builder.hpp:77
flecs::event_builder_base::entity
Base & entity(flecs::entity_t e)
Set entity for which to emit event.
Definition builder.hpp:85
flecs::event_builder_base::id
Base & id(entity_t first, entity_t second)
Add pair to emit for.
Definition builder.hpp:65
flecs::event_builder_base::id
Base & id(entity_t second)
Add pair to emit for.
Definition builder.hpp:56
flecs::event_builder_base::id
Base & id()
Add pair to emit for.
Definition builder.hpp:44
flecs::event_builder_typed
Definition builder.hpp:141
flecs::event_builder
Definition builder.hpp:136
flecs::table
Definition table.hpp:18
flecs::world
The world.
Definition world.hpp:137
flecs::world::event
flecs::event_builder event(flecs::entity_t evt) const
Create a new event.