From 6b95c614bb63d25ea35f95d48473c7e5f82c66fb Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Fri, 21 Feb 2025 12:36:03 +0100 Subject: [PATCH] view: setup placeholders on view packs - close #1224 --- src/entt/entity/view.hpp | 5 +++-- test/entt/entity/view.cpp | 28 +++++++++++++++++++++++++++- 2 files changed, 30 insertions(+), 3 deletions(-) diff --git a/src/entt/entity/view.hpp b/src/entt/entity/view.hpp index 07e0dda62..42cc77ad1 100644 --- a/src/entt/entity/view.hpp +++ b/src/entt/entity/view.hpp @@ -52,7 +52,8 @@ template()..., other.template storage()...}; - elem.filter = {view.template storage()..., other.template storage()...}; + auto filter_or_placeholder = [placeholder = elem.placeholder](auto *value) { return (value == nullptr) ? placeholder : value; }; + elem.filter = {filter_or_placeholder(view.template storage())..., filter_or_placeholder(other.template storage())...}; elem.refresh(); return elem; } @@ -255,7 +256,7 @@ class basic_common_view { protected: /*! @cond TURN_OFF_DOXYGEN */ basic_common_view() noexcept { - for(size_type pos{}; pos < Exclude; ++pos) { + for(size_type pos{}, last = filter.size(); pos < last; ++pos) { filter[pos] = placeholder; } } diff --git a/test/entt/entity/view.cpp b/test/entt/entity/view.cpp index 69cf86667..db187c56a 100644 --- a/test/entt/entity/view.cpp +++ b/test/entt/entity/view.cpp @@ -1393,7 +1393,7 @@ TEST(MultiStorageView, Storage) { TEST(MultiStorageView, SwapStorage) { std::tuple, entt::storage, entt::storage, entt::storage> storage{}; - entt::basic_view>, entt::exclude_t>> view; + entt::basic_view>, entt::exclude_t>> view{}; const entt::entity entity{0}; ASSERT_FALSE(view); @@ -1583,3 +1583,29 @@ TEST(View, Pipe) { ASSERT_NE(pack32.storage(), nullptr); ASSERT_NE(pack32.storage(), nullptr); } + +TEST(View, PipeWithPlaceholder) { + entt::storage storage{}; + const entt::entity entity{0}; + + const entt::basic_view view{storage}; + entt::basic_view>, entt::exclude_t>> other{}; + + other.storage(storage); + + ASSERT_FALSE(view.contains(entity)); + ASSERT_FALSE(other.contains(entity)); + + auto pack = view | other; + + ASSERT_FALSE(pack.contains(entity)); + + storage.emplace(entity); + + ASSERT_TRUE(view.contains(entity)); + ASSERT_TRUE(other.contains(entity)); + + pack = view | other; + + ASSERT_TRUE(pack.contains(entity)); +}