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
6#pragma once
7
9#include <stdio.h>
10
11namespace flecs
12{
13
21template<typename Base>
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
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
75 this->assert_term_ref();
77 return *this;
78 }
79
82
83protected:
84 virtual flecs::world_t* world_v() = 0;
85
86 void assert_term_ref() {
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>
105 term_builder_i() : term_(nullptr) { }
106
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
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 != NULL, ECS_INVALID_PARAMETER, NULL);
159 this->src();
160 if (name[0] == '$') {
161 this->var(&name[1]);
162 } else {
163 this->name(name);
164 }
165 return *this;
166 }
167
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 != NULL, ECS_INVALID_PARAMETER, NULL);
186 this->first();
187 if (name[0] == '$') {
188 this->var(&name[1]);
189 } else {
190 this->name(name);
191 }
192 return *this;
193 }
194
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 != NULL, ECS_INVALID_PARAMETER, NULL);
213 this->second();
214 if (name[0] == '$') {
215 this->var(&name[1]);
216 } else {
217 this->name(name);
218 }
219 return *this;
220 }
221
226 this->assert_term_ref();
228 "up traversal can only be applied to term source");
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
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
277 this->assert_term_ref();
278 term_->trav = trav;
279 this->term_ref_->id |= flags;
280 return *this;
281 }
282
285 this->assert_term();
286 term_->id |= flags;
287 return *this;
288 }
289
292 this->assert_term();
293 term_->inout = static_cast<int16_t>(inout);
294 return *this;
295 }
296
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
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() {
401 return *this;
402 }
403
406
407protected:
408 virtual flecs::world_t* world_v() override = 0;
409
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() {
423 "no active term (call .with() first)");
424 }
425
426 operator Base&() {
427 return *static_cast<Base*>(this);
428 }
429};
430
431}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:661
#define ecs_check(condition, error_code,...)
Check.
Definition log.h:517
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_flags64_t flags64_t
64-bit flags type.
Definition c_types.hpp:36
oper_kind_t
Operator kind.
Definition c_types.hpp:49
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_flags32_t flags32_t
32-bit flags type.
Definition c_types.hpp:35
ecs_world_t world_t
World type.
Definition c_types.hpp:18
inout_kind_t
Inout kind.
Definition c_types.hpp:39
@ NotFrom
NotFrom operator.
Definition c_types.hpp:56
@ OrFrom
OrFrom operator.
Definition c_types.hpp:55
@ AndFrom
AndFrom operator.
Definition c_types.hpp:54
@ And
And operator.
Definition c_types.hpp:50
@ Optional
Optional operator.
Definition c_types.hpp:53
@ Or
Or operator.
Definition c_types.hpp:51
@ Not
Not operator.
Definition c_types.hpp:52
@ In
In.
Definition c_types.hpp:44
@ Out
Out.
Definition c_types.hpp:45
@ InOut
InOut.
Definition c_types.hpp:43
@ InOutNone
InOutNone.
Definition c_types.hpp:41
@ EcsInOutFilter
Same as InOutNone + prevents term from triggering observers.
Definition flecs.h:707
@ EcsNot
The term must not match.
Definition flecs.h:717
Compile-time utilities for deriving query attributes from a parameter pack.
Type that describes a reference to an entity or variable in a term.
Definition flecs.h:795
const char * name
Name.
Definition flecs.h:802
ecs_entity_t id
Entity ID.
Definition flecs.h:796
Type that describes a term (single element in a query).
Definition flecs.h:810
ecs_term_ref_t src
Source of term.
Definition flecs.h:816
ecs_id_t id
Component ID to be matched by term.
Definition flecs.h:811
int16_t oper
Operator of term.
Definition flecs.h:825
ecs_term_ref_t second
Second element of pair.
Definition flecs.h:818
ecs_entity_t trav
Relationship to traverse when looking for the component.
Definition flecs.h:820
int16_t inout
Access to contents matched by term.
Definition flecs.h:824
ecs_term_ref_t first
Component or first element of pair.
Definition flecs.h:817
Entity.
Definition entity.hpp:30
Term builder interface.
Base & filter()
Mark the term as a filter.
Base & inout(flecs::inout_kind_t inout)
Set read/write access of the term.
Base & or_from()
Short for oper(flecs::OrFrom).
void set_term(ecs_term_t *term)
Set the current term pointer.
Base & second(flecs::entity_t id)
Select the second identifier, initialize it with an entity ID.
Base & cascade(flecs::entity_t trav=0)
The cascade flag is like up(), but returns results in breadth-first order.
Base & oper(flecs::oper_kind_t oper)
Set the operator of the term.
Base & first(const char *name)
Select the first identifier, initialize it with a name.
Base & read_write()
Short for inout_stage(flecs::InOut).
Base & term(id_t id)
Set the term ID.
Base & parent()
Same as up().
Base & inout()
Short for inout(flecs::InOut).
Base & desc()
Use with cascade() to iterate results in descending (bottom-to-top) order.
Base & in()
Short for inout(flecs::In).
Base & inout_stage(flecs::inout_kind_t inout)
Set read/write access for a stage.
Base & trav(flecs::entity_t trav, flecs::flags32_t flags=0)
Specify the relationship to traverse, and flags to indicate direction.
Base & or_()
Short for oper(flecs::Or).
Base & first()
Select the first identifier, initialize it with the ID associated with the type.
Base & second()
Select the second identifier, initialize it with the ID associated with the type.
Base & src()
Call prior to setting values for the src identifier.
Base & and_from()
Short for oper(flecs::AndFrom).
Base & src()
Select the src identifier, initialize it with the ID associated with the type.
Base & out()
Short for inout(flecs::Out).
Base & up()
Traverse upwards using the specified relationship type.
Base & and_()
Short for oper(flecs::And).
ecs_term_t * term_
Pointer to the current term.
Base & optional()
Short for oper(flecs::Optional).
Base & cascade()
Cascade using the specified relationship type.
Base & second()
Call prior to setting values for the second identifier.
Base & src(flecs::entity_t id)
Select the src identifier, initialize it with an entity ID.
Base & read()
Short for inout_stage(flecs::In).
term_builder_i(ecs_term_t *term_ptr)
Construct from a term pointer.
Base & up(flecs::entity_t trav=0)
The up flag indicates that the term identifier may be substituted by traversing a relationship upward...
Base & first(flecs::entity_t id)
Select the first identifier, initialize it with an entity ID.
Base & inout_none()
Short for inout(flecs::InOutNone).
term_builder_i()
Default constructor.
Base & src(const char *name)
Select the src identifier, initialize it with a name.
Base & id_flags(id_t flags)
Set ID flags for the term.
Base & second(const char *name)
Select the second identifier, initialize it with a name.
Base & not_from()
Short for oper(flecs::NotFrom).
Base & not_()
Short for oper(flecs::Not).
Base & write()
Short for inout_stage(flecs::Out).
Base & first()
Call prior to setting values for the first identifier.
Term identifier builder.
Definition builder_i.hpp:22
Base & name(const char *name)
Specify the value of the identifier by name.
Definition builder_i.hpp:58
Base & id(flecs::entity_t id)
Specify the value of the identifier by ID.
Definition builder_i.hpp:37
ecs_term_ref_t * term_ref_
Pointer to the current term reference.
Definition builder_i.hpp:81
virtual ~term_ref_builder_i()
Destructor.
Definition builder_i.hpp:27
Base & entity(flecs::entity_t entity)
Specify the value of the identifier by ID.
Definition builder_i.hpp:51
Base & var(const char *var_name)
Specify that the identifier is a variable (resolved at query evaluation time).
Definition builder_i.hpp:66
Base & self()
The self flag indicates that the term identifier itself is used.
Definition builder_i.hpp:30
Base & flags(flecs::flags64_t flags)
Override the term ID flags.
Definition builder_i.hpp:74
term_ref_builder_i()
Default constructor.
Definition builder_i.hpp:24
Class that describes a term.
Definition impl.hpp:16