diff --git a/src/entt/entity/helper.hpp b/src/entt/entity/helper.hpp index 718baa100..45288f821 100644 --- a/src/entt/entity/helper.hpp +++ b/src/entt/entity/helper.hpp @@ -164,11 +164,12 @@ struct sigh_helper { /** * @brief Binds a properly initialized helper to a given signal type. * @tparam Type Type of signal to bind the helper to. + * @param id Optional name for the underlying storage to use. * @return A helper for a given registry and signal type. */ template - auto with() noexcept { - return sigh_helper{*bucket}; + auto with(const id_type id = type_hash::value()) noexcept { + return sigh_helper{*bucket, id}; } /** @@ -190,7 +191,17 @@ private: */ template struct sigh_helper final: sigh_helper { - using sigh_helper::sigh_helper; + /*! @brief Registry type. */ + using registry_type = Registry; + + /** + * @brief Constructs a helper for a given registry. + * @param ref A valid reference to a registry. + * @param id Optional name for the underlying storage to use. + */ + sigh_helper(registry_type &ref, const id_type id = type_hash::value()) + : sigh_helper{ref}, + name{id} {} /** * @brief Forwards the call to `on_construct` on the underlying storage. @@ -201,7 +212,7 @@ struct sigh_helper final: sigh_helper { */ template auto on_construct(Args &&...args) { - this->registry().template storage().on_construct().template connect(std::forward(args)...); + this->registry().template storage(name).on_construct().template connect(std::forward(args)...); return *this; } @@ -214,7 +225,7 @@ struct sigh_helper final: sigh_helper { */ template auto on_update(Args &&...args) { - this->registry().template storage().on_update().template connect(std::forward(args)...); + this->registry().template storage(name).on_update().template connect(std::forward(args)...); return *this; } @@ -227,9 +238,12 @@ struct sigh_helper final: sigh_helper { */ template auto on_destroy(Args &&...args) { - this->registry().template storage().on_destroy().template connect(std::forward(args)...); + this->registry().template storage(name).on_destroy().template connect(std::forward(args)...); return *this; } + +private: + id_type name; }; /** diff --git a/test/entt/entity/helper.cpp b/test/entt/entity/helper.cpp index 278fbe62e..65262eda0 100644 --- a/test/entt/entity/helper.cpp +++ b/test/entt/entity/helper.cpp @@ -130,6 +130,8 @@ TEST(Helper, ToEntityStableType) { } TEST(Helper, SighHelper) { + using namespace entt::literals; + entt::registry registry{}; const auto entt = registry.create(); entt::sigh_helper helper{registry}; @@ -144,9 +146,26 @@ TEST(Helper, SighHelper) { ASSERT_EQ(counter, 0); - registry.emplace(entt, 42); - registry.replace(entt, 0); + registry.emplace(entt); + registry.replace(entt); registry.erase(entt); ASSERT_EQ(counter, 3); + + helper.with("other"_hs) + .on_construct<&sigh_callback>(counter) + .on_update<&sigh_callback>(counter) + .on_destroy<&sigh_callback>(counter); + + registry.emplace(entt); + registry.replace(entt); + registry.erase(entt); + + ASSERT_EQ(counter, 3); + + registry.storage("other"_hs).emplace(entt); + registry.storage("other"_hs).patch(entt); + registry.storage("other"_hs).erase(entt); + + ASSERT_EQ(counter, 6); }