Skip to content

Commit

Permalink
Add ecs_query_get
Browse files Browse the repository at this point in the history
  • Loading branch information
SanderMertens committed Dec 17, 2024
1 parent e18da5a commit 1050c12
Show file tree
Hide file tree
Showing 7 changed files with 96 additions and 2 deletions.
13 changes: 13 additions & 0 deletions distr/flecs.c
Original file line number Diff line number Diff line change
Expand Up @@ -33672,6 +33672,19 @@ const ecs_query_t* ecs_query_get_cache_query(
}
}

const ecs_query_t* ecs_query_get(
const ecs_world_t *world,
ecs_entity_t query)
{
const EcsPoly *poly_comp = ecs_get_pair(world, query, EcsPoly, EcsQuery);
if (!poly_comp) {
return NULL;
} else {
flecs_poly_assert(poly_comp->poly, ecs_query_t);
return poly_comp->poly;
}
}

/**
* @file query/util.c
* @brief Query utilities.
Expand Down
13 changes: 13 additions & 0 deletions distr/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -8136,6 +8136,19 @@ FLECS_API
bool ecs_query_changed(
ecs_query_t *query);

/** Get query object.
* Returns the query object. Can be used to access various information about
* the query.
*
* @param world The world.
* @param query The query.
* @return The query object.
*/
FLECS_API
const ecs_query_t* ecs_query_get(
const ecs_world_t *world,
ecs_entity_t query);

/** Skip a table while iterating.
* This operation lets the query iterator know that a table was skipped while
* iterating. A skipped table will not reset its changed state, and the query
Expand Down
13 changes: 13 additions & 0 deletions include/flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -4896,6 +4896,19 @@ FLECS_API
bool ecs_query_changed(
ecs_query_t *query);

/** Get query object.
* Returns the query object. Can be used to access various information about
* the query.
*
* @param world The world.
* @param query The query.
* @return The query object.
*/
FLECS_API
const ecs_query_t* ecs_query_get(
const ecs_world_t *world,
ecs_entity_t query);

/** Skip a table while iterating.
* This operation lets the query iterator know that a table was skipped while
* iterating. A skipped table will not reset its changed state, and the query
Expand Down
13 changes: 13 additions & 0 deletions src/query/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -541,3 +541,16 @@ const ecs_query_t* ecs_query_get_cache_query(
return impl->cache->query;
}
}

const ecs_query_t* ecs_query_get(
const ecs_world_t *world,
ecs_entity_t query)
{
const EcsPoly *poly_comp = ecs_get_pair(world, query, EcsPoly, EcsQuery);
if (!poly_comp) {
return NULL;
} else {
flecs_poly_assert(poly_comp->poly, ecs_query_t);
return poly_comp->poly;
}
}
4 changes: 3 additions & 1 deletion test/query/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -697,7 +697,9 @@
"entity_iteration_w_match_empty_tables",
"get_cache_query_uncached",
"get_cache_query_cached",
"get_cache_query_partially_cached"
"get_cache_query_partially_cached",
"get_query",
"get_query_not_a_query"
]
}, {
"id": "Combinations",
Expand Down
30 changes: 30 additions & 0 deletions test/query/src/Basic.c
Original file line number Diff line number Diff line change
Expand Up @@ -11428,3 +11428,33 @@ void Basic_get_cache_query_partially_cached(void) {

ecs_fini(world);
}

void Basic_get_query(void) {
ecs_world_t *world = ecs_mini();

ECS_COMPONENT(world, Position);

ecs_entity_t qe = ecs_new(world);

ecs_query_t *q = ecs_query(world, {
.entity = qe,
.expr = "Position",
.cache_kind = EcsQueryCacheAuto
});

test_assert(q != NULL);
test_assert(q->entity == qe);
test_assert(ecs_query_get(world, qe) == q);

ecs_fini(world);
}

void Basic_get_query_not_a_query(void) {
ecs_world_t *world = ecs_mini();

ecs_entity_t qe = ecs_new(world);

test_assert(ecs_query_get(world, qe) == NULL);

ecs_fini(world);
}
12 changes: 11 additions & 1 deletion test/query/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,8 @@ void Basic_entity_iteration_w_match_empty_tables(void);
void Basic_get_cache_query_uncached(void);
void Basic_get_cache_query_cached(void);
void Basic_get_cache_query_partially_cached(void);
void Basic_get_query(void);
void Basic_get_query_not_a_query(void);

// Testsuite 'Combinations'
void Combinations_setup(void);
Expand Down Expand Up @@ -4815,6 +4817,14 @@ bake_test_case Basic_testcases[] = {
{
"get_cache_query_partially_cached",
Basic_get_cache_query_partially_cached
},
{
"get_query",
Basic_get_query
},
{
"get_query_not_a_query",
Basic_get_query_not_a_query
}
};

Expand Down Expand Up @@ -10477,7 +10487,7 @@ static bake_test_suite suites[] = {
"Basic",
Basic_setup,
NULL,
230,
232,
Basic_testcases,
1,
Basic_params
Expand Down

0 comments on commit 1050c12

Please sign in to comment.