Skip to content

Commit

Permalink
Add query builder overload for .with(name, id)
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Sep 12, 2024
1 parent 9ca3121 commit c8ace29
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 1 deletion.
9 changes: 9 additions & 0 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -29546,6 +29546,15 @@ struct query_builder_i : term_builder_i<Base> {
return *this;
}

Base& with(const char *r, entity_t o) {
this->term();
*this->term_ = flecs::term().first(r).second(o);
if (this->term_->inout == EcsInOutDefault) {
this->inout_none();
}
return *this;
}

template<typename First>
Base& with(id_t o) {
return this->with(_::type<First>::id(this->world_v()), o);
Expand Down
9 changes: 9 additions & 0 deletions include/flecs/addons/cpp/mixins/query/builder_i.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,15 @@ struct query_builder_i : term_builder_i<Base> {
return *this;
}

Base& with(const char *r, entity_t o) {
this->term();
*this->term_ = flecs::term().first(r).second(o);
if (this->term_->inout == EcsInOutDefault) {
this->inout_none();
}
return *this;
}

template<typename First>
Base& with(id_t o) {
return this->with(_::type<First>::id(this->world_v()), o);
Expand Down
2 changes: 2 additions & 0 deletions test/cpp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,7 @@
"with_pair_components",
"with_pair_component_id",
"with_pair_component_name",
"with_pair_name_component_id",
"with_enum",
"without_id",
"without_name",
Expand All @@ -784,6 +785,7 @@
"without_pair_components",
"without_pair_component_id",
"without_pair_component_name",
"without_pair_name_component_id",
"without_enum",
"write_id",
"write_name",
Expand Down
52 changes: 52 additions & 0 deletions test/cpp/src/QueryBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3809,6 +3809,32 @@ void QueryBuilder_with_pair_component_id(void) {
test_int(count, 1);
}

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

flecs::entity Likes = ecs.entity("Likes");
flecs::entity Apples = ecs.entity("Apples");
flecs::entity Pears = ecs.entity("Pears");

flecs::query<> q =
ecs.query_builder()
.with<Position>()
.with("Likes", Apples)
.cache_kind(cache_kind)
.build();

auto e1 = ecs.entity().add<Position>().add(Likes, Apples);
ecs.entity().add<Position>().add(Likes, Pears);

int32_t count = 0;
q.each([&](flecs::entity e) {
count ++;
test_assert(e == e1);
});

test_int(count, 1);
}

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

Expand Down Expand Up @@ -4055,6 +4081,32 @@ void QueryBuilder_without_pair_component_name(void) {
test_int(count, 1);
}

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

flecs::entity Likes = ecs.entity("Likes");
flecs::entity Apples = ecs.entity("Apples");
flecs::entity Pears = ecs.entity("Pears");

flecs::query<> q =
ecs.query_builder()
.with<Position>()
.without("Likes", Apples)
.cache_kind(cache_kind)
.build();

ecs.entity().add<Position>().add(Likes, Apples);
auto e2 = ecs.entity().add<Position>().add(Likes, Pears);

int32_t count = 0;
q.each([&](flecs::entity e) {
count ++;
test_assert(e == e2);
});

test_int(count, 1);
}

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

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 @@ -746,6 +746,7 @@ void QueryBuilder_with_pair_name(void);
void QueryBuilder_with_pair_components(void);
void QueryBuilder_with_pair_component_id(void);
void QueryBuilder_with_pair_component_name(void);
void QueryBuilder_with_pair_name_component_id(void);
void QueryBuilder_with_enum(void);
void QueryBuilder_without_id(void);
void QueryBuilder_without_name(void);
Expand All @@ -755,6 +756,7 @@ void QueryBuilder_without_pair_name(void);
void QueryBuilder_without_pair_components(void);
void QueryBuilder_without_pair_component_id(void);
void QueryBuilder_without_pair_component_name(void);
void QueryBuilder_without_pair_name_component_id(void);
void QueryBuilder_without_enum(void);
void QueryBuilder_write_id(void);
void QueryBuilder_write_name(void);
Expand Down Expand Up @@ -4238,6 +4240,10 @@ bake_test_case QueryBuilder_testcases[] = {
"with_pair_component_name",
QueryBuilder_with_pair_component_name
},
{
"with_pair_name_component_id",
QueryBuilder_with_pair_name_component_id
},
{
"with_enum",
QueryBuilder_with_enum
Expand Down Expand Up @@ -4274,6 +4280,10 @@ bake_test_case QueryBuilder_testcases[] = {
"without_pair_component_name",
QueryBuilder_without_pair_component_name
},
{
"without_pair_name_component_id",
QueryBuilder_without_pair_name_component_id
},
{
"without_enum",
QueryBuilder_without_enum
Expand Down Expand Up @@ -6635,7 +6645,7 @@ static bake_test_suite suites[] = {
"QueryBuilder",
QueryBuilder_setup,
NULL,
164,
166,
QueryBuilder_testcases,
1,
QueryBuilder_params
Expand Down

0 comments on commit c8ace29

Please sign in to comment.