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 type_index(flecs::id_t id) const {
48 return ecs_table_get_type_index(m_world, m_table, id);
49 }
50
56 template <typename T>
57 int32_t type_index() const {
58 return type_index(_::cpp_type<T>::id(m_world));
59 }
60
66 int32_t type_index(flecs::entity_t first, flecs::entity_t second) const {
67 return type_index(ecs_pair(first, second));
68 }
69
75 template <typename First>
76 int32_t type_index(flecs::entity_t second) const {
77 return type_index(_::cpp_type<First>::id(m_world), second);
78 }
79
85 template <typename First, typename Second>
86 int32_t type_index() const {
87 return type_index<First>(_::cpp_type<Second>::id(m_world));
88 }
89
95 int32_t column_index(flecs::id_t id) const {
96 return ecs_table_get_column_index(m_world, m_table, id);
97 }
98
104 template <typename T>
105 int32_t column_index() const {
106 return column_index(_::cpp_type<T>::id(m_world));
107 }
108
114 int32_t column_index(flecs::entity_t first, flecs::entity_t second) const {
115 return column_index(ecs_pair(first, second));
116 }
117
123 template <typename First>
124 int32_t column_index(flecs::entity_t second) const {
125 return column_index(_::cpp_type<First>::id(m_world), second);
126 }
127
133 template <typename First, typename Second>
134 int32_t column_index() const {
135 return column_index<First>(_::cpp_type<Second>::id(m_world));
136 }
137
143 bool has(flecs::id_t id) const {
144 return type_index(id) != -1;
145 }
146
152 template <typename T>
153 bool has() const {
154 return type_index<T>() != -1;
155 }
156
163 bool has(flecs::entity_t first, flecs::entity_t second) const {
164 return type_index(first, second) != -1;
165 }
166
173 template <typename First>
174 bool has(flecs::entity_t second) const {
175 return type_index<First>(second) != -1;
176 }
177
184 template <typename First, typename Second>
185 bool has() const {
186 return type_index<First, Second>() != -1;
187 }
188
194 virtual void* get_column(int32_t index) const {
195 return ecs_table_get_column(m_table, index, 0);
196 }
197
203 void* get(flecs::id_t id) const {
204 int32_t index = column_index(id);
205 if (index == -1) {
206 return NULL;
207 }
208 return get_column(index);
209 }
210
217 void* get(flecs::entity_t first, flecs::entity_t second) const {
218 return get(ecs_pair(first, second));
219 }
220
226 template <typename T, if_t< is_actual<T>::value > = 0>
227 T* get() const {
228 return static_cast<T*>(get(_::cpp_type<T>::id(m_world)));
229 }
230
236 template <typename T, if_t< is_enum<T>::value > = 0>
237 T* get() const {
238 return static_cast<T*>(get(_::cpp_type<T>::id(m_world)));
239 }
240
246 template <typename T, typename A = actual_type_t<T>,
247 if_t< flecs::is_pair<T>::value > = 0>
248 A* get() const {
249 return static_cast<A*>(get(_::cpp_type<T>::id(m_world)));
250 }
251
258 template <typename First>
259 First* get(flecs::entity_t second) const {
260 return static_cast<First*>(get(_::cpp_type<First>::id(m_world), second));
261 }
262
269 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
270 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
271 A* get() const {
272 return static_cast<A*>(get<First>(_::cpp_type<Second>::id(m_world)));
273 }
274
276 size_t column_size(int32_t index) {
277 return ecs_table_get_column_size(m_table, index);
278 }
279
285 int32_t depth(flecs::entity_t rel) {
286 return ecs_table_get_depth(m_world, m_table, rel);
287 }
288
294 template <typename Rel>
295 int32_t depth() {
296 return depth(_::cpp_type<Rel>::id(m_world));
297 }
298
299 /* Implicit conversion to table_t */
300 operator table_t*() const {
301 return m_table;
302 }
303
304protected:
305 world_t *m_world;
306 table_t *m_table;
307};
308
311 : table()
312 , m_offset(0)
313 , m_count(0) { }
314
315 table_range(world_t *world, table_t *t, int32_t offset, int32_t count)
316 : table(world, t)
317 , m_offset(offset)
318 , m_count(count) { }
319
320 int32_t offset() const {
321 return m_offset;
322 }
323
324 int32_t count() const {
325 return m_count;
326 }
327
333 void* get_column(int32_t index) const override {
334 return ecs_table_get_column(m_table, index, m_offset);
335 }
336
337private:
338 int32_t m_offset = 0;
339 int32_t m_count = 0;
340};
341
344}
char * ecs_table_str(const ecs_world_t *world, const ecs_table_t *table)
Convert table to string.
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 by column index.
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.
int32_t ecs_table_get_column_index(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t id)
Get column index for id.
int32_t ecs_table_get_type_index(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t id)
Get type index for id.
size_t ecs_table_get_column_size(const ecs_table_t *table, int32_t index)
Get column size from table.
void * get_column(int32_t index) const override
Get pointer to component array by column index.
Definition table.hpp:333
T * get() const
Get pointer to component array by component.
Definition table.hpp:227
int32_t type_index(flecs::entity_t first, flecs::entity_t second) const
Find type index for pair.
Definition table.hpp:66
First * get(flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:259
flecs::string str() const
Convert table type to string.
Definition table.hpp:28
int32_t column_index(flecs::id_t id) const
Find column index for (component) id.
Definition table.hpp:95
bool has(flecs::id_t id) const
Test if table has (component) id.
Definition table.hpp:143
int32_t type_index() const
Find type index for pair.
Definition table.hpp:86
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:174
void * get(flecs::id_t id) const
Get pointer to component array by component.
Definition table.hpp:203
int32_t depth()
Get depth for given relationship.
Definition table.hpp:295
int32_t type_index() const
Find type index for type.
Definition table.hpp:57
int32_t column_index() const
Find column index for pair.
Definition table.hpp:134
bool has(flecs::entity_t first, flecs::entity_t second) const
Test if table has the pair.
Definition table.hpp:163
A * get() const
Get pointer to component array by pair.
Definition table.hpp:271
void * get(flecs::entity_t first, flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:217
int32_t column_index(flecs::entity_t second) const
Find column index for pair.
Definition table.hpp:124
bool has() const
Test if table has the type.
Definition table.hpp:153
bool has() const
Test if table has the pair.
Definition table.hpp:185
int32_t depth(flecs::entity_t rel)
Get depth for given relationship.
Definition table.hpp:285
int32_t type_index(flecs::id_t id) const
Find type index for (component) id.
Definition table.hpp:47
int32_t column_index() const
Find column index for type.
Definition table.hpp:105
flecs::type type() const
Get table type.
Definition table.hpp:33
int32_t column_index(flecs::entity_t first, flecs::entity_t second) const
Find column index for pair.
Definition table.hpp:114
size_t column_size(int32_t index)
Get column size.
Definition table.hpp:276
A * get() const
Get pointer to component array by component.
Definition table.hpp:248
virtual void * get_column(int32_t index) const
Get pointer to component array by column index.
Definition table.hpp:194
int32_t type_index(flecs::entity_t second) const
Find type index for pair.
Definition table.hpp:76
Type class.
Definition type.hpp:21
The world.
Definition world.hpp:132