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 ...);
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 if (terms.populate(iter)) {
203 invoke_callback< each_ref_column >(iter, m_func, 0, terms.m_terms);
205 invoke_callback< each_column >(iter, m_func, 0, terms.m_terms);
210 static void run(ecs_iter_t *iter) {
211 auto self =
static_cast<const each_invoker*
>(iter->binding_ctx);
212 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
217 static void run_add(ecs_iter_t *iter) {
220 iter->binding_ctx = ctx->on_add;
225 static void run_remove(ecs_iter_t *iter) {
228 iter->binding_ctx = ctx->on_remove;
233 static void run_set(ecs_iter_t *iter) {
236 iter->binding_ctx = ctx->on_set;
241 static bool instanced() {
248 template <
template<
typename X,
typename =
int>
class ColumnType,
249 typename... Args, if_t<
250 sizeof...(Components) ==
sizeof...(Args) && PassEntity> = 0>
251 static void invoke_callback(
252 ecs_iter_t *iter,
const Func& func,
size_t, Terms&, Args... comps)
254 ECS_TABLE_LOCK(iter->world, iter->table);
257 size_t count =
static_cast<size_t>(iter->count);
260 "no entities returned, use each() without flecs::entity argument");
262 for (
size_t i = 0; i < count; i ++) {
264 (ColumnType< remove_reference_t<Components> >(comps, i)
268 ECS_TABLE_UNLOCK(iter->world, iter->table);
273 template <
template<
typename X,
typename =
int>
class ColumnType,
274 typename... Args,
int Enabled = PassIter, if_t<
275 sizeof...(Components) ==
sizeof...(Args) && Enabled> = 0>
276 static void invoke_callback(
277 ecs_iter_t *iter,
const Func& func,
size_t, Terms&, Args... comps)
279 size_t count =
static_cast<size_t>(iter->count);
286 flecs::iter it(iter);
288 ECS_TABLE_LOCK(iter->world, iter->table);
290 for (
size_t i = 0; i < count; i ++) {
291 func(it, i, (ColumnType< remove_reference_t<Components> >(comps, i)
295 ECS_TABLE_UNLOCK(iter->world, iter->table);
299 template <
template<
typename X,
typename =
int>
class ColumnType,
300 typename... Args, if_t<
301 sizeof...(Components) ==
sizeof...(Args) && !PassEntity && !PassIter> = 0>
302 static void invoke_callback(
303 ecs_iter_t *iter,
const Func& func,
size_t, Terms&, Args... comps)
305 size_t count =
static_cast<size_t>(iter->count);
312 flecs::iter it(iter);
314 ECS_TABLE_LOCK(iter->world, iter->table);
316 for (
size_t i = 0; i < count; i ++) {
317 func( (ColumnType< remove_reference_t<Components> >(comps, i)
321 ECS_TABLE_UNLOCK(iter->world, iter->table);
324 template <
template<
typename X,
typename =
int>
class ColumnType,
325 typename... Args, if_t<
sizeof...(Components) !=
sizeof...(Args) > = 0>
326 static void invoke_callback(ecs_iter_t *iter,
const Func& func,
327 size_t index, Terms& columns, Args... comps)
329 invoke_callback<ColumnType>(
330 iter, func, index + 1, columns, comps..., columns[index]);
346 using Terms =
typename term_ptrs<Components ...>::array;
349 template < if_not_t< is_same< decay_t<Func>, decay_t<Func>& >::value > = 0>
351 : m_func(FLECS_MOV(func)) { }
359 void invoke(ecs_iter_t *iter)
const {
361 terms.populate(iter);
362 invoke_callback(iter, m_func, 0, terms.m_terms);
366 static void run(ecs_iter_t *iter) {
367 auto self =
static_cast<const iter_invoker*
>(iter->binding_ctx);
368 ecs_assert(self !=
nullptr, ECS_INTERNAL_ERROR, NULL);
373 static bool instanced() {
378 template <
typename... Args, if_t<!
sizeof...(Args) && IterOnly> = 0>
379 static void invoke_callback(ecs_iter_t *iter,
const Func& func,
380 size_t, Terms&, Args...)
382 flecs::iter it(iter);
384 ECS_TABLE_LOCK(iter->world, iter->table);
388 ECS_TABLE_UNLOCK(iter->world, iter->table);
391 template <
typename... Targs, if_t<!IterOnly &&
392 (
sizeof...(Targs) ==
sizeof...(Components))> = 0>
393 static void invoke_callback(ecs_iter_t *iter,
const Func& func,
size_t,
394 Terms&, Targs... comps)
396 flecs::iter it(iter);
398 ECS_TABLE_LOCK(iter->world, iter->table);
400 func(it, (
static_cast<
403 actual_type_t<Components>
> >* >
406 ECS_TABLE_UNLOCK(iter->world, iter->table);
409 template <
typename... Targs, if_t<!IterOnly &&
410 (
sizeof...(Targs) !=
sizeof...(Components)) > = 0>
411 static void invoke_callback(ecs_iter_t *iter,
const Func& func,
412 size_t index, Terms& columns, Targs... comps)
414 invoke_callback(iter, func, index + 1, columns, comps...,
436 static bool const_args() {
437 static flecs::array<bool,
sizeof...(Args)> is_const_args ({
438 flecs::is_const<flecs::remove_reference_t<Args>>::value...
441 for (
auto is_const : is_const_args) {
469 for (int32_t
column : columns) {
491 template <
typename Func>
492 static bool invoke_read(world_t *
world, entity_t e,
const Func& func) {
505 if ((has_components = get_ptrs(
world, r,
table, ptrs))) {
506 invoke_callback(func, 0, ptrs);
511 return has_components;
514 template <
typename Func>
515 static bool invoke_write(world_t *
world, entity_t e,
const Func& func) {
528 if ((has_components = get_ptrs(
world, r,
table, ptrs))) {
529 invoke_callback(func, 0, ptrs);
534 return has_components;
537 template <
typename Func>
538 static bool invoke_get(world_t *
world, entity_t e,
const Func& func) {
540 return invoke_read(
world, e, func);
542 return invoke_write(
world, e, func);
559 template <
typename Func>
560 static bool invoke_get_mut(world_t *
world, entity_t
id,
const Func& func) {
590 elem = store_added(added, elem, prev, next, w.
id<Args>()),
598 ids.array = added.ptr();
599 ids.count =
static_cast<ecs_size_t
>(elem);
604 if (!get_ptrs(w, r,
table, ptrs)) {
612 get_mut_ptrs(
world,
id, ptrs);
615 invoke_callback(func, 0, ptrs);
631 template <
typename Func,
typename ... TArgs,
632 if_t<
sizeof...(TArgs) ==
sizeof...(Args)> = 0>
633 static void invoke_callback(
634 const Func& f,
size_t,
ArrayType&, TArgs&& ... comps)
636 f(*
static_cast<typename base_arg_type<Args>::type*
>(comps)...);
639 template <
typename Func,
typename ... TArgs,
640 if_t<
sizeof...(TArgs) !=
sizeof...(Args)> = 0>
641 static void invoke_callback(
const Func& f,
size_t arg,
ArrayType& ptrs,
644 invoke_callback(f, arg + 1, ptrs, comps..., ptrs[arg]);
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_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.