Function each

Summary

#include <include/flecs/addons/cpp/entity_view.hpp>

(1) template <typename Func>
    void each(const Func &func) const

(2) template <typename Func>
    void each(flecs::id_t first, flecs::id_t second, const Func &func) const

(3) template <typename Func>
    void each(const flecs::entity_view &rel, const Func &func) const

(4) template <typename First, typename Func>
    void each(const Func &func) const

Function overload

Synopsis

#include <include/flecs/addons/cpp/entity_view.hpp>

template <typename Func>
void each(const Func &func) const

Description

Iterate (component) ids of an entity. The function parameter must match the following signature: void(*)(flecs::id id)

Parameters

func - The function invoked for each id.

Source

Lines 98-120 in include/flecs/addons/cpp/mixins/entity/impl.hpp. Line 108 in include/flecs/addons/cpp/entity_view.hpp.

template <typename Func>
inline void entity_view::each(const Func& func) const {
    const ecs_type_t *type = ecs_get_type(m_world, m_id);
    if (!type) {
        return;
    }

    const ecs_id_t *ids = type->array;
    int32_t count = type->count;

    for (int i = 0; i < count; i ++) {
        ecs_id_t id = ids[i];
        flecs::id ent(m_world, id);
        func(ent); 

        // Union object is not stored in type, so handle separately
        if (ECS_PAIR_FIRST(id) == EcsUnion) {
            ent = flecs::id(m_world, ECS_PAIR_SECOND(id),
                ecs_get_target(m_world, m_id, ECS_PAIR_SECOND(id), 0));
            func(ent);
        }
    }
}

Synopsis

#include <include/flecs/addons/cpp/entity_view.hpp>

template <typename Func>
void each(flecs::id_t first, flecs::id_t second, const Func &func) const

Description

Iterate matching pair ids of an entity. The function parameter must match the following signature: void(*)(flecs::id id)

Parameters

func - The function invoked for each id.

Source

Lines 122-151 in include/flecs/addons/cpp/mixins/entity/impl.hpp. Line 117 in include/flecs/addons/cpp/entity_view.hpp.

template <typename Func>
inline void entity_view::each(flecs::id_t pred, flecs::id_t obj, const Func& func) const {
    flecs::world_t *real_world = const_cast<flecs::world_t*>(
        ecs_get_world(m_world));

    const ecs_table_t *table = ecs_get_table(m_world, m_id);
    if (!table) {
        return;
    }

    const ecs_type_t *type = ecs_table_get_type(table);
    if (!type) {
        return;
    }

    flecs::id_t pattern = pred;
    if (obj) {
        pattern = ecs_pair(pred, obj);
    }

    int32_t cur = 0;
    id_t *ids = type->array;

    while (-1 != (cur = ecs_search_offset(real_world, table, cur, pattern, 0)))
    {
        flecs::id ent(m_world, ids[cur]);
        func(ent);
        cur ++;
    }
}

Synopsis

#include <include/flecs/addons/cpp/entity_view.hpp>

template <typename Func>
void each(const flecs::entity_view &rel, const Func &func) const

Description

Iterate targets for a given relationship. The function parameter must match the following signature: void(*)(flecs::entity target)

Parameters

rel - The relationship for which to iterate the targets.

func - The function invoked for each target.

Source

Lines 153-159 in include/flecs/addons/cpp/mixins/entity/impl.hpp. Line 127 in include/flecs/addons/cpp/entity_view.hpp.

template <typename Func>
inline void entity_view::each(const flecs::entity_view& rel, const Func& func) const {
    return this->each(rel, flecs::Wildcard, [&](flecs::id id) {
        flecs::entity obj = id.second();
        func(obj);
    });
}

Synopsis

#include <include/flecs/addons/cpp/entity_view.hpp>

template <typename First, typename Func>
void each(const Func &func) const

Description

Iterate targets for a given relationship. The function parameter must match the following signature: void(*)(flecs::entity target)

Template Parameters

First - The relationship for which to iterate the targets.

Parameters

func - The function invoked for each target.

Source

Lines 136-139 in include/flecs/addons/cpp/entity_view.hpp.

template <typename First, typename Func>
void each(const Func& func) const { 
    return each(_::cpp_type<First>::id(m_world), func);
}