From da1f56d046d4de0949017da3db3a8c8f0f41a1b4 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 27 Feb 2025 09:52:05 +0100 Subject: [PATCH] mixin: support custom private/protected registries - close #1222 --- src/entt/entity/mixin.hpp | 32 ++++++++++++++--------------- test/common/registry.h | 21 ++++++++++++++++++- test/entt/entity/reactive_mixin.cpp | 6 +++--- test/entt/entity/sigh_mixin.cpp | 11 +++------- 4 files changed, 42 insertions(+), 28 deletions(-) diff --git a/src/entt/entity/mixin.hpp b/src/entt/entity/mixin.hpp index 5793a6f67..00267684f 100644 --- a/src/entt/entity/mixin.hpp +++ b/src/entt/entity/mixin.hpp @@ -36,20 +36,6 @@ template struct has_on_destroy(), std::declval().create()))>> : std::true_type {}; -template -auto *any_to_owner(any &value) noexcept { - using base_type = basic_registry; - auto *reg = any_cast(&value); - - if constexpr(!std::is_same_v) { - if(!reg) { - reg = any_cast(&value); - } - } - - return reg; -} - } // namespace internal /*! @endcond */ @@ -130,7 +116,14 @@ private: } void bind_any(any value) noexcept final { - owner = internal::any_to_owner(value); + owner = any_cast(&value); + + if constexpr(!std::is_same_v) { + if(owner == nullptr) { + owner = any_cast(&value); + } + } + underlying_type::bind_any(std::move(value)); } @@ -421,7 +414,14 @@ class basic_reactive_mixin final: public Type { private: void bind_any(any value) noexcept final { - owner = internal::any_to_owner(value); + owner = any_cast(&value); + + if constexpr(!std::is_same_v) { + if(owner == nullptr) { + owner = any_cast(&value); + } + } + underlying_type::bind_any(std::move(value)); } diff --git a/test/common/registry.h b/test/common/registry.h index 55db7f1fd..93bff384b 100644 --- a/test/common/registry.h +++ b/test/common/registry.h @@ -6,7 +6,26 @@ namespace test { template -struct basic_custom_registry: entt::basic_registry {}; +struct custom_registry: private entt::basic_registry { + using base_type = entt::basic_registry; + + template + friend class entt::basic_sigh_mixin; + + template + friend class entt::basic_reactive_mixin; + +public: + using allocator_type = typename base_type::allocator_type; + using entity_type = typename base_type::entity_type; + + using base_type::base_type; + + using base_type::storage; + using base_type::create; + using base_type::emplace; + using base_type::insert; +}; } // namespace test diff --git a/test/entt/entity/reactive_mixin.cpp b/test/entt/entity/reactive_mixin.cpp index e26907051..579242eca 100644 --- a/test/entt/entity/reactive_mixin.cpp +++ b/test/entt/entity/reactive_mixin.cpp @@ -31,7 +31,7 @@ void remove(Type &storage, const typename Type::registry_type &, const typename template struct entt::storage_type, std::enable_if_t>> { - using type = entt::basic_sigh_mixin, test::basic_custom_registry>; + using type = entt::basic_sigh_mixin, test::custom_registry>; }; template @@ -454,7 +454,7 @@ ENTT_DEBUG_TYPED_TEST(ReactiveMixinDeathTest, Registry) { TYPED_TEST(ReactiveMixin, CustomRegistry) { using value_type = typename TestFixture::type; - using registry_type = test::basic_custom_registry; + using registry_type = test::custom_registry; registry_type registry; entt::basic_reactive_mixin, registry_type> pool; @@ -476,7 +476,7 @@ TYPED_TEST(ReactiveMixin, CustomRegistry) { ENTT_DEBUG_TYPED_TEST(ReactiveMixinDeathTest, CustomRegistry) { using value_type = typename TestFixture::type; - using registry_type = test::basic_custom_registry; + using registry_type = test::custom_registry; entt::basic_reactive_mixin, registry_type> pool; ASSERT_DEATH([[maybe_unused]] auto ®istry = pool.registry(), ""); ASSERT_DEATH([[maybe_unused]] const auto ®istry = std::as_const(pool).registry(), ""); diff --git a/test/entt/entity/sigh_mixin.cpp b/test/entt/entity/sigh_mixin.cpp index 4406618df..0ecbf8eb9 100644 --- a/test/entt/entity/sigh_mixin.cpp +++ b/test/entt/entity/sigh_mixin.cpp @@ -48,11 +48,6 @@ void listener(std::size_t &counter, Registry &, typename Registry::entity_type) ++counter; } -template -struct entt::storage_type, std::enable_if_t>> { - using type = entt::basic_sigh_mixin, test::basic_custom_registry>; -}; - template struct SighMixin: testing::Test { using type = Type; @@ -433,7 +428,7 @@ TYPED_TEST(SighMixin, Swap) { TEST(SighMixin, AutoSignal) { entt::registry registry; - auto const entity = registry.create(); + const auto entity = registry.create(); bool created{}; bool updated{}; @@ -488,7 +483,7 @@ ENTT_DEBUG_TYPED_TEST(SighMixinDeathTest, Registry) { TYPED_TEST(SighMixin, CustomRegistry) { using value_type = typename TestFixture::type; - using registry_type = test::basic_custom_registry; + using registry_type = test::custom_registry; registry_type registry; entt::basic_sigh_mixin, registry_type> pool; @@ -520,7 +515,7 @@ TYPED_TEST(SighMixin, CustomRegistry) { ENTT_DEBUG_TYPED_TEST(SighMixinDeathTest, CustomRegistry) { using value_type = typename TestFixture::type; - using registry_type = test::basic_custom_registry; + using registry_type = test::custom_registry; entt::basic_sigh_mixin, registry_type> pool; ASSERT_DEATH([[maybe_unused]] auto ®istry = pool.registry(), ""); ASSERT_DEATH([[maybe_unused]] const auto ®istry = std::as_const(pool).registry(), "");