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
213
214 /* get */
215
221 void* try_get(flecs::id_t id) const {
222 int32_t index = column_index(id);
223 if (index == -1) {
224 return NULL;
225 }
226 return get_column(index);
227 }
228
235 void* try_get(flecs::entity_t first, flecs::entity_t second) const {
236 return try_get(ecs_pair(first, second));
237 }
238
244 template <typename T, if_t< is_actual<T>::value > = 0>
245 T* try_get() const {
246 return static_cast<T*>(try_get(_::type<T>::id(world_)));
247 }
248
254 template <typename T, typename A = actual_type_t<T>,
255 if_t< flecs::is_pair<T>::value > = 0>
256 A* try_get() const {
257 return static_cast<A*>(try_get(_::type<T>::id(world_)));
258 }
259
266 template <typename First>
267 First* try_get(flecs::entity_t second) const {
268 return static_cast<First*>(try_get(_::type<First>::id(world_), second));
269 }
270
271
272 /* get */
273
279 void* get(flecs::id_t id) const {
280 int32_t index = column_index(id);
281 if (index == -1) {
282 return NULL;
283 }
284 void *r = get_column(index);
285 ecs_assert(r != nullptr, ECS_INVALID_OPERATION,
286 "invalid get_mut: table does not have component (use try_get)");
287 return r;
288 }
289
296 void* get(flecs::entity_t first, flecs::entity_t second) const {
297 return get(ecs_pair(first, second));
298 }
299
305 template <typename T, if_t< is_actual<T>::value > = 0>
306 T* get() const {
307 return static_cast<T*>(get(_::type<T>::id(world_)));
308 }
309
315 template <typename T, typename A = actual_type_t<T>,
316 if_t< flecs::is_pair<T>::value > = 0>
317 A* get() const {
318 return static_cast<A*>(get(_::type<T>::id(world_)));
319 }
320
327 template <typename First>
328 First* get(flecs::entity_t second) const {
329 return static_cast<First*>(get(_::type<First>::id(world_), second));
330 }
331
332
339 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
340 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
341 A* get() const {
342 return static_cast<A*>(get<First>(_::type<Second>::id(world_)));
343 }
344
346 size_t column_size(int32_t index) const {
347 return ecs_table_get_column_size(table_, index);
348 }
349
355 int32_t depth(flecs::entity_t rel) const {
356 return ecs_table_get_depth(world_, table_, rel);
357 }
358
364 template <typename Rel>
365 int32_t depth() const {
366 return depth(_::type<Rel>::id(world_));
367 }
368
370 ecs_table_records_t records() const {
371 return flecs_table_records(table_);
372 }
373
375 uint64_t id() const {
376 return flecs_table_id(table_);
377 }
378
380 void lock() const {
381 ecs_table_lock(world_, table_);
382 }
383
385 void unlock() const {
386 ecs_table_unlock(world_, table_);
387 }
388
390 bool has_flags(ecs_flags32_t flags) const {
391 return ecs_table_has_flags(table_, flags);
392 }
393
398 table_t* get_table() const {
399 return table_;
400 }
401
402 /* Implicit conversion to table_t */
403 operator table_t*() const {
404 return table_;
405 }
406
407protected:
408 world_t *world_;
409 table_t *table_;
410};
411
414 : table()
415 , offset_(0)
416 , count_(0) { }
417
418 table_range(world_t *world, table_t *t, int32_t offset, int32_t count)
419 : table(world, t)
420 , offset_(offset)
421 , count_(count) { }
422
423 int32_t offset() const {
424 return offset_;
425 }
426
427 int32_t count() const {
428 return count_;
429 }
430
436 void* get_column(int32_t index) const override {
437 return ecs_table_get_column(table_, index, offset_);
438 }
439
440private:
441 int32_t offset_ = 0;
442 int32_t count_ = 0;
443};
444
447}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
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:436
T * get() const
Get pointer to component array by component.
Definition table.hpp:306
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:365
First * get(flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:328
A * try_get() const
Get pointer to component array by component.
Definition table.hpp:256
uint64_t id() const
Get table id.
Definition table.hpp:375
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
T * try_get() const
Get pointer to component array by component.
Definition table.hpp:245
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:355
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:279
First * try_get(flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:267
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:390
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:341
void * get(flecs::entity_t first, flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:296
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:380
void * try_get(flecs::entity_t first, flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:235
bool has() const
Test if table has the pair.
Definition table.hpp:200
table_t * get_table() const
Get table.
Definition table.hpp:398
size_t column_size(int32_t index) const
Get column size.
Definition table.hpp:346
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:370
void unlock() const
Unlock table.
Definition table.hpp:385
A * get() const
Get pointer to component array by component.
Definition table.hpp:317
virtual void * get_column(int32_t index) const
Get pointer to component array by column index.
Definition table.hpp:209
void * try_get(flecs::id_t id) const
Get pointer to component array by component.
Definition table.hpp:221
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