18 void *on_add =
nullptr;
19 void *on_remove =
nullptr;
20 void *on_set =
nullptr;
26 if (on_add && free_on_add) {
29 if (on_remove && free_on_remove) {
30 free_on_remove(on_remove);
32 if (on_set && free_on_set) {
46template <
typename ... Components>
51 populate(
iter, 0,
static_cast<
53 remove_pointer_t<Components>
>
58 populate_self(
iter, 0,
static_cast<
60 remove_pointer_t<Components>
>
69 template <
typename T,
typename... Targs,
70 typename A = remove_pointer_t<actual_type_t<T>>,
71 if_not_t< is_empty<A>::value > = 0>
72 void populate(
const ecs_iter_t *
iter,
size_t index, T, Targs... comps) {
73 if (
iter->row_fields & (1llu << index)) {
75 fields_[index].is_row =
true;
76 fields_[index].is_ref =
true;
77 fields_[index].index =
static_cast<int8_t
>(index);
80 static_cast<int8_t
>(index));
81 fields_[index].is_ref =
iter->sources[index] != 0;
84 populate(
iter, index + 1, comps ...);
87 template <
typename T,
typename... Targs,
88 typename A = remove_pointer_t<actual_type_t<T>>,
89 if_t< is_empty<A>::value > = 0>
90 void populate(
const ecs_iter_t *
iter,
size_t index, T, Targs... comps) {
91 populate(
iter, index + 1, comps ...);
94 void populate_self(
const ecs_iter_t*,
size_t) { }
96 template <
typename T,
typename... Targs,
97 typename A = remove_pointer_t<actual_type_t<T>>,
98 if_not_t< is_empty<A>::value > = 0>
99 void populate_self(
const ecs_iter_t *
iter,
size_t index, T, Targs... comps) {
101 static_cast<int8_t
>(index));
102 fields_[index].is_ref =
false;
104 populate_self(
iter, index + 1, comps ...);
107 template <
typename T,
typename... Targs,
108 typename A = remove_pointer_t<actual_type_t<T>>,
109 if_t< is_empty<A>::value > = 0>
110 void populate_self(
const ecs_iter_t *
iter,
size_t index, T, Targs... comps) {
111 populate(
iter, index + 1, comps ...);
119template <
typename T,
typename =
int>
125 : field_(
field), row_(row) {
136 !is_empty<actual_type_t<T>>::value &&
is_actual<T>::value > >
143 return static_cast<T*
>(this->field_.ptr)[this->row_];
152 !is_empty<actual_type_t<T>>::value && !
is_actual<T>::value> >
159 return static_cast<actual_type_t<T>*
>(this->field_.ptr)[this->row_];
166struct each_field<T, if_t< is_empty<actual_type_t<T>>::value &&
167 !is_pointer<T>::value > >
174 return actual_type_t<T>();
182 !is_empty<actual_type_t<T>>::value > >
188 actual_type_t<T> get_row() {
189 if (this->field_.ptr) {
190 return &
static_cast<actual_type_t<T>
>(this->field_.ptr)[this->row_];
200template <
typename T,
typename =
int>
218 static_cast<int8_t
>(row));
224template <
typename Func,
typename ... Components>
226 using Terms =
typename field_ptrs<Components ...>::array;
228 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
230 : func_(FLECS_MOV(func)) { }
241 iter->flags |= EcsIterCppEach;
243 if (
iter->ref_fields |
iter->up_fields) {
244 terms.populate(
iter);
245 invoke_unpack< each_ref_field >(
iter, func_, 0, terms.fields_);
247 terms.populate_self(
iter);
248 invoke_unpack< each_field >(
iter, func_, 0, terms.fields_);
255 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
265 static void destruct(
void *obj) {
266 _::free_obj<each_delegate>(obj);
273 iter->callback_ctx = ctx->on_add;
281 iter->callback_ctx = ctx->on_remove;
289 iter->callback_ctx = ctx->on_set;
295 template <
template<
typename X,
typename =
int>
class ColumnType,
298 decltype(std::declval<const Fn&>()(
299 std::declval<flecs::entity>(),
300 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
301 static void invoke_callback(
305 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
310 template <
template<
typename X,
typename =
int>
class ColumnType,
313 decltype(std::declval<const Fn&>()(
314 std::declval<flecs::iter&>(),
315 std::declval<size_t&>(),
316 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
317 static void invoke_callback(
321 func(it, i, (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
326 template <
template<
typename X,
typename =
int>
class ColumnType,
329 decltype(std::declval<const Fn&>()(
330 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
331 static void invoke_callback(
334 func((ColumnType< remove_reference_t<Components> >(
iter, comps, i)
338 template <
template<
typename X,
typename =
int>
class ColumnType,
339 typename... Args, if_t<
340 sizeof...(Components) ==
sizeof...(Args)> = 0>
341 static void invoke_unpack(
342 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
344 ECS_TABLE_LOCK(
iter->world,
iter->table);
346 size_t count =
static_cast<size_t>(
iter->count);
347 if (count == 0 && !
iter->table) {
353 for (
size_t i = 0; i < count; i ++) {
354 invoke_callback<ColumnType>(
iter, func, i, comps...);
357 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
360 template <
template<
typename X,
typename =
int>
class ColumnType,
361 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
363 size_t index, Terms& columns, Args... comps)
365 invoke_unpack<ColumnType>(
366 iter, func, index + 1, columns, comps..., columns[index]);
373template <
typename Func,
typename ... Components>
375 using Terms =
typename field_ptrs<Components ...>::array;
377 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
379 : func_(FLECS_MOV(func)) { }
390 iter->flags |= EcsIterCppEach;
392 if (
iter->ref_fields |
iter->up_fields) {
393 terms.populate(
iter);
394 return invoke_callback< each_ref_field >(
iter, func_, 0, terms.fields_);
396 terms.populate_self(
iter);
397 return invoke_callback< each_field >(
iter, func_, 0, terms.fields_);
404 template <
template<
typename X,
typename =
int>
class ColumnType,
407 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
408 decltype(bool(std::declval<const Fn&>()(
409 std::declval<flecs::entity>(),
410 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
412 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
414 ECS_TABLE_LOCK(
iter->world,
iter->table);
417 size_t count =
static_cast<size_t>(
iter->count);
420 for (
size_t i = 0; i < count; i ++) {
422 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
430 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
437 template <
template<
typename X,
typename =
int>
class ColumnType,
440 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
441 decltype(bool(std::declval<const Fn&>()(
442 std::declval<flecs::iter&>(),
443 std::declval<size_t&>(),
444 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
446 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
448 size_t count =
static_cast<size_t>(
iter->count);
458 ECS_TABLE_LOCK(
iter->world,
iter->table);
460 for (
size_t i = 0; i < count; i ++) {
462 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
470 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
476 template <
template<
typename X,
typename =
int>
class ColumnType,
479 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
480 decltype(bool(std::declval<const Fn&>()(
481 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
483 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
485 size_t count =
static_cast<size_t>(
iter->count);
495 ECS_TABLE_LOCK(
iter->world,
iter->table);
497 for (
size_t i = 0; i < count; i ++) {
499 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
507 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
512 template <
template<
typename X,
typename =
int>
class ColumnType,
513 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
515 size_t index, Terms& columns, Args... comps)
517 return invoke_callback<ColumnType>(
518 iter, func, index + 1, columns, comps..., columns[index]);
528template <
typename Func>
530 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
532 : func_(FLECS_MOV(func)) { }
542 iter->flags &= ~EcsIterIsValid;
549 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
561template <
typename Func>
564 : func_(FLECS_MOV(func)) { }
572 template <
typename F,
573 decltype(std::declval<const F&>()(std::declval<flecs::entity>()), 0) = 0>
576 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
580 template <
typename F,
581 decltype(std::declval<const F&>()(), 0) = 0>
584 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
591template <
typename Func,
typename Event>
594 : func_(FLECS_MOV(func)) { }
602 template <
typename F,
603 decltype(std::declval<const F&>()(
604 std::declval<Event&>()), 0) = 0>
608 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
610 "entity observer invoked without payload");
612 Event *data =
static_cast<Event*
>(
iter->
param);
616 template <
typename F,
617 decltype(std::declval<const F&>()(
618 std::declval<flecs::entity>(),
619 std::declval<Event&>()), 0) = 0>
623 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
625 "entity observer invoked without payload");
627 Event *data =
static_cast<Event*
>(
iter->
param);
639template<
typename ... Args>
642template<
typename ... Args>
649 static bool const_args() {
650 static flecs::array<bool,
sizeof...(Args)> is_const_args ({
651 flecs::is_const<flecs::remove_reference_t<Args>>::value...
654 for (
auto is_const : is_const_args) {
688 for (int32_t column : columns) {
717 template <
typename Func>
718 static bool invoke_read(world_t *
world, entity_t e,
const Func& func) {
730 bool has_components = get_ptrs(
world, e, r,
table, ptrs);
731 if (has_components) {
732 invoke_callback(func, 0, ptrs);
737 return has_components;
740 template <
typename Func>
741 static bool invoke_write(world_t *
world, entity_t e,
const Func& func) {
753 bool has_components = get_ptrs(
world, e, r,
table, ptrs);
754 if (has_components) {
755 invoke_callback(func, 0, ptrs);
760 return has_components;
763 template <
typename Func>
764 static bool invoke_get(world_t *
world, entity_t e,
const Func& func) {
766 return invoke_read(
world, e, func);
768 return invoke_write(
world, e, func);
785 template <
typename Func>
786 static bool invoke_ensure(world_t *
world, entity_t
id,
const Func& func) {
816 elem = store_added(added, elem, prev, next, w.
id<Args>()),
825 ids.
array = added.ptr();
826 ids.
count =
static_cast<ecs_size_t
>(elem);
831 if (!get_ptrs(w,
id, r,
table, ptrs)) {
839 ensure_ptrs(
world,
id, ptrs);
842 invoke_callback(func, 0, ptrs);
858 template <
typename Func,
typename ... TArgs,
859 if_t<
sizeof...(TArgs) ==
sizeof...(Args)> = 0>
860 static void invoke_callback(
861 const Func& f,
size_t,
ArrayType&, TArgs&& ... comps)
863 f(*
static_cast<typename base_arg_type<Args>::type*
>(comps)...);
866 template <
typename Func,
typename ... TArgs,
867 if_t<
sizeof...(TArgs) !=
sizeof...(Args)> = 0>
868 static void invoke_callback(
const Func& f,
size_t arg,
ArrayType& ptrs,
871 invoke_callback(f, arg + 1, ptrs, comps..., ptrs[arg]);
875template <
typename Func,
typename U =
int>
880template <
typename Func>
885 "function must have at least one argument");
891template <
typename Func,
typename ... Args>
#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 stores entities and components for a specific type.
flecs::id id(E value) const
Convert enum constant to entity.
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
void * ecs_get_mut_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get a mutable pointer to a component.
void * ecs_record_get_by_column(const ecs_record_t *record, int32_t column, size_t size)
Get component pointer from column/record.
void ecs_read_end(const ecs_record_t *record)
End read access to entity.
void * ecs_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Get a mutable pointer to a component.
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.
ecs_record_t * ecs_record_find(const ecs_world_t *world, ecs_entity_t entity)
Find record for entity.
const ecs_record_t * ecs_read_begin(ecs_world_t *world, ecs_entity_t entity)
Begin read access to entity.
ecs_entity_t ecs_field_src(const ecs_iter_t *it, int8_t index)
Return field source.
void * ecs_field_at_w_size(const ecs_iter_t *it, size_t size, int8_t index, int32_t row)
Get data for field at specified row.
void * ecs_field_w_size(const ecs_iter_t *it, size_t size, int8_t index)
Get data for field.
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.
bool ecs_table_has_flags(ecs_table_t *table, ecs_flags32_t flags)
Test table for flags.
int32_t ecs_table_column_count(const ecs_table_t *table)
Return number of columns in table.
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.
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.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
ecs_table_t * table
Identifies a type (and table) in world.
A type is a list of (component) ids.
ecs_id_t * array
Array with ids.
int32_t count
Number of elements in array.
Wrapper class around a field.
Class that wraps around a flecs::id_t.
Class for iterating over query results.
void * param()
Access param.
flecs::field< const flecs::entity_t > entities() const
Get readonly access to entity ids.
bool is_stage() const
Test if is a stage.
bool is_deferred() const
Test whether deferring is enabled.