18 void *on_add =
nullptr;
19 void *on_remove =
nullptr;
20 void *on_set =
nullptr;
21 void *on_replace =
nullptr;
28 if (on_add && free_on_add) {
31 if (on_remove && free_on_remove) {
32 free_on_remove(on_remove);
34 if (on_set && free_on_set) {
37 if (on_replace && free_on_replace) {
38 free_on_replace(on_replace);
51template <
typename ... Components>
56 populate(
iter, 0,
static_cast<
58 remove_pointer_t<Components>
>
63 populate_self(
iter, 0,
static_cast<
65 remove_pointer_t<Components>
>
74 template <
typename T,
typename... Targs,
75 typename A = remove_pointer_t<actual_type_t<T>>,
76 if_not_t< is_empty<A>::value > = 0>
77 void populate(
const ecs_iter_t *
iter,
size_t index, T, Targs... comps) {
78 if (
iter->row_fields & (1llu << index)) {
80 fields_[index].is_row =
true;
81 fields_[index].is_ref =
true;
82 fields_[index].index =
static_cast<int8_t
>(index);
85 static_cast<int8_t
>(index));
86 fields_[index].is_ref =
iter->sources[index] != 0;
89 populate(
iter, index + 1, comps ...);
92 template <
typename T,
typename... Targs,
93 typename A = remove_pointer_t<actual_type_t<T>>,
94 if_t< is_empty<A>::value > = 0>
95 void populate(
const ecs_iter_t *
iter,
size_t index, T, Targs... comps) {
96 populate(
iter, index + 1, comps ...);
99 void populate_self(
const ecs_iter_t*,
size_t) { }
101 template <
typename T,
typename... Targs,
102 typename A = remove_pointer_t<actual_type_t<T>>,
103 if_not_t< is_empty<A>::value > = 0>
104 void populate_self(
const ecs_iter_t *
iter,
size_t index, T, Targs... comps) {
106 static_cast<int8_t
>(index));
107 fields_[index].is_ref =
false;
109 populate_self(
iter, index + 1, comps ...);
112 template <
typename T,
typename... Targs,
113 typename A = remove_pointer_t<actual_type_t<T>>,
114 if_t< is_empty<A>::value > = 0>
115 void populate_self(
const ecs_iter_t *
iter,
size_t index, T, Targs... comps) {
116 populate(
iter, index + 1, comps ...);
124template <
typename T,
typename =
int>
130 : field_(
field), row_(row) {
141 !is_empty<actual_type_t<T>>::value &&
is_actual<T>::value > >
148 return static_cast<T*
>(this->field_.ptr)[this->row_];
157 !is_empty<actual_type_t<T>>::value && !
is_actual<T>::value> >
164 return static_cast<actual_type_t<T>*
>(this->field_.ptr)[this->row_];
171struct each_field<T, if_t< is_empty<actual_type_t<T>>::value &&
172 !is_pointer<T>::value > >
179 return actual_type_t<T>();
187 !is_empty<actual_type_t<T>>::value > >
193 actual_type_t<T> get_row() {
194 if (this->field_.ptr) {
195 return &
static_cast<actual_type_t<T>
>(this->field_.ptr)[this->row_];
205template <
typename T,
typename =
int>
223 static_cast<int32_t
>(row));
229template <
typename Func,
typename ... Components>
231 using Terms =
typename field_ptrs<Components ...>::array;
233 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
235 : func_(FLECS_MOV(func)) { }
246 iter->flags |= EcsIterCppEach;
248 if (
iter->ref_fields |
iter->up_fields) {
249 terms.populate(
iter);
250 invoke_unpack< each_ref_field >(
iter, func_, 0, terms.fields_);
252 terms.populate_self(
iter);
253 invoke_unpack< each_field >(
iter, func_, 0, terms.fields_);
260 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
268 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
280 static void destruct(
void *obj) {
281 _::free_obj<each_delegate>(obj);
288 iter->callback_ctx = ctx->on_add;
296 iter->callback_ctx = ctx->on_remove;
304 iter->callback_ctx = ctx->on_set;
312 iter->callback_ctx = ctx->on_replace;
318 template <
template<
typename X,
typename =
int>
class ColumnType,
321 decltype(std::declval<const Fn&>()(
322 std::declval<flecs::entity>(),
323 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
324 static void invoke_callback(
328 "query does not return entities ($this variable is not populated)");
330 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
335 template <
template<
typename X,
typename =
int>
class ColumnType,
338 decltype(std::declval<const Fn&>()(
339 std::declval<flecs::iter&>(),
340 std::declval<size_t&>(),
341 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
342 static void invoke_callback(
346 func(it, i, (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
351 template <
template<
typename X,
typename =
int>
class ColumnType,
354 decltype(std::declval<const Fn&>()(
355 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
356 static void invoke_callback(
359 func((ColumnType< remove_reference_t<Components> >(
iter, comps, i)
363 template <
template<
typename X,
typename =
int>
class ColumnType,
364 typename... Args, if_t<
365 sizeof...(Components) ==
sizeof...(Args)> = 0>
366 static void invoke_unpack(
367 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
369 ECS_TABLE_LOCK(
iter->world,
iter->table);
371 size_t count =
static_cast<size_t>(
iter->count);
372 if (count == 0 && !
iter->table) {
378 for (
size_t i = 0; i < count; i ++) {
379 invoke_callback<ColumnType>(
iter, func, i, comps...);
382 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
385 template <
template<
typename X,
typename =
int>
class ColumnType,
386 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
388 size_t index, Terms& columns, Args... comps)
390 invoke_unpack<ColumnType>(
391 iter, func, index + 1, columns, comps..., columns[index]);
398template <
typename Func,
typename ... Components>
400 using Terms =
typename field_ptrs<Components ...>::array;
402 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
404 : func_(FLECS_MOV(func)) { }
415 iter->flags |= EcsIterCppEach;
417 if (
iter->ref_fields |
iter->up_fields) {
418 terms.populate(
iter);
419 return invoke_callback< each_ref_field >(
iter, func_, 0, terms.fields_);
421 terms.populate_self(
iter);
422 return invoke_callback< each_field >(
iter, func_, 0, terms.fields_);
429 template <
template<
typename X,
typename =
int>
class ColumnType,
432 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
433 decltype(bool(std::declval<const Fn&>()(
434 std::declval<flecs::entity>(),
435 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
437 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
439 ECS_TABLE_LOCK(
iter->world,
iter->table);
442 size_t count =
static_cast<size_t>(
iter->count);
445 for (
size_t i = 0; i < count; i ++) {
447 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
455 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
462 template <
template<
typename X,
typename =
int>
class ColumnType,
465 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
466 decltype(bool(std::declval<const Fn&>()(
467 std::declval<flecs::iter&>(),
468 std::declval<size_t&>(),
469 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
471 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
473 size_t count =
static_cast<size_t>(
iter->count);
483 ECS_TABLE_LOCK(
iter->world,
iter->table);
485 for (
size_t i = 0; i < count; i ++) {
487 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
495 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
501 template <
template<
typename X,
typename =
int>
class ColumnType,
504 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
505 decltype(bool(std::declval<const Fn&>()(
506 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
508 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
510 size_t count =
static_cast<size_t>(
iter->count);
520 ECS_TABLE_LOCK(
iter->world,
iter->table);
522 for (
size_t i = 0; i < count; i ++) {
524 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
532 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
537 template <
template<
typename X,
typename =
int>
class ColumnType,
538 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
540 size_t index, Terms& columns, Args... comps)
542 return invoke_callback<ColumnType>(
543 iter, func, index + 1, columns, comps..., columns[index]);
553template <
typename Func>
555 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
557 : func_(FLECS_MOV(func)) { }
567 iter->flags &= ~EcsIterIsValid;
574 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
586template <
typename Func>
589 : func_(FLECS_MOV(func)) { }
597 template <
typename F,
598 decltype(std::declval<const F&>()(std::declval<flecs::entity>()), 0) = 0>
601 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
605 template <
typename F,
606 decltype(std::declval<const F&>()(), 0) = 0>
609 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
616template <
typename Func,
typename Event>
619 : func_(FLECS_MOV(func)) { }
627 template <
typename F,
628 decltype(std::declval<const F&>()(
629 std::declval<Event&>()), 0) = 0>
633 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
635 "entity observer invoked without payload");
637 Event *data =
static_cast<Event*
>(
iter->
param);
641 template <
typename F,
642 decltype(std::declval<const F&>()(
643 std::declval<flecs::entity>(),
644 std::declval<Event&>()), 0) = 0>
648 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
650 "entity observer invoked without payload");
652 Event *data =
static_cast<Event*
>(
iter->
param);
664template<
typename ... Args>
667template<
typename ... Args>
674 static bool const_args() {
675 static flecs::array<bool,
sizeof...(Args)> is_const_args ({
676 flecs::is_const<flecs::remove_reference_t<Args>>::value...
679 for (
auto is_const : is_const_args) {
708 for (int32_t column : columns) {
720 ptrs[i ++] = ecs_record_get_by_column(r, column, 0);
737 template <
typename Func>
738 static bool invoke_read(world_t *
world, entity_t e,
const Func& func) {
750 bool has_components = get_ptrs(
world, e, r,
table, ptrs);
751 if (has_components) {
752 invoke_callback(func, 0, ptrs);
757 return has_components;
760 template <
typename Func>
761 static bool invoke_write(world_t *
world, entity_t e,
const Func& func) {
773 bool has_components = get_ptrs(
world, e, r,
table, ptrs);
774 if (has_components) {
775 invoke_callback(func, 0, ptrs);
780 return has_components;
783 template <
typename Func>
784 static bool invoke_get(world_t *
world, entity_t e,
const Func& func) {
786 return invoke_read(
world, e, func);
788 return invoke_write(
world, e, func);
806 InvokeCtx(flecs::table_t *table_arg) :
table(table_arg) { }
807 flecs::table_t *
table;
808 size_t component_count = 0;
812 static int invoke_add(
815 flecs::id_t component_id,
818 ecs_table_diff_t diff;
819 flecs::table_t *next = flecs_table_traverse_add(
820 w, ctx.table, &component_id, &diff);
821 if (next != ctx.table) {
822 ctx.added[ctx.component_count] = component_id;
823 ctx.component_count ++;
825 if (diff.added_flags & EcsTableHasDontFragment) {
828 ctx.added[ctx.component_count] = component_id;
829 ctx.component_count ++;
838 template <
typename Func>
839 static bool invoke_ensure(
866 InvokeCtx ctx(table);
867 DummyArray dummy_before ({ (
868 invoke_add(w,
id, w.
id<Args>(), ctx)
874 if (table != ctx.table) {
876 ids.
array = ctx.added.ptr();
877 ids.
count =
static_cast<ecs_size_t
>(ctx.component_count);
878 ecs_commit(world,
id, r, ctx.table, &ids, NULL);
882 if (!get_ptrs(w,
id, r, table, ptrs)) {
886 ECS_TABLE_LOCK(world, table);
890 ensure_ptrs(world,
id, ptrs);
893 invoke_callback(func, 0, ptrs);
896 ECS_TABLE_UNLOCK(world, table);
900 DummyArray dummy_after ({
909 template <
typename Func,
typename ... TArgs,
910 if_t<
sizeof...(TArgs) ==
sizeof...(Args)> = 0>
911 static void invoke_callback(
912 const Func& f,
size_t, ArrayType&, TArgs&& ... comps)
914 f(*
static_cast<typename base_arg_type<Args>::type*
>(comps)...);
917 template <
typename Func,
typename ... TArgs,
918 if_t<
sizeof...(TArgs) !=
sizeof...(Args)> = 0>
919 static void invoke_callback(
const Func& f,
size_t arg, ArrayType& ptrs,
922 invoke_callback(f, arg + 1, ptrs, comps..., ptrs[arg]);
926template <
typename Func,
typename U =
int>
931template <
typename Func>
936 "function must have at least one argument");
942template <
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_record_t ecs_record_t
Information about an entity, like its table and row.
struct ecs_table_t ecs_table_t
A table stores entities and components for a specific type.
flecs::entity entity(Args &&... args) const
Create an entity.
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_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id, size_t size)
Get a mutable pointer to a component.
void ecs_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t id)
Signal that a component has been modified.
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.
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.
A type is a list of (component) ids.
ecs_id_t * array
Array with ids.
int32_t count
Number of elements in array.
const Self & add() const
Add a component to an entity.
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 next()
Progress iterator.
bool is_stage() const
Test if is a stage.
bool is_deferred() const
Test whether deferring is enabled.