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
47 int32_t type_index(flecs::id_t id) const {
48 return ecs_table_get_type_index(world_, table_, id);
49 }
50
56 template <typename T>
57 int32_t type_index() const {
58 return type_index(_::type<T>::id(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(_::type<First>::id(world_), second);
78 }
79
85 template <typename First, typename Second>
86 int32_t type_index() const {
87 return type_index<First>(_::type<Second>::id(world_));
88 }
89
95 int32_t column_index(flecs::id_t id) const {
96 return ecs_table_get_column_index(world_, table_, id);
97 }
98
104 template <typename T>
105 int32_t column_index() const {
106 return column_index(_::type<T>::id(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(_::type<First>::id(world_), second);
126 }
127
133 template <typename First, typename Second>
134 int32_t column_index() const {
135 return column_index<First>(_::type<Second>::id(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(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(_::type<T>::id(world_)));
229 }
230
236 template <typename T, if_t< is_enum<T>::value > = 0>
237 T* get() const {
238 return static_cast<T*>(get(_::type<T>::id(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(_::type<T>::id(world_)));
250 }
251
258 template <typename First>
259 First* get(flecs::entity_t second) const {
260 return static_cast<First*>(get(_::type<First>::id(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>(_::type<Second>::id(world_)));
273 }
274
276 size_t column_size(int32_t index) {
277 return ecs_table_get_column_size(table_, index);
278 }
279
285 int32_t depth(flecs::entity_t rel) {
286 return ecs_table_get_depth(world_, table_, rel);
287 }
288
294 template <typename Rel>
295 int32_t depth() {
296 return depth(_::type<Rel>::id(world_));
297 }
298
303 table_t* get_table() const {
304 return table_;
305 }
306
307 /* Implicit conversion to table_t */
308 operator table_t*() const {
309 return table_;
310 }
311
312protected:
313 world_t *world_;
314 table_t *table_;
315};
316
319 : table()
320 , offset_(0)
321 , count_(0) { }
322
323 table_range(world_t *world, table_t *t, int32_t offset, int32_t count)
324 : table(world, t)
325 , offset_(offset)
326 , count_(count) { }
327
328 int32_t offset() const {
329 return offset_;
330 }
331
332 int32_t count() const {
333 return count_;
334 }
335
341 void* get_column(int32_t index) const override {
342 return ecs_table_get_column(table_, index, offset_);
343 }
344
345private:
346 int32_t offset_ = 0;
347 int32_t count_ = 0;
348};
349
352}
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:341
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
table_t * get_table() const
Get table.
Definition table.hpp:303
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:137