From 9630977c2dfdd8de73daa5878dbfcaceb43e1d92 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Mon, 16 Sep 2024 10:55:15 +0200 Subject: [PATCH] sparse set: generic bind function --- src/entt/entity/mixin.hpp | 16 ++++++---------- src/entt/entity/sparse_set.hpp | 31 ++++++++++++++++++++++++++++--- test/common/mixin.hpp | 12 ++++++------ 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/src/entt/entity/mixin.hpp b/src/entt/entity/mixin.hpp index e4a9331a7..f273288e2 100644 --- a/src/entt/entity/mixin.hpp +++ b/src/entt/entity/mixin.hpp @@ -114,6 +114,12 @@ private: return it; } + void bind_any(any value) noexcept final { + auto *reg = any_cast(&value); + owner = reg ? reg : owner; + underlying_type::bind_any(std::move(value)); + } + public: /*! @brief Allocator type. */ using allocator_type = typename underlying_type::allocator_type; @@ -331,16 +337,6 @@ public: } } - /** - * @brief Forwards variables to derived classes, if any. - * @param value A variable wrapped in an opaque container. - */ - void bind(any value) noexcept final { - auto *reg = any_cast(&value); - owner = reg ? reg : owner; - underlying_type::bind(std::move(value)); - } - private: basic_registry_type *owner; sigh_type construction; diff --git a/src/entt/entity/sparse_set.hpp b/src/entt/entity/sparse_set.hpp index 1a0afe9b0..1369a7376 100644 --- a/src/entt/entity/sparse_set.hpp +++ b/src/entt/entity/sparse_set.hpp @@ -369,6 +369,10 @@ protected: return --(end() - static_cast(pos)); } + /*! @brief Forwards variables to derived classes, if any. */ + // NOLINTNEXTLINE(performance-unnecessary-value-param) + virtual void bind_any(any) noexcept {} + public: /*! @brief Allocator type. */ using allocator_type = Allocator; @@ -1045,9 +1049,30 @@ public: return *info; } - /*! @brief Forwards variables to derived classes, if any. */ - // NOLINTNEXTLINE(performance-unnecessary-value-param) - virtual void bind(any) noexcept {} + /** + * @brief Forwards variables to derived classes, if any. + * @tparam Type Type of the element to forward. + * @param value The element to forward. + * @return Nothing. + */ + template + [[deprecated("avoid wrapping elements with basic_any")]] std::enable_if_t>, basic_any<>>> + bind(Type &&value) noexcept { + // backward compatibility + bind_any(std::forward(value)); + } + + /** + * @brief Forwards variables to derived classes, if any. + * @tparam Type Type of the element to forward. + * @param value The element to forward. + * @return Nothing. + */ + template + std::enable_if_t>, basic_any<>>> + bind(Type &&value) noexcept { + bind_any(forward_as_any(std::forward(value))); + } private: sparse_container_type sparse; diff --git a/test/common/mixin.hpp b/test/common/mixin.hpp index 0f14dda02..55e2d8211 100644 --- a/test/common/mixin.hpp +++ b/test/common/mixin.hpp @@ -13,17 +13,17 @@ class assure_loop_mixin: public Type { using underlying_type = Type; using registry_type = entt::basic_registry; + void bind_any(entt::any value) noexcept override { + if(auto *owner = entt::any_cast(&value); owner) { + owner->template storage(); + } + } + public: using allocator_type = typename underlying_type::allocator_type; using entity_type = typename underlying_type::entity_type; using Type::Type; - - void bind(entt::any value) noexcept override { - if(auto *owner = entt::any_cast(&value); owner) { - owner->template storage(); - } - } }; } // namespace test