Skip to content

Commit

Permalink
Add term_index method to cpp iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
jpeletier authored and SanderMertens committed Jul 11, 2024
1 parent 2888c9f commit 5404dce
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 2 deletions.
6 changes: 6 additions & 0 deletions flecs.h
Original file line number Diff line number Diff line change
Expand Up @@ -22165,6 +22165,12 @@ struct iter {
return ecs_field_column_index(m_iter, index);
}

/** Obtain term that triggered an observer
*/
int32_t term_index() const {
return m_iter->term_index + 1; // in iter_t, the term index is zero-based, so add 1.
}

/** Convert current iterator result to string.
*/
flecs::string str() const {
Expand Down
6 changes: 6 additions & 0 deletions include/flecs/addons/cpp/iter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,12 @@ struct iter {
return ecs_field_column_index(m_iter, index);
}

/** Obtain term that triggered an observer
*/
int32_t term_index() const {
return m_iter->term_index + 1; // in iter_t, the term index is zero-based, so add 1.
}

/** Convert current iterator result to string.
*/
flecs::string str() const {
Expand Down
3 changes: 2 additions & 1 deletion test/cpp_api/project.json
Original file line number Diff line number Diff line change
Expand Up @@ -924,7 +924,8 @@
"on_add_with_pair_singleton",
"add_in_yield_existing",
"add_in_yield_existing_multi",
"name_from_root"
"name_from_root",
"term_index"
]
}, {
"id": "Filter",
Expand Down
20 changes: 20 additions & 0 deletions test/cpp_api/src/Observer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -816,3 +816,23 @@ void Observer_name_from_root(void) {
flecs::entity ns = ecs.entity("::ns");
test_assert(ns == sys.parent());
}

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

auto e1 = ecs.entity().add<Position>().add<Velocity>();

int32_t last_term = -1;

ecs.observer<const Position, const Velocity>()
.event(flecs::OnSet)
.each([&](flecs::iter& it, size_t row, const Position &p, const Velocity &v) {
last_term = it.term_index();
});

e1.set<Position>({ 10, 20 });
test_int(last_term, 1);

e1.set<Velocity>({ 30, 40 });
test_int(last_term, 2);
}
7 changes: 6 additions & 1 deletion test/cpp_api/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -891,6 +891,7 @@ void Observer_on_add_with_pair_singleton(void);
void Observer_add_in_yield_existing(void);
void Observer_add_in_yield_existing_multi(void);
void Observer_name_from_root(void);
void Observer_term_index(void);

// Testsuite 'Filter'
void Filter_term_each_component(void);
Expand Down Expand Up @@ -4825,6 +4826,10 @@ bake_test_case Observer_testcases[] = {
{
"name_from_root",
Observer_name_from_root
},
{
"term_index",
Observer_term_index
}
};

Expand Down Expand Up @@ -6763,7 +6768,7 @@ static bake_test_suite suites[] = {
"Observer",
NULL,
NULL,
32,
33,
Observer_testcases
},
{
Expand Down

0 comments on commit 5404dce

Please sign in to comment.