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<int32_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 "query does not return entities ($this variable is not populated)");
307 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
312 template <
template<
typename X,
typename =
int>
class ColumnType,
315 decltype(std::declval<const Fn&>()(
316 std::declval<flecs::iter&>(),
317 std::declval<size_t&>(),
318 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
319 static void invoke_callback(
323 func(it, i, (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
328 template <
template<
typename X,
typename =
int>
class ColumnType,
331 decltype(std::declval<const Fn&>()(
332 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
333 static void invoke_callback(
336 func((ColumnType< remove_reference_t<Components> >(
iter, comps, i)
340 template <
template<
typename X,
typename =
int>
class ColumnType,
341 typename... Args, if_t<
342 sizeof...(Components) ==
sizeof...(Args)> = 0>
343 static void invoke_unpack(
344 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
346 ECS_TABLE_LOCK(
iter->world,
iter->table);
348 size_t count =
static_cast<size_t>(
iter->count);
349 if (count == 0 && !
iter->table) {
355 for (
size_t i = 0; i < count; i ++) {
356 invoke_callback<ColumnType>(
iter, func, i, comps...);
359 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
362 template <
template<
typename X,
typename =
int>
class ColumnType,
363 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
365 size_t index, Terms& columns, Args... comps)
367 invoke_unpack<ColumnType>(
368 iter, func, index + 1, columns, comps..., columns[index]);
375template <
typename Func,
typename ... Components>
377 using Terms =
typename field_ptrs<Components ...>::array;
379 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
381 : func_(FLECS_MOV(func)) { }
392 iter->flags |= EcsIterCppEach;
394 if (
iter->ref_fields |
iter->up_fields) {
395 terms.populate(
iter);
396 return invoke_callback< each_ref_field >(
iter, func_, 0, terms.fields_);
398 terms.populate_self(
iter);
399 return invoke_callback< each_field >(
iter, func_, 0, terms.fields_);
406 template <
template<
typename X,
typename =
int>
class ColumnType,
409 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
410 decltype(bool(std::declval<const Fn&>()(
411 std::declval<flecs::entity>(),
412 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
414 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
416 ECS_TABLE_LOCK(
iter->world,
iter->table);
419 size_t count =
static_cast<size_t>(
iter->count);
422 for (
size_t i = 0; i < count; i ++) {
424 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
432 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
439 template <
template<
typename X,
typename =
int>
class ColumnType,
442 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
443 decltype(bool(std::declval<const Fn&>()(
444 std::declval<flecs::iter&>(),
445 std::declval<size_t&>(),
446 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
448 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
450 size_t count =
static_cast<size_t>(
iter->count);
460 ECS_TABLE_LOCK(
iter->world,
iter->table);
462 for (
size_t i = 0; i < count; i ++) {
464 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
472 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
478 template <
template<
typename X,
typename =
int>
class ColumnType,
481 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
482 decltype(bool(std::declval<const Fn&>()(
483 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
485 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
487 size_t count =
static_cast<size_t>(
iter->count);
497 ECS_TABLE_LOCK(
iter->world,
iter->table);
499 for (
size_t i = 0; i < count; i ++) {
501 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
509 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
514 template <
template<
typename X,
typename =
int>
class ColumnType,
515 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
517 size_t index, Terms& columns, Args... comps)
519 return invoke_callback<ColumnType>(
520 iter, func, index + 1, columns, comps..., columns[index]);
530template <
typename Func>
532 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
534 : func_(FLECS_MOV(func)) { }
544 iter->flags &= ~EcsIterIsValid;
551 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
563template <
typename Func>
566 : func_(FLECS_MOV(func)) { }
574 template <
typename F,
575 decltype(std::declval<const F&>()(std::declval<flecs::entity>()), 0) = 0>
578 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
582 template <
typename F,
583 decltype(std::declval<const F&>()(), 0) = 0>
586 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
593template <
typename Func,
typename Event>
596 : func_(FLECS_MOV(func)) { }
604 template <
typename F,
605 decltype(std::declval<const F&>()(
606 std::declval<Event&>()), 0) = 0>
610 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
612 "entity observer invoked without payload");
614 Event *data =
static_cast<Event*
>(
iter->
param);
618 template <
typename F,
619 decltype(std::declval<const F&>()(
620 std::declval<flecs::entity>(),
621 std::declval<Event&>()), 0) = 0>
625 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
627 "entity observer invoked without payload");
629 Event *data =
static_cast<Event*
>(
iter->
param);
641template<
typename ... Args>
644template<
typename ... Args>
651 static bool const_args() {
652 static flecs::array<bool,
sizeof...(Args)> is_const_args ({
653 flecs::is_const<flecs::remove_reference_t<Args>>::value...
656 for (
auto is_const : is_const_args) {
690 for (int32_t column : columns) {
719 template <
typename Func>
720 static bool invoke_read(world_t *
world, entity_t e,
const Func& func) {
732 bool has_components = get_ptrs(
world, e, r,
table, ptrs);
733 if (has_components) {
734 invoke_callback(func, 0, ptrs);
739 return has_components;
742 template <
typename Func>
743 static bool invoke_write(world_t *
world, entity_t e,
const Func& func) {
755 bool has_components = get_ptrs(
world, e, r,
table, ptrs);
756 if (has_components) {
757 invoke_callback(func, 0, ptrs);
762 return has_components;
765 template <
typename Func>
766 static bool invoke_get(world_t *
world, entity_t e,
const Func& func) {
768 return invoke_read(
world, e, func);
770 return invoke_write(
world, e, func);
787 template <
typename Func>
788 static bool invoke_ensure(world_t *
world, entity_t
id,
const Func& func) {
818 elem = store_added(added, elem, prev, next, w.
id<Args>()),
827 ids.
array = added.ptr();
828 ids.
count =
static_cast<ecs_size_t
>(elem);
833 if (!get_ptrs(w,
id, r,
table, ptrs)) {
841 ensure_ptrs(
world,
id, ptrs);
844 invoke_callback(func, 0, ptrs);
860 template <
typename Func,
typename ... TArgs,
861 if_t<
sizeof...(TArgs) ==
sizeof...(Args)> = 0>
862 static void invoke_callback(
863 const Func& f,
size_t,
ArrayType&, TArgs&& ... comps)
865 f(*
static_cast<typename base_arg_type<Args>::type*
>(comps)...);
868 template <
typename Func,
typename ... TArgs,
869 if_t<
sizeof...(TArgs) !=
sizeof...(Args)> = 0>
870 static void invoke_callback(
const Func& f,
size_t arg,
ArrayType& ptrs,
873 invoke_callback(f, arg + 1, ptrs, comps..., ptrs[arg]);
877template <
typename Func,
typename U =
int>
882template <
typename Func>
887 "function must have at least one argument");
893template <
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.