Flecs: include/flecs/addons/cpp/table.hpp Source File
Description: A fast entity component system (ECS) for C & C++

| Flecs v4.1 A fast entity component system (ECS) for C & C++

Loading...
Searching...
No Matches
table.hpp
Go to the documentation of this file.
1
5
6#pragma once
7
8namespace flecs {
9
17
23 struct table {
25 table() : world_(nullptr), table_(nullptr) { }
26
32 table(world_t *world, table_t *t)
33 : world_(world)
34 , table_(t) { }
35
37 virtual ~table() { }
38
40 flecs::string str() const {
41 return flecs::string(ecs_table_str(world_, table_));
42 }
43
45 flecs::type type() const {
46 return flecs::type(world_, ecs_table_get_type(table_));
47 }
48
50 int32_t count() const {
51 return ecs_table_count(table_);
52 }
53
55 int32_t size() const {
56 return ecs_table_size(table_);
57 }
58
60 const flecs::entity_t* entities() const {
61 return ecs_table_entities(table_);
62 }
63
65 void clear_entities() const {
66 ecs_table_clear_entities(world_, table_);
67 }
68
74 int32_t type_index(flecs::id_t id) const {
75 return ecs_table_get_type_index(world_, table_, id);
76 }
77
83 template <typename T>
84 int32_t type_index() const {
85 return type_index(_::type<T>::id(world_));
86 }
87
94 int32_t type_index(flecs::entity_t first, flecs::entity_t second) const {
95 return type_index(ecs_pair(first, second));
96 }
97
104 template <typename First>
105 int32_t type_index(flecs::entity_t second) const {
106 return type_index(_::type<First>::id(world_), second);
107 }
108
115 template <typename First, typename Second>
116 int32_t type_index() const {
117 return type_index<First>(_::type<Second>::id(world_));
118 }
119
125 int32_t column_index(flecs::id_t id) const {
126 return ecs_table_get_column_index(world_, table_, id);
127 }
128
134 template <typename T>
135 int32_t column_index() const {
136 return column_index(_::type<T>::id(world_));
137 }
138
145 int32_t column_index(flecs::entity_t first, flecs::entity_t second) const {
146 return column_index(ecs_pair(first, second));
147 }
148
155 template <typename First>
156 int32_t column_index(flecs::entity_t second) const {
157 return column_index(_::type<First>::id(world_), second);
158 }
159
166 template <typename First, typename Second>
167 int32_t column_index() const {
168 return column_index<First>(_::type<Second>::id(world_));
169 }
170
176 bool has(flecs::id_t id) const {
177 return type_index(id) != -1;
178 }
179
185 template <typename T>
186 bool has() const {
187 return type_index<T>() != -1;
188 }
189
196 bool has(flecs::entity_t first, flecs::entity_t second) const {
197 return type_index(first, second) != -1;
198 }
199
206 template <typename First>
207 bool has(flecs::entity_t second) const {
208 return type_index<First>(second) != -1;
209 }
210
217 template <typename First, typename Second>
218 bool has() const {
219 return type_index<First, Second>() != -1;
220 }
221
227 virtual void* get_column(int32_t index) const {
228 return ecs_table_get_column(table_, index, 0);
229 }
230
231
232 /* get */
233
239 void* try_get(flecs::id_t id) const {
240 int32_t index = column_index(id);
241 if (index == -1) {
242 return NULL;
243 }
244 return get_column(index);
245 }
246
253 void* try_get(flecs::entity_t first, flecs::entity_t second) const {
254 return try_get(ecs_pair(first, second));
255 }
256
262 template <typename T, if_t< is_actual<T>::value > = 0>
263 T* try_get() const {
264 return static_cast<T*>(try_get(_::type<T>::id(world_)));
265 }
266
272 template <typename T, typename A = actual_type_t<T>,
273 if_t< flecs::is_pair<T>::value > = 0>
274 A* try_get() const {
275 return static_cast<A*>(try_get(_::type<T>::id(world_)));
276 }
277
284 template <typename First>
285 First* try_get(flecs::entity_t second) const {
286 return static_cast<First*>(try_get(_::type<First>::id(world_), second));
287 }
288
289
290 /* get */
291
297 void* get(flecs::id_t id) const {
298 int32_t index = column_index(id);
299 if (index == -1) {
300 return NULL;
301 }
302 void *r = get_column(index);
303 ecs_assert(r != nullptr, ECS_INVALID_OPERATION,
304 "invalid get: table does not have component (use try_get())");
305 return r;
306 }
307
314 void* get(flecs::entity_t first, flecs::entity_t second) const {
315 return get(ecs_pair(first, second));
316 }
317
323 template <typename T, if_t< is_actual<T>::value > = 0>
324 T* get() const {
325 return static_cast<T*>(get(_::type<T>::id(world_)));
326 }
327
333 template <typename T, typename A = actual_type_t<T>,
334 if_t< flecs::is_pair<T>::value > = 0>
335 A* get() const {
336 return static_cast<A*>(get(_::type<T>::id(world_)));
337 }
338
345 template <typename First>
346 First* get(flecs::entity_t second) const {
347 return static_cast<First*>(get(_::type<First>::id(world_), second));
348 }
349
350
357 template <typename First, typename Second, typename P = flecs::pair<First, Second>,
358 typename A = actual_type_t<P>, if_not_t< flecs::is_pair<First>::value> = 0>
359 A* get() const {
360 return static_cast<A*>(get<First>(_::type<Second>::id(world_)));
361 }
362
368 size_t column_size(int32_t index) const {
369 return ecs_table_get_column_size(table_, index);
370 }
371
377 int32_t depth(flecs::entity_t rel) const {
378 return ecs_table_get_depth(world_, table_, rel);
379 }
380
386 template <typename Rel>
387 int32_t depth() const {
388 return depth(_::type<Rel>::id(world_));
389 }
390
395 ecs_table_records_t records() const {
396 return flecs_table_records(table_);
397 }
398
403 uint64_t id() const {
404 return flecs_table_id(table_);
405 }
406
408 void lock() const {
409 ecs_table_lock(world_, table_);
410 }
411
413 void unlock() const {
414 ecs_table_unlock(world_, table_);
415 }
416
422 bool has_flags(ecs_flags32_t flags) const {
423 return ecs_table_has_flags(table_, flags);
424 }
425
430 table_t* get_table() const {
431 return table_;
432 }
433
435 operator table_t*() const {
436 return table_;
437 }
438
439protected:
440 world_t *world_;
441 table_t *table_;
442};
443
449 struct table_range : table {
451 table_range()
452 : table()
453 , offset_(0)
454 , count_(0) { }
455
463 table_range(world_t *world, table_t *t, int32_t offset, int32_t count)
464 : table(world, t)
465 , offset_(offset)
466 , count_(count) { }
467
469 int32_t offset() const {
470 return offset_;
471 }
472
474 int32_t count() const {
475 return count_;
476 }
477
483 void* get_column(int32_t index) const override {
484 return ecs_table_get_column(table_, index, offset_);
485 }
486
487private:
488 int32_t offset_ = 0;
489 int32_t count_ = 0;
490};
491
493
494}
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
ECS_INVALID_OPERATION
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:659
flecs::table_t
ecs_table_t table_t
Table type.
Definition c_types.hpp:23
flecs::id_t
ecs_id_t id_t
ID type.
Definition c_types.hpp:20
flecs::entity_t
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
flecs::world_t
ecs_world_t world_t
World type.
Definition c_types.hpp:18
ecs_table_str
char * ecs_table_str(const ecs_world_t *world, const ecs_table_t *table)
Convert a table to a string.
ecs_table_get_type
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get the type for a table.
ecs_table_get_column_index
int32_t ecs_table_get_column_index(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t component)
Get the column index for a component.
ecs_table_size
int32_t ecs_table_size(const ecs_table_t *table)
Return the allocated size of the table.
ecs_table_has_flags
bool ecs_table_has_flags(ecs_table_t *table, ecs_flags32_t flags)
Test a table for flags.
ecs_table_get_column
void * ecs_table_get_column(const ecs_table_t *table, int32_t index, int32_t offset)
Get a column from a table by column index.
ecs_table_count
int32_t ecs_table_count(const ecs_table_t *table)
Return the number of entities in the table.
ecs_table_entities
const ecs_entity_t * ecs_table_entities(const ecs_table_t *table)
Return the array with entity IDs for the table.
ecs_table_unlock
void ecs_table_unlock(ecs_world_t *world, ecs_table_t *table)
Unlock a table.
ecs_table_get_depth
int32_t ecs_table_get_depth(const ecs_world_t *world, const ecs_table_t *table, ecs_entity_t rel)
Return the depth for a table in the tree for the specified relationship.
ecs_table_lock
void ecs_table_lock(ecs_world_t *world, ecs_table_t *table)
Lock a table.
ecs_table_get_column_size
size_t ecs_table_get_column_size(const ecs_table_t *table, int32_t index)
Get the column size from a table.
ecs_table_get_type_index
int32_t ecs_table_get_type_index(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t component)
Get the type index for a component.
ecs_table_clear_entities
void ecs_table_clear_entities(ecs_world_t *world, ecs_table_t *table)
Remove all entities in a table.
flecs::_::type
Definition flecs.hpp:39
flecs::string
Owned string wrapper.
Definition string.hpp:15
flecs::table_range::table_range
table_range(world_t *world, table_t *t, int32_t offset, int32_t count)
Construct a table range from a world, table, offset, and count.
Definition table.hpp:463
flecs::table_range::get_column
void * get_column(int32_t index) const override
Get a pointer to the component array by column index.
Definition table.hpp:483
flecs::table_range::count
int32_t count() const
Get the number of entities in the range.
Definition table.hpp:474
flecs::table_range::offset
int32_t offset() const
Get the offset of the range.
Definition table.hpp:469
flecs::table_range::table_range
table_range()
Default constructor.
Definition table.hpp:451
flecs::table::get
T * get() const
Get a pointer to the component array by component.
Definition table.hpp:324
flecs::table::type_index
int32_t type_index(flecs::entity_t first, flecs::entity_t second) const
Find the type index for a pair.
Definition table.hpp:94
flecs::table::depth
int32_t depth() const
Get the depth for a given relationship.
Definition table.hpp:387
flecs::table::get
First * get(flecs::entity_t second) const
Get a pointer to the component array by pair.
Definition table.hpp:346
flecs::table::try_get
A * try_get() const
Get a pointer to the component array by component.
Definition table.hpp:274
flecs::table::id
uint64_t id() const
Get the table ID.
Definition table.hpp:403
flecs::table::str
flecs::string str() const
Convert the table type to a string.
Definition table.hpp:40
flecs::table::column_index
int32_t column_index(flecs::id_t id) const
Find the column index for a (component) ID.
Definition table.hpp:125
flecs::table::try_get
T * try_get() const
Get a pointer to the component array by component.
Definition table.hpp:263
flecs::table::table
table(world_t *world, table_t *t)
Construct a table from a world and C table pointer.
Definition table.hpp:32
flecs::table::has
bool has(flecs::id_t id) const
Test if the table has a (component) ID.
Definition table.hpp:176
flecs::table::type_index
int32_t type_index() const
Find the type index for a pair.
Definition table.hpp:116
flecs::table::depth
int32_t depth(flecs::entity_t rel) const
Get the depth for a given relationship.
Definition table.hpp:377
flecs::table::count
int32_t count() const
Get the table count.
Definition table.hpp:50
flecs::table::~table
virtual ~table()
Destructor.
Definition table.hpp:37
flecs::table::has
bool has(flecs::entity_t second) const
Test if the table has the pair.
Definition table.hpp:207
flecs::table::get
void * get(flecs::id_t id) const
Get a pointer to the component array by component.
Definition table.hpp:297
flecs::table::try_get
First * try_get(flecs::entity_t second) const
Get a pointer to the component array by pair.
Definition table.hpp:285
flecs::table::table
table()
Default constructor.
Definition table.hpp:25
flecs::table::type_index
int32_t type_index() const
Find the type index for a type.
Definition table.hpp:84
flecs::table::column_index
int32_t column_index() const
Find the column index for a pair.
Definition table.hpp:167
flecs::table::has_flags
bool has_flags(ecs_flags32_t flags) const
Check if the table has flags.
Definition table.hpp:422
flecs::table::entities
const flecs::entity_t * entities() const
Get the array of entity IDs.
Definition table.hpp:60
flecs::table::has
bool has(flecs::entity_t first, flecs::entity_t second) const
Test if the table has the pair.
Definition table.hpp:196
flecs::table::get
A * get() const
Get a pointer to the component array by pair.
Definition table.hpp:359
flecs::table::get
void * get(flecs::entity_t first, flecs::entity_t second) const
Get a pointer to the component array by pair.
Definition table.hpp:314
flecs::table::column_index
int32_t column_index(flecs::entity_t second) const
Find the column index for a pair.
Definition table.hpp:156
flecs::table::has
bool has() const
Test if the table has the type.
Definition table.hpp:186
flecs::table::lock
void lock() const
Lock the table.
Definition table.hpp:408
flecs::table::try_get
void * try_get(flecs::entity_t first, flecs::entity_t second) const
Get a pointer to the component array by pair.
Definition table.hpp:253
flecs::table::has
bool has() const
Test if the table has the pair.
Definition table.hpp:218
flecs::table::get_table
table_t * get_table() const
Get the table.
Definition table.hpp:430
flecs::table::column_size
size_t column_size(int32_t index) const
Get the column size.
Definition table.hpp:368
flecs::table::type_index
int32_t type_index(flecs::id_t id) const
Find the type index for a (component) ID.
Definition table.hpp:74
flecs::table::column_index
int32_t column_index() const
Find the column index for a type.
Definition table.hpp:135
flecs::table::clear_entities
void clear_entities() const
Delete entities in the table.
Definition table.hpp:65
flecs::table::type
flecs::type type() const
Get the table type.
Definition table.hpp:45
flecs::table::column_index
int32_t column_index(flecs::entity_t first, flecs::entity_t second) const
Find the column index for a pair.
Definition table.hpp:145
flecs::table::size
int32_t size() const
Get the number of allocated elements in the table.
Definition table.hpp:55
flecs::table::records
ecs_table_records_t records() const
Get the table records array.
Definition table.hpp:395
flecs::table::unlock
void unlock() const
Unlock the table.
Definition table.hpp:413
flecs::table::get
A * get() const
Get a pointer to the component array by component.
Definition table.hpp:335
flecs::table::get_column
virtual void * get_column(int32_t index) const
Get a pointer to the component array by column index.
Definition table.hpp:227
flecs::table::try_get
void * try_get(flecs::id_t id) const
Get a pointer to the component array by component.
Definition table.hpp:239
flecs::table::type_index
int32_t type_index(flecs::entity_t second) const
Find the type index for a pair.
Definition table.hpp:105
flecs::type
Type class.
Definition type.hpp:21
flecs::world
The world.
Definition world.hpp:246