Browse Source

view: non-const excluded types are always accepted (close #507)

Michele Caini 5 years ago
parent
commit
be93643808
2 changed files with 13 additions and 14 deletions
  1. 9 10
      src/entt/entity/view.hpp
  2. 4 4
      test/entt/entity/view.cpp

+ 9 - 10
src/entt/entity/view.hpp

@@ -158,8 +158,9 @@ class basic_view<Entity, exclude_t<Exclude...>, Component...> {
         underlying_iterator it;
     };
 
-    basic_view(pool_type<Component> &... component, pool_type<Exclude> &... epool) ENTT_NOEXCEPT
-        : pools{&component..., &epool...}
+    basic_view(pool_type<Component> &... component, unpack_as_t<const sparse_set<Entity>, Exclude> &... epool) ENTT_NOEXCEPT
+        : pools{&component...},
+          filter{&epool...}
     {}
 
     [[nodiscard]] const sparse_set<Entity> & candidate() const ENTT_NOEXCEPT {
@@ -315,8 +316,7 @@ public:
      */
     [[nodiscard]] iterator begin() const {
         const auto &view = candidate();
-        const filter_type ignore{std::get<pool_type<Exclude> *>(pools)...};
-        return iterator{view, unchecked(view), ignore, view.begin()};
+        return iterator{view, unchecked(view), filter, view.begin()};
     }
 
     /**
@@ -336,8 +336,7 @@ public:
      */
     [[nodiscard]] iterator end() const {
         const auto &view = candidate();
-        const filter_type ignore{std::get<pool_type<Exclude> *>(pools)...};
-        return iterator{view, unchecked(view), ignore, view.end()};
+        return iterator{view, unchecked(view), filter, view.end()};
     }
 
     /**
@@ -368,8 +367,7 @@ public:
      */
     [[nodiscard]] iterator find(const entity_type entt) const {
         const auto &view = candidate();
-        const filter_type ignore{std::get<pool_type<Exclude> *>(pools)...};
-        iterator it{view, unchecked(view), ignore, view.find(entt)};
+        iterator it{view, unchecked(view), filter, view.find(entt)};
         return (it != end() && *it == entt) ? it : end();
     }
 
@@ -380,7 +378,7 @@ public:
      */
     [[nodiscard]] bool contains(const entity_type entt) const {
         return (std::get<pool_type<Component> *>(pools)->contains(entt) && ...)
-                && (!std::get<pool_type<Exclude> *>(pools)->contains(entt) && ...);
+                && std::none_of(filter.begin(), filter.end(), [entt](const auto *cpool) { return cpool->contains(entt); });
     }
 
     /**
@@ -465,7 +463,8 @@ public:
     }
 
 private:
-    const std::tuple<pool_type<Component> *..., pool_type<Exclude> *...> pools;
+    const std::tuple<pool_type<Component> *...> pools;
+    filter_type filter;
 };
 
 

+ 4 - 4
test/entt/entity/view.cpp

@@ -510,8 +510,6 @@ TEST(MultiComponentView, ExcludedComponents) {
     registry.emplace<int>(e1, 1);
     registry.emplace<char>(e1);
 
-    const auto view = registry.view<int>(entt::exclude<char>);
-
     const auto e2 = registry.create();
     registry.emplace<int>(e2, 2);
 
@@ -519,11 +517,13 @@ TEST(MultiComponentView, ExcludedComponents) {
     registry.emplace<int>(e3, 3);
     registry.emplace<char>(e3);
 
+    const auto view = std::as_const(registry).view<const int>(entt::exclude<char>);
+
     for(const auto entity: view) {
         ASSERT_TRUE(entity == e0 || entity == e2);
 
         if(entity == e0) {
-            ASSERT_EQ(view.get<int>(e0), 0);
+            ASSERT_EQ(view.get<const int>(e0), 0);
         } else if(entity == e2) {
             ASSERT_EQ(view.get(e2), 2);
         }
@@ -540,7 +540,7 @@ TEST(MultiComponentView, ExcludedComponents) {
         if(entity == e1) {
             ASSERT_EQ(view.get(e1), 1);
         } else if(entity == e3) {
-            ASSERT_EQ(view.get<int>(e3), 3);
+            ASSERT_EQ(view.get<const int>(e3), 3);
         }
     }
 }