From 8e9a6a4f0603a7cda8b2bc4993d0f67777935fed Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 6 Mar 2020 23:23:39 +0100 Subject: [PATCH] registry: fixed a bug that affects late group initialization (close #440) --- src/entt/entity/registry.hpp | 8 ++------ test/entt/entity/group.cpp | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/src/entt/entity/registry.hpp b/src/entt/entity/registry.hpp index 36fcff3fe..11a42e1eb 100644 --- a/src/entt/entity/registry.hpp +++ b/src/entt/entity/registry.hpp @@ -1405,12 +1405,8 @@ public: } } else { // we cannot iterate backwards because we want to leave behind valid entities in case of owned types - std::for_each(std::get<0>(cpools).data(), std::get<0>(cpools).data() + std::get<0>(cpools).size(), [this, cpools, handler](const auto entity) { - if(has..., std::decay_t...>(entity) && !any(entity)) { - if(const auto pos = handler->current; !(std::get<0>(cpools).index(entity) < ++handler->current)) { - (std::get> &>(cpools).swap(std::get> &>(cpools).data()[pos], entity), ...); - } - } + std::for_each(std::get<0>(cpools).data(), std::get<0>(cpools).data() + std::get<0>(cpools).size(), [this, handler](const auto entity) { + handler->template maybe_valid_if...>>>(*this, entity); }); } } diff --git a/test/entt/entity/group.cpp b/test/entt/entity/group.cpp index 7a98ebb8a..8c0f89d9a 100644 --- a/test/entt/entity/group.cpp +++ b/test/entt/entity/group.cpp @@ -1121,7 +1121,7 @@ TEST(OwningGroup, SignalRace) { ASSERT_EQ(registry.group(entt::get).size(), 1u); } -TEST(OwningGroup, AfterFact) { +TEST(OwningGroup, StableLateInitialization) { entt::registry registry; for(std::size_t i{}; i < 30u; ++i) { @@ -1133,3 +1133,20 @@ TEST(OwningGroup, AfterFact) { // thanks to @pgruenbacher for pointing out this corner case ASSERT_EQ((registry.group().size()), 5u); } + +TEST(OwningGroup, PreventEarlyOptOut) { + entt::registry registry; + + registry.assign(registry.create(), 3); + + const auto entity = registry.create(); + registry.assign(entity, 'c'); + registry.assign(entity, 2); + + // thanks to @pgruenbacher for pointing out this corner case + registry.group().each([entity](const auto entt, const auto &c, const auto &i) { + ASSERT_EQ(entity, entt); + ASSERT_EQ(c, 'c'); + ASSERT_EQ(i, 2); + }); +}