diff --git a/src/entt/entity/mixin.hpp b/src/entt/entity/mixin.hpp index f96b62c87..ac20f755c 100644 --- a/src/entt/entity/mixin.hpp +++ b/src/entt/entity/mixin.hpp @@ -259,6 +259,27 @@ class basic_sigh_mixin final: public Type { return sink{destruction}; } + /** + * @brief Checks if a mixin refers to a valid registry. + * @return True if the mixin refers to a valid registry, false otherwise. + */ + [[nodiscard]] explicit operator bool() const noexcept { + return (owner != nullptr); + } + + /** + * @brief Returns a pointer to the underlying registry, if any. + * @return A pointer to the underlying registry, if any. + */ + [[nodiscard]] const registry_type ®istry() const noexcept { + return owner_or_assert(); + } + + /*! @copydoc registry */ + [[nodiscard]] registry_type ®istry() noexcept { + return owner_or_assert(); + } + /** * @brief Emplace elements into a storage. * diff --git a/test/entt/entity/sigh_mixin.cpp b/test/entt/entity/sigh_mixin.cpp index 71a5ea6cb..f788927e1 100644 --- a/test/entt/entity/sigh_mixin.cpp +++ b/test/entt/entity/sigh_mixin.cpp @@ -10,6 +10,7 @@ #include #include #include +#include "../../common/config.h" #include "../../common/entity.h" #include "../../common/linter.hpp" #include "../../common/non_default_constructible.h" @@ -57,9 +58,13 @@ struct SighMixin: testing::Test { using type = Type; }; +template +using SighMixinDeathTest = SighMixin; + using SighMixinTypes = ::testing::Types; TYPED_TEST_SUITE(SighMixin, SighMixinTypes, ); +TYPED_TEST_SUITE(SighMixinDeathTest, SighMixinTypes, ); TYPED_TEST(SighMixin, Functionalities) { using value_type = typename TestFixture::type; @@ -459,14 +464,41 @@ TEST(SighMixin, AutoSignal) { ASSERT_FALSE(registry.valid(entity)); } +TYPED_TEST(SighMixin, Registry) { + using value_type = typename TestFixture::type; + + entt::registry registry; + entt::sigh_mixin> pool; + + ASSERT_FALSE(pool); + + pool.bind(registry); + + ASSERT_TRUE(pool); + ASSERT_EQ(&pool.registry(), ®istry); + ASSERT_EQ(&std::as_const(pool).registry(), ®istry); +} + +ENTT_DEBUG_TYPED_TEST(SighMixinDeathTest, Registry) { + using value_type = typename TestFixture::type; + entt::sigh_mixin> pool; + ASSERT_DEATH([[maybe_unused]] auto ®istry = pool.registry(), ""); + ASSERT_DEATH([[maybe_unused]] const auto ®istry = std::as_const(pool).registry(), ""); +} + TYPED_TEST(SighMixin, CustomRegistry) { using value_type = typename TestFixture::type; using registry_type = test::basic_custom_registry; registry_type registry; - auto &pool = registry.storage(); + entt::basic_sigh_mixin, registry_type> pool; + const std::array entity{registry.create(), registry.create()}; + + ASSERT_FALSE(pool); - testing::StaticAssertTypeEq, registry_type> &>(); + pool.bind(static_cast &>(registry)); + + ASSERT_TRUE(pool); std::size_t on_construct{}; std::size_t on_destroy{}; @@ -474,8 +506,8 @@ TYPED_TEST(SighMixin, CustomRegistry) { pool.on_construct().template connect<&listener>(on_construct); pool.on_destroy().template connect<&listener>(on_destroy); - pool.emplace(registry.create()); - pool.emplace(registry.create()); + pool.emplace(entity[0u]); + pool.emplace(entity[1u]); ASSERT_EQ(on_construct, 2u); ASSERT_EQ(on_destroy, 0u); @@ -486,6 +518,14 @@ TYPED_TEST(SighMixin, CustomRegistry) { ASSERT_EQ(on_destroy, 2u); } +ENTT_DEBUG_TYPED_TEST(SighMixinDeathTest, CustomRegistry) { + using value_type = typename TestFixture::type; + using registry_type = test::basic_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(), ""); +} + TYPED_TEST(SighMixin, CustomAllocator) { using value_type = typename TestFixture::type; using storage_type = entt::sigh_mixin>>;