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

| 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
5
6#pragma once
7
8namespace flecs {
9
17
18 struct 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
28 flecs::string str() const {
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
412 struct table_range : table {
413 table_range()
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
446
447}
ecs_assert
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:368
ecs_table_str
char * ecs_table_str(const ecs_world_t *world, const ecs_table_t *table)
Convert table to string.
ecs_table_get_type
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get type for table.
ecs_table_size
int32_t ecs_table_size(const ecs_table_t *table)
Returns allocated size of table.
ecs_table_has_flags
bool ecs_table_has_flags(ecs_table_t *table, ecs_flags32_t flags)
Test table for flags.
ecs_table_get_column
void * ecs_table_get_column(const ecs_table_t *table, int32_t index, int32_t offset)
Get column from table by column index.
ecs_table_count
int32_t ecs_table_count(const ecs_table_t *table)
Returns the number of entities in the table.
ecs_table_entities
const ecs_entity_t * ecs_table_entities(const ecs_table_t *table)
Returns array with entity ids for 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 depth for table in tree for relationship rel.
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 id)
Get column index for id.
ecs_table_lock
void ecs_table_lock(ecs_world_t *world, ecs_table_t *table)
Lock 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 id)
Get type index for id.
ecs_table_get_column_size
size_t ecs_table_get_column_size(const ecs_table_t *table, int32_t index)
Get column size from table.
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
Definition string.hpp:13
flecs::table_range::get_column
void * get_column(int32_t index) const override
Get pointer to component array by column index.
Definition table.hpp:436
flecs::table::get
T * get() const
Get pointer to component array by component.
Definition table.hpp:306
flecs::table::type_index
int32_t type_index(flecs::entity_t first, flecs::entity_t second) const
Find type index for pair.
Definition table.hpp:81
flecs::table::depth
int32_t depth() const
Get depth for given relationship.
Definition table.hpp:365
flecs::table::get
First * get(flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:328
flecs::table::try_get
A * try_get() const
Get pointer to component array by component.
Definition table.hpp:256
flecs::table::id
uint64_t id() const
Get table id.
Definition table.hpp:375
flecs::table::str
flecs::string str() const
Convert table type to string.
Definition table.hpp:28
flecs::table::column_index
int32_t column_index(flecs::id_t id) const
Find column index for (component) id.
Definition table.hpp:110
flecs::table::try_get
T * try_get() const
Get pointer to component array by component.
Definition table.hpp:245
flecs::table::has
bool has(flecs::id_t id) const
Test if table has (component) id.
Definition table.hpp:158
flecs::table::type_index
int32_t type_index() const
Find type index for pair.
Definition table.hpp:101
flecs::table::depth
int32_t depth(flecs::entity_t rel) const
Get depth for given relationship.
Definition table.hpp:355
flecs::table::count
int32_t count() const
Get table count.
Definition table.hpp:38
flecs::table::has
bool has(flecs::entity_t second) const
Test if table has the pair.
Definition table.hpp:189
flecs::table::get
void * get(flecs::id_t id) const
Get pointer to component array by component.
Definition table.hpp:279
flecs::table::try_get
First * try_get(flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:267
flecs::table::type_index
int32_t type_index() const
Find type index for type.
Definition table.hpp:72
flecs::table::column_index
int32_t column_index() const
Find column index for pair.
Definition table.hpp:149
flecs::table::has_flags
bool has_flags(ecs_flags32_t flags) const
Check if table has flags.
Definition table.hpp:390
flecs::table::entities
const flecs::entity_t * entities() const
Get array with entity ids.
Definition table.hpp:48
flecs::table::has
bool has(flecs::entity_t first, flecs::entity_t second) const
Test if table has the pair.
Definition table.hpp:178
flecs::table::get
A * get() const
Get pointer to component array by pair.
Definition table.hpp:341
flecs::table::get
void * get(flecs::entity_t first, flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:296
flecs::table::column_index
int32_t column_index(flecs::entity_t second) const
Find column index for pair.
Definition table.hpp:139
flecs::table::has
bool has() const
Test if table has the type.
Definition table.hpp:168
flecs::table::lock
void lock() const
Lock table.
Definition table.hpp:380
flecs::table::try_get
void * try_get(flecs::entity_t first, flecs::entity_t second) const
Get pointer to component array by pair.
Definition table.hpp:235
flecs::table::has
bool has() const
Test if table has the pair.
Definition table.hpp:200
flecs::table::get_table
table_t * get_table() const
Get table.
Definition table.hpp:398
flecs::table::column_size
size_t column_size(int32_t index) const
Get column size.
Definition table.hpp:346
flecs::table::type_index
int32_t type_index(flecs::id_t id) const
Find type index for (component) id.
Definition table.hpp:62
flecs::table::column_index
int32_t column_index() const
Find column index for type.
Definition table.hpp:120
flecs::table::clear_entities
void clear_entities() const
Delete entities in table.
Definition table.hpp:53
flecs::table::type
flecs::type type() const
Get table type.
Definition table.hpp:33
flecs::table::column_index
int32_t column_index(flecs::entity_t first, flecs::entity_t second) const
Find column index for pair.
Definition table.hpp:129
flecs::table::size
int32_t size() const
Get number of allocated elements in table.
Definition table.hpp:43
flecs::table::records
ecs_table_records_t records() const
Get table records array.
Definition table.hpp:370
flecs::table::unlock
void unlock() const
Unlock table.
Definition table.hpp:385
flecs::table::get
A * get() const
Get pointer to component array by component.
Definition table.hpp:317
flecs::table::get_column
virtual void * get_column(int32_t index) const
Get pointer to component array by column index.
Definition table.hpp:209
flecs::table::try_get
void * try_get(flecs::id_t id) const
Get pointer to component array by component.
Definition table.hpp:221
flecs::table::type_index
int32_t type_index(flecs::entity_t second) const
Find type index for pair.
Definition table.hpp:91
flecs::type
Type class.
Definition type.hpp:21
flecs::world
The world.
Definition world.hpp:172