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 const ecs_filter_t *f = ecs_query_get_filter(m_query);
101 ecs_assert(f != NULL, ECS_INVALID_PARAMETER, NULL);
102
103 for (int i = 0; i < f->term_count; i ++) {
104 flecs::term t(m_world, f->terms[i]);
105 func(t);
106 }
107 }
108
109 filter_base filter() const {
110 return filter_base(m_world, ecs_query_get_filter(m_query));
111 }
112
113 flecs::term term(int32_t index) const {
114 const ecs_filter_t *f = ecs_query_get_filter(m_query);
115 ecs_assert(f != NULL, ECS_INVALID_PARAMETER, NULL);
116 return flecs::term(m_world, f->terms[index]);
117 }
118
119 int32_t field_count() const {
120 const ecs_filter_t *f = ecs_query_get_filter(m_query);
121 return f->term_count;
122 }
123
124 flecs::string str() const {
125 const ecs_filter_t *f = ecs_query_get_filter(m_query);
126 char *result = ecs_filter_str(m_world, f);
127 return flecs::string(result);
128 }
129
130 flecs::entity entity() const {
131 return flecs::entity(m_world, ecs_get_entity(m_query));
132 }
133
134 operator query<>() const;
135
136protected:
137 world_t *m_world;
138 query_t *m_query;
139};
140
141template<typename ... Components>
142struct query final : query_base, iterable<Components...> {
143public:
144 flecs::world world() const {
145 return flecs::world(m_world);
147
148private:
149 using Terms = typename _::term_ptrs<Components...>::array;
150
151 ecs_iter_t get_iter(flecs::world_t *world) const override {
152 if (!world) {
153 world = m_world;
154 }
155 return ecs_query_iter(world, m_query);
156 }
157
158 ecs_iter_next_action_t next_action() const override {
159 return ecs_query_next;
160 }
161
162 ecs_iter_next_action_t next_each_action() const override {
164 }
165
166public:
167 using query_base::query_base;
168};
169
170// Mixin implementation
171template <typename... Comps, typename... Args>
172inline flecs::query<Comps...> world::query(Args &&... args) const {
173 return flecs::query_builder<Comps...>(m_world, FLECS_FWD(args)...)
174 .build();
175}
176
177template <typename... Comps, typename... Args>
178inline flecs::query_builder<Comps...> world::query_builder(Args &&... args) const {
179 return flecs::query_builder<Comps...>(m_world, FLECS_FWD(args)...);
180}
181
182// Builder implementation
183template <typename Base, typename ... Components>
185 m_desc->parent = parent;
186 return *static_cast<Base*>(this);
187}
188
189// query_base implementation
190inline query_base::operator query<>() const {
191 return flecs::query<>(m_world, m_query);
192}
193
194} // namespace flecs
#define ecs_assert(condition, error_code,...)
Assert.
Definition: log.h:352
#define ecs_abort(error_code,...)
Abort.
Definition: log.h:343
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:407
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.
ecs_term_t * terms_buffer
For filters with lots of terms an outside array can be provided.
Definition: flecs.h:841
Filters alllow for ad-hoc quick filtering of entity tables.
Definition: flecs.h:617
ecs_term_t * terms
Array containing terms for filter.
Definition: flecs.h:620
int32_t term_count
Number of elements in terms array.
Definition: flecs.h:621
Used with ecs_query_init.
Definition: flecs.h:870
ecs_filter_desc_t filter
Filter for the query.
Definition: flecs.h:874
Type that contains information about a query group.
Definition: flecs.h:1056
void * ctx
Group context, returned by on_group_create.
Definition: flecs.h:1059
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:184
Query builder.
Definition: builder.hpp:24
Class that describes a term.
Definition: impl.hpp:16
The world.
Definition: world.hpp:113
Builder base class.