Flecs v3.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
8#include "../term/builder_i.hpp"
9
10namespace flecs
11{
12
17template<typename Base, typename ... Components>
19 filter_builder_i(ecs_filter_desc_t *desc, int32_t term_index = 0)
20 : m_term_index(term_index)
21 , m_expr_count(0)
22 , m_desc(desc) { }
23
24 Base& instanced() {
25 m_desc->instanced = true;
26 return *this;
27 }
28
29 Base& expr(const char *expr) {
30 ecs_check(m_expr_count == 0, ECS_INVALID_OPERATION,
31 "filter_builder::expr() called more than once");
32 m_desc->expr = expr;
33 m_expr_count ++;
34
35 error:
36 return *this;
37 }
38
39 /* With/without shorthand notation. */
40
41 template <typename ... Args>
42 Base& with(Args&&... args) {
43 return this->term(FLECS_FWD(args)...);
44 }
45
46 template <typename T, typename ... Args>
47 Base& with(Args&&... args) {
48 return this->term<T>(FLECS_FWD(args)...);
49 }
50
51 template <typename First, typename Second>
52 Base& with() {
53 return this->term<First, Second>();
54 }
55
56 template <typename ... Args>
57 Base& without(Args&&... args) {
58 return this->term(FLECS_FWD(args)...).not_();
59 }
60
61 template <typename T, typename ... Args>
62 Base& without(Args&&... args) {
63 return this->term<T>(FLECS_FWD(args)...).not_();
64 }
65
66 template <typename First, typename Second>
67 Base& without() {
68 return this->term<First, Second>().not_();
69 }
70
71 /* Write/read shorthand notation */
72
73 Base& write() {
75 return *this;
76 }
77
78 template <typename ... Args>
79 Base& write(Args&&... args) {
80 return this->term(FLECS_FWD(args)...).write();
81 }
82
83 template <typename T, typename ... Args>
84 Base& write(Args&&... args) {
85 return this->term<T>(FLECS_FWD(args)...).write();
86 }
87
88 template <typename First, typename Second>
89 Base& write() {
90 return this->term<First, Second>().write();
91 }
92
93 Base& read() {
95 return *this;
96 }
97
98 template <typename ... Args>
99 Base& read(Args&&... args) {
100 return this->term(FLECS_FWD(args)...).read();
101 }
102
103 template <typename T, typename ... Args>
104 Base& read(Args&&... args) {
105 return this->term<T>(FLECS_FWD(args)...).read();
106 }
107
108 template <typename First, typename Second>
109 Base& read() {
110 return this->term<First, Second>().read();
111 }
112
113 /* Term notation for more complex query features */
114
115 Base& term() {
116 if (this->m_term) {
117 ecs_check(ecs_term_is_initialized(this->m_term),
118 ECS_INVALID_OPERATION,
119 "filter_builder::term() called without initializing term");
120 }
121
122 if (m_term_index >= ECS_TERM_DESC_CACHE_SIZE) {
123 if (m_term_index == ECS_TERM_DESC_CACHE_SIZE) {
124 m_desc->terms_buffer = ecs_os_calloc_n(
125 ecs_term_t, m_term_index + 1);
126 ecs_os_memcpy_n(m_desc->terms_buffer, m_desc->terms,
127 ecs_term_t, m_term_index);
128 ecs_os_memset_n(m_desc->terms, 0,
130 } else {
131 m_desc->terms_buffer = ecs_os_realloc_n(m_desc->terms_buffer,
132 ecs_term_t, m_term_index + 1);
133 }
134
135 m_desc->terms_buffer_count = m_term_index + 1;
136
137 this->set_term(&m_desc->terms_buffer[m_term_index]);
138 } else {
139 this->set_term(&m_desc->terms[m_term_index]);
140 }
141
142 m_term_index ++;
143
144 error:
145 return *this;
146 }
147
148 Base& term_at(int32_t term_index) {
149 ecs_assert(term_index > 0, ECS_INVALID_PARAMETER, NULL);
150 int32_t prev_index = m_term_index;
151 m_term_index = term_index - 1;
152 this->term();
153 m_term_index = prev_index;
154 ecs_assert(ecs_term_is_initialized(this->m_term),
155 ECS_INVALID_PARAMETER, NULL);
156 return *this;
157 }
158
159 Base& arg(int32_t term_index) {
160 return this->term_at(term_index);
161 }
162
163 template<typename T>
164 Base& term() {
165 this->term();
166 *this->m_term = flecs::term(_::cpp_type<T>::id(this->world_v())).move();
167 this->m_term->inout = static_cast<ecs_inout_kind_t>(
168 _::type_to_inout<T>());
169 return *this;
170 }
171
172 Base& term(id_t id) {
173 this->term();
174 *this->m_term = flecs::term(id).move();
175 return *this;
176 }
177
178 Base& term(const char *name) {
179 this->term();
180 *this->m_term = flecs::term().first(name).move();
181 return *this;
182 }
183
184 Base& term(const char *first, const char *second) {
185 this->term();
186 *this->m_term = flecs::term().first(first).second(second).move();
187 return *this;
188 }
189
190 Base& term(entity_t r, entity_t o) {
191 this->term();
192 *this->m_term = flecs::term(r, o).move();
193 return *this;
194 }
195
196 Base& term(entity_t r, const char *o) {
197 this->term();
198 *this->m_term = flecs::term(r).second(o).move();
199 return *this;
200 }
201
202 template<typename First>
203 Base& term(id_t o) {
204 return this->term(_::cpp_type<First>::id(this->world_v()), o);
205 }
206
207 template<typename First>
208 Base& term(const char *second) {
209 return this->term(_::cpp_type<First>::id(this->world_v())).second(second);
210 }
211
212 template<typename First, typename Second>
213 Base& term() {
214 return this->term<First>(_::cpp_type<Second>::id(this->world_v()));
215 }
216
217 template <typename E, if_t< is_enum<E>::value > = 0>
218 Base& term(E value) {
219 flecs::entity_t r = _::cpp_type<E>::id(this->world_v());
220 auto o = enum_type<E>(this->world_v()).entity(value);
221 return this->term(r, o);
222 }
223
224 Base& term(flecs::term& term) {
225 this->term();
226 *this->m_term = term.move();
227 return *this;
228 }
229
230 Base& term(flecs::term&& term) {
231 this->term();
232 *this->m_term = term.move();
233 return *this;
234 }
235
236protected:
237 virtual flecs::world_t* world_v() = 0;
238 int32_t m_term_index;
239 int32_t m_expr_count;
240
241private:
242 operator Base&() {
243 return *static_cast<Base*>(this);
244 }
245
246 ecs_filter_desc_t *m_desc;
247};
248
249}
#define ecs_assert(condition, error_code,...)
Assert.
Definition: log.h:352
#define ecs_check(condition, error_code,...)
Check.
Definition: log.h:388
bool ecs_term_is_initialized(const ecs_term_t *term)
Test whether a term is set.
#define ECS_TERM_DESC_CACHE_SIZE
Maximum number of terms in desc (larger, as these are temp objects)
Definition: flecs.h:177
ecs_inout_kind_t
Specify read/write access for term.
Definition: flecs.h:475
Used with ecs_filter_init.
Definition: flecs.h:770
ecs_term_t * terms_buffer
For filters with lots of terms an outside array can be provided.
Definition: flecs.h:778
bool instanced
When true, terms returned by an iterator may either contain 1 or N elements, where terms with N eleme...
Definition: flecs.h:791
ecs_term_t terms[(16)]
Terms of the filter.
Definition: flecs.h:775
int32_t terms_buffer_count
Number of terms in array provided in terms_buffer.
Definition: flecs.h:781
const char * expr
Filter expression.
Definition: flecs.h:797
Type that describes a term (single element in a query)
Definition: flecs.h:529
ecs_inout_kind_t inout
Access to contents matched by term.
Definition: flecs.h:539
Filter builder interface.
Definition: builder_i.hpp:18
Term builder interface.
Definition: builder_i.hpp:141
Base & read()
Short for inout_stage(flecs::In).
Definition: builder_i.hpp:298
Base & write()
Short for inout_stage(flecs::Out).
Definition: builder_i.hpp:291
Class that describes a term.
Definition: impl.hpp:16