Skip to content

Commit

Permalink
Add flecs::iter::entities() function
Browse files Browse the repository at this point in the history
  • Loading branch information
ZeroErrors authored and SanderMertens committed Jul 12, 2023
1 parent 061a5e0 commit 3ba680a
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 2 deletions.
8 changes: 8 additions & 0 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -20900,6 +20900,14 @@ struct iter {
return get_unchecked_field(index);
}

/** Get readonly access to entity ids.
*
* @return The entity ids.
*/
flecs::column<const flecs::entity_t> entities() const {
return flecs::column<const flecs::entity_t>(m_iter->entities, static_cast<size_t>(m_iter->count), false);
}

/** Obtain the total number of tables the iterator will iterate over. */
int32_t table_count() const {
return m_iter->table_count;
Expand Down
10 changes: 9 additions & 1 deletion include/flecs/addons/cpp/iter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,14 @@ struct iter {
return get_unchecked_field(index);
}

/** Get readonly access to entity ids.
*
* @return The entity ids.
*/
flecs::column<const flecs::entity_t> entities() const {
return flecs::column<const flecs::entity_t>(m_iter->entities, static_cast<size_t>(m_iter->count), false);
}

/** Obtain the total number of tables the iterator will iterate over. */
int32_t table_count() const {
return m_iter->table_count;
Expand Down Expand Up @@ -471,4 +479,4 @@ struct iter {

} // namespace flecs

/** @} */
/** @} */
18 changes: 18 additions & 0 deletions test/cpp_api/src/Query.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2229,3 +2229,21 @@ void Query_worker_iter_captured_query() {
test_int(count, 1);
}();
}

void Query_iter_entities() {
flecs::world ecs;

auto e1 = ecs.entity().set<Position>({10, 20});
auto e2 = ecs.entity().set<Position>({10, 20});
auto e3 = ecs.entity().set<Position>({10, 20});

ecs.query<Position>()
.iter([&](flecs::iter& it) {
test_int(it.count(), 3);

auto entities = it.entities();
test_assert(entities[0] == e1);
test_assert(entities[1] == e2);
test_assert(entities[2] == e3);
});
}
7 changes: 6 additions & 1 deletion test/cpp_api/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -563,6 +563,7 @@ 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_iter_entities(void);

// Testsuite 'QueryBuilder'
void QueryBuilder_builder_assign_same_type(void);
Expand Down Expand Up @@ -3417,6 +3418,10 @@ bake_test_case Query_testcases[] = {
{
"worker_iter_captured_query",
Query_worker_iter_captured_query
},
{
"iter_entities",
Query_iter_entities
}
};

Expand Down Expand Up @@ -6122,7 +6127,7 @@ static bake_test_suite suites[] = {
"Query",
NULL,
NULL,
78,
79,
Query_testcases
},
{
Expand Down

0 comments on commit 3ba680a

Please sign in to comment.