Flecs: include/flecs/addons/cpp/mixins/term/builder_i.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_i.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8#include "../../utils/signature.hpp"
9#include <stdio.h>
10
11namespace flecs
12{
13
21template<typename Base>
22 struct term_ref_builder_i {
24 term_ref_builder_i() : term_ref_(nullptr) { }
25
27 virtual ~term_ref_builder_i() { }
28
30 Base& self() {
31 this->assert_term_ref();
32 term_ref_->id |= flecs::Self;
33 return *this;
34 }
35
37 Base& id(flecs::entity_t id) {
38 this->assert_term_ref();
39 term_ref_->id = id;
40 return *this;
41 }
42
51 Base& entity(flecs::entity_t entity) {
52 this->assert_term_ref();
53 term_ref_->id = entity | flecs::IsEntity;
54 return *this;
55 }
56
58 Base& name(const char *name) {
59 this->assert_term_ref();
60 term_ref_->id |= flecs::IsEntity;
61 term_ref_->name = const_cast<char*>(name);
62 return *this;
63 }
64
66 Base& var(const char *var_name) {
67 this->assert_term_ref();
68 term_ref_->id |= flecs::IsVariable;
69 term_ref_->name = const_cast<char*>(var_name);
70 return *this;
71 }
72
74 Base& flags(flecs::flags64_t flags) {
75 this->assert_term_ref();
76 term_ref_->id = flags;
77 return *this;
78 }
79
81 ecs_term_ref_t *term_ref_;
82
83protected:
84 virtual flecs::world_t* world_v() = 0;
85
86 void assert_term_ref() {
87 ecs_assert(term_ref_ != nullptr, ECS_INVALID_PARAMETER,
88 "no active term (call .with() first)");
89 }
90
91private:
92 operator Base&() {
93 return *static_cast<Base*>(this);
94 }
95};
96
102template<typename Base>
103 struct term_builder_i : term_ref_builder_i<Base> {
105 term_builder_i() : term_(nullptr) { }
106
108 term_builder_i(ecs_term_t *term_ptr) {
109 set_term(term_ptr);
110 }
111
113 Base& term(id_t id) {
114 return this->id(id);
115 }
116
118 Base& src() {
119 this->assert_term();
120 this->term_ref_ = &term_->src;
121 return *this;
122 }
123
127 Base& first() {
128 this->assert_term();
129 this->term_ref_ = &term_->first;
130 return *this;
131 }
132
135 Base& second() {
136 this->assert_term();
137 this->term_ref_ = &term_->second;
138 return *this;
139 }
140
142 Base& src(flecs::entity_t id) {
143 this->src();
144 this->id(id);
145 return *this;
146 }
147
149 template<typename T>
150 Base& src() {
151 this->src(_::type<T>::id(this->world_v()));
152 return *this;
153 }
154
157 Base& src(const char *name) {
158 ecs_assert(name != nullptr, ECS_INVALID_PARAMETER, nullptr);
159 this->src();
160 if (name[0] == '$') {
161 this->var(&name[1]);
162 } else {
163 this->name(name);
164 }
165 return *this;
166 }
167
169 Base& first(flecs::entity_t id) {
170 this->first();
171 this->id(id);
172 return *this;
173 }
174
176 template<typename T>
177 Base& first() {
178 this->first(_::type<T>::id(this->world_v()));
179 return *this;
180 }
181
184 Base& first(const char *name) {
185 ecs_assert(name != nullptr, ECS_INVALID_PARAMETER, nullptr);
186 this->first();
187 if (name[0] == '$') {
188 this->var(&name[1]);
189 } else {
190 this->name(name);
191 }
192 return *this;
193 }
194
196 Base& second(flecs::entity_t id) {
197 this->second();
198 this->id(id);
199 return *this;
200 }
201
203 template<typename T>
204 Base& second() {
205 this->second(_::type<T>::id(this->world_v()));
206 return *this;
207 }
208
211 Base& second(const char *name) {
212 ecs_assert(name != nullptr, ECS_INVALID_PARAMETER, nullptr);
213 this->second();
214 if (name[0] == '$') {
215 this->var(&name[1]);
216 } else {
217 this->name(name);
218 }
219 return *this;
220 }
221
225 Base& up(flecs::entity_t trav = 0) {
226 this->assert_term_ref();
227 ecs_check(this->term_ref_ != &term_->first, ECS_INVALID_PARAMETER,
228 "up traversal can only be applied to term source");
229 ecs_check(this->term_ref_ != &term_->second, ECS_INVALID_PARAMETER,
230 "up traversal can only be applied to term source");
231 this->term_ref_->id |= flecs::Up;
232 if (trav) {
233 term_->trav = trav;
234 }
235 error:
236 return *this;
237 }
238
240 template <typename Trav>
241 Base& up() {
242 return this->up(_::type<Trav>::id(this->world_v()));
243 }
244
247 Base& cascade(flecs::entity_t trav = 0) {
248 this->assert_term_ref();
249 this->up();
250 this->term_ref_->id |= flecs::Cascade;
251 if (trav) {
252 term_->trav = trav;
253 }
254 return *this;
255 }
256
258 template <typename Trav>
259 Base& cascade() {
260 return this->cascade(_::type<Trav>::id(this->world_v()));
261 }
262
264 Base& desc() {
265 this->assert_term_ref();
266 this->term_ref_->id |= flecs::Desc;
267 return *this;
268 }
269
271 Base& parent() {
272 return this->up();
273 }
274
276 Base& trav(flecs::entity_t trav, flecs::flags32_t flags = 0) {
277 this->assert_term_ref();
278 term_->trav = trav;
279 this->term_ref_->id |= flags;
280 return *this;
281 }
282
284 Base& id_flags(id_t flags) {
285 this->assert_term();
286 term_->id |= flags;
287 return *this;
288 }
289
291 Base& inout(flecs::inout_kind_t inout) {
292 this->assert_term();
293 term_->inout = static_cast<int16_t>(inout);
294 return *this;
295 }
296
305 Base& inout_stage(flecs::inout_kind_t inout) {
306 this->assert_term();
307 term_->inout = static_cast<int16_t>(inout);
308 if (term_->oper != EcsNot) {
309 this->src().entity(0);
310 }
311 return *this;
312 }
313
317 Base& write() {
318 return this->inout_stage(flecs::Out);
319 }
320
324 Base& read() {
325 return this->inout_stage(flecs::In);
326 }
327
331 Base& read_write() {
332 return this->inout_stage(flecs::InOut);
333 }
334
336 Base& in() {
337 return this->inout(flecs::In);
338 }
339
341 Base& out() {
342 return this->inout(flecs::Out);
343 }
344
346 Base& inout() {
347 return this->inout(flecs::InOut);
348 }
349
351 Base& inout_none() {
352 return this->inout(flecs::InOutNone);
353 }
354
356 Base& oper(flecs::oper_kind_t oper) {
357 this->assert_term();
358 term_->oper = static_cast<int16_t>(oper);
359 return *this;
360 }
361
363 Base& and_() {
364 return this->oper(flecs::And);
365 }
366
368 Base& or_() {
369 return this->oper(flecs::Or);
370 }
371
373 Base& not_() {
374 return this->oper(flecs::Not);
375 }
376
378 Base& optional() {
379 return this->oper(flecs::Optional);
380 }
381
383 Base& and_from() {
384 return this->oper(flecs::AndFrom);
385 }
386
388 Base& or_from() {
389 return this->oper(flecs::OrFrom);
390 }
391
393 Base& not_from() {
394 return this->oper(flecs::NotFrom);
395 }
396
399 Base& filter() {
400 term_->inout = EcsInOutFilter;
401 return *this;
402 }
403
405 ecs_term_t *term_;
406
407protected:
408 virtual flecs::world_t* world_v() override = 0;
409
411 void set_term(ecs_term_t *term) {
412 term_ = term;
413 if (term) {
414 this->term_ref_ = &term_->src; // default to source
415 } else {
416 this->term_ref_ = nullptr;
417 }
418 }
419
420private:
421 void assert_term() {
422 ecs_assert(term_ != nullptr, ECS_INVALID_PARAMETER,
423 "no active term (call .with() first)");
424 }
425
426 operator Base&() {
427 return *static_cast<Base*>(this);
428 }
429};
430
431}
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
ECS_INVALID_PARAMETER
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
ecs_check
#define ecs_check(condition, error_code,...)
Check.
Definition log.h:527
flecs::id_t
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
flecs::flags64_t
ecs_flags64_t flags64_t
64-bit flags type.
Definition c_types.hpp:36
flecs::oper_kind_t
oper_kind_t
Operator kind.
Definition c_types.hpp:49
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
flecs::flags32_t
ecs_flags32_t flags32_t
32-bit flags type.
Definition c_types.hpp:35
flecs::world_t
ecs_world_t world_t
World type.
Definition c_types.hpp:18
flecs::inout_kind_t
inout_kind_t
Inout kind.
Definition c_types.hpp:39
flecs::NotFrom
@ NotFrom
NotFrom operator.
Definition c_types.hpp:56
flecs::OrFrom
@ OrFrom
OrFrom operator.
Definition c_types.hpp:55
flecs::AndFrom
@ AndFrom
AndFrom operator.
Definition c_types.hpp:54
flecs::And
@ And
And operator.
Definition c_types.hpp:50
flecs::Optional
@ Optional
Optional operator.
Definition c_types.hpp:53
flecs::Or
@ Or
Or operator.
Definition c_types.hpp:51
flecs::Not
@ Not
Not operator.
Definition c_types.hpp:52
flecs::In
@ In
In.
Definition c_types.hpp:44
flecs::Out
@ Out
Out.
Definition c_types.hpp:45
flecs::InOut
@ InOut
InOut.
Definition c_types.hpp:43
flecs::InOutNone
@ InOutNone
InOutNone.
Definition c_types.hpp:41
EcsInOutFilter
@ EcsInOutFilter
Same as InOutNone + prevents term from triggering observers.
Definition flecs.h:711
EcsNot
@ EcsNot
The term must not match.
Definition flecs.h:721
signature.hpp
Compile-time utilities for deriving query attributes from a parameter pack.
ecs_term_ref_t
Type that describes a reference to an entity or variable in a term.
Definition flecs.h:799
ecs_term_t
Type that describes a term (single element in a query).
Definition flecs.h:814
flecs::_::type
Definition flecs.hpp:39
flecs::term_builder_i::filter
Base & filter()
Mark the term as a filter.
Definition builder_i.hpp:399
flecs::term_builder_i::inout
Base & inout(flecs::inout_kind_t inout)
Set read/write access of the term.
Definition builder_i.hpp:291
flecs::term_builder_i::or_from
Base & or_from()
Short for oper(flecs::OrFrom).
Definition builder_i.hpp:388
flecs::term_builder_i::set_term
void set_term(ecs_term_t *term)
Set the current term pointer.
Definition builder_i.hpp:411
flecs::term_builder_i::second
Base & second(flecs::entity_t id)
Select the second identifier, initialize it with an entity ID.
Definition builder_i.hpp:196
flecs::term_builder_i::cascade
Base & cascade(flecs::entity_t trav=0)
The cascade flag is like up(), but returns results in breadth-first order.
Definition builder_i.hpp:247
flecs::term_builder_i::oper
Base & oper(flecs::oper_kind_t oper)
Set the operator of the term.
Definition builder_i.hpp:356
flecs::term_builder_i::first
Base & first(const char *name)
Select the first identifier, initialize it with a name.
Definition builder_i.hpp:184
flecs::term_builder_i::read_write
Base & read_write()
Short for inout_stage(flecs::InOut).
Definition builder_i.hpp:331
flecs::term_builder_i::term
Base & term(id_t id)
Set the term ID.
Definition builder_i.hpp:113
flecs::term_builder_i::parent
Base & parent()
Same as up().
Definition builder_i.hpp:271
flecs::term_builder_i::inout
Base & inout()
Short for inout(flecs::InOut).
Definition builder_i.hpp:346
flecs::term_builder_i::desc
Base & desc()
Use with cascade() to iterate results in descending (bottom-to-top) order.
Definition builder_i.hpp:264
flecs::term_builder_i::in
Base & in()
Short for inout(flecs::In).
Definition builder_i.hpp:336
flecs::term_builder_i::inout_stage
Base & inout_stage(flecs::inout_kind_t inout)
Set read/write access for a stage.
Definition builder_i.hpp:305
flecs::term_builder_i::trav
Base & trav(flecs::entity_t trav, flecs::flags32_t flags=0)
Specify the relationship to traverse, and flags to indicate direction.
Definition builder_i.hpp:276
flecs::term_builder_i::or_
Base & or_()
Short for oper(flecs::Or).
Definition builder_i.hpp:368
flecs::term_builder_i::first
Base & first()
Select the first identifier, initialize it with the ID associated with the type.
Definition builder_i.hpp:177
flecs::term_builder_i::second
Base & second()
Select the second identifier, initialize it with the ID associated with the type.
Definition builder_i.hpp:204
flecs::term_builder_i::src
Base & src()
Call prior to setting values for the src identifier.
Definition builder_i.hpp:118
flecs::term_builder_i::and_from
Base & and_from()
Short for oper(flecs::AndFrom).
Definition builder_i.hpp:383
flecs::term_builder_i::src
Base & src()
Select the src identifier, initialize it with the ID associated with the type.
Definition builder_i.hpp:150
flecs::term_builder_i::out
Base & out()
Short for inout(flecs::Out).
Definition builder_i.hpp:341
flecs::term_builder_i::up
Base & up()
Traverse upwards using the specified relationship type.
Definition builder_i.hpp:241
flecs::term_builder_i::and_
Base & and_()
Short for oper(flecs::And).
Definition builder_i.hpp:363
flecs::term_builder_i::term_
ecs_term_t * term_
Pointer to the current term.
Definition builder_i.hpp:405
flecs::term_builder_i::optional
Base & optional()
Short for oper(flecs::Optional).
Definition builder_i.hpp:378
flecs::term_builder_i::cascade
Base & cascade()
Cascade using the specified relationship type.
Definition builder_i.hpp:259
flecs::term_builder_i::second
Base & second()
Call prior to setting values for the second identifier.
Definition builder_i.hpp:135
flecs::term_builder_i::src
Base & src(flecs::entity_t id)
Select the src identifier, initialize it with an entity ID.
Definition builder_i.hpp:142
flecs::term_builder_i::read
Base & read()
Short for inout_stage(flecs::In).
Definition builder_i.hpp:324
flecs::term_builder_i::term_builder_i
term_builder_i(ecs_term_t *term_ptr)
Construct from a term pointer.
Definition builder_i.hpp:108
flecs::term_builder_i::up
Base & up(flecs::entity_t trav=0)
The up flag indicates that the term identifier may be substituted by traversing a relationship upward...
Definition builder_i.hpp:225
flecs::term_builder_i::first
Base & first(flecs::entity_t id)
Select the first identifier, initialize it with an entity ID.
Definition builder_i.hpp:169
flecs::term_builder_i::inout_none
Base & inout_none()
Short for inout(flecs::InOutNone).
Definition builder_i.hpp:351
flecs::term_builder_i::term_builder_i
term_builder_i()
Default constructor.
Definition builder_i.hpp:105
flecs::term_builder_i::src
Base & src(const char *name)
Select the src identifier, initialize it with a name.
Definition builder_i.hpp:157
flecs::term_builder_i::id_flags
Base & id_flags(id_t flags)
Set ID flags for the term.
Definition builder_i.hpp:284
flecs::term_builder_i::second
Base & second(const char *name)
Select the second identifier, initialize it with a name.
Definition builder_i.hpp:211
flecs::term_builder_i::not_from
Base & not_from()
Short for oper(flecs::NotFrom).
Definition builder_i.hpp:393
flecs::term_builder_i::not_
Base & not_()
Short for oper(flecs::Not).
Definition builder_i.hpp:373
flecs::term_builder_i::write
Base & write()
Short for inout_stage(flecs::Out).
Definition builder_i.hpp:317
flecs::term_builder_i::first
Base & first()
Call prior to setting values for the first identifier.
Definition builder_i.hpp:127
flecs::term_ref_builder_i::name
Base & name(const char *name)
Specify the value of the identifier by name.
Definition builder_i.hpp:58
flecs::term_ref_builder_i::id
Base & id(flecs::entity_t id)
Specify the value of the identifier by ID.
Definition builder_i.hpp:37
flecs::term_ref_builder_i::term_ref_
ecs_term_ref_t * term_ref_
Pointer to the current term reference.
Definition builder_i.hpp:81
flecs::term_ref_builder_i::~term_ref_builder_i
virtual ~term_ref_builder_i()
Destructor.
Definition builder_i.hpp:27
flecs::term_ref_builder_i::entity
Base & entity(flecs::entity_t entity)
Specify the value of the identifier by ID.
Definition builder_i.hpp:51
flecs::term_ref_builder_i::var
Base & var(const char *var_name)
Specify that the identifier is a variable (resolved at query evaluation time).
Definition builder_i.hpp:66
flecs::term_ref_builder_i::self
Base & self()
The self flag indicates that the term identifier itself is used.
Definition builder_i.hpp:30
flecs::term_ref_builder_i::flags
Base & flags(flecs::flags64_t flags)
Override the term ID flags.
Definition builder_i.hpp:74
flecs::term_ref_builder_i::term_ref_builder_i
term_ref_builder_i()
Default constructor.
Definition builder_i.hpp:24