Flecs v4.1
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
17struct query_base {
20
23 : query_(q) {
24 flecs_poly_claim(q);
25 }
26
29 : query_(ECS_CONST_CAST(query_t*, q)) {
30 flecs_poly_claim(q);
31 }
32
35 if (desc->entity && desc->terms[0].id == 0) {
36 const flecs::Poly *query_poly = ecs_get_pair(
37 world, desc->entity, EcsPoly, EcsQuery);
38 if (query_poly) {
39 query_ = static_cast<flecs::query_t*>(query_poly->poly);
40 flecs_poly_claim(query_);
41 return;
42 }
43 }
44
45 query_ = ecs_query_init(world, desc);
46 }
47
49 query_base(const query_base& obj) {
50 this->query_ = obj.query_;
51 if (this->query_)
52 {
53 flecs_poly_claim(this->query_);
54 }
55 }
56
59 this->~query_base();
60 this->query_ = obj.query_;
61 if (this->query_)
62 {
63 flecs_poly_claim(this->query_);
64 }
65 return *this;
66 }
67
69 query_base(query_base&& obj) noexcept {
70 this->query_ = obj.query_;
71 obj.query_ = nullptr;
72 }
73
75 query_base& operator=(query_base&& obj) noexcept {
76 this->query_ = obj.query_;
77 obj.query_ = nullptr;
78 return *this;
79 }
80
83 return flecs::entity(query_->world, query_->entity);
84 }
85
87 const flecs::query_t* c_ptr() const {
88 return query_;
89 }
90
92 operator const flecs::query_t*() const {
93 return query_;
94 }
95
97 operator bool() const {
98 return query_ != nullptr;
99 }
100
106 void destruct() {
107 ecs_assert(query_->entity != 0, ECS_INVALID_OPERATION, "destruct() "
108 "should only be called on queries associated with entities");
109 ecs_query_fini(query_);
110 query_ = nullptr;
111 }
112
115 if (query_ && !query_->entity) {
116 if (!flecs_poly_release(query_)) {
117 ecs_query_fini(query_);
118 query_ = nullptr;
119 }
120 }
121 }
122
123#ifdef FLECS_CACHED_QUERIES
133 bool changed() const {
134 return ecs_query_changed(query_);
135 }
136
142 const flecs::query_group_info_t* group_info(uint64_t group_id) const {
143 return ecs_query_get_group_info(query_, group_id);
144 }
145
155
161 void* group_ctx(uint64_t group_id) const {
162 const flecs::query_group_info_t *gi = group_info(group_id);
163 if (gi) {
164 return gi->ctx;
165 } else {
166 return nullptr;
167 }
168 }
169#endif
170
172 template <typename Func>
173 void each_term(const Func& func) {
174 for (int i = 0; i < query_->term_count; i ++) {
175 flecs::term t(query_->world, query_->terms[i]);
176 func(t);
177 t.reset(); // prevent freeing resources
178 }
179 }
180
182 flecs::term term(int32_t index) const {
183 return flecs::term(query_->world, query_->terms[index]);
184 }
185
187 int32_t term_count() const {
188 return query_->term_count;
189 }
190
192 int32_t field_count() const {
193 return query_->field_count;
194 }
195
196#ifdef FLECS_QUERY_PLANS
198 int32_t find_var(const char *name) const {
199 return ecs_query_find_var(query_, name);
200 }
201
207 char *result = ecs_query_plan(query_);
208 return flecs::string(result);
209 }
210#endif
211
212 bool has(flecs::entity_t e) const {
213 ecs_iter_t it;
214 bool result = ecs_query_has(query_, e, &it);
215 if (result) {
216 ecs_iter_fini(&it);
217 }
218 return result;
219 }
220
221 bool has(const flecs::table& t) const {
222 ecs_iter_t it;
223 bool result = ecs_query_has_table(query_, t.get_table(), &it);
224 if (result) {
225 ecs_iter_fini(&it);
226 }
227 return result;
228 }
229
230 bool has(const flecs::table_range& range) const {
231 ecs_table_range_t r = {
232 range.get_table(),
233 range.offset(),
234 range.count()
235 };
236 ecs_iter_t it;
237 bool result = ecs_query_has_range(query_, &r, &it);
238 if (result) {
239 ecs_iter_fini(&it);
240 }
241 return result;
242 }
243
246 char *result = ecs_query_str(query_);
247 return flecs::string(result);
248 }
249
251 operator query<>() const;
252
253# ifdef FLECS_JSON
254# include "../json/query.inl"
255# endif
256
257protected:
258 query_t *query_ = nullptr;
259};
260
265template<typename ... Components>
266struct query : query_base, iterable<Components...> {
267private:
268 using Fields = typename _::field_ptrs<Components...>::array;
269
270public:
272
274 query() : query_base() { } // necessary not to confuse MSVC
275
277 query(const query& obj) : query_base(obj) { }
278
280 query& operator=(const query& obj) {
282 return *this;
283 }
284
286 query(query&& obj) noexcept : query_base(FLECS_MOV(obj)) { }
287
289 query& operator=(query&& obj) noexcept {
290 query_base::operator=(FLECS_FWD(obj));
291 return *this;
292 }
293
294#ifdef FLECS_CACHED_QUERIES
297 const flecs::query_t *q = ecs_query_get_cache_query(query_);
298 return flecs::query<>(q);
299 }
300#endif
301
302private:
303 ecs_iter_t get_iter(flecs::world_t *world) const override {
304 ecs_assert(query_ != nullptr, ECS_INVALID_PARAMETER,
305 "cannot iterate invalid query");
306 if (!world) {
307 world = query_->world;
308 }
309 return ecs_query_iter(world, query_);
310 }
311
312 ecs_iter_next_action_t next_action() const override {
313 return ecs_query_next;
314 }
315};
316
317// World mixin implementation
318template <typename... Comps, typename... Args>
319inline conditional_t<sizeof...(Args) == 0 && _::is_sparse_query<Comps...>::value,
321world::query(Args &&... args) const {
322 if constexpr (sizeof...(Args) == 0 && _::is_sparse_query<Comps...>::value) {
323 return flecs::sparse_query<Comps...>(world_);
324 } else {
325 return flecs::query_builder<Comps...>(world_, FLECS_FWD(args)...)
326 .build();
327 }
328}
329
330inline flecs::query<> world::query(flecs::entity query_entity) const {
331 ecs_query_desc_t desc = {};
332 desc.entity = query_entity;
333 return flecs::query<>(world_, &desc);
334}
335
336template <typename... Comps, typename... Args>
337inline flecs::query_builder<Comps...> world::query_builder(Args &&... args) const {
338 return flecs::query_builder<Comps...>(world_, FLECS_FWD(args)...);
339}
340
341template <typename ... Components>
342inline sparse_query<Components...>::operator flecs::query<Components...>() const {
343 return flecs::query_builder<Components...>(world_).build();
344}
345
346// world::each
347namespace _ {
348
349// Each with entity parameter
350template<typename Func, typename ... Args>
352
353template<typename Func, typename E>
354struct query_delegate_w_ent<Func, arg_list<E> >
355{
356 query_delegate_w_ent(const flecs::world& world, Func&& func) {
358
359 for (int32_t i = 0; i < entities.alive_count; i ++) {
360 func(flecs::entity(world, entities.ids[i]));
361 }
362 }
363};
364
365template<typename Func, typename E, typename ... Args>
366struct query_delegate_w_ent<Func, arg_list<E, Args ...> >
367{
368 query_delegate_w_ent(const flecs::world& world, Func&& func) {
369 auto f = world.query<Args ...>();
370 f.each(FLECS_MOV(func));
371 }
372};
373
374// Each without entity parameter
375template<typename Func, typename ... Args>
377
378template<typename Func, typename ... Args>
379struct query_delegate_no_ent<Func, arg_list<Args ...> >
380{
381 query_delegate_no_ent(const flecs::world& world, Func&& func) {
382 auto f = world.query<Args ...>();
383 f.each(FLECS_MOV(func));
384 }
385};
386
387// Switch between function with and without entity parameter
388template<typename Func, typename T = int>
390
391template <typename Func>
392struct query_delegate<Func, if_t<is_same<first_arg_t<Func>, flecs::entity>::value> > {
393 query_delegate(const flecs::world& world, Func&& func) {
395 }
396};
397
398template <typename Func>
399struct query_delegate<Func, if_not_t<is_same<first_arg_t<Func>, flecs::entity>::value> > {
400 query_delegate(const flecs::world& world, Func&& func) {
402 }
403};
404
405}
406
407template <typename Func>
408inline void world::each(Func&& func) const {
409 _::query_delegate<Func> f_delegate(*this, FLECS_MOV(func));
410}
411
412template <typename T, typename Func>
413inline void world::each(Func&& func) const {
414 ecs_iter_t it = ecs_each_id(world_, _::type<T>::id(world_));
415
416 while (ecs_each_next(&it)) {
417 _::each_delegate<Func, T>(func).invoke(&it);
418 }
419}
420
421template <typename Func>
422inline void world::each(flecs::id_t each_id, Func&& func) const {
423 ecs_iter_t it = ecs_each_id(world_, each_id);
424
425 while (ecs_each_next(&it)) {
426 _::each_delegate<Func>(func).invoke(&it);
427 }
428}
429
430// query_base implementation
431inline query_base::operator flecs::query<> () const {
432 return flecs::query<>(query_);
433}
434
435}
typename first_arg< Func >::type first_arg_t
Convenience alias for the first argument type of a callable.
const ecs_entity_t EcsQuery
Tag added to queries.
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:669
#define ECS_INVALID_PARAMETER
Invalid parameter error code.
Definition log.h:671
flecs::query_builder< Comps... > query_builder(Args &&... args) const
Create a query builder.
conditional_t< sizeof...(Args)==0 &&_::is_sparse_query< Comps... >::value, flecs::sparse_query< Comps... >, flecs::query< Comps... > > query(Args &&... args) const
Create a query.
void each(Func &&func) const
Iterate over all entities with components in the argument list of the function.
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_iter_t ecs_each_id(const ecs_world_t *world, ecs_id_t component)
Iterate all entities with a specified (component ID).
bool ecs_each_next(ecs_iter_t *it)
Progress an iterator created with ecs_each_id().
#define ecs_get_pair(world, subject, First, second)
Get the first element of a pair.
Definition flecs_c.h:343
bool(* ecs_iter_next_action_t)(ecs_iter_t *it)
Function prototype for iterating an iterator.
Definition flecs.h:599
void ecs_iter_fini(ecs_iter_t *it)
Clean up iterator resources.
bool ecs_query_has_table(const ecs_query_t *query, ecs_table_t *table, ecs_iter_t *it)
Match a table with a query.
bool ecs_query_next(ecs_iter_t *it)
Progress a 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 a query group.
bool ecs_query_has_range(const ecs_query_t *query, ecs_table_range_t *range, ecs_iter_t *it)
Match a range with a query.
int32_t ecs_query_find_var(const ecs_query_t *query, const char *name)
Find a variable index.
void ecs_query_fini(ecs_query_t *query)
Delete a query.
bool ecs_query_has(const ecs_query_t *query, ecs_entity_t entity, ecs_iter_t *it)
Match an entity with a query.
char * ecs_query_plan(const ecs_query_t *query)
Convert a 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 a query to a 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)
Return whether the query data changed since the last iteration.
const ecs_map_t * ecs_query_get_groups(const ecs_query_t *query)
Return the map with query groups.
const ecs_query_t * ecs_query_get_cache_query(const ecs_query_t *query)
Get the query used to populate the cache.
ecs_entities_t ecs_get_entities(const ecs_world_t *world)
Return entity identifiers in the world.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get the world from a poly.
Query builder.
JSON query mixin.
Component for storing a poly object.
Definition flecs.h:1624
ecs_poly_t * poly
Pointer to poly object.
Definition flecs.h:1625
Type returned by ecs_get_entities().
Definition flecs.h:2059
int32_t alive_count
Number of alive entity IDs.
Definition flecs.h:2062
const ecs_entity_t * ids
Array with all entity IDs in the world.
Definition flecs.h:2060
Iterator.
Definition flecs.h:1190
Used with ecs_query_init().
Definition flecs.h:1323
ecs_term_t terms[32]
Query terms.
Definition flecs.h:1328
ecs_entity_t entity
Entity associated with query (optional).
Definition flecs.h:1390
Type that contains information about a query group.
Definition flecs.h:1592
void * ctx
Group context, returned by on_group_create.
Definition flecs.h:1596
Queries are lists of constraints (terms) that match entities.
Definition flecs.h:856
ecs_world_t * world
World or stage the query was created with.
Definition flecs.h:893
ecs_entity_t entity
Entity associated with query (optional).
Definition flecs.h:891
ecs_term_t * terms
Query terms.
Definition flecs.h:859
int8_t term_count
Number of query terms.
Definition flecs.h:868
int8_t field_count
Number of fields returned by the query.
Definition flecs.h:869
ecs_id_t id
Component ID to be matched by term.
Definition flecs.h:833
Entity.
Definition entity.hpp:30
Base class for iterable query objects.
Definition iterable.hpp:22
Base class for queries.
Definition impl.hpp:17
const flecs::query_t * c_ptr() const
Get a pointer to the underlying C query.
Definition impl.hpp:87
bool changed() const
Return whether the query data changed since the last iteration.
Definition impl.hpp:133
query_base & operator=(const query_base &obj)
Copy assignment operator.
Definition impl.hpp:58
flecs::entity entity() const
Get the entity associated with the query.
Definition impl.hpp:82
void each_term(const Func &func)
Iterate each term in the query, invoking a callback for each.
Definition impl.hpp:173
query_base(query_base &&obj) noexcept
Move constructor.
Definition impl.hpp:69
query_base()
Default constructor.
Definition impl.hpp:19
flecs::map< uint64_t, void * > groups() const
Iterate the active groups of a grouped query.
Definition impl.hpp:152
void * group_ctx(uint64_t group_id) const
Get context for a group.
Definition impl.hpp:161
query_base(const query_t *q)
Construct from a const query pointer.
Definition impl.hpp:28
flecs::string plan() const
Return a string representing the query plan.
Definition impl.hpp:206
flecs::term term(int32_t index) const
Get term at the specified index.
Definition impl.hpp:182
query_base(query_t *q)
Construct from a mutable query pointer.
Definition impl.hpp:22
query_base(world_t *world, const ecs_query_desc_t *desc)
Construct from a world and a query descriptor.
Definition impl.hpp:34
query_base & operator=(query_base &&obj) noexcept
Move assignment operator.
Definition impl.hpp:75
query_base(const query_base &obj)
Copy constructor.
Definition impl.hpp:49
~query_base()
Destructor.
Definition impl.hpp:114
int32_t term_count() const
Get the number of terms in the query.
Definition impl.hpp:187
const flecs::query_group_info_t * group_info(uint64_t group_id) const
Get info for a group.
Definition impl.hpp:142
flecs::string str() const
Convert the query to a string expression.
Definition impl.hpp:245
void destruct()
Free a persistent query.
Definition impl.hpp:106
int32_t find_var(const char *name) const
Find a variable by name.
Definition impl.hpp:198
int32_t field_count() const
Get the number of fields in the query.
Definition impl.hpp:192
Query builder.
Definition builder.hpp:24
Typed query.
Definition impl.hpp:266
query_base()
Default constructor.
Definition impl.hpp:19
query()
Default constructor.
Definition impl.hpp:274
query(const query &obj)
Copy constructor.
Definition impl.hpp:277
flecs::query cache_query() const
Get the cache query, if any.
Definition impl.hpp:296
query & operator=(query &&obj) noexcept
Move assignment operator.
Definition impl.hpp:289
query & operator=(const query &obj)
Copy assignment operator.
Definition impl.hpp:280
query(query &&obj) noexcept
Move constructor.
Definition impl.hpp:286
Query that iterates sparse component storages directly.
Owned string wrapper.
Definition string.hpp:15
Table range.
Definition table.hpp:449
int32_t count() const
Get the number of entities in the range.
Definition table.hpp:474
int32_t offset() const
Get the offset of the range.
Definition table.hpp:469
Table.
Definition table.hpp:23
table_t * get_table() const
Get the table.
Definition table.hpp:430
Class that describes a term.
Definition impl.hpp:16
void reset()
Reset the term to its default state.
Definition impl.hpp:82
The world.
Definition world.hpp:244
world_t * world_
Pointer to the underlying C world.
Definition world.hpp:1423
enable_if_t< false==V, int > if_not_t
Convenience enable_if alias for negated conditions.
Definition utils.hpp:172
enable_if_t< V, int > if_t
Convenience enable_if alias using int as default type.
Definition utils.hpp:168