Flecs v4.0
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() : world_(nullptr), table_(nullptr) { }
20
21 table(world_t *world, table_t *t)
22 : world_(world)
23 , table_(t) { }
24
25 virtual ~table() { }
26
29 return flecs::string(ecs_table_str(world_, table_));
30 }
31
33 flecs::type type() const {
34 return flecs::type(world_, ecs_table_get_type(table_));
35 }
36
38 int32_t count() const {
39 return ecs_table_count(table_);
40 }
41
43 int32_t size() const {
44 return ecs_table_size(table_);
45 }
46
48 const flecs::entity_t* entities() const {
49 return ecs_table_entities(table_);
50 }
51
53 void clear_entities() const {
54 ecs_table_clear_entities(world_, table_);
55 }
56
62 int32_t type_index(flecs::id_t id) const {
63 return ecs_table_get_type_index(world_, table_, id);
64 }
65
71 template <typename T>
72 int32_t type_index() const {
73 return type_index(_::type<T>::id(world_));
74 }
75
81 int32_t type_index(flecs::entity_t first, flecs::entity_t second) const {
82 return type_index(ecs_pair(first, second));
83 }
84
90 template <typename First>
91 int32_t type_index(flecs::entity_t second) const {
92 return type_index(_::type<First>::id(world_), second);
93 }
94
100 template <typename First, typename Second>
101 int32_t type_index() const {
102 return type_index<First>(_::type<Second>::id(world_));
103 }
104
110 int32_t column_index(flecs::id_t id) const {
111 return ecs_table_get_column_index(world_, table_, id);
112 }
113
119 template <typename T>
120 int32_t column_index() const {
121 return column_index(_::type<T>::id(world_));
122 }
123
129 int32_t column_index(flecs::entity_t first, flecs::entity_t second) const {
130 return column_index(ecs_pair(first, second));
131 }
132
138 template <typename First>
139 int32_t column_index(flecs::entity_t second) const {
140 return column_index(_::type<First>::id(world_), second);
141 }
142
148 template <typename First, typename Second>
149 int32_t column_index() const {
150 return column_index<First>(_::type<Second>::id(world_));
151 }
152
158 bool has(flecs::id_t id) const {
159 return type_index(id) != -1;
160 }
161
167 template <typename T>
168 bool has() const {
169 return type_index<T>() != -1;
170 }
171
178 bool has(flecs::entity_t first, flecs::entity_t second) const {
179 return type_index(first, second) != -1;
180 }
181
188 template <typename First>
189 bool has(flecs::entity_t second) const {
190 return type_index<First>(second) != -1;
191 }
192
199 template <typename First, typename Second>
200 bool has() const {
201 return type_index<First, Second>() != -1;
202 }
203
209 virtual void* get_column(int32_t index) const {
210 return ecs_table_get_column(table_, index, 0);
211 }
212
218 void* get(flecs::id_t id) const {
219 int32_t index = column_index(id);
220 if (index == -1) {
221 return NULL;
222 }
223 return get_column(index);
224 }
225
232 void* get(flecs::entity_t first, flecs::entity_t second) const {
233 return get(ecs_pair(first, second));
234 }
235
241 template <typename T, if_t< is_actual<T>::value > = 0>
242 T* get() const {
243 return static_cast<T*>(get(_::type<T>::id(world_)));
244 }
245
251 template <typename T, if_t< is_enum<T>::value > = 0>
252 T* get() const {
253 return static_cast<T*>(get(_::type<T>::id(world_)));
254 }
255
261 template <typename T, typename A = actual_type_t<T>,
262 if_t< flecs::is_pair<T>::value > = 0>
263 A* get() const {
264 return static_cast<A*>(get(_::type<T>::id(world_)));
265 }
266
273 template <typename First>
274 First* get(flecs::entity_t second) const {
275 return static_cast<First*>(get(_::type<First>::id(world_), second));
276 }
277
284 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
285 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
286 A* get() const {
287 return static_cast<A*>(get<First>(_::type<Second>::id(world_)));
288 }
289
291 size_t column_size(int32_t index) const {
292 return ecs_table_get_column_size(table_, index);
293 }
294
300 int32_t depth(flecs::entity_t rel) const {
301 return ecs_table_get_depth(world_, table_, rel);
302 }
303
309 template <typename Rel>
310 int32_t depth() const {
311 return depth(_::type<Rel>::id(world_));
312 }
313
315 ecs_table_records_t records() const {
316 return flecs_table_records(table_);
317 }
318
320 uint64_t id() const {
321 return flecs_table_id(table_);
322 }
323
325 void lock() const {
326 ecs_table_lock(world_, table_);
327 }
328
330 void unlock() const {
331 ecs_table_unlock(world_, table_);
332 }
333
335 bool has_flags(ecs_flags32_t flags) const {
336 return ecs_table_has_flags(table_, flags);
337 }
338
343 table_t* get_table() const {
344 return table_;
345 }
346
347 /* Implicit conversion to table_t */
348 operator table_t*() const {
349 return table_;
350 }
351
352protected:
353 world_t *world_;
354 table_t *table_;
355};
356
359 : table()
360 , offset_(0)
361 , count_(0) { }
362
363 table_range(world_t *world, table_t *t, int32_t offset, int32_t count)
364 : table(world, t)
365 , offset_(offset)
366 , count_(count) { }
367
368 int32_t offset() const {
369 return offset_;
370 }
371
372 int32_t count() const {
373 return count_;
374 }
375
381 void* get_column(int32_t index) const override {
382 return ecs_table_get_column(table_, index, offset_);
383 }
384
385private:
386 int32_t offset_ = 0;
387 int32_t count_ = 0;
388};
389
392}
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.
int32_t ecs_table_size(const ecs_table_t *table)
Returns allocated size of table.
bool ecs_table_has_flags(ecs_table_t *table, ecs_flags32_t flags)
Test table for flags.
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 entities in the table.
const ecs_entity_t * ecs_table_entities(const ecs_table_t *table)
Returns array with entity ids for table.
void ecs_table_unlock(ecs_world_t *world, ecs_table_t *table)
Unlock a 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.
void ecs_table_lock(ecs_world_t *world, ecs_table_t *table)
Lock a table.
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 ecs_table_clear_entities(ecs_world_t *world, ecs_table_t *table)
Remove all entities in a table.
void * get_column(int32_t index) const override
Get pointer to component array by column index.
Definition table.hpp:381
T * get() const
Get pointer to component array by component.
Definition table.hpp:242
int32_t type_index(flecs::entity_t first, flecs::entity_t second) const
Find type index for pair.
Definition table.hpp:81
int32_t depth() const
Get depth for given relationship.
Definition table.hpp:310
First * get(flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:274
uint64_t id() const
Get table id.
Definition table.hpp:320
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:110
bool has(flecs::id_t id) const
Test if table has (component) id.
Definition table.hpp:158
int32_t type_index() const
Find type index for pair.
Definition table.hpp:101
int32_t depth(flecs::entity_t rel) const
Get depth for given relationship.
Definition table.hpp:300
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:189
void * get(flecs::id_t id) const
Get pointer to component array by component.
Definition table.hpp:218
int32_t type_index() const
Find type index for type.
Definition table.hpp:72
int32_t column_index() const
Find column index for pair.
Definition table.hpp:149
bool has_flags(ecs_flags32_t flags) const
Check if table has flags.
Definition table.hpp:335
const flecs::entity_t * entities() const
Get array with entity ids.
Definition table.hpp:48
bool has(flecs::entity_t first, flecs::entity_t second) const
Test if table has the pair.
Definition table.hpp:178
A * get() const
Get pointer to component array by pair.
Definition table.hpp:286
void * get(flecs::entity_t first, flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:232
int32_t column_index(flecs::entity_t second) const
Find column index for pair.
Definition table.hpp:139
bool has() const
Test if table has the type.
Definition table.hpp:168
void lock() const
Lock table.
Definition table.hpp:325
bool has() const
Test if table has the pair.
Definition table.hpp:200
table_t * get_table() const
Get table.
Definition table.hpp:343
size_t column_size(int32_t index) const
Get column size.
Definition table.hpp:291
int32_t type_index(flecs::id_t id) const
Find type index for (component) id.
Definition table.hpp:62
int32_t column_index() const
Find column index for type.
Definition table.hpp:120
void clear_entities() const
Delete entities in table.
Definition table.hpp:53
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:129
int32_t size() const
Get number of allocated elements in table.
Definition table.hpp:43
ecs_table_records_t records() const
Get table records array.
Definition table.hpp:315
void unlock() const
Unlock table.
Definition table.hpp:330
A * get() const
Get pointer to component array by component.
Definition table.hpp:263
virtual void * get_column(int32_t index) const
Get pointer to component array by column index.
Definition table.hpp:209
int32_t type_index(flecs::entity_t second) const
Find type index for pair.
Definition table.hpp:91
Type class.
Definition type.hpp:21
The world.
Definition world.hpp:137