Class query

Synopsis

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

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

Description

No description yet.

Methods

query overload
begin
each overload

Mentioned in

each_worker
ECS_DEPRECATED
end
iter overload
iter_worker

Source

Lines 138-266 in include/flecs/cpp/query.hpp.

template<typename ... Components>
class query : public query_base {
    using Columns = typename _::column_args<Components...>::Columns;

public:
    query() : query_base(nullptr, nullptr) { }

    explicit query(const world& world) {
        flecs::stringstream str;
        if (!_::pack_args_to_string<Components...>(world.c_ptr(), str, true)) {
            ecs_abort(ECS_INVALID_PARAMETER, NULL);
        }

        m_world = world.c_ptr();
        m_query = ecs_query_new(world.c_ptr(), str.str().c_str());
    }

    explicit query(const world& world, query_base& parent) {
        flecs::stringstream str;
        if (!_::pack_args_to_string<Components...>(world.c_ptr(), str, true)) {
            ecs_abort(ECS_INVALID_PARAMETER, NULL);
        }

        m_world = world.c_ptr();
        m_query = ecs_subquery_new(world.c_ptr(), parent.c_ptr(), str.str().c_str());
    }

    explicit query(const world& world, const char *expr) {
        flecs::stringstream str;
        m_world = world.c_ptr();
        if (!_::pack_args_to_string<Components...>(world.c_ptr(), str, true)) {
            m_query = ecs_query_new(world.c_ptr(), expr);
        } else {
            str << "," << expr;
            m_query = ecs_query_new(world.c_ptr(), str.str().c_str());
        }
    }

    explicit query(const world& world, query_base& parent, const char *expr) {
        flecs::stringstream str;
        m_world = world.c_ptr();
        if (!_::pack_args_to_string<Components...>(world.c_ptr(), str, true)) {
            m_query = ecs_subquery_new(world.c_ptr(), parent.c_ptr(), expr);
        } else {
            str << "," << expr;
            m_query = ecs_subquery_new(world.c_ptr(), parent.c_ptr(), str.str().c_str());
        }
    }

    query_iterator<Components...> begin() const;

    query_iterator<Components...> end() const;

    template <typename Func>
    void each(Func&& func) const {
        ecs_iter_t it = ecs_query_iter(m_query);

        while (ecs_query_next(&it)) {
            _::column_args<Components...> columns(&it);
            _::each_invoker<Func, Components...>::call_system(
                &it, func, 0, columns.m_columns);
        }
    } 

    template <typename Func>
    void each(const flecs::filter& filter, Func&& func) const {
        ecs_iter_t it = ecs_query_iter(m_query);
        const filter_t* filter_ptr = filter.c_ptr();

        while (ecs_query_next_w_filter(&it, filter_ptr)) {
            _::column_args<Components...> columns(&it);
            _::each_invoker<Func, Components...>::call_system(
                &it, func, 0, columns.m_columns);
        }
    }

    template <typename Func>
    void each_worker(int32_t stage_current, int32_t stage_count, Func&& func) const {
        ecs_iter_t it = ecs_query_iter(m_query);

        while (ecs_query_next_worker(&it, stage_current, stage_count)) {
            _::column_args<Components...> columns(&it);
            _::each_invoker<Func, Components...>::call_system(
                &it, func, 0, columns.m_columns);
        }
    }

    template <typename Func>
    ECS_DEPRECATED("use each or iter")
    void action(Func&& func) const {
        ecs_iter_t it = ecs_query_iter(m_query);

        while (ecs_query_next(&it)) {
            _::column_args<Components...> columns(&it);
            _::action_invoker<Func, Components...>::call_system(&it, func, 0, columns.m_columns);
        }
    }

    template <typename Func>
    void iter(Func&& func) const {
        ecs_iter_t it = ecs_query_iter(m_query);

        while (ecs_query_next(&it)) {
            _::column_args<Components...> columns(&it);
            _::iter_invoker<Func, Components...>::call_system(&it, func, 0, columns.m_columns);
        }
    }  

    template <typename Func>
    void iter(const flecs::filter& filter, Func&& func) const {
        ecs_iter_t it = ecs_query_iter(m_query);        
        const filter_t* filter_ptr = filter.c_ptr();

        while (ecs_query_next_w_filter(&it, filter_ptr)) {
            _::column_args<Components...> columns(&it);
            _::iter_invoker<Func, Components...>::call_system(&it, func, 0, columns.m_columns);
        }
    }

    template <typename Func>
    void iter_worker(int32_t stage_current, int32_t stage_count, Func&& func) const {
        ecs_iter_t it = ecs_query_iter(m_query);

        while (ecs_query_next_worker(&it, stage_current, stage_count)) {
            _::column_args<Components...> columns(&it);
            _::iter_invoker<Func, Components...>::call_system(&it, func, 0, columns.m_columns);
        }
    }      
};