16 void *on_add =
nullptr;
17 void *on_remove =
nullptr;
18 void *on_set =
nullptr;
24 if (on_add && free_on_add) {
27 if (on_remove && free_on_remove) {
28 free_on_remove(on_remove);
30 if (on_set && free_on_set) {
42template <
typename ... Components>
46 bool populate(
const ecs_iter_t *
iter) {
47 return populate(
iter, 0,
static_cast<
49 remove_pointer_t<Components>
>
57 bool populate(
const ecs_iter_t*,
size_t) {
return false; }
59 template <
typename T,
typename... Targs>
60 bool populate(
const ecs_iter_t *
iter,
size_t index, T, Targs... comps) {
61 m_terms[index].ptr =
iter->ptrs[index];
62 bool is_ref =
iter->sources &&
iter->sources[index] != 0;
63 m_terms[index].is_ref = is_ref;
64 is_ref |= populate(
iter, index + 1, comps ...);
73template <
typename T,
typename =
int>
79 : m_term(
term), m_row(row) { }
89 !is_empty<actual_type_t<T>>::value &&
is_actual<T>::value > >
96 return static_cast<T*
>(this->m_term.ptr)[this->m_row];
105 !is_empty<actual_type_t<T>>::value && !
is_actual<T>::value> >
112 return static_cast<actual_type_t<T>*
>(this->m_term.ptr)[this->m_row];
121 !is_pointer<T>::value > >
128 return actual_type_t<T>();
136 !is_empty<actual_type_t<T>>::value > >
142 actual_type_t<T> get_row() {
143 if (this->m_term.ptr) {
144 return &
static_cast<actual_type_t<T>
>(this->m_term.ptr)[this->m_row];
154template <
typename T,
typename =
int>
172template <
typename Func,
typename ... Components>
176 static constexpr bool PassEntity =
181 static constexpr bool PassIter =
185 "each() must have at least one argument");
187 using Terms =
typename term_ptrs<Components ...>::array;
189 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
191 : m_func(FLECS_MOV(func)) { }
199 void invoke(ecs_iter_t *
iter)
const {
202 iter->flags |= EcsIterCppEach;
204 if (terms.populate(
iter)) {
205 invoke_callback< each_ref_column >(
iter, m_func, 0, terms.m_terms);
207 invoke_callback< each_column >(
iter, m_func, 0, terms.m_terms);
212 static void run(ecs_iter_t *
iter) {
214 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
224 static void destruct(
void *obj) {
225 _::free_obj<each_delegate>(
static_cast<each_delegate*
>(obj));
229 static void run_add(ecs_iter_t *
iter) {
232 iter->binding_ctx = ctx->on_add;
237 static void run_remove(ecs_iter_t *
iter) {
240 iter->binding_ctx = ctx->on_remove;
245 static void run_set(ecs_iter_t *
iter) {
248 iter->binding_ctx = ctx->on_set;
253 static bool instanced() {
260 template <
template<
typename X,
typename =
int>
class ColumnType,
261 typename... Args, if_t<
262 sizeof...(Components) ==
sizeof...(Args) && PassEntity> = 0>
263 static void invoke_callback(
264 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
266 ECS_TABLE_LOCK(
iter->world,
iter->table);
269 size_t count =
static_cast<size_t>(
iter->count);
272 "no entities returned, use each() without flecs::entity argument");
274 for (
size_t i = 0; i < count; i ++) {
276 (ColumnType< remove_reference_t<Components> >(comps, i)
280 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
285 template <
template<
typename X,
typename =
int>
class ColumnType,
286 typename... Args,
int Enabled = PassIter, if_t<
287 sizeof...(Components) ==
sizeof...(Args) && Enabled> = 0>
288 static void invoke_callback(
289 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
291 size_t count =
static_cast<size_t>(
iter->count);
300 ECS_TABLE_LOCK(
iter->world,
iter->table);
302 for (
size_t i = 0; i < count; i ++) {
303 func(it, i, (ColumnType< remove_reference_t<Components> >(comps, i)
307 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
311 template <
template<
typename X,
typename =
int>
class ColumnType,
312 typename... Args, if_t<
313 sizeof...(Components) ==
sizeof...(Args) && !PassEntity && !PassIter> = 0>
314 static void invoke_callback(
315 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
317 size_t count =
static_cast<size_t>(
iter->count);
326 ECS_TABLE_LOCK(
iter->world,
iter->table);
328 for (
size_t i = 0; i < count; i ++) {
329 func( (ColumnType< remove_reference_t<Components> >(comps, i)
333 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
336 template <
template<
typename X,
typename =
int>
class ColumnType,
337 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
338 static void invoke_callback(ecs_iter_t *
iter,
const Func& func,
339 size_t index, Terms& columns, Args... comps)
341 invoke_callback<ColumnType>(
342 iter, func, index + 1, columns, comps..., columns[index]);
348template <
typename Func,
typename ... Components>
352 static constexpr bool PassEntity =
357 static constexpr bool PassIter =
361 "each() must have at least one argument");
363 using Terms =
typename term_ptrs<Components ...>::array;
365 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
367 : m_func(FLECS_MOV(func)) { }
378 if (terms.populate(
iter)) {
379 return invoke_callback< each_ref_column >(
iter, m_func, 0, terms.m_terms);
381 return invoke_callback< each_column >(
iter, m_func, 0, terms.m_terms);
386 static bool instanced() {
393 template <
template<
typename X,
typename =
int>
class ColumnType,
394 typename... Args, if_t<
395 sizeof...(Components) ==
sizeof...(Args) && PassEntity> = 0>
397 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
399 ECS_TABLE_LOCK(
iter->world,
iter->table);
402 size_t count =
static_cast<size_t>(
iter->count);
406 "no entities returned, use find() without flecs::entity argument");
408 for (
size_t i = 0; i < count; i ++) {
410 (ColumnType< remove_reference_t<Components> >(comps, i)
418 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
425 template <
template<
typename X,
typename =
int>
class ColumnType,
426 typename... Args,
int Enabled = PassIter, if_t<
427 sizeof...(Components) ==
sizeof...(Args) && Enabled> = 0>
429 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
431 size_t count =
static_cast<size_t>(
iter->count);
441 ECS_TABLE_LOCK(
iter->world,
iter->table);
443 for (
size_t i = 0; i < count; i ++) {
444 if (func(it, i, (ColumnType< remove_reference_t<Components> >(comps, i)
452 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
458 template <
template<
typename X,
typename =
int>
class ColumnType,
459 typename... Args, if_t<
460 sizeof...(Components) ==
sizeof...(Args) && !PassEntity && !PassIter> = 0>
462 ecs_iter_t *
iter,
const Func& func,
size_t, Terms&, Args... comps)
464 size_t count =
static_cast<size_t>(
iter->count);
474 ECS_TABLE_LOCK(
iter->world,
iter->table);
476 for (
size_t i = 0; i < count; i ++) {
477 if (func( (ColumnType< remove_reference_t<Components> >(comps, i)
485 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
490 template <
template<
typename X,
typename =
int>
class ColumnType,
491 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
493 size_t index, Terms& columns, Args... comps)
495 return invoke_callback<ColumnType>(
496 iter, func, index + 1, columns, comps..., columns[index]);
506template <
typename Func,
typename ... Components>
511 using Terms =
typename term_ptrs<Components ...>::array;
514 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
516 : m_func(FLECS_MOV(func)) { }
524 void invoke(ecs_iter_t *
iter)
const {
526 terms.populate(
iter);
527 invoke_callback(
iter, m_func, 0, terms.m_terms);
531 static void run(ecs_iter_t *
iter) {
533 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
538 static bool instanced() {
543 template <
typename... Args, if_t<!
sizeof...(Args) && IterOnly> = 0>
544 static void invoke_callback(ecs_iter_t *
iter,
const Func& func,
545 size_t, Terms&, Args...)
549 ECS_TABLE_LOCK(
iter->world,
iter->table);
553 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
556 template <
typename... Targs, if_t<!IterOnly &&
557 (
sizeof...(Targs) ==
sizeof...(Components))> = 0>
558 static void invoke_callback(ecs_iter_t *
iter,
const Func& func,
size_t,
559 Terms&, Targs... comps)
563 ECS_TABLE_LOCK(
iter->world,
iter->table);
565 func(it, (
static_cast<
568 actual_type_t<Components>
> >* >
571 ECS_TABLE_UNLOCK(
iter->world,
iter->table);
574 template <
typename... Targs, if_t<!IterOnly &&
575 (
sizeof...(Targs) !=
sizeof...(Components)) > = 0>
576 static void invoke_callback(ecs_iter_t *
iter,
const Func& func,
577 size_t index, Terms& columns, Targs... comps)
579 invoke_callback(
iter, func, index + 1, columns, comps...,
591template <
typename Func>
594 : m_func(FLECS_MOV(func)) { }
597 static void run(ecs_iter_t *
iter) {
601 template <typename F, if_t<arity<F>::value == 1> = 0>
602 static void invoke(ecs_iter_t *
iter) {
604 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
608 template <typename F, if_t<arity<F>::value == 0> = 0>
609 static void invoke(ecs_iter_t *
iter) {
611 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
618template <
typename Func,
typename Event>
621 : m_func(FLECS_MOV(func)) { }
624 static void run(ecs_iter_t *
iter) {
629 template <typename F, if_t<arity<F>::value == 1> = 0>
630 static void invoke(ecs_iter_t *
iter) {
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, if_t<arity<F>::value == 2> = 0>
642 static void invoke(ecs_iter_t *
iter) {
645 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
647 "entity observer invoked without payload");
649 Event *data =
static_cast<Event*
>(
iter->
param);
661template<
typename ... Args>
664template<
typename ... Args>
671 static bool const_args() {
672 static flecs::array<bool,
sizeof...(Args)> is_const_args ({
673 flecs::is_const<flecs::remove_reference_t<Args>>::value...
676 for (
auto is_const : is_const_args) {
704 for (int32_t column : columns) {
726 template <
typename Func>
727 static bool invoke_read(world_t *
world, entity_t e,
const Func& func) {
739 bool has_components = get_ptrs(
world, r,
table, ptrs);
740 if (has_components) {
741 invoke_callback(func, 0, ptrs);
746 return has_components;
749 template <
typename Func>
750 static bool invoke_write(world_t *
world, entity_t e,
const Func& func) {
762 bool has_components = get_ptrs(
world, r,
table, ptrs);
763 if (has_components) {
764 invoke_callback(func, 0, ptrs);
769 return has_components;
772 template <
typename Func>
773 static bool invoke_get(world_t *
world, entity_t e,
const Func& func) {
775 return invoke_read(
world, e, func);
777 return invoke_write(
world, e, func);
794 template <
typename Func>
795 static bool invoke_ensure(world_t *
world, entity_t
id,
const Func& func) {
825 elem = store_added(added, elem, prev, next, w.
id<Args>()),
833 ids.array = added.ptr();
834 ids.count =
static_cast<ecs_size_t
>(elem);
839 if (!get_ptrs(w, r,
table, ptrs)) {
847 ensure_ptrs(
world,
id, ptrs);
850 invoke_callback(func, 0, ptrs);
866 template <
typename Func,
typename ... TArgs,
867 if_t<
sizeof...(TArgs) ==
sizeof...(Args)> = 0>
868 static void invoke_callback(
869 const Func& f,
size_t,
ArrayType&, TArgs&& ... comps)
871 f(*
static_cast<typename base_arg_type<Args>::type*
>(comps)...);
874 template <
typename Func,
typename ... TArgs,
875 if_t<
sizeof...(TArgs) !=
sizeof...(Args)> = 0>
876 static void invoke_callback(
const Func& f,
size_t arg,
ArrayType& ptrs,
879 invoke_callback(f, arg + 1, ptrs, comps..., ptrs[arg]);
883template <
typename Func,
typename U =
int>
888template <
typename Func>
893 "function must have at least one argument");
899template <
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::id id(E value) const
Convert enum constant to entity.
void(* ecs_ctx_free_t)(void *ctx)
Function to cleanup context data.
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.
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, int32_t index)
Return field source.
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.
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.
void * ecs_record_get_column(const ecs_record_t *r, int32_t column, size_t c_size)
Get component pointer from column/record.
ecs_record_t * ecs_record_find(const ecs_world_t *world, ecs_entity_t entity)
Find record for entity.
const ecs_world_t * ecs_get_world(const ecs_poly_t *poly)
Get world from poly.
A type is a list of (component) ids.
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.
Class that describes a term.
bool is_stage() const
Test if is a stage.
bool is_deferred() const
Test whether deferring is enabled.