Browse Source

mixin: full support for derived registry types

Michele Caini 1 year ago
parent
commit
4f0b7d31dd
4 changed files with 18 additions and 7 deletions
  1. 0 1
      TODO
  2. 16 4
      src/entt/entity/mixin.hpp
  3. 1 1
      test/entt/entity/reactive_mixin.cpp
  4. 1 1
      test/entt/entity/sigh_mixin.cpp

+ 0 - 1
TODO

@@ -40,7 +40,6 @@ TODO:
 * refine the storage fallback mechanism for views (ie alloc?)
 * sigh_mixin: automatic signal registration
 * sigh_mixin: change cb signature from reg/entt to storage/entt (breaking for the good)
-* mixin bind_any support for both registry types
 * reactive_mixin: note about no-auto-disconnect mechanism
 * reactive_mixin: in-code and md doc about callback signature
 * don't pass reactive storage by default to callback

+ 16 - 4
src/entt/entity/mixin.hpp

@@ -36,6 +36,20 @@ template<typename Type, typename Registry>
 struct has_on_destroy<Type, Registry, std::void_t<decltype(Type::on_destroy(std::declval<Registry &>(), std::declval<Registry>().create()))>>
     : std::true_type {};
 
+template<typename Type>
+auto *any_to_owner(any &value) noexcept {
+    using base_type = basic_registry<typename Type::entity_type, typename Type::allocator_type>;
+    base_type *reg = any_cast<base_type>(&value);
+
+    if constexpr(!std::is_same_v<Type, base_type>) {
+        if(!reg) {
+            reg = any_cast<Type>(&value);
+        }
+    }
+
+    return reg;
+}
+
 } // namespace internal
 /*! @endcond */
 
@@ -116,8 +130,7 @@ private:
     }
 
     void bind_any(any value) noexcept final {
-        auto *reg = any_cast<basic_registry_type>(&value);
-        owner = reg ? reg : owner;
+        owner = internal::any_to_owner<registry_type>(value);
         underlying_type::bind_any(std::move(value));
     }
 
@@ -393,8 +406,7 @@ class basic_reactive_mixin final: public Type {
 
 private:
     void bind_any(any value) noexcept final {
-        auto *reg = any_cast<basic_registry_type>(&value);
-        owner = reg ? reg : owner;
+        owner = internal::any_to_owner<registry_type>(value);
         underlying_type::bind_any(std::move(value));
     }
 

+ 1 - 1
test/entt/entity/reactive_mixin.cpp

@@ -463,7 +463,7 @@ TYPED_TEST(ReactiveMixin, CustomRegistry) {
 
     ASSERT_FALSE(pool);
 
-    pool.bind(static_cast<entt::basic_registry<test::entity> &>(registry));
+    pool.bind(registry);
 
     ASSERT_TRUE(pool);
 

+ 1 - 1
test/entt/entity/sigh_mixin.cpp

@@ -496,7 +496,7 @@ TYPED_TEST(SighMixin, CustomRegistry) {
 
     ASSERT_FALSE(pool);
 
-    pool.bind(static_cast<entt::basic_registry<test::entity> &>(registry));
+    pool.bind(registry);
 
     ASSERT_TRUE(pool);