From 5983f23f2ac35d857d5ed581f67393d2bc807e18 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Tue, 17 Sep 2024 15:30:51 -0700 Subject: [PATCH] Add flecs::iter::other_table() Differential Revision: D62449718 Pull Request resolved: https://github.com/SanderMertens/flecs/pull/1352 --- distr/flecs.h | 6 +++ include/flecs/addons/cpp/impl/iter.hpp | 4 ++ include/flecs/addons/cpp/iter.hpp | 2 + test/cpp/project.json | 5 ++- test/cpp/src/Observer.cpp | 62 ++++++++++++++++++++++++++ test/cpp/src/main.cpp | 17 ++++++- 6 files changed, 94 insertions(+), 2 deletions(-) diff --git a/distr/flecs.h b/distr/flecs.h index 1db12b5aaa..2ca9ce4bd5 100644 --- a/distr/flecs.h +++ b/distr/flecs.h @@ -22510,6 +22510,8 @@ struct iter { flecs::table table() const; + flecs::table other_table() const; + flecs::table_range range() const; /** Access ctx. @@ -32403,6 +32405,10 @@ inline flecs::table iter::table() const { return flecs::table(iter_->real_world, iter_->table); } +inline flecs::table iter::other_table() const { + return flecs::table(iter_->real_world, iter_->other_table); +} + inline flecs::table_range iter::range() const { return flecs::table_range(iter_->real_world, iter_->table, iter_->offset, iter_->count); diff --git a/include/flecs/addons/cpp/impl/iter.hpp b/include/flecs/addons/cpp/impl/iter.hpp index da6120627b..ac2ea43ed6 100644 --- a/include/flecs/addons/cpp/impl/iter.hpp +++ b/include/flecs/addons/cpp/impl/iter.hpp @@ -54,6 +54,10 @@ inline flecs::table iter::table() const { return flecs::table(iter_->real_world, iter_->table); } +inline flecs::table iter::other_table() const { + return flecs::table(iter_->real_world, iter_->other_table); +} + inline flecs::table_range iter::range() const { return flecs::table_range(iter_->real_world, iter_->table, iter_->offset, iter_->count); diff --git a/include/flecs/addons/cpp/iter.hpp b/include/flecs/addons/cpp/iter.hpp index 76a3d03503..74942080c1 100644 --- a/include/flecs/addons/cpp/iter.hpp +++ b/include/flecs/addons/cpp/iter.hpp @@ -117,6 +117,8 @@ struct iter { flecs::table table() const; + flecs::table other_table() const; + flecs::table_range range() const; /** Access ctx. diff --git a/test/cpp/project.json b/test/cpp/project.json index 7fa11666eb..15e484d2a1 100644 --- a/test/cpp/project.json +++ b/test/cpp/project.json @@ -918,7 +918,10 @@ "register_twice_w_each", "register_twice_w_run", "register_twice_w_run_each", - "register_twice_w_each_run" + "register_twice_w_each_run", + "other_table", + "other_table_w_pair", + "other_table_w_pair_wildcard" ] }, { "id": "ComponentLifecycle", diff --git a/test/cpp/src/Observer.cpp b/test/cpp/src/Observer.cpp index 6555d67bd4..c22c8d9e95 100644 --- a/test/cpp/src/Observer.cpp +++ b/test/cpp/src/Observer.cpp @@ -1136,3 +1136,65 @@ void Observer_register_twice_w_each_run(void) { ecs.entity().set(Position{10, 20}); test_int(count2, 1); } + +void Observer_other_table(void) { + flecs::world ecs; + + int32_t count = 0; + + ecs.observer() + .event(flecs::OnAdd) + .each([&](flecs::iter& it, size_t, Velocity&) { + test_assert(it.table().has()); + test_assert(!it.other_table().has()); + count ++; + }); + + flecs::entity e = ecs.entity().add().add(); + + test_int(count, 1); +} + +void Observer_other_table_w_pair(void) { + flecs::world ecs; + + struct Likes {}; + struct Apples {}; + + int32_t count = 0; + + ecs.observer() + .with() + .event(flecs::OnAdd) + .each([&](flecs::iter& it, size_t) { + test_assert((it.table().has())); + test_assert((!it.other_table().has())); + count ++; + }); + + flecs::entity e = ecs.entity().add().add(); + + test_int(count, 1); +} + +void Observer_other_table_w_pair_wildcard(void) { + flecs::world ecs; + + struct Likes {}; + struct Apples {}; + + int32_t count = 0; + + ecs.observer() + .with() + .event(flecs::OnAdd) + .each([&](flecs::iter& it, size_t) { + test_assert((it.table().has(flecs::Wildcard))); + test_assert((!it.other_table().has(flecs::Wildcard))); + count ++; + }); + + flecs::entity e = ecs.entity().add().add(); + + test_int(count, 1); +} diff --git a/test/cpp/src/main.cpp b/test/cpp/src/main.cpp index ee4b9128dc..6f13671bf9 100644 --- a/test/cpp/src/main.cpp +++ b/test/cpp/src/main.cpp @@ -886,6 +886,9 @@ void Observer_register_twice_w_each(void); void Observer_register_twice_w_run(void); void Observer_register_twice_w_run_each(void); void Observer_register_twice_w_each_run(void); +void Observer_other_table(void); +void Observer_other_table_w_pair(void); +void Observer_other_table_w_pair_wildcard(void); // Testsuite 'ComponentLifecycle' void ComponentLifecycle_ctor_on_add(void); @@ -4789,6 +4792,18 @@ bake_test_case Observer_testcases[] = { { "register_twice_w_each_run", Observer_register_twice_w_each_run + }, + { + "other_table", + Observer_other_table + }, + { + "other_table_w_pair", + Observer_other_table_w_pair + }, + { + "other_table_w_pair_wildcard", + Observer_other_table_w_pair_wildcard } }; @@ -6661,7 +6676,7 @@ static bake_test_suite suites[] = { "Observer", NULL, NULL, - 46, + 49, Observer_testcases }, {