mixin: full support for derived registry types

This commit is contained in:
Michele Caini
2024-10-02 10:31:07 +02:00
parent 9eef230340
commit 4f0b7d31dd
4 changed files with 18 additions and 7 deletions

1
TODO
View File

@@ -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

View File

@@ -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));
}

View File

@@ -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);

View File

@@ -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);