Skip to content
Flecs v4.1
table.hpp
Go to the documentation of this file.
1/**
2 * @file addons/cpp/table.hpp
3 * @brief Direct access to table data.
4 */
5
6#pragma once
7
8namespace flecs {
9
10/**
11 * @defgroup cpp_tables Tables
12 * @ingroup cpp_core
13 * Table operations.
14 *
15 * @{
16 */
17
18/** Table.
19 * A table stores entities with the same set of components.
20 *
21 * @ingroup cpp_tables
22 */
23struct table {
24 /** Default constructor. */
25 table() : world_(nullptr), table_(nullptr) { }
26
27 /** Construct a table from a world and C table pointer.
28 *
29 * @param world The world.
30 * @param t Pointer to the C table.
31 */
33 : world_(world)
34 , table_(t) { }
35
36 /** Destructor. */
37 virtual ~table() { }
38
39 /** Convert the table type to a string. */
41 return flecs::string(ecs_table_str(world_, table_));
42 }
43
44 /** Get the table type. */
45 flecs::type type() const {
46 return flecs::type(world_, ecs_table_get_type(table_));
47 }
48
49 /** Get the table count. */
50 int32_t count() const {
51 return ecs_table_count(table_);
52 }
53
54 /** Get the number of allocated elements in the table. */
55 int32_t size() const {
56 return ecs_table_size(table_);
57 }
58
59 /** Get the array of entity IDs. */
60 const flecs::entity_t* entities() const {
61 return ecs_table_entities(table_);
62 }
63
64 /** Delete entities in the table. */
65 void clear_entities() const {
66 ecs_table_clear_entities(world_, table_);
67 }
68
69 template <typename... T, typename... Args>
70 int32_t type_index(Args... args) const {
71 return ecs_table_get_type_index(world_, table_, _::make_id<T...>(world_, args...).id);
72 }
73
74 template <typename... T, typename... Args>
75 int32_t column_index(Args... args) const {
76 return ecs_table_get_column_index(world_, table_, _::make_id<T...>(world_, args...).id);
77 }
78
79 template <typename... T, typename... Args>
80 bool has(Args... args) const {
81 return type_index<T...>(args...) != -1;
82 }
83
84 /** Get a pointer to the component array by column index.
85 *
86 * @param index The column index.
87 * @return Pointer to the column, nullptr if not a component.
88 */
89 virtual void* get_column(int32_t index) const {
90 return ecs_table_get_column(table_, index, 0);
91 }
92
93
94 template <typename... T, typename... Args>
95 auto try_get(Args... args) const {
96 return get_ptr<false>(_::make_id<T...>(world_, args...));
97 }
98
99 template <typename... T, typename... Args>
100 auto get(Args... args) const {
101 return get_ptr<true>(_::make_id<T...>(world_, args...));
102 }
103
104 /** Get the column size.
105 *
106 * @param index The column index.
107 * @return The size of the column's component type.
108 */
109 size_t column_size(int32_t index) const {
110 return ecs_table_get_column_size(table_, index);
111 }
112
113 /** Get the depth for a given relationship.
114 *
115 * @param rel The relationship.
116 * @return The depth.
117 */
118 int32_t depth(flecs::entity_t rel) const {
119 return ecs_table_get_depth(world_, table_, rel);
120 }
121
122 /** Get the depth for a given relationship.
123 *
124 * @tparam Rel The relationship.
125 * @return The depth.
126 */
127 template <typename Rel>
128 int32_t depth() const {
129 return depth(_::type<Rel>::id(world_));
130 }
131
132 /** Get the table records array.
133 *
134 * @return The table records.
135 */
136 ecs_table_records_t records() const {
137 return flecs_table_records(table_);
138 }
139
140 /** Get the table ID.
141 *
142 * @return The table ID.
143 */
144 uint64_t id() const {
145 return flecs_table_id(table_);
146 }
147
148 /** Lock the table. */
149 void lock() const {
150 ecs_table_lock(world_, table_);
151 }
152
153 /** Unlock the table. */
154 void unlock() const {
155 ecs_table_unlock(world_, table_);
156 }
157
158 /** Check if the table has flags.
159 *
160 * @param flags The flags to check for.
161 * @return True if the table has the specified flags.
162 */
163 bool has_flags(ecs_flags32_t flags) const {
164 return ecs_table_has_flags(table_, flags);
165 }
166
167 /** Get the table.
168 *
169 * @return The table.
170 */
172 return table_;
173 }
174
175 /** Implicit conversion to table_t*. */
176 operator table_t*() const {
177 return table_;
178 }
179
180private:
181 template <bool Required, typename Id>
182 typename Id::type* get_ptr(Id id) const {
183 int32_t index = column_index(id.id);
184 if (index == -1) {
185 return nullptr;
186 }
187 auto ptr = static_cast<typename Id::type*>(get_column(index));
188 if constexpr (Required) {
189 ecs_assert(ptr != nullptr, ECS_INVALID_OPERATION,
190 "invalid get: table does not have component (use try_get())");
191 }
192 return ptr;
193 }
194
195protected:
196 world_t *world_;
197 table_t *table_;
198};
199
200/** Table range.
201 * A table range represents a contiguous range of entities in a table.
202 *
203 * @ingroup cpp_tables
204 */
206 /** Default constructor. */
208 : table()
209 , offset_(0)
210 , count_(0) { }
211
212 /** Construct a table range from a world, table, offset, and count.
213 *
214 * @param world The world.
215 * @param t Pointer to the C table.
216 * @param offset The starting row offset.
217 * @param count The number of rows in the range.
218 */
219 table_range(world_t *world, table_t *t, int32_t offset, int32_t count)
220 : table(world, t)
221 , offset_(offset)
222 , count_(count) { }
223
224 /** Get the offset of the range. */
225 int32_t offset() const {
226 return offset_;
227 }
228
229 /** Get the number of entities in the range. */
230 int32_t count() const {
231 return count_;
232 }
233
234 /** Get a pointer to the component array by column index.
235 *
236 * @param index The column index.
237 * @return Pointer to the column, nullptr if not a component.
238 */
239 void* get_column(int32_t index) const override {
240 return ecs_table_get_column(table_, index, offset_);
241 }
242
243private:
244 int32_t offset_ = 0;
245 int32_t count_ = 0;
246};
247
248/** @} */
249
250}
#define ecs_assert(condition, error_code,...)
Assert.
Definition log.h:473
#define ECS_INVALID_OPERATION
Invalid operation error code.
Definition log.h:669
ecs_table_t table_t
Table type.
Definition c_types.hpp:23
ecs_entity_t entity_t
Entity type.
Definition c_types.hpp:21
ecs_world_t world_t
World type.
Definition c_types.hpp:18
char * ecs_table_str(const ecs_world_t *world, const ecs_table_t *table)
Convert a table to a string.
const ecs_type_t * ecs_table_get_type(const ecs_table_t *table)
Get the type for a table.
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.
int32_t ecs_table_size(const ecs_table_t *table)
Return the allocated size of the table.
bool ecs_table_has_flags(ecs_table_t *table, ecs_flags32_t flags)
Test a table for flags.
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.
int32_t ecs_table_count(const ecs_table_t *table)
Return the number of entities in the table.
const ecs_entity_t * ecs_table_entities(const ecs_table_t *table)
Return the array with entity IDs for the 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 the depth for a table in the tree for the specified relationship.
void ecs_table_lock(ecs_world_t *world, ecs_table_t *table)
Lock a table.
size_t ecs_table_get_column_size(const ecs_table_t *table, int32_t index)
Get the column size from a table.
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.
void ecs_table_clear_entities(ecs_world_t *world, ecs_table_t *table)
Remove all entities in a table.
Owned string wrapper.
Definition string.hpp:15
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:219
void * get_column(int32_t index) const override
Get a pointer to the component array by column index.
Definition table.hpp:239
int32_t count() const
Get the number of entities in the range.
Definition table.hpp:230
int32_t offset() const
Get the offset of the range.
Definition table.hpp:225
table_range()
Default constructor.
Definition table.hpp:207
int32_t depth() const
Get the depth for a given relationship.
Definition table.hpp:128
uint64_t id() const
Get the table ID.
Definition table.hpp:144
flecs::string str() const
Convert the table type to a string.
Definition table.hpp:40
table(world_t *world, table_t *t)
Construct a table from a world and C table pointer.
Definition table.hpp:32
int32_t depth(flecs::entity_t rel) const
Get the depth for a given relationship.
Definition table.hpp:118
int32_t count() const
Get the table count.
Definition table.hpp:50
virtual ~table()
Destructor.
Definition table.hpp:37
table()
Default constructor.
Definition table.hpp:25
bool has_flags(ecs_flags32_t flags) const
Check if the table has flags.
Definition table.hpp:163
const flecs::entity_t * entities() const
Get the array of entity IDs.
Definition table.hpp:60
void lock() const
Lock the table.
Definition table.hpp:149
table_t * get_table() const
Get the table.
Definition table.hpp:171
size_t column_size(int32_t index) const
Get the column size.
Definition table.hpp:109
void clear_entities() const
Delete entities in the table.
Definition table.hpp:65
flecs::type type() const
Get the table type.
Definition table.hpp:45
int32_t size() const
Get the number of allocated elements in the table.
Definition table.hpp:55
ecs_table_records_t records() const
Get the table records array.
Definition table.hpp:136
void unlock() const
Unlock the table.
Definition table.hpp:154
virtual void * get_column(int32_t index) const
Get a pointer to the component array by column index.
Definition table.hpp:89
Type class.
Definition type.hpp:21
The world.
Definition world.hpp:129