Flecs v4.0
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
impl.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include "builder.hpp"
9
10namespace flecs
11{
12
13struct query_base {
14 query_base() { }
15
17 : query_(q) {
18 flecs_poly_claim(q);
19 }
20
21 query_base(const query_t *q)
22 : query_(ECS_CONST_CAST(query_t*, q)) {
23 flecs_poly_claim(q);
24 }
25
26 query_base(world_t *world, ecs_query_desc_t *desc) {
27 if (desc->entity && desc->terms[0].id == 0) {
28 const flecs::Poly *query_poly = ecs_get_pair(
29 world, desc->entity, EcsPoly, EcsQuery);
30 if (query_poly) {
31 query_ = static_cast<flecs::query_t*>(query_poly->poly);
32 flecs_poly_claim(query_);
33 return;
34 }
35 }
36
37 query_ = ecs_query_init(world, desc);
38 }
39
40 query_base(const query_base& obj) {
41 this->query_ = obj.query_;
42 flecs_poly_claim(this->query_);
43 }
44
45 query_base& operator=(const query_base& obj) {
46 this->query_ = obj.query_;
47 flecs_poly_claim(this->query_);
48 return *this;
49 }
50
51 query_base(query_base&& obj) noexcept {
52 this->query_ = obj.query_;
53 obj.query_ = nullptr;
54 }
55
56 query_base& operator=(query_base&& obj) noexcept {
57 this->query_ = obj.query_;
58 obj.query_ = nullptr;
59 return *this;
60 }
61
63 return flecs::entity(query_->world, query_->entity);
64 }
65
66 const flecs::query_t* c_ptr() const {
67 return query_;
68 }
69
70 operator const flecs::query_t*() const {
71 return query_;
72 }
73
74 operator bool() const {
75 return query_ != nullptr;
76 }
77
83 void destruct() {
84 ecs_assert(query_->entity != 0, ECS_INVALID_OPERATION, "destruct() "
85 "should only be called on queries associated with entities");
86 ecs_query_fini(query_);
87 query_ = nullptr;
88 }
89
90 ~query_base() {
91 /* Only free if query is not associated with entity, such as system
92 * queries and named queries. Named queries have to be either explicitly
93 * deleted with the .destruct() method, or will be deleted when the
94 * world is deleted. */
95 if (query_ && !query_->entity) {
96 if (!flecs_poly_release(query_)) {
97 ecs_query_fini(query_);
98 query_ = nullptr;
99 }
100 }
101 }
102
112 bool changed() const {
113 return ecs_query_changed(query_);
114 }
115
121 const flecs::query_group_info_t* group_info(uint64_t group_id) const {
122 return ecs_query_get_group_info(query_, group_id);
123 }
124
130 void* group_ctx(uint64_t group_id) const {
131 const flecs::query_group_info_t *gi = group_info(group_id);
132 if (gi) {
133 return gi->ctx;
134 } else {
135 return NULL;
136 }
137 }
138
139 template <typename Func>
140 void each_term(const Func& func) {
141 for (int i = 0; i < query_->term_count; i ++) {
142 flecs::term t(query_->world, query_->terms[i]);
143 func(t);
144 t.reset(); // prevent freeing resources
145 }
146 }
147
148 flecs::term term(int32_t index) {
149 return flecs::term(query_->world, query_->terms[index]);
150 }
151
152 int32_t term_count() {
153 return query_->term_count;
154 }
155
156 int32_t field_count() {
157 return query_->field_count;
158 }
159
160 int32_t find_var(const char *name) {
161 return ecs_query_find_var(query_, name);
162 }
163
164 flecs::string str() {
165 char *result = ecs_query_str(query_);
166 return flecs::string(result);
167 }
168
174 char *result = ecs_query_plan(query_);
175 return flecs::string(result);
176 }
177
178 operator query<>() const;
179
180protected:
181 query_t *query_ = nullptr;
182};
183
184template<typename ... Components>
185struct query : query_base, iterable<Components...> {
186private:
187 using Fields = typename _::field_ptrs<Components...>::array;
188
189public:
190 using query_base::query_base;
191
192 query() : query_base() { } // necessary not to confuse msvc
193
194 query(const query& obj) : query_base(obj) { }
195
196 query& operator=(const query& obj) {
197 query_base::operator=(obj);
198 return *this;
199 }
200
201 query(query&& obj) noexcept : query_base(FLECS_MOV(obj)) { }
202
203 query& operator=(query&& obj) noexcept {
204 query_base::operator=(FLECS_FWD(obj));
205 return *this;
206 }
207
208private:
209 ecs_iter_t get_iter(flecs::world_t *world) const override {
210 ecs_assert(query_ != nullptr, ECS_INVALID_PARAMETER,
211 "cannot iterate invalid query");
212 if (!world) {
213 world = query_->world;
214 }
215 return ecs_query_iter(world, query_);
216 }
217
218 ecs_iter_next_action_t next_action() const override {
219 return ecs_query_next;
220 }
221};
222
223// World mixin implementation
224template <typename... Comps, typename... Args>
225inline flecs::query<Comps...> world::query(Args &&... args) const {
226 return flecs::query_builder<Comps...>(world_, FLECS_FWD(args)...)
227 .build();
228}
229
230inline flecs::query<> world::query(flecs::entity query_entity) const {
231 ecs_query_desc_t desc = {};
232 desc.entity = query_entity;
233 return flecs::query<>(world_, &desc);
234}
235
236template <typename... Comps, typename... Args>
237inline flecs::query_builder<Comps...> world::query_builder(Args &&... args) const {
238 return flecs::query_builder<Comps...>(world_, FLECS_FWD(args)...);
239}
240
241// world::each
242namespace _ {
243
244// Each with entity parameter
245template<typename Func, typename ... Args>
247
248template<typename Func, typename E, typename ... Args>
249struct query_delegate_w_ent<Func, arg_list<E, Args ...> >
250{
251 query_delegate_w_ent(const flecs::world& world, Func&& func) {
252 auto f = world.query<Args ...>();
253 f.each(FLECS_MOV(func));
254 }
255};
256
257// Each without entity parameter
258template<typename Func, typename ... Args>
260
261template<typename Func, typename ... Args>
262struct query_delegate_no_ent<Func, arg_list<Args ...> >
263{
264 query_delegate_no_ent(const flecs::world& world, Func&& func) {
265 auto f = world.query<Args ...>();
266 f.each(FLECS_MOV(func));
267 }
268};
269
270// Switch between function with & without entity parameter
271template<typename Func, typename T = int>
273
274template <typename Func>
275struct query_delegate<Func, if_t<is_same<first_arg_t<Func>, flecs::entity>::value> > {
276 query_delegate(const flecs::world& world, Func&& func) {
278 }
279};
280
281template <typename Func>
282struct query_delegate<Func, if_not_t<is_same<first_arg_t<Func>, flecs::entity>::value> > {
283 query_delegate(const flecs::world& world, Func&& func) {
285 }
286};
287
288}
289
290template <typename Func>
291inline void world::each(Func&& func) const {
292 _::query_delegate<Func> f_delegate(*this, FLECS_MOV(func));
293}
294
295template <typename T, typename Func>
296inline void world::each(Func&& func) const {
297 ecs_iter_t it = ecs_each_id(world_, _::type<T>::id());
298
299 while (ecs_each_next(&it)) {
300 _::each_delegate<Func, T>(func).invoke(&it);
301 }
302}
303
304template <typename Func>
305inline void world::each(flecs::id_t each_id, Func&& func) const {
306 ecs_iter_t it = ecs_each_id(world_, each_id);
307
308 while (ecs_each_next(&it)) {
309 _::each_delegate<Func>(func).invoke(&it);
310 }
311}
312
313// query_base implementation
314inline query_base::operator flecs::query<> () const {
315 return flecs::query<>(query_);
316}
317
318}
const ecs_entity_t EcsQuery
Tag added to queries.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:352
untyped_component & array(int32_t elem_count)
Register array metadata for component.
flecs::query< Comps... > query(Args &&... args) const
Create a query.
Definition impl.hpp:225
flecs::query_builder< Comps... > query_builder(Args &&... args) const
Create a query builder.
Definition impl.hpp:237
void each(Func &&func) const
Iterate over all entities with components in argument list of function.
Definition impl.hpp:291
bool ecs_each_next(ecs_iter_t *it)
Progress an iterator created with ecs_each_id().
ecs_iter_t ecs_each_id(const ecs_world_t *world, ecs_id_t id)
Iterate all entities with specified (component id).
bool(* ecs_iter_next_action_t)(ecs_iter_t *it)
Function prototype for iterating an iterator.
Definition flecs.h:557
bool ecs_query_next(ecs_iter_t *it)
Progress query iterator.
const ecs_query_group_info_t * ecs_query_get_group_info(const ecs_query_t *query, uint64_t group_id)
Get information about query group.
int32_t ecs_query_find_var(const ecs_query_t *query, const char *name)
Find variable index.
void ecs_query_fini(ecs_query_t *query)
Delete a query.
char * ecs_query_plan(const ecs_query_t *query)
Convert query to a string.
ecs_iter_t ecs_query_iter(const ecs_world_t *world, const ecs_query_t *query)
Create a query iterator.
char * ecs_query_str(const ecs_query_t *query)
Convert query to string expression.
ecs_query_t * ecs_query_init(ecs_world_t *world, const ecs_query_desc_t *desc)
Create a query.
bool ecs_query_changed(ecs_query_t *query)
Returns whether the query data changed since the last iteration.
Query builder.
Component for storing a poly object.
Definition flecs.h:1456
ecs_poly_t * poly
Pointer to poly object.
Definition flecs.h:1457
Iterator.
Definition flecs.h:1062
Used with ecs_query_init().
Definition flecs.h:1162
ecs_term_t terms[32]
Query terms.
Definition flecs.h:1167
ecs_entity_t entity
Entity associated with query (optional)
Definition flecs.h:1229
Type that contains information about a query group.
Definition flecs.h:1425
void * ctx
Group context, returned by on_group_create.
Definition flecs.h:1428
Queries are lists of constraints (terms) that match entities.
Definition flecs.h:792
ecs_term_t terms[32]
Query terms.
Definition flecs.h:795
ecs_world_t * world
World or stage query was created with.
Definition flecs.h:824
ecs_entity_t entity
Entity associated with query (optional)
Definition flecs.h:822
int8_t term_count
Number of query terms.
Definition flecs.h:801
int8_t field_count
Number of fields returned by query.
Definition flecs.h:802
ecs_id_t id
Component id to be matched by term.
Definition flecs.h:769
Entity.
Definition entity.hpp:30
bool changed() const
Returns whether the query data changed since the last iteration.
Definition impl.hpp:112
void * group_ctx(uint64_t group_id) const
Get context for group.
Definition impl.hpp:130
flecs::string plan() const
Returns a string representing the query plan.
Definition impl.hpp:173
const flecs::query_group_info_t * group_info(uint64_t group_id) const
Get info for group.
Definition impl.hpp:121
void destruct()
Free persistent query.
Definition impl.hpp:83
Query builder.
Definition decl.hpp:23
Class that describes a term.
Definition impl.hpp:16
The world.
Definition world.hpp:137