Skip to content

Commit

Permalink
Allow calling set_group/set_var on const query objects
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 4, 2024
1 parent d4462b4 commit 686d085
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 15 deletions.
14 changes: 7 additions & 7 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -28306,34 +28306,34 @@ struct iterable {
return this->iter().first();
}

iter_iterable<Components...> set_var(int var_id, flecs::entity_t value) {
iter_iterable<Components...> set_var(int var_id, flecs::entity_t value) const {
return this->iter().set_var(var_id, value);
}

iter_iterable<Components...> set_var(const char *name, flecs::entity_t value) {
iter_iterable<Components...> set_var(const char *name, flecs::entity_t value) const {
return this->iter().set_var(name, value);
}

iter_iterable<Components...> set_var(const char *name, flecs::table_t *value) {
iter_iterable<Components...> set_var(const char *name, flecs::table_t *value) const {
return this->iter().set_var(name, value);
}

iter_iterable<Components...> set_var(const char *name, ecs_table_range_t value) {
iter_iterable<Components...> set_var(const char *name, ecs_table_range_t value) const {
return this->iter().set_var(name, value);
}

iter_iterable<Components...> set_var(const char *name, flecs::table_range value) {
iter_iterable<Components...> set_var(const char *name, flecs::table_range value) const {
return this->iter().set_var(name, value);
}

// Limit results to tables with specified group id (grouped queries only)
iter_iterable<Components...> set_group(uint64_t group_id) {
iter_iterable<Components...> set_group(uint64_t group_id) const {
return this->iter().set_group(group_id);
}

// Limit results to tables with specified group id (grouped queries only)
template <typename Group>
iter_iterable<Components...> set_group() {
iter_iterable<Components...> set_group() const {
return this->iter().template set_group<Group>();
}

Expand Down
14 changes: 7 additions & 7 deletions include/flecs/addons/cpp/utils/iterable.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,34 +110,34 @@ struct iterable {
return this->iter().first();
}

iter_iterable<Components...> set_var(int var_id, flecs::entity_t value) {
iter_iterable<Components...> set_var(int var_id, flecs::entity_t value) const {
return this->iter().set_var(var_id, value);
}

iter_iterable<Components...> set_var(const char *name, flecs::entity_t value) {
iter_iterable<Components...> set_var(const char *name, flecs::entity_t value) const {
return this->iter().set_var(name, value);
}

iter_iterable<Components...> set_var(const char *name, flecs::table_t *value) {
iter_iterable<Components...> set_var(const char *name, flecs::table_t *value) const {
return this->iter().set_var(name, value);
}

iter_iterable<Components...> set_var(const char *name, ecs_table_range_t value) {
iter_iterable<Components...> set_var(const char *name, ecs_table_range_t value) const {
return this->iter().set_var(name, value);
}

iter_iterable<Components...> set_var(const char *name, flecs::table_range value) {
iter_iterable<Components...> set_var(const char *name, flecs::table_range value) const {
return this->iter().set_var(name, value);
}

// Limit results to tables with specified group id (grouped queries only)
iter_iterable<Components...> set_group(uint64_t group_id) {
iter_iterable<Components...> set_group(uint64_t group_id) const {
return this->iter().set_group(group_id);
}

// Limit results to tables with specified group id (grouped queries only)
template <typename Group>
iter_iterable<Components...> set_group() {
iter_iterable<Components...> set_group() const {
return this->iter().template set_group<Group>();
}

Expand Down
3 changes: 3 additions & 0 deletions test/cpp/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -669,6 +669,9 @@
"captured_query",
"page_iter_captured_query",
"worker_iter_captured_query",
"set_group_captured_query",
"set_var_captured_query",
"set_var_id_captured_query",
"iter_entities",
"iter_get_pair_w_id",
"find",
Expand Down
82 changes: 82 additions & 0 deletions test/cpp/src/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2755,6 +2755,88 @@ void Query_worker_iter_captured_query(void) {
}();
}


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

flecs::entity Rel = ecs.entity();
flecs::entity TgtA = ecs.entity();
flecs::entity TgtB = ecs.entity();

flecs::query<Position> q = ecs.query_builder<Position>()
.group_by(Rel)
.build();

/* flecs::entity e_1 = */ ecs.entity().set<Position>({10, 20}).add(Rel, TgtA);
flecs::entity e_2 = ecs.entity().set<Position>({20, 30}).add(Rel, TgtB);

[=]() {
int count = 0;
q.set_group(TgtB).each([&](flecs::entity e, Position& p) {
test_assert(e == e_2);
test_int(p.x, 20);
test_int(p.y, 30);
count ++;
});
test_int(count, 1);
}();
}

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

flecs::entity Rel = ecs.entity();
flecs::entity TgtA = ecs.entity();
flecs::entity TgtB = ecs.entity();

flecs::query<Position> q = ecs.query_builder<Position>()
.with(Rel, "$var")
.build();

/* flecs::entity e_1 = */ ecs.entity().set<Position>({10, 20}).add(Rel, TgtA);
flecs::entity e_2 = ecs.entity().set<Position>({20, 30}).add(Rel, TgtB);

[=]() {
int count = 0;
q.set_var("var", TgtB).each([&](flecs::entity e, Position& p) {
test_assert(e == e_2);
test_int(p.x, 20);
test_int(p.y, 30);
count ++;
});
test_int(count, 1);
}();
}

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

flecs::entity Rel = ecs.entity();
flecs::entity TgtA = ecs.entity();
flecs::entity TgtB = ecs.entity();

flecs::query<Position> q = ecs.query_builder<Position>()
.with(Rel, "$var")
.build();

int var = q.find_var("var");
test_assert(var != -1);

/* flecs::entity e_1 = */ ecs.entity().set<Position>({10, 20}).add(Rel, TgtA);
flecs::entity e_2 = ecs.entity().set<Position>({20, 30}).add(Rel, TgtB);

[=]() {
int count = 0;
q.set_var(var, TgtB).each([&](flecs::entity e, Position& p) {
test_assert(e == e_2);
test_int(p.x, 20);
test_int(p.y, 30);
count ++;
});
test_int(count, 1);
}();
}

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

Expand Down
17 changes: 16 additions & 1 deletion test/cpp/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -645,6 +645,9 @@ void Query_instanced_nested_query_w_world(void);
void Query_captured_query(void);
void Query_page_iter_captured_query(void);
void Query_worker_iter_captured_query(void);
void Query_set_group_captured_query(void);
void Query_set_var_captured_query(void);
void Query_set_var_id_captured_query(void);
void Query_iter_entities(void);
void Query_iter_get_pair_w_id(void);
void Query_find(void);
Expand Down Expand Up @@ -3902,6 +3905,18 @@ bake_test_case Query_testcases[] = {
"worker_iter_captured_query",
Query_worker_iter_captured_query
},
{
"set_group_captured_query",
Query_set_group_captured_query
},
{
"set_var_captured_query",
Query_set_var_captured_query
},
{
"set_var_id_captured_query",
Query_set_var_id_captured_query
},
{
"iter_entities",
Query_iter_entities
Expand Down Expand Up @@ -6923,7 +6938,7 @@ static bake_test_suite suites[] = {
"Query",
NULL,
NULL,
118,
121,
Query_testcases
},
{
Expand Down

0 comments on commit 686d085

Please sign in to comment.