Flecs v3.2
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
15
16struct query_base {
18 : m_world(nullptr)
19 , m_query(nullptr) { }
20
21 query_base(world_t *world, query_t *query = nullptr)
22 : m_world(world)
23 , m_query(query) { }
24
25 query_base(world_t *world, ecs_query_desc_t *desc)
26 : m_world(world)
27 {
28 m_query = ecs_query_init(world, desc);
29
30 if (!m_query) {
31 ecs_abort(ECS_INVALID_PARAMETER, NULL);
32 }
33
34 if (desc->filter.terms_buffer) {
35 ecs_os_free(desc->filter.terms_buffer);
36 }
37 }
38
39 operator query_t*() const {
40 return m_query;
41 }
42
52 bool changed() const {
53 return ecs_query_changed(m_query, 0);
54 }
55
63 bool orphaned() const {
64 return ecs_query_orphaned(m_query);
65 }
66
72 const flecs::query_group_info_t* group_info(uint64_t group_id) const {
73 return ecs_query_get_group_info(m_query, group_id);
74 }
75
81 void* group_ctx(uint64_t group_id) const {
82 const flecs::query_group_info_t *gi = group_info(group_id);
83 if (gi) {
84 return gi->ctx;
85 } else {
86 return NULL;
87 }
88 }
89
92 void destruct() {
93 ecs_query_fini(m_query);
94 m_world = nullptr;
95 m_query = nullptr;
96 }
97
98 template <typename Func>
99 void each_term(const Func& func) const {
100 this->filter().each_term(func);
101 }
102
103 filter_base filter() const {
104 return filter_base(m_world, ecs_query_get_filter(m_query));
105 }
106
107 flecs::term term(int32_t index) const {
108 const ecs_filter_t *f = ecs_query_get_filter(m_query);
109 ecs_assert(f != NULL, ECS_INVALID_PARAMETER, NULL);
110 return flecs::term(m_world, f->terms[index]);
111 }
112
113 int32_t field_count() const {
114 const ecs_filter_t *f = ecs_query_get_filter(m_query);
115 return f->term_count;
116 }
117
118 flecs::string str() const {
119 const ecs_filter_t *f = ecs_query_get_filter(m_query);
120 char *result = ecs_filter_str(m_world, f);
121 return flecs::string(result);
122 }
123
124 flecs::entity entity() const {
125 return flecs::entity(m_world, ecs_get_entity(m_query));
126 }
127
128 operator query<>() const;
129
130protected:
131 world_t *m_world;
132 query_t *m_query;
133};
134
135template<typename ... Components>
136struct query final : query_base, iterable<Components...> {
137public:
138 flecs::world world() const {
139 return flecs::world(m_world);
140 }
141
142private:
143 using Terms = typename _::term_ptrs<Components...>::array;
144
145 ecs_iter_t get_iter(flecs::world_t *world) const override {
146 if (!world) {
147 world = m_world;
148 }
149 return ecs_query_iter(world, m_query);
150 }
151
152 ecs_iter_next_action_t next_action() const override {
153 return ecs_query_next;
154 }
155
156 ecs_iter_next_action_t next_each_action() const override {
158 }
159
160public:
161 using query_base::query_base;
162};
163
164// Mixin implementation
165template <typename... Comps, typename... Args>
166inline flecs::query<Comps...> world::query(Args &&... args) const {
167 return flecs::query_builder<Comps...>(m_world, FLECS_FWD(args)...)
168 .build();
169}
170
171template <typename... Comps, typename... Args>
172inline flecs::query_builder<Comps...> world::query_builder(Args &&... args) const {
173 return flecs::query_builder<Comps...>(m_world, FLECS_FWD(args)...);
174}
175
176// Builder implementation
177template <typename Base, typename ... Components>
179 m_desc->parent = parent;
180 return *static_cast<Base*>(this);
181}
182
183// query_base implementation
184inline query_base::operator query<>() const {
185 return flecs::query<>(m_world, m_query);
186}
187
188} // namespace flecs
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:351
#define ecs_abort(error_code,...)
Abort.
Definition log.h:342
flecs::query_builder< Comps... > query_builder(Args &&... args) const
Create a query builder.
flecs::query< Comps... > query(Args &&... args) const
Create a query.
char * ecs_filter_str(const ecs_world_t *world, const ecs_filter_t *filter)
Convert filter to string expression.
bool(* ecs_iter_next_action_t)(ecs_iter_t *it)
Function prototype for iterating an iterator.
Definition flecs.h:567
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.
bool ecs_query_next_instanced(ecs_iter_t *iter)
Same as ecs_query_next, but always instanced.
void ecs_query_fini(ecs_query_t *query)
Destroy a query.
ecs_query_t * ecs_query_init(ecs_world_t *world, const ecs_query_desc_t *desc)
Create a query.
ecs_iter_t ecs_query_iter(const ecs_world_t *world, ecs_query_t *query)
Return a query iterator.
bool ecs_query_next(ecs_iter_t *iter)
Progress the query iterator.
bool ecs_query_orphaned(const ecs_query_t *query)
Returns whether query is orphaned.
const ecs_filter_t * ecs_query_get_filter(const ecs_query_t *query)
Get filter from a query.
bool ecs_query_changed(ecs_query_t *query, const ecs_iter_t *it)
Returns whether the query data changed since the last iteration.
ecs_entity_t ecs_get_entity(const ecs_poly_t *poly)
Get entity from poly.
Query builder.
ecs_term_t * terms_buffer
For filters with lots of terms an outside array can be provided.
Definition flecs.h:1006
Filters allow for ad-hoc quick filtering of entity tables.
Definition flecs.h:786
ecs_term_t * terms
Array containing terms for filter.
Definition flecs.h:794
int8_t term_count
Number of elements in terms array.
Definition flecs.h:789
Used with ecs_query_init().
Definition flecs.h:1035
ecs_filter_desc_t filter
Filter for the query.
Definition flecs.h:1039
Type that contains information about a query group.
Definition flecs.h:1291
void * ctx
Group context, returned by on_group_create.
Definition flecs.h:1294
Entity.
Definition entity.hpp:30
bool changed() const
Returns whether the query data changed since the last iteration.
Definition impl.hpp:52
void * group_ctx(uint64_t group_id) const
Get context for group.
Definition impl.hpp:81
bool orphaned() const
Returns whether query is orphaned.
Definition impl.hpp:63
const flecs::query_group_info_t * group_info(uint64_t group_id) const
Get info for group.
Definition impl.hpp:72
void destruct()
Free the query.
Definition impl.hpp:92
Base & observable(const query_base &parent)
Specify parent query (creates subquery)
Definition impl.hpp:178
Query builder.
Definition builder.hpp:24
Class that describes a term.
Definition impl.hpp:16
The world.
Definition world.hpp:132