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 "no entities returned, use each() without flecs::entity argument");
308 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
313 template <
template<
typename X,
typename =
int>
class ColumnType,
316 decltype(std::declval<const Fn&>()(
317 std::declval<flecs::iter&>(),
318 std::declval<size_t&>(),
319 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
320 static void invoke_callback(
324 func(it, i, (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
329 template <
template<
typename X,
typename =
int>
class ColumnType,
332 decltype(std::declval<const Fn&>()(
333 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...), 0) = 0>
334 static void invoke_callback(
337 func((ColumnType< remove_reference_t<Components> >(
iter, comps, i)
341 template <
template<
typename X,
typename =
int>
class ColumnType,
342 typename... Args, if_t<
343 sizeof...(Components) ==
sizeof...(Args)> = 0>
344 static void invoke_unpack(
345 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
347 ECS_TABLE_LOCK(
iter->world,
iter->table);
349 size_t count =
static_cast<size_t>(
iter->count);
350 if (count == 0 && !
iter->table) {
356 for (
size_t i = 0; i < count; i ++) {
357 invoke_callback<ColumnType>(
iter, func, i, comps...);
360 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
363 template <
template<
typename X,
typename =
int>
class ColumnType,
364 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
366 size_t index, Terms& columns, Args... comps)
368 invoke_unpack<ColumnType>(
369 iter, func, index + 1, columns, comps..., columns[index]);
376template <
typename Func,
typename ... Components>
378 using Terms =
typename field_ptrs<Components ...>::array;
380 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
382 : func_(FLECS_MOV(func)) { }
393 iter->flags |= EcsIterCppEach;
395 if (
iter->ref_fields |
iter->up_fields) {
396 terms.populate(
iter);
397 return invoke_callback< each_ref_field >(
iter, func_, 0, terms.fields_);
399 terms.populate_self(
iter);
400 return invoke_callback< each_field >(
iter, func_, 0, terms.fields_);
407 template <
template<
typename X,
typename =
int>
class ColumnType,
410 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
411 decltype(bool(std::declval<const Fn&>()(
412 std::declval<flecs::entity>(),
413 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
415 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
417 ECS_TABLE_LOCK(
iter->world,
iter->table);
420 size_t count =
static_cast<size_t>(
iter->count);
424 "no entities returned, use find() without flecs::entity argument");
426 for (
size_t i = 0; i < count; i ++) {
428 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
436 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
443 template <
template<
typename X,
typename =
int>
class ColumnType,
446 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
447 decltype(bool(std::declval<const Fn&>()(
448 std::declval<flecs::iter&>(),
449 std::declval<size_t&>(),
450 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
452 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
454 size_t count =
static_cast<size_t>(
iter->count);
464 ECS_TABLE_LOCK(
iter->world,
iter->table);
466 for (
size_t i = 0; i < count; i ++) {
468 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
476 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
482 template <
template<
typename X,
typename =
int>
class ColumnType,
485 if_t<
sizeof...(Components) ==
sizeof...(Args)> = 0,
486 decltype(bool(std::declval<const Fn&>()(
487 std::declval<ColumnType< remove_reference_t<Components> > >().get_row()...))) =
true>
489 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
491 size_t count =
static_cast<size_t>(
iter->count);
501 ECS_TABLE_LOCK(
iter->world,
iter->table);
503 for (
size_t i = 0; i < count; i ++) {
505 (ColumnType< remove_reference_t<Components> >(
iter, comps, i)
513 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
518 template <
template<
typename X,
typename =
int>
class ColumnType,
519 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
521 size_t index, Terms& columns, Args... comps)
523 return invoke_callback<ColumnType>(
524 iter, func, index + 1, columns, comps..., columns[index]);
534template <
typename Func>
536 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
538 : func_(FLECS_MOV(func)) { }
548 iter->flags &= ~EcsIterIsValid;
555 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
567template <
typename Func>
570 : func_(FLECS_MOV(func)) { }
578 template <
typename F,
579 decltype(std::declval<const F&>()(std::declval<flecs::entity>()), 0) = 0>
582 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
586 template <
typename F,
587 decltype(std::declval<const F&>()(), 0) = 0>
590 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
597template <
typename Func,
typename Event>
600 : func_(FLECS_MOV(func)) { }
608 template <
typename F,
609 decltype(std::declval<const F&>()(
610 std::declval<Event&>()), 0) = 0>
614 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
616 "entity observer invoked without payload");
618 Event *data =
static_cast<Event*
>(
iter->
param);
622 template <
typename F,
623 decltype(std::declval<const F&>()(
624 std::declval<flecs::entity>(),
625 std::declval<Event&>()), 0) = 0>
629 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
631 "entity observer invoked without payload");
633 Event *data =
static_cast<Event*
>(
iter->
param);
645template<
typename ... Args>
648template<
typename ... Args>
655 static bool const_args() {
656 static flecs::array<bool,
sizeof...(Args)> is_const_args ({
657 flecs::is_const<flecs::remove_reference_t<Args>>::value...
660 for (
auto is_const : is_const_args) {
694 for (int32_t column : columns) {
723 template <
typename Func>
724 static bool invoke_read(world_t *
world, entity_t e,
const Func& func) {
736 bool has_components = get_ptrs(
world, e, r,
table, ptrs);
737 if (has_components) {
738 invoke_callback(func, 0, ptrs);
743 return has_components;
746 template <
typename Func>
747 static bool invoke_write(world_t *
world, entity_t e,
const Func& func) {
759 bool has_components = get_ptrs(
world, e, r,
table, ptrs);
760 if (has_components) {
761 invoke_callback(func, 0, ptrs);
766 return has_components;
769 template <
typename Func>
770 static bool invoke_get(world_t *
world, entity_t e,
const Func& func) {
772 return invoke_read(
world, e, func);
774 return invoke_write(
world, e, func);
791 template <
typename Func>
792 static bool invoke_ensure(world_t *
world, entity_t
id,
const Func& func) {
822 elem = store_added(added, elem, prev, next, w.
id<Args>()),
831 ids.
array = added.ptr();
832 ids.
count =
static_cast<ecs_size_t
>(elem);
837 if (!get_ptrs(w,
id, r,
table, ptrs)) {
845 ensure_ptrs(
world,
id, ptrs);
848 invoke_callback(func, 0, ptrs);
864 template <
typename Func,
typename ... TArgs,
865 if_t<
sizeof...(TArgs) ==
sizeof...(Args)> = 0>
866 static void invoke_callback(
867 const Func& f,
size_t,
ArrayType&, TArgs&& ... comps)
869 f(*
static_cast<typename base_arg_type<Args>::type*
>(comps)...);
872 template <
typename Func,
typename ... TArgs,
873 if_t<
sizeof...(TArgs) !=
sizeof...(Args)> = 0>
874 static void invoke_callback(
const Func& f,
size_t arg,
ArrayType& ptrs,
877 invoke_callback(f, arg + 1, ptrs, comps..., ptrs[arg]);
881template <
typename Func,
typename U =
int>
886template <
typename Func>
891 "function must have at least one argument");
897template <
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.