From 02ec48ddc52f6d8cfe5d8c455abfd7bcb95014d2 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Thu, 14 Mar 2024 08:25:01 +0100 Subject: [PATCH] sigh_mixin: avoid using weak ranges twice - close #1123 --- src/entt/entity/mixin.hpp | 5 +++-- test/entt/entity/sigh_mixin.cpp | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 2 deletions(-) diff --git a/src/entt/entity/mixin.hpp b/src/entt/entity/mixin.hpp index 49233f89e..67802b95e 100644 --- a/src/entt/entity/mixin.hpp +++ b/src/entt/entity/mixin.hpp @@ -271,11 +271,12 @@ public: */ template void insert(It first, It last, Args &&...args) { + auto from = underlying_type::size(); underlying_type::insert(first, last, std::forward(args)...); if(auto ® = owner_or_assert(); !construction.empty()) { - for(; first != last; ++first) { - construction.publish(reg, *first); + for(const auto to = underlying_type::size(); from != to; ++from) { + construction.publish(reg, underlying_type::operator[](from)); } } } diff --git a/test/entt/entity/sigh_mixin.cpp b/test/entt/entity/sigh_mixin.cpp index f8e599f7e..add698130 100644 --- a/test/entt/entity/sigh_mixin.cpp +++ b/test/entt/entity/sigh_mixin.cpp @@ -115,6 +115,23 @@ TYPED_TEST(SighMixin, Functionalities) { ASSERT_EQ(pool.size(), 0u); } +TYPED_TEST(SighMixin, InsertWeakRange) { + using value_type = typename TestFixture::type; + + entt::registry registry; + auto &pool = registry.storage(); + const auto view = registry.view(entt::exclude); + [[maybe_unused]] const std::array entity{registry.create(), registry.create()}; + std::size_t on_construct{}; + + ASSERT_EQ(on_construct, 0u); + + pool.on_construct().template connect<&listener>(on_construct); + pool.insert(view.begin(), view.end()); + + ASSERT_EQ(on_construct, 2u); +} + TEST(SighMixin, NonDefaultConstructibleType) { entt::registry registry; auto &pool = registry.storage();