16 void *on_add =
nullptr;
17 void *on_remove =
nullptr;
18 void *on_set =
nullptr;
24 if (on_add && free_on_add) {
27 if (on_remove && free_on_remove) {
28 free_on_remove(on_remove);
30 if (on_set && free_on_set) {
42template <
typename ... Components>
46 bool populate(
const ecs_iter_t *
iter) {
47 return populate(
iter, 0,
static_cast<
49 remove_pointer_t<Components>
>
57 bool populate(
const ecs_iter_t*,
size_t) {
return false; }
59 template <
typename T,
typename... Targs>
60 bool populate(
const ecs_iter_t *
iter,
size_t index, T, Targs... comps) {
61 m_terms[index].ptr =
iter->ptrs[index];
62 bool is_ref =
iter->sources &&
iter->sources[index] != 0;
63 m_terms[index].is_ref = is_ref;
64 is_ref |= populate(
iter, index + 1, comps ...);
73template <
typename T,
typename =
int>
79 : m_term(
term), m_row(row) { }
89 !is_empty<actual_type_t<T>>::value &&
is_actual<T>::value > >
96 return static_cast<T*
>(this->m_term.ptr)[this->m_row];
105 !is_empty<actual_type_t<T>>::value && !
is_actual<T>::value> >
112 return static_cast<actual_type_t<T>*
>(this->m_term.ptr)[this->m_row];
121 !is_pointer<T>::value > >
128 return actual_type_t<T>();
136 !is_empty<actual_type_t<T>>::value > >
143 if (this->m_term.ptr) {
144 return &
static_cast<actual_type_t<T>
>(this->m_term.ptr)[this->m_row];
154template <
typename T,
typename =
int>
172template <
typename Func,
typename ... Components>
176 static constexpr bool PassEntity =
181 static constexpr bool PassIter =
185 "each() must have at least one argument");
187 using Terms =
typename term_ptrs<Components ...>::array;
189 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
191 : m_func(FLECS_MOV(func)) { }
199 void invoke(ecs_iter_t *
iter)
const {
202 if (terms.populate(
iter)) {
203 invoke_callback< each_ref_column >(
iter, m_func, 0, terms.m_terms);
205 invoke_callback< each_column >(
iter, m_func, 0, terms.m_terms);
210 static void run(ecs_iter_t *
iter) {
212 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
217 static void run_add(ecs_iter_t *
iter) {
220 iter->binding_ctx = ctx->on_add;
225 static void run_remove(ecs_iter_t *
iter) {
228 iter->binding_ctx = ctx->on_remove;
233 static void run_set(ecs_iter_t *
iter) {
236 iter->binding_ctx = ctx->on_set;
241 static bool instanced() {
248 template <
template<
typename X,
typename =
int>
class ColumnType,
249 typename... Args, if_t<
250 sizeof...(Components) ==
sizeof...(Args) && PassEntity> = 0>
251 static void invoke_callback(
252 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
254 ECS_TABLE_LOCK(
iter->world,
iter->table);
257 size_t count =
static_cast<size_t>(
iter->count);
260 "no entities returned, use each() without flecs::entity argument");
262 for (
size_t i = 0; i < count; i ++) {
264 (ColumnType< remove_reference_t<Components> >(comps, i)
268 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
273 template <
template<
typename X,
typename =
int>
class ColumnType,
274 typename... Args,
int Enabled = PassIter, if_t<
275 sizeof...(Components) ==
sizeof...(Args) && Enabled> = 0>
276 static void invoke_callback(
277 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
279 size_t count =
static_cast<size_t>(
iter->count);
288 ECS_TABLE_LOCK(
iter->world,
iter->table);
290 for (
size_t i = 0; i < count; i ++) {
291 func(it, i, (ColumnType< remove_reference_t<Components> >(comps, i)
295 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
299 template <
template<
typename X,
typename =
int>
class ColumnType,
300 typename... Args, if_t<
301 sizeof...(Components) ==
sizeof...(Args) && !PassEntity && !PassIter> = 0>
302 static void invoke_callback(
303 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
305 size_t count =
static_cast<size_t>(
iter->count);
314 ECS_TABLE_LOCK(
iter->world,
iter->table);
316 for (
size_t i = 0; i < count; i ++) {
317 func( (ColumnType< remove_reference_t<Components> >(comps, i)
321 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
324 template <
template<
typename X,
typename =
int>
class ColumnType,
325 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
326 static void invoke_callback(ecs_iter_t *
iter,
const Func& func,
327 size_t index, Terms& columns, Args... comps)
329 invoke_callback<ColumnType>(
330 iter, func, index + 1, columns, comps..., columns[index]);
341template <
typename Func,
typename ... Components>
346 using Terms =
typename term_ptrs<Components ...>::array;
349 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
351 : m_func(FLECS_MOV(func)) { }
359 void invoke(ecs_iter_t *
iter)
const {
361 terms.populate(
iter);
362 invoke_callback(
iter, m_func, 0, terms.m_terms);
366 static void run(ecs_iter_t *
iter) {
368 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
373 static bool instanced() {
378 template <
typename... Args, if_t<!
sizeof...(Args) && IterOnly> = 0>
379 static void invoke_callback(ecs_iter_t *
iter,
const Func& func,
380 size_t, Terms&, Args...)
384 ECS_TABLE_LOCK(
iter->world,
iter->table);
388 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
391 template <
typename... Targs, if_t<!IterOnly &&
392 (
sizeof...(Targs) ==
sizeof...(Components))> = 0>
393 static void invoke_callback(ecs_iter_t *
iter,
const Func& func,
size_t,
394 Terms&, Targs... comps)
398 ECS_TABLE_LOCK(
iter->world,
iter->table);
400 func(it, (
static_cast<
403 actual_type_t<Components>
> >* >
406 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
409 template <
typename... Targs, if_t<!IterOnly &&
410 (
sizeof...(Targs) !=
sizeof...(Components)) > = 0>
411 static void invoke_callback(ecs_iter_t *
iter,
const Func& func,
412 size_t index, Terms& columns, Targs... comps)
414 invoke_callback(
iter, func, index + 1, columns, comps...,
426template<
typename ... Args>
429template<
typename ... Args>
436 static bool const_args() {
437 static flecs::array<bool,
sizeof...(Args)> is_const_args ({
438 flecs::is_const<flecs::remove_reference_t<Args>>::value...
441 for (
auto is_const : is_const_args) {
455 if (!storage_table) {
470 for (int32_t
column : columns) {
492 template <
typename Func>
493 static bool invoke_read(world_t *
world, entity_t e,
const Func& func) {
506 if ((has_components = get_ptrs(
world, r,
table, ptrs))) {
507 invoke_callback(func, 0, ptrs);
512 return has_components;
515 template <
typename Func>
516 static bool invoke_write(world_t *
world, entity_t e,
const Func& func) {
529 if ((has_components = get_ptrs(
world, r,
table, ptrs))) {
530 invoke_callback(func, 0, ptrs);
535 return has_components;
538 template <
typename Func>
539 static bool invoke_get(world_t *
world, entity_t e,
const Func& func) {
541 return invoke_read(
world, e, func);
543 return invoke_write(
world, e, func);
560 template <
typename Func>
561 static bool invoke_get_mut(world_t *
world, entity_t
id,
const Func& func) {
591 elem = store_added(added, elem, prev, next, w.
id<Args>()),
599 ids.array = added.ptr();
600 ids.count =
static_cast<ecs_size_t
>(elem);
605 if (!get_ptrs(w, r,
table, ptrs)) {
613 get_mut_ptrs(
world,
id, ptrs);
616 invoke_callback(func, 0, ptrs);
632 template <
typename Func,
typename ... TArgs,
633 if_t<
sizeof...(TArgs) ==
sizeof...(Args)> = 0>
634 static void invoke_callback(
635 const Func& f,
size_t,
ArrayType&, TArgs&& ... comps)
637 f(*
static_cast<typename base_arg_type<Args>::type*
>(comps)...);
640 template <
typename Func,
typename ... TArgs,
641 if_t<
sizeof...(TArgs) !=
sizeof...(Args)> = 0>
642 static void invoke_callback(
const Func& f,
size_t arg,
ArrayType& ptrs,
645 invoke_callback(f, arg + 1, ptrs, comps..., ptrs[arg]);
649template <
typename Func,
typename U =
int>
654template <
typename Func>
659 "function must have at least one argument");
#define ecs_assert(condition, error_code,...)
Assert.
#define ecs_abort(error_code,...)
Abort.
ecs_id_t ecs_entity_t
An entity identifier.
struct ecs_world_t ecs_world_t
A world is the container for all ECS data and supporting features.
struct ecs_table_t ecs_table_t
A table is where entities and components are stored.
flecs::entity id(E value) const
Convert enum constant to entity.
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
void ecs_read_end(const ecs_record_t *record)
End read access to entity.
void ecs_write_end(ecs_record_t *record)
End exclusive write access to entity.
ecs_record_t * ecs_write_begin(ecs_world_t *world, ecs_entity_t entity)
Begin exclusive write access to entity.
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Signal that a component has been modified.
const ecs_record_t * ecs_read_begin(ecs_world_t *world, ecs_entity_t entity)
Begin read access to entity.
void * ecs_get_mut_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get a mutable pointer to a component.
ecs_table_t * ecs_table_add_id(ecs_world_t *world, ecs_table_t *table, ecs_id_t id)
Get table that has all components of current table plus the specified id.
int32_t ecs_search_offset(const ecs_world_t *world, const ecs_table_t *table, int32_t offset, ecs_id_t id, ecs_id_t *id_out)
Search for component id in table type starting from an offset.
bool ecs_commit(ecs_world_t *world, ecs_entity_t entity, ecs_record_t *record, ecs_table_t *table, const ecs_type_t *added, const ecs_type_t *removed)
Commit (move) entity to a table.
ecs_table_t * ecs_table_get_storage_table(const ecs_table_t *table)
Get storage type for table.
void * ecs_record_get_column(const ecs_record_t *r, int32_t column, size_t c_size)
Get component pointer from column/record.
ecs_record_t * ecs_record_find(const ecs_world_t *world, ecs_entity_t entity)
Find record for entity.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
An array with (component) ids.
Wrapper class around a column.
Class that wraps around a flecs::id_t.
Class for iterating over query results.
Class that describes a term.
bool is_stage() const
Test if is a stage.
bool is_deferred() const
Test whether deferring is enabled.