Class query

Synopsis

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

template<typename ... Components>
class query : public query_base

Description

No description yet.

Mentioned in

Methods

query overload
each
each_worker
iter
iter_worker

Source

Lines 103-170 in include/flecs/addons/cpp/query.hpp.

template<typename ... Components>
class query : public query_base {
    using Terms = typename _::term_ptrs<Components...>::array;

public:
    query() { }

    query(world_t *world, query_t *q)
        : query_base(world, q) { }

    explicit query(const world& world, const char *expr = nullptr) 
        : query_base(world.c_ptr())
    {
        auto qb = world.query_builder<Components ...>()
            .expr(expr);

        if (!expr) {
            qb.substitute_default();
        }

        m_query = qb;
    }

    explicit query(const world& world, query_base& parent, const char *expr = nullptr) 
        : query_base(world.c_ptr())    
    {
        auto qb = world.query_builder<Components ...>()
            .parent(parent)
            .expr(expr);

        if (!expr) {
            qb.substitute_default();
        }

        m_query = qb;
    }

    template <typename Func>
    void each(Func&& func) const {
        iterate<_::each_invoker>(std::forward<Func>(func), ecs_query_next);
    } 

    template <typename Func>
    void each_worker(int32_t stage_current, int32_t stage_count, Func&& func) const {
        iterate<_::each_invoker>(std::forward<Func>(func), 
            ecs_query_next_worker, stage_current, stage_count);
    }

    template <typename Func>
    void iter(Func&& func) const { 
        iterate<_::iter_invoker>(std::forward<Func>(func), ecs_query_next);
    }

    template <typename Func>
    void iter_worker(int32_t stage_current, int32_t stage_count, Func&& func) const {
        iterate<_::iter_invoker>(std::forward<Func>(func), 
            ecs_query_next_worker, stage_current, stage_count);
    }

private:
    template < template<typename Func, typename ... Comps> class Invoker, typename Func, typename NextFunc, typename ... Args>
    void iterate(Func&& func, NextFunc next, Args &&... args) const {
        ecs_iter_t it = ecs_query_iter(m_world, m_query);
        while (next(&it, std::forward<Args>(args)...)) {
            Invoker<Func, Components...>(func).invoke(&it);
        }
    }
};