Skip to content

Commit

Permalink
#1448 Fix issue with using query.find(flecs::entity, ...) and QueryMa…
Browse files Browse the repository at this point in the history
…tchEmptyTables
  • Loading branch information
SanderMertens authored Nov 22, 2024
1 parent 2a23e6e commit e3219d7
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
6 changes: 0 additions & 6 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -26153,9 +26153,6 @@ struct each_delegate : public delegate {
static void invoke_callback(
ecs_iter_t *iter, const Func& func, size_t i, Args... comps)
{
ecs_assert(iter->count > 0, ECS_INVALID_OPERATION,
"no entities returned, use each() without flecs::entity argument");

func(flecs::entity(iter->world, iter->entities[i]),
(ColumnType< remove_reference_t<Components> >(iter, comps, i)
.get_row())...);
Expand Down Expand Up @@ -26272,9 +26269,6 @@ struct find_delegate : public delegate {
size_t count = static_cast<size_t>(iter->count);
flecs::entity result;

ecs_assert(count > 0, ECS_INVALID_OPERATION,
"no entities returned, use find() without flecs::entity argument");

for (size_t i = 0; i < count; i ++) {
if (func(flecs::entity(world, iter->entities[i]),
(ColumnType< remove_reference_t<Components> >(iter, comps, i)
Expand Down
6 changes: 0 additions & 6 deletions include/flecs/addons/cpp/delegate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,9 +301,6 @@ struct each_delegate : public delegate {
static void invoke_callback(
ecs_iter_t *iter, const Func& func, size_t i, Args... comps)
{
ecs_assert(iter->count > 0, ECS_INVALID_OPERATION,
"no entities returned, use each() without flecs::entity argument");

func(flecs::entity(iter->world, iter->entities[i]),
(ColumnType< remove_reference_t<Components> >(iter, comps, i)
.get_row())...);
Expand Down Expand Up @@ -420,9 +417,6 @@ struct find_delegate : public delegate {
size_t count = static_cast<size_t>(iter->count);
flecs::entity result;

ecs_assert(count > 0, ECS_INVALID_OPERATION,
"no entities returned, use find() without flecs::entity argument");

for (size_t i = 0; i < count; i ++) {
if (func(flecs::entity(world, iter->entities[i]),
(ColumnType< remove_reference_t<Components> >(iter, comps, i)
Expand Down
2 changes: 2 additions & 0 deletions test/cpp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -674,6 +674,8 @@
"find",
"find_not_found",
"find_w_entity",
"find_w_match_empty_tables",
"find_w_entity_w_match_empty_tables",
"optional_pair_term",
"empty_tables_each",
"empty_tables_each_w_entity",
Expand Down
36 changes: 36 additions & 0 deletions test/cpp/src/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,42 @@ void Query_find_w_entity(void) {
test_assert(r == e2);
}

void Query_find_w_match_empty_tables(void) {
flecs::world ecs;

auto e1 = ecs.entity().set<Position>({10, 20}).add<Velocity>();
e1.destruct(); // creates empty table
auto e2 = ecs.entity().set<Position>({20, 30});

auto q = ecs.query_builder<Position>()
.query_flags(EcsQueryMatchEmptyTables)
.build();

auto r = q.find([](Position& p) {
return p.x == 20;
});

test_assert(r == e2);
}

void Query_find_w_entity_w_match_empty_tables(void) {
flecs::world ecs;

auto e1 = ecs.entity().set<Position>({10, 20}).add<Velocity>();
e1.destruct(); // creates empty table
auto e2 = ecs.entity().set<Position>({20, 30});

auto q = ecs.query_builder<Position>()
.query_flags(EcsQueryMatchEmptyTables)
.build();

auto r = q.find([](flecs::entity e, Position& p) {
return p.x == 20;
});

test_assert(r == e2);
}

// Generic lambdas are a C++14 feature.

struct GenericLambdaFindEntity {
Expand Down
12 changes: 11 additions & 1 deletion test/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -650,6 +650,8 @@ void Query_iter_get_pair_w_id(void);
void Query_find(void);
void Query_find_not_found(void);
void Query_find_w_entity(void);
void Query_find_w_match_empty_tables(void);
void Query_find_w_entity_w_match_empty_tables(void);
void Query_optional_pair_term(void);
void Query_empty_tables_each(void);
void Query_empty_tables_each_w_entity(void);
Expand Down Expand Up @@ -3919,6 +3921,14 @@ bake_test_case Query_testcases[] = {
"find_w_entity",
Query_find_w_entity
},
{
"find_w_match_empty_tables",
Query_find_w_match_empty_tables
},
{
"find_w_entity_w_match_empty_tables",
Query_find_w_entity_w_match_empty_tables
},
{
"optional_pair_term",
Query_optional_pair_term
Expand Down Expand Up @@ -6908,7 +6918,7 @@ static bake_test_suite suites[] = {
"Query",
NULL,
NULL,
116,
118,
Query_testcases
},
{
Expand Down

0 comments on commit e3219d7

Please sign in to comment.