Skip to content

Commit

Permalink
#124 fix compiler error when passing functions to each() or iter() wi…
Browse files Browse the repository at this point in the history
…thout &
  • Loading branch information
SanderMertens committed Jan 8, 2022
1 parent beb30de commit 39ffd94
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 4 deletions.
4 changes: 3 additions & 1 deletion flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -15477,13 +15477,14 @@ struct each_invoker : public invoker {
// If the number of arguments in the function signature is one more than the
// number of components in the query, an extra entity arg is required.
static constexpr bool PassEntity =
sizeof...(Components) == (arity<Func>::value - 1);
sizeof...(Components) == (arity<decay_t<Func>>::value - 1);

static_assert(arity<Func>::value > 0,
"each() must have at least one argument");

using Terms = typename term_ptrs<Components ...>::array;

template < if_not_t< is_same< void(Func), void(Func)& >::value > = 0>
explicit each_invoker(Func&& func) noexcept
: m_func(std::move(func)) { }

Expand Down Expand Up @@ -15586,6 +15587,7 @@ struct iter_invoker : invoker {
using Terms = typename term_ptrs<Components ...>::array;

public:
template < if_not_t< is_same< void(Func), void(Func)& >::value > = 0>
explicit iter_invoker(Func&& func) noexcept
: m_func(std::move(func)) { }

Expand Down
4 changes: 3 additions & 1 deletion include/flecs/addons/cpp/invoker.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -130,13 +130,14 @@ struct each_invoker : public invoker {
// If the number of arguments in the function signature is one more than the
// number of components in the query, an extra entity arg is required.
static constexpr bool PassEntity =
sizeof...(Components) == (arity<Func>::value - 1);
sizeof...(Components) == (arity<decay_t<Func>>::value - 1);

static_assert(arity<Func>::value > 0,
"each() must have at least one argument");

using Terms = typename term_ptrs<Components ...>::array;

template < if_not_t< is_same< void(Func), void(Func)& >::value > = 0>
explicit each_invoker(Func&& func) noexcept
: m_func(std::move(func)) { }

Expand Down Expand Up @@ -239,6 +240,7 @@ struct iter_invoker : invoker {
using Terms = typename term_ptrs<Components ...>::array;

public:
template < if_not_t< is_same< void(Func), void(Func)& >::value > = 0>
explicit iter_invoker(Func&& func) noexcept
: m_func(std::move(func)) { }

Expand Down
6 changes: 5 additions & 1 deletion test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,11 @@
"un_instanced_query_w_singleton_iter",
"un_instanced_query_w_base_iter",
"query_each_from_component",
"query_iter_from_component"
"query_iter_from_component",
"query_each_w_func_ptr",
"query_iter_w_func_ptr",
"query_each_w_func_no_ptr",
"query_iter_w_func_no_ptr"
]
}, {
"id": "QueryBuilder",
Expand Down
79 changes: 79 additions & 0 deletions test/cpp_api/src/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1716,3 +1716,82 @@ void Query_query_iter_from_component() {
});
test_int(count, 2);
}

static int invoked_count = 0;

void EachFunc(flecs::entity e, Position& p) {
invoked_count ++;
p.x ++;
p.y ++;
}

void IterFunc(flecs::iter& it, Position* p) {
test_int(it.count(), 1);
invoked_count ++;
p->x ++;
p->y ++;
}

void Query_query_each_w_func_ptr() {
flecs::world w;

auto e = w.entity().set<Position>({10, 20});

auto q = w.query<Position>();

q.each(&EachFunc);

test_int(invoked_count, 1);

const Position *ptr = e.get<Position>();
test_int(ptr->x, 11);
test_int(ptr->y, 21);
}

void Query_query_iter_w_func_ptr() {
flecs::world w;

auto e = w.entity().set<Position>({10, 20});

auto q = w.query<Position>();

q.iter(&IterFunc);

test_int(invoked_count, 1);

const Position *ptr = e.get<Position>();
test_int(ptr->x, 11);
test_int(ptr->y, 21);
}

void Query_query_each_w_func_no_ptr() {
flecs::world w;

auto e = w.entity().set<Position>({10, 20});

auto q = w.query<Position>();

q.each(EachFunc);

test_int(invoked_count, 1);

const Position *ptr = e.get<Position>();
test_int(ptr->x, 11);
test_int(ptr->y, 21);
}

void Query_query_iter_w_func_no_ptr() {
flecs::world w;

auto e = w.entity().set<Position>({10, 20});

auto q = w.query<Position>();

q.iter(IterFunc);

test_int(invoked_count, 1);

const Position *ptr = e.get<Position>();
test_int(ptr->x, 11);
test_int(ptr->y, 21);
}
22 changes: 21 additions & 1 deletion test/cpp_api/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,10 @@ void Query_un_instanced_query_w_singleton_iter(void);
void Query_un_instanced_query_w_base_iter(void);
void Query_query_each_from_component(void);
void Query_query_iter_from_component(void);
void Query_query_each_w_func_ptr(void);
void Query_query_iter_w_func_ptr(void);
void Query_query_each_w_func_no_ptr(void);
void Query_query_iter_w_func_no_ptr(void);

// Testsuite 'QueryBuilder'
void QueryBuilder_builder_assign_same_type(void);
Expand Down Expand Up @@ -2327,6 +2331,22 @@ bake_test_case Query_testcases[] = {
{
"query_iter_from_component",
Query_query_iter_from_component
},
{
"query_each_w_func_ptr",
Query_query_each_w_func_ptr
},
{
"query_iter_w_func_ptr",
Query_query_iter_w_func_ptr
},
{
"query_each_w_func_no_ptr",
Query_query_each_w_func_no_ptr
},
{
"query_iter_w_func_no_ptr",
Query_query_iter_w_func_no_ptr
}
};

Expand Down Expand Up @@ -3772,7 +3792,7 @@ static bake_test_suite suites[] = {
"Query",
NULL,
NULL,
56,
60,
Query_testcases
},
{
Expand Down

0 comments on commit 39ffd94

Please sign in to comment.