Flecs v4.0
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
builder_i.hpp
Go to the documentation of this file.
1
6#pragma once
7
9
10namespace flecs
11{
12
17template<typename Base, typename ... Components>
19 query_builder_i(ecs_query_desc_t *desc, int32_t term_index = 0)
20 : term_index_(term_index)
21 , expr_count_(0)
22 , desc_(desc) { }
23
24 Base& query_flags(ecs_flags32_t flags) {
25 desc_->flags |= flags;
26 return *this;
27 }
28
29 Base& cache_kind(query_cache_kind_t kind) {
30 desc_->cache_kind = static_cast<ecs_query_cache_kind_t>(kind);
31 return *this;
32 }
33
34 Base& cached() {
35 return cache_kind(flecs::QueryCacheAuto);
36 }
37
38 Base& expr(const char *expr) {
39 ecs_check(expr_count_ == 0, ECS_INVALID_OPERATION,
40 "query_builder::expr() called more than once");
41 desc_->expr = expr;
42 expr_count_ ++;
43
44 error:
45 return *this;
46 }
47
48 /* With methods */
49
50 template<typename T>
51 Base& with() {
52 this->term();
53 *this->term_ = flecs::term(_::type<T>::id(this->world_v()));
54 this->term_->inout = static_cast<ecs_inout_kind_t>(
55 _::type_to_inout<T>());
56 if (this->term_->inout == EcsInOutDefault) {
57 this->inout_none();
58 }
59 return *this;
60 }
61
62 Base& with(id_t id) {
63 this->term();
64 *this->term_ = flecs::term(id);
65 if (this->term_->inout == EcsInOutDefault) {
66 this->inout_none();
67 }
68 return *this;
69 }
70
71 Base& with(const char *name) {
72 this->term();
73 *this->term_ = flecs::term().first(name);
74 if (this->term_->inout == EcsInOutDefault) {
75 this->inout_none();
76 }
77 return *this;
78 }
79
80 Base& with(const char *first, const char *second) {
81 this->term();
82 *this->term_ = flecs::term().first(first).second(second);
83 if (this->term_->inout == EcsInOutDefault) {
84 this->inout_none();
85 }
86 return *this;
87 }
88
89 Base& with(entity_t r, entity_t o) {
90 this->term();
91 *this->term_ = flecs::term(r, o);
92 if (this->term_->inout == EcsInOutDefault) {
93 this->inout_none();
94 }
95 return *this;
96 }
97
98 Base& with(entity_t r, const char *o) {
99 this->term();
100 *this->term_ = flecs::term(r).second(o);
101 if (this->term_->inout == EcsInOutDefault) {
102 this->inout_none();
103 }
104 return *this;
105 }
106
107 template<typename First>
108 Base& with(id_t o) {
109 return this->with(_::type<First>::id(this->world_v()), o);
110 }
111
112 template<typename First>
113 Base& with(const char *second) {
114 return this->with(_::type<First>::id(this->world_v())).second(second);
115 }
116
117 template<typename First, typename Second>
118 Base& with() {
119 return this->with<First>(_::type<Second>::id(this->world_v()));
120 }
121
122 template <typename E, if_t< is_enum<E>::value > = 0>
123 Base& with(E value) {
124 flecs::entity_t r = _::type<E>::id(this->world_v());
125 auto o = enum_type<E>(this->world_v()).entity(value);
126 return this->with(r, o);
127 }
128
129 Base& with(flecs::term& term) {
130 this->term();
131 *this->term_ = term;
132 return *this;
133 }
134
135 Base& with(flecs::term&& term) {
136 this->term();
137 *this->term_ = term;
138 return *this;
139 }
140
141 /* Without methods, shorthand for .with(...).not_(). */
142
143 template <typename ... Args>
144 Base& without(Args&&... args) {
145 return this->with(FLECS_FWD(args)...).not_();
146 }
147
148 template <typename T, typename ... Args>
149 Base& without(Args&&... args) {
150 return this->with<T>(FLECS_FWD(args)...).not_();
151 }
152
153 template <typename First, typename Second>
154 Base& without() {
155 return this->with<First, Second>().not_();
156 }
157
158 /* Write/read methods */
159
160 Base& write() {
162 return *this;
163 }
164
165 template <typename ... Args>
166 Base& write(Args&&... args) {
167 return this->with(FLECS_FWD(args)...).write();
168 }
169
170 template <typename T, typename ... Args>
171 Base& write(Args&&... args) {
172 return this->with<T>(FLECS_FWD(args)...).write();
173 }
174
175 template <typename First, typename Second>
176 Base& write() {
177 return this->with<First, Second>().write();
178 }
179
180 Base& read() {
182 return *this;
183 }
184
185 template <typename ... Args>
186 Base& read(Args&&... args) {
187 return this->with(FLECS_FWD(args)...).read();
188 }
189
190 template <typename T, typename ... Args>
191 Base& read(Args&&... args) {
192 return this->with<T>(FLECS_FWD(args)...).read();
193 }
194
195 template <typename First, typename Second>
196 Base& read() {
197 return this->with<First, Second>().read();
198 }
199
200 /* Scope_open/scope_close shorthand notation. */
201 Base& scope_open() {
202 return this->with(flecs::ScopeOpen).entity(0);
203 }
204
205 Base& scope_close() {
206 return this->with(flecs::ScopeClose).entity(0);
207 }
208
209 /* Term notation for more complex query features */
210
211 Base& term() {
212 if (this->term_) {
214 ECS_INVALID_OPERATION,
215 "query_builder::term() called without initializing term");
216 }
217
218 ecs_check(term_index_ < FLECS_TERM_COUNT_MAX,
219 ECS_INVALID_PARAMETER, "maximum number of terms exceeded");
220
221 this->set_term(&desc_->terms[term_index_]);
222
223 term_index_ ++;
224
225 error:
226 return *this;
227 }
228
229 Base& term_at(int32_t term_index) {
230 ecs_assert(term_index >= 0, ECS_INVALID_PARAMETER, NULL);
231 int32_t prev_index = term_index_;
232 term_index_ = term_index;
233 this->term();
234 term_index_ = prev_index;
236 ECS_INVALID_PARAMETER, NULL);
237 return *this;
238 }
239
258 template <typename T>
259 Base& order_by(int(*compare)(flecs::entity_t, const T*, flecs::entity_t, const T*)) {
260 ecs_order_by_action_t cmp = reinterpret_cast<ecs_order_by_action_t>(compare);
261 return this->order_by(_::type<T>::id(this->world_v()), cmp);
262 }
263
270 Base& order_by(flecs::entity_t component, int(*compare)(flecs::entity_t, const void*, flecs::entity_t, const void*)) {
271 desc_->order_by_callback = reinterpret_cast<ecs_order_by_action_t>(compare);
272 desc_->order_by = component;
273 return *this;
274 }
275
293 template <typename T>
294 Base& group_by(uint64_t(*group_by_action)(flecs::world_t*, flecs::table_t *table, flecs::id_t id, void* ctx)) {
295 ecs_group_by_action_t action = reinterpret_cast<ecs_group_by_action_t>(group_by_action);
296 return this->group_by(_::type<T>::id(this->world_v()), action);
297 }
298
305 Base& group_by(flecs::entity_t component, uint64_t(*group_by_action)(flecs::world_t*, flecs::table_t *table, flecs::id_t id, void* ctx)) {
306 desc_->group_by_callback = reinterpret_cast<ecs_group_by_action_t>(group_by_action);
307 desc_->group_by = component;
308 return *this;
309 }
310
316 template <typename T>
317 Base& group_by() {
318 return this->group_by(_::type<T>::id(this->world_v()), nullptr);
319 }
320
326 Base& group_by(flecs::entity_t component) {
327 return this->group_by(component, nullptr);
328 }
329
335 Base& group_by_ctx(void *ctx, ecs_ctx_free_t ctx_free = nullptr) {
336 desc_->group_by_ctx = ctx;
337 desc_->group_by_ctx_free = ctx_free;
338 return *this;
339 }
340
344 desc_->on_group_create = action;
345 return *this;
346 }
347
351 desc_->on_group_delete = action;
352 return *this;
353 }
354
355protected:
356 virtual flecs::world_t* world_v() override = 0;
357 int32_t term_index_;
358 int32_t expr_count_;
359
360private:
361 operator Base&() {
362 return *static_cast<Base*>(this);
363 }
364
365 ecs_query_desc_t *desc_;
366};
367
368}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
#define ecs_check(condition, error_code,...)
Check.
Definition log.h:399
uint64_t(* ecs_group_by_action_t)(ecs_world_t *world, ecs_table_t *table, ecs_id_t group_id, void *ctx)
Callback used for grouping tables in a query.
Definition flecs.h:579
void(* ecs_group_delete_action_t)(ecs_world_t *world, uint64_t group_id, void *group_ctx, void *group_by_ctx)
Callback invoked when a query deletes an existing group.
Definition flecs.h:592
void *(* ecs_group_create_action_t)(ecs_world_t *world, uint64_t group_id, void *group_by_ctx)
Callback invoked when a query creates a new group.
Definition flecs.h:586
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
Definition flecs.h:608
int(* ecs_order_by_action_t)(ecs_entity_t e1, const void *ptr1, ecs_entity_t e2, const void *ptr2)
Callback used for comparing components.
Definition flecs.h:561
#define FLECS_TERM_COUNT_MAX
Maximum number of terms in queries.
Definition flecs.h:278
bool ecs_term_is_initialized(const ecs_term_t *term)
Test whether a term is set.
ecs_query_cache_kind_t
Specify cache policy for query.
Definition flecs.h:675
ecs_inout_kind_t
Specify read/write access for term.
Definition flecs.h:654
@ EcsInOutDefault
InOut for regular terms, In for shared terms.
Definition flecs.h:655
Used with ecs_query_init().
Definition flecs.h:1150
ecs_id_t group_by
Component id to be used for grouping.
Definition flecs.h:1181
ecs_term_t terms[32]
Query terms.
Definition flecs.h:1155
ecs_ctx_free_t group_by_ctx_free
Function to free group_by_ctx.
Definition flecs.h:1202
void * group_by_ctx
Context to pass to group_by.
Definition flecs.h:1199
ecs_entity_t order_by
Component to sort on, used together with order_by_callback or order_by_table_callback.
Definition flecs.h:1177
ecs_order_by_action_t order_by_callback
Callback used for ordering query results.
Definition flecs.h:1169
ecs_group_create_action_t on_group_create
Callback that is invoked when a new group is created.
Definition flecs.h:1192
ecs_group_by_action_t group_by_callback
Callback used for grouping results.
Definition flecs.h:1188
ecs_group_delete_action_t on_group_delete
Callback that is invoked when an existing group is deleted.
Definition flecs.h:1196
ecs_flags32_t flags
Flags for enabling query features.
Definition flecs.h:1164
ecs_query_cache_kind_t cache_kind
Caching policy of query.
Definition flecs.h:1161
const char * expr
Query DSL expression (optional)
Definition flecs.h:1158
int16_t inout
Access to contents matched by term.
Definition flecs.h:774
Component class.
Definition flecs.hpp:30
Query builder interface.
Definition builder_i.hpp:18
Base & group_by()
Group and sort matched tables.
Base & on_group_delete(ecs_group_delete_action_t action)
Specify on_group_delete action.
Base & group_by(flecs::entity_t component, uint64_t(*group_by_action)(flecs::world_t *, flecs::table_t *table, flecs::id_t id, void *ctx))
Group and sort matched tables.
Base & on_group_create(ecs_group_create_action_t action)
Specify on_group_create action.
Base & group_by(flecs::entity_t component)
Group and sort matched tables.
Base & group_by(uint64_t(*group_by_action)(flecs::world_t *, flecs::table_t *table, flecs::id_t id, void *ctx))
Group and sort matched tables.
Base & order_by(int(*compare)(flecs::entity_t, const T *, flecs::entity_t, const T *))
Sort the output of a query.
Base & group_by_ctx(void *ctx, ecs_ctx_free_t ctx_free=nullptr)
Specify context to be passed to group_by function.
Base & order_by(flecs::entity_t component, int(*compare)(flecs::entity_t, const void *, flecs::entity_t, const void *))
Sort the output of a query.
Term builder interface.
Definition builder_i.hpp:99
Base & read()
Short for inout_stage(flecs::In).
Base & inout_none()
Short for inout(flecs::In)
Base & write()
Short for inout_stage(flecs::Out).
Class that describes a term.
Definition impl.hpp:16
Term builder interface.