Browse Source

sigh mixin: operator bool and registry functions

Michele Caini 1 year ago
parent
commit
61b5c193e8
2 changed files with 65 additions and 4 deletions
  1. 21 0
      src/entt/entity/mixin.hpp
  2. 44 4
      test/entt/entity/sigh_mixin.cpp

+ 21 - 0
src/entt/entity/mixin.hpp

@@ -259,6 +259,27 @@ public:
         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 &registry() const noexcept {
+        return owner_or_assert();
+    }
+
+    /*! @copydoc registry */
+    [[nodiscard]] registry_type &registry() noexcept {
+        return owner_or_assert();
+    }
+
     /**
      * @brief Emplace elements into a storage.
      *

+ 44 - 4
test/entt/entity/sigh_mixin.cpp

@@ -10,6 +10,7 @@
 #include <entt/entity/mixin.hpp>
 #include <entt/entity/registry.hpp>
 #include <entt/entity/storage.hpp>
+#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<typename Type>
+using SighMixinDeathTest = SighMixin<Type>;
+
 using SighMixinTypes = ::testing::Types<int, test::pointer_stable>;
 
 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<entt::storage<value_type>> pool;
+
+    ASSERT_FALSE(pool);
+
+    pool.bind(registry);
+
+    ASSERT_TRUE(pool);
+    ASSERT_EQ(&pool.registry(), &registry);
+    ASSERT_EQ(&std::as_const(pool).registry(), &registry);
+}
+
+ENTT_DEBUG_TYPED_TEST(SighMixinDeathTest, Registry) {
+    using value_type = typename TestFixture::type;
+    entt::sigh_mixin<entt::storage<value_type>> pool;
+    ASSERT_DEATH([[maybe_unused]] auto &registry = pool.registry(), "");
+    ASSERT_DEATH([[maybe_unused]] const auto &registry = std::as_const(pool).registry(), "");
+}
+
 TYPED_TEST(SighMixin, CustomRegistry) {
     using value_type = typename TestFixture::type;
     using registry_type = test::basic_custom_registry<test::entity>;
 
     registry_type registry;
-    auto &pool = registry.storage<value_type>();
+    entt::basic_sigh_mixin<entt::basic_storage<value_type, test::entity>, registry_type> pool;
+    const std::array entity{registry.create(), registry.create()};
+
+    ASSERT_FALSE(pool);
 
-    testing::StaticAssertTypeEq<decltype(pool), entt::basic_sigh_mixin<entt::basic_storage<value_type, test::entity>, registry_type> &>();
+    pool.bind(static_cast<entt::basic_registry<test::entity> &>(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<registry_type>>(on_construct);
     pool.on_destroy().template connect<&listener<registry_type>>(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<test::entity>;
+    entt::basic_sigh_mixin<entt::basic_storage<value_type, test::entity>, registry_type> pool;
+    ASSERT_DEATH([[maybe_unused]] auto &registry = pool.registry(), "");
+    ASSERT_DEATH([[maybe_unused]] const auto &registry = std::as_const(pool).registry(), "");
+}
+
 TYPED_TEST(SighMixin, CustomAllocator) {
     using value_type = typename TestFixture::type;
     using storage_type = entt::sigh_mixin<entt::basic_storage<value_type, entt::entity, test::throwing_allocator<value_type>>>;