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_impl(
iter, std::index_sequence_for<Components...>{});
60 populate_self_impl(
iter, std::index_sequence_for<Components...>{});
68 using A = remove_pointer_t<actual_type_t<T>>;
69 if constexpr (!is_empty_v<A>) {
70 if (
iter->row_fields & (1llu << index)) {
72 fields_[index].is_row =
true;
73 fields_[index].is_ref =
true;
74 fields_[index].index =
static_cast<int8_t
>(index);
77 static_cast<int8_t
>(index));
78 fields_[index].is_ref =
iter->sources[index] != 0;
85 (void)
iter; (void)index;
87 using A = remove_pointer_t<actual_type_t<T>>;
88 if constexpr (!is_empty_v<A>) {
90 static_cast<int8_t
>(index));
91 fields_[index].is_ref =
false;
95 template <
size_t... Is>
96 void populate_impl(
const ecs_iter_t *
iter, std::index_sequence<Is...>) {
98 (populate_field<Components>(
iter, Is), ...);
101 template <
size_t... Is>
102 void populate_self_impl(
const ecs_iter_t *
iter, std::index_sequence<Is...>) {
104 (populate_self_field<Components>(
iter, Is), ...);
112template <
typename T,
typename =
int>
118 : field_(
field), row_(row) {
129 !is_empty<actual_type_t<T>>::value &&
is_actual<T>::value > >
136 return static_cast<T*
>(this->field_.ptr)[this->row_];
145 !is_empty<actual_type_t<T>>::value && !
is_actual<T>::value> >
152 return static_cast<actual_type_t<T>*
>(this->field_.ptr)[this->row_];
159struct each_field<T, if_t< is_empty<actual_type_t<T>>::value &&
160 !is_pointer<T>::value > >
167 return actual_type_t<T>();
175 !is_empty<actual_type_t<T>>::value > >
181 actual_type_t<T> get_row() {
182 if (this->field_.ptr) {
183 return &
static_cast<actual_type_t<T>
>(this->field_.ptr)[this->row_];
193template <
typename T,
typename =
int>
211 static_cast<int32_t
>(row));
217template <
typename Func,
typename ... Components>
219 using Terms =
typename field_ptrs<Components ...>::array;
221 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
223 : func_(FLECS_MOV(func)) { }
234 iter->flags |= EcsIterCppEach;
236 if (
iter->ref_fields |
iter->up_fields) {
237 terms.populate(
iter);
238 invoke_unpack< each_ref_field >(
iter, func_, 0, terms.fields_);
240 terms.populate_self(
iter);
241 invoke_unpack< each_field >(
iter, func_, 0, terms.fields_);
248 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
256 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
268 static void destruct(
void *obj) {
269 _::free_obj<each_delegate>(obj);
276 iter->callback_ctx = ctx->on_add;
284 iter->callback_ctx = ctx->on_remove;
292 iter->callback_ctx = ctx->on_set;
300 iter->callback_ctx = ctx->on_replace;
306 template <
template<
typename X,
typename =
int>
class ColumnType,
309 decltype(std::declval<const Fn&>()(
310 std::declval<flecs::entity>(),
311 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
312 static void invoke_callback(
316 "query does not return entities ($this variable is not populated)");
318 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
323 template <
template<
typename X,
typename =
int>
class ColumnType,
326 decltype(std::declval<const Fn&>()(
327 std::declval<flecs::iter&>(),
328 std::declval<size_t&>(),
329 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
330 static void invoke_callback(
334 func(it, i, (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
339 template <
template<
typename X,
typename =
int>
class ColumnType,
342 decltype(std::declval<const Fn&>()(
343 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
344 static void invoke_callback(
347 func((ColumnType< remove_reference_t<Components> >(
iter, comps, i)
351 template <
template<
typename X,
typename =
int>
class ColumnType,
352 typename... Args, if_t<
353 sizeof...(Components) ==
sizeof...(Args)> = 0>
354 static void invoke_unpack(
355 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
357 ECS_TABLE_LOCK(
iter->world,
iter->table);
359 size_t count =
static_cast<size_t>(
iter->count);
360 if (count == 0 && !
iter->table) {
366 for (
size_t i = 0; i < count; i ++) {
367 invoke_callback<ColumnType>(
iter, func, i, comps...);
370 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
373 template <
template<
typename X,
typename =
int>
class ColumnType,
374 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
376 size_t index, Terms& columns, Args... comps)
378 invoke_unpack<ColumnType>(
379 iter, func, index + 1, columns, comps..., columns[index]);
386template <
typename Func,
typename ... Components>
388 using Terms =
typename field_ptrs<Components ...>::array;
390 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
392 : func_(FLECS_MOV(func)) { }
403 iter->flags |= EcsIterCppEach;
405 if (
iter->ref_fields |
iter->up_fields) {
406 terms.populate(
iter);
407 return invoke_callback< each_ref_field >(
iter, func_, 0, terms.fields_);
409 terms.populate_self(
iter);
410 return invoke_callback< each_field >(
iter, func_, 0, terms.fields_);
417 template <
template<
typename X,
typename =
int>
class ColumnType,
420 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
421 decltype(bool(std::declval<const Fn&>()(
422 std::declval<flecs::entity>(),
423 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
425 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
427 ECS_TABLE_LOCK(
iter->world,
iter->table);
430 size_t count =
static_cast<size_t>(
iter->count);
433 for (
size_t i = 0; i < count; i ++) {
435 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
443 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
450 template <
template<
typename X,
typename =
int>
class ColumnType,
453 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
454 decltype(bool(std::declval<const Fn&>()(
455 std::declval<flecs::iter&>(),
456 std::declval<size_t&>(),
457 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
459 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
461 size_t count =
static_cast<size_t>(
iter->count);
471 ECS_TABLE_LOCK(
iter->world,
iter->table);
473 for (
size_t i = 0; i < count; i ++) {
475 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
483 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
489 template <
template<
typename X,
typename =
int>
class ColumnType,
492 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
493 decltype(bool(std::declval<const Fn&>()(
494 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
496 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
498 size_t count =
static_cast<size_t>(
iter->count);
508 ECS_TABLE_LOCK(
iter->world,
iter->table);
510 for (
size_t i = 0; i < count; i ++) {
512 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
520 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
525 template <
template<
typename X,
typename =
int>
class ColumnType,
526 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
528 size_t index, Terms& columns, Args... comps)
530 return invoke_callback<ColumnType>(
531 iter, func, index + 1, columns, comps..., columns[index]);
541template <
typename Func>
543 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
545 : func_(FLECS_MOV(func)) { }
555 iter->flags &= ~EcsIterIsValid;
562 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
574template <
typename Func>
577 : func_(FLECS_MOV(func)) { }
585 template <
typename F,
586 decltype(std::declval<const F&>()(std::declval<flecs::entity>()), 0) = 0>
589 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
593 template <
typename F,
594 decltype(std::declval<const F&>()(), 0) = 0>
597 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
604template <
typename Func,
typename Event>
607 : func_(FLECS_MOV(func)) { }
615 template <
typename F,
616 decltype(std::declval<const F&>()(
617 std::declval<Event&>()), 0) = 0>
621 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
623 "entity observer invoked without payload");
625 Event *data =
static_cast<Event*
>(
iter->
param);
629 template <
typename F,
630 decltype(std::declval<const F&>()(
631 std::declval<flecs::entity>(),
632 std::declval<Event&>()), 0) = 0>
636 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
638 "entity observer invoked without payload");
640 Event *data =
static_cast<Event*
>(
iter->
param);
652template<
typename ... Args>
655template<
typename ... Args>
662 static constexpr bool const_args() {
663 return (is_const_v<remove_reference_t<Args>> && ...);
687 for (int32_t column : columns) {
699 ptrs[i ++] = ecs_record_get_by_column(r, column, 0);
716 template <
typename Func>
717 static bool invoke_read(world_t *
world, entity_t e,
const Func& func) {
729 bool has_components = get_ptrs(
world, e, r,
table, ptrs);
730 if (has_components) {
731 invoke_callback(func, 0, ptrs);
736 return has_components;
739 template <
typename Func>
740 static bool invoke_write(world_t *
world, entity_t e,
const Func& func) {
752 bool has_components = get_ptrs(
world, e, r,
table, ptrs);
753 if (has_components) {
754 invoke_callback(func, 0, ptrs);
759 return has_components;
762 template <
typename Func>
763 static bool invoke_get(world_t *
world, entity_t e,
const Func& func) {
764 if constexpr (const_args()) {
765 return invoke_read(
world, e, func);
767 return invoke_write(
world, e, func);
785 InvokeCtx(flecs::table_t *table_arg) :
table(table_arg) { }
786 flecs::table_t *
table;
787 size_t component_count = 0;
791 static int invoke_add(
794 flecs::id_t component_id,
797 ecs_table_diff_t diff;
798 flecs::table_t *next = flecs_table_traverse_add(
799 w, ctx.table, &component_id, &diff);
800 if (next != ctx.table) {
801 ctx.added[ctx.component_count] = component_id;
802 ctx.component_count ++;
804 if (diff.added_flags & EcsTableHasDontFragment) {
807 ctx.added[ctx.component_count] = component_id;
808 ctx.component_count ++;
817 template <
typename Func>
818 static bool invoke_ensure(
845 InvokeCtx ctx(table);
846 DummyArray dummy_before ({ (
847 invoke_add(w,
id, w.
id<Args>(), ctx)
853 if (table != ctx.table) {
855 ids.
array = ctx.added.ptr();
856 ids.
count =
static_cast<ecs_size_t
>(ctx.component_count);
857 ecs_commit(world,
id, r, ctx.table, &ids, NULL);
861 if (!get_ptrs(w,
id, r, table, ptrs)) {
865 ECS_TABLE_LOCK(world, table);
869 ensure_ptrs(world,
id, ptrs);
872 invoke_callback(func, 0, ptrs);
875 ECS_TABLE_UNLOCK(world, table);
879 DummyArray dummy_after ({
888 template <
typename Func,
typename ... TArgs,
889 if_t<
sizeof...(TArgs) ==
sizeof...(Args)> = 0>
890 static void invoke_callback(
891 const Func& f,
size_t, ArrayType&, TArgs&& ... comps)
893 f(*
static_cast<typename base_arg_type<Args>::type*
>(comps)...);
896 template <
typename Func,
typename ... TArgs,
897 if_t<
sizeof...(TArgs) !=
sizeof...(Args)> = 0>
898 static void invoke_callback(
const Func& f,
size_t arg, ArrayType& ptrs,
901 invoke_callback(f, arg + 1, ptrs, comps..., ptrs[arg]);
905template <
typename Func,
typename U =
int>
910template <
typename Func>
915 "function must have at least one argument");
921template <
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_modified_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Signal that a component has been modified.
void * ecs_get_mut_id(const ecs_world_t *world, ecs_entity_t entity, ecs_id_t component)
Get a mutable pointer to a component.
void * ecs_ensure_id(ecs_world_t *world, ecs_entity_t entity, ecs_id_t component, size_t size)
Ensure entity has component, return pointer.
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.
int32_t ecs_table_get_column_index(const ecs_world_t *world, const ecs_table_t *table, ecs_id_t component)
Get column index for component.
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.
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.