From 5c45da6d9d858c763ab5e022ebbe335b3818ac82 Mon Sep 17 00:00:00 2001 From: Sander Mertens Date: Tue, 17 Sep 2024 13:41:57 -0700 Subject: [PATCH] Add flecs::iter::other_table() (#1352) Summary: Pull Request resolved: https://github.com/SanderMertens/flecs/pull/1352 This diff upstreams a change that adds an `flecs::iter::other_table()` method. Differential Revision: D62449718 --- 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 1db12b5aa..2ca9ce4bd 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 da6120627..ac2ea43ed 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 76a3d0350..74942080c 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 7fa11666e..15e484d2a 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 6555d67bd..c22c8d9e9 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 ee4b9128d..6f13671bf 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 }, {