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

| 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
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
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
90 Base& entity(flecs::entity_t e) {
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
144 struct event_builder : event_builder_base<event_builder, void> {
145 using event_builder_base::event_builder_base;
146};
147
149template <typename E>
150 struct event_builder_typed : event_builder_base<event_builder_typed<E>, E> {
151private:
152 using Class = event_builder_typed<E>;
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
171
172}
flecs::table_t
ecs_table_t table_t
Table type.
Definition c_types.hpp:23
flecs::id_t
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
flecs::type_t
ecs_type_t type_t
Type type.
Definition c_types.hpp:22
flecs::world_t
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_emit
void ecs_emit(ecs_world_t *world, ecs_event_desc_t *desc)
Send an event.
ecs_enqueue
void ecs_enqueue(ecs_world_t *world, ecs_event_desc_t *desc)
Enqueue an event.
ecs_get_world
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
ecs_event_desc_t
Used with ecs_emit().
Definition flecs.h:1410
flecs::_::type
Definition flecs.hpp:39
flecs::event_builder_base::enqueue
void enqueue()
Enqueue the event.
Definition builder.hpp:124
flecs::event_builder_base::id
Base & id(Enum value)
Add an enum constant to emit for.
Definition builder.hpp:75
flecs::event_builder_base::ctx
Base & ctx(E *ptr)
Set event data (mutable).
Definition builder.hpp:110
flecs::event_builder_base::id
Base & id()
Add a component to emit for.
Definition builder.hpp:31
flecs::event_builder_base::id
Base & id(flecs::id_t id)
Add a (component) ID to emit for.
Definition builder.hpp:82
flecs::event_builder_base::entity
Base & entity(flecs::entity_t e)
Set the entity for which to emit the event.
Definition builder.hpp:90
flecs::event_builder_base::id
Base & id(entity_t first, entity_t second)
Add a pair to emit for.
Definition builder.hpp:65
flecs::event_builder_base::emit
void emit()
Emit the event.
Definition builder.hpp:116
flecs::event_builder_base::id
Base & id(entity_t second)
Add a pair to emit for.
Definition builder.hpp:56
flecs::event_builder_base::table
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
flecs::event_builder_base::id
Base & id()
Add a pair to emit for.
Definition builder.hpp:44
flecs::event_builder_base::ctx
Base & ctx(const E *ptr)
Set event data (const).
Definition builder.hpp:104
flecs::event_builder_typed
Typed event builder.
Definition builder.hpp:150
flecs::event_builder_typed::ctx
Class & ctx(const E &ptr)
Set event data (const reference).
Definition builder.hpp:158
flecs::event_builder_typed::ctx
Class & ctx(E &&ptr)
Set event data (rvalue reference).
Definition builder.hpp:164
flecs::event_builder
Untyped event builder.
Definition builder.hpp:144
flecs::world
The world.
Definition world.hpp:246
flecs::world::event
flecs::event_builder event(flecs::entity_t evt) const
Create a new event.