Flecs v3.2
A fast entity component system (ECS) for C & C++
Loading...
Searching...
No Matches
table.hpp
Go to the documentation of this file.
1
6#pragma once
7
8namespace flecs {
9
18struct table {
19 table() : m_world(nullptr), m_table(nullptr) { }
20
21 table(world_t *world, table_t *t)
22 : m_world(world)
23 , m_table(t) { }
24
25 virtual ~table() { }
26
29 return flecs::string(ecs_table_str(m_world, m_table));
30 }
31
33 flecs::type type() const {
34 return flecs::type(m_world, ecs_table_get_type(m_table));
35 }
36
38 int32_t count() const {
39 return ecs_table_count(m_table);
40 }
41
47 int32_t search(flecs::id_t id) const {
48 return ecs_search(m_world, m_table, id, 0);
49 }
50
56 template <typename T>
57 int32_t search() const {
58 return search(_::cpp_type<T>::id(m_world));
59 }
60
66 int32_t search(flecs::entity_t first, flecs::entity_t second) const {
67 return search(ecs_pair(first, second));
68 }
69
75 template <typename First>
76 int32_t search(flecs::entity_t second) const {
77 return search(_::cpp_type<First>::id(m_world), second);
78 }
79
85 template <typename First, typename Second>
86 int32_t search() const {
87 return search<First>(_::cpp_type<Second>::id(m_world));
88 }
89
95 bool has(flecs::id_t id) const {
96 return search(id) != -1;
97 }
98
104 template <typename T>
105 bool has() const {
106 return search<T>() != -1;
107 }
108
115 bool has(flecs::entity_t first, flecs::entity_t second) const {
116 return search(first, second) != -1;
117 }
118
125 template <typename First>
126 bool has(flecs::entity_t second) const {
127 return search<First>(second) != -1;
128 }
129
136 template <typename First, typename Second>
137 bool has() const {
138 return search<First, Second>() != -1;
139 }
140
146 virtual void* get_by_index(int32_t index) const {
147 return ecs_table_get_column(m_table, index, 0);
148 }
149
155 void* get(flecs::id_t id) const {
156 int32_t index = search(id);
157 if (index == -1) {
158 return NULL;
159 }
160 return get_by_index(index);
161 }
162
169 void* get(flecs::entity_t first, flecs::entity_t second) const {
170 return get(ecs_pair(first, second));
171 }
172
178 template <typename T, if_t< is_actual<T>::value > = 0>
179 T* get() const {
180 return static_cast<T*>(get(_::cpp_type<T>::id(m_world)));
181 }
182
188 template <typename T, typename A = actual_type_t<T>,
189 if_t< flecs::is_pair<T>::value > = 0>
190 A* get() const {
191 return static_cast<A*>(get(_::cpp_type<T>::id(m_world)));
192 }
193
200 template <typename First>
201 First* get(flecs::entity_t second) const {
202 return static_cast<First*>(get(_::cpp_type<First>::id(m_world), second));
203 }
204
211 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
212 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
213 A* get() const {
214 return static_cast<A*>(get<First>(_::cpp_type<Second>::id(m_world)));
215 }
216
218 size_t column_size(int32_t column_index) {
219 return ecs_table_get_column_size(m_table, column_index);
220 }
221
227 int32_t depth(flecs::entity_t rel) {
228 return ecs_table_get_depth(m_world, m_table, rel);
229 }
230
236 template <typename Rel>
237 int32_t depth() {
238 return depth(_::cpp_type<Rel>::id(m_world));
239 }
240
241 /* Implicit conversion to table_t */
242 operator table_t*() const {
243 return m_table;
244 }
245
246protected:
247 world_t *m_world;
248 table_t *m_table;
249};
250
252 table_range()
253 : table()
254 , m_offset(0)
255 , m_count(0) { }
256
257 table_range(world_t *world, table_t *t, int32_t offset, int32_t count)
258 : table(world, t)
259 , m_offset(offset)
260 , m_count(count) { }
261
262 int32_t offset() const {
263 return m_offset;
264 }
265
266 int32_t count() const {
267 return m_count;
268 }
269
275 void* get_by_index(int32_t index) const override {
276 return ecs_table_get_column(m_table, index, m_offset);
277 }
278
279private:
280 int32_t m_offset = 0;
281 int32_t m_count = 0;
282};
283
286}
char * ecs_table_str(const ecs_world_t *world, const ecs_table_t *table)
Convert table to string.
int32_t ecs_search(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t id, ecs_id_t *id_out)
Search for component id in table type.
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get type for table.
void * ecs_table_get_column(const ecs_table_t *table, int32_t index, int32_t offset)
Get column from table.
int32_t ecs_table_count(const ecs_table_t *table)
Returns the number of records in the table.
int32_t ecs_table_get_depth(const ecs_world_t *world, const ecs_table_t *table, ecs_entity_t rel)
Return depth for table in tree for relationship rel.
size_t ecs_table_get_column_size(const ecs_table_t *table, int32_t index)
Get column size from table.
void * get_by_index(int32_t index) const override
Get pointer to component array by column index.
Definition: table.hpp:275
T * get() const
Get pointer to component array by component.
Definition: table.hpp:179
int32_t search(flecs::entity_t second) const
Find index for pair.
Definition: table.hpp:76
int32_t search() const
Find index for pair.
Definition: table.hpp:86
First * get(flecs::entity_t second) const
Get pointer to component array by pair.
Definition: table.hpp:201
flecs::string str() const
Convert table type to string.
Definition: table.hpp:28
int32_t search() const
Find index for type.
Definition: table.hpp:57
bool has(flecs::id_t id) const
Test if table has (component) id.
Definition: table.hpp:95
size_t column_size(int32_t column_index)
Get column size.
Definition: table.hpp:218
int32_t count() const
Get table count.
Definition: table.hpp:38
bool has(flecs::entity_t second) const
Test if table has the pair.
Definition: table.hpp:126
void * get(flecs::id_t id) const
Get pointer to component array by component.
Definition: table.hpp:155
int32_t depth()
Get depth for given relationship.
Definition: table.hpp:237
int32_t search(flecs::entity_t first, flecs::entity_t second) const
Find index for pair.
Definition: table.hpp:66
bool has(flecs::entity_t first, flecs::entity_t second) const
Test if table has the pair.
Definition: table.hpp:115
A * get() const
Get pointer to component array by pair.
Definition: table.hpp:213
void * get(flecs::entity_t first, flecs::entity_t second) const
Get pointer to component array by pair.
Definition: table.hpp:169
bool has() const
Test if table has the type.
Definition: table.hpp:105
bool has() const
Test if table has the pair.
Definition: table.hpp:137
int32_t depth(flecs::entity_t rel)
Get depth for given relationship.
Definition: table.hpp:227
int32_t search(flecs::id_t id) const
Find index for (component) id.
Definition: table.hpp:47
flecs::type type() const
Get table type.
Definition: table.hpp:33
virtual void * get_by_index(int32_t index) const
Get pointer to component array by column index.
Definition: table.hpp:146
A * get() const
Get pointer to component array by component.
Definition: table.hpp:190
Type class.
Definition: type.hpp:21
The world.
Definition: world.hpp:113