Explorar o código

view: make operator bool work properly with partially initialized views

Michele Caini %!s(int64=2) %!d(string=hai) anos
pai
achega
736ef35805
Modificáronse 2 ficheiros con 34 adicións e 6 borrados
  1. 7 6
      src/entt/entity/view.hpp
  2. 27 0
      test/entt/entity/view.cpp

+ 7 - 6
src/entt/entity/view.hpp

@@ -449,11 +449,12 @@ public:
     }
     }
 
 
     /**
     /**
-     * @brief Checks if a view is properly initialized.
-     * @return True if the view is properly initialized, false otherwise.
+     * @brief Checks if a view is fully initialized.
+     * @return True if the view is fully initialized, false otherwise.
      */
      */
     [[nodiscard]] explicit operator bool() const noexcept {
     [[nodiscard]] explicit operator bool() const noexcept {
-        return view != nullptr;
+        return std::apply([](const auto *...curr) { return ((curr != nullptr) && ...); }, pools)
+               && std::apply([](const auto *...curr) { return ((curr != nullptr) && ...); }, filter);
     }
     }
 
 
     /**
     /**
@@ -784,11 +785,11 @@ public:
     }
     }
 
 
     /**
     /**
-     * @brief Checks if a view is properly initialized.
-     * @return True if the view is properly initialized, false otherwise.
+     * @brief Checks if a view is fully initialized.
+     * @return True if the view is fully initialized, false otherwise.
      */
      */
     [[nodiscard]] explicit operator bool() const noexcept {
     [[nodiscard]] explicit operator bool() const noexcept {
-        return view != nullptr;
+        return (std::get<0>(pools) != nullptr);
     }
     }
 
 
     /**
     /**

+ 27 - 0
test/entt/entity/view.cpp

@@ -71,6 +71,17 @@ TEST(SingleComponentView, Functionalities) {
     ASSERT_FALSE(invalid);
     ASSERT_FALSE(invalid);
 }
 }
 
 
+TEST(SingleComponentView, InvalidView) {
+    entt::basic_view<entt::get_t<entt::storage<int>>, entt::exclude_t<>> view{};
+
+    ASSERT_FALSE(view);
+
+    entt::storage<int> storage;
+    view.storage(storage);
+
+    ASSERT_TRUE(view);
+}
+
 ENTT_DEBUG_TEST(SingleComponentViewDeathTest, InvalidView) {
 ENTT_DEBUG_TEST(SingleComponentViewDeathTest, InvalidView) {
     entt::view<entt::get_t<int>> view{};
     entt::view<entt::get_t<int>> view{};
 
 
@@ -593,6 +604,22 @@ TEST(MultiComponentView, Functionalities) {
     ASSERT_FALSE(invalid);
     ASSERT_FALSE(invalid);
 }
 }
 
 
+TEST(MultiComponentView, InvalidView) {
+    entt::basic_view<entt::get_t<entt::storage<int>>, entt::exclude_t<entt::storage<char>>> view{};
+
+    ASSERT_FALSE(view);
+
+    entt::storage<int> storage;
+    view.storage(storage);
+
+    ASSERT_FALSE(view);
+
+    entt::storage<char> other;
+    view.storage(other);
+
+    ASSERT_TRUE(view);
+}
+
 ENTT_DEBUG_TEST(MultiComponentViewDeathTest, InvalidView) {
 ENTT_DEBUG_TEST(MultiComponentViewDeathTest, InvalidView) {
     entt::view<entt::get_t<int>, entt::exclude_t<char>> view{};
     entt::view<entt::get_t<int>, entt::exclude_t<char>> view{};