Kaynağa Gözat

view: ::handle returns a pointer rather than a reference

Michele Caini 2 yıl önce
ebeveyn
işleme
a20829e700
3 değiştirilmiş dosya ile 40 ekleme ve 47 silme
  1. 1 1
      TODO
  2. 6 8
      src/entt/entity/view.hpp
  3. 33 38
      test/entt/entity/view.cpp

+ 1 - 1
TODO

@@ -15,7 +15,7 @@ TODO (high prio):
 * doc: exclude only views, bump entities, signals on entity creation/destruction
 * registry: replace destroy with a drop-all method that doesn't care about validity
 * resource cache: avoid using shared ptr with loader and the others
-* make view::handle return a pointer (similar to ::storage)
+* rework view tests with typed tests and whatnot
 
 WIP:
 * get rid of observers, storage based views made them pointless - document alternatives

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

@@ -313,12 +313,11 @@ public:
     }
 
     /**
-     * @brief Returns the leading storage of a view.
+     * @brief Returns the leading storage of a view, if any.
      * @return The leading storage of the view.
      */
-    [[nodiscard]] const common_type &handle() const noexcept {
-        ENTT_ASSERT(view != nullptr, "Handle not available");
-        return *view;
+    [[nodiscard]] const common_type *handle() const noexcept {
+        return view;
     }
 
     /**
@@ -623,12 +622,11 @@ public:
         : basic_view{std::get<0>(value)} {}
 
     /**
-     * @brief Returns the leading storage of a view.
+     * @brief Returns the leading storage of a view, if any.
      * @return The leading storage of the view.
      */
-    [[nodiscard]] const common_type &handle() const noexcept {
-        ENTT_ASSERT(view != nullptr, "Handle not available");
-        return *view;
+    [[nodiscard]] const common_type *handle() const noexcept {
+        return view;
     }
 
     /**

+ 33 - 38
test/entt/entity/view.cpp

@@ -6,7 +6,6 @@
 #include <gtest/gtest.h>
 #include <entt/entity/registry.hpp>
 #include <entt/entity/view.hpp>
-#include "../common/config.h"
 
 struct empty_type {};
 
@@ -101,12 +100,6 @@ TEST(SingleComponentView, InvalidView) {
     ASSERT_TRUE(view);
 }
 
-ENTT_DEBUG_TEST(SingleComponentViewDeathTest, InvalidView) {
-    entt::view<entt::get_t<int>> view{};
-
-    ASSERT_DEATH([[maybe_unused]] auto &&handle = view.handle(), "");
-}
-
 TEST(SingleComponentView, Constructors) {
     entt::storage<int> storage{};
 
@@ -118,7 +111,8 @@ TEST(SingleComponentView, Constructors) {
     ASSERT_TRUE(from_storage);
     ASSERT_TRUE(from_tuple);
 
-    ASSERT_EQ(&from_storage.handle(), &from_tuple.handle());
+    ASSERT_NE(from_storage.handle(), nullptr);
+    ASSERT_EQ(from_storage.handle(), from_tuple.handle());
 }
 
 TEST(SingleComponentView, Handle) {
@@ -126,17 +120,19 @@ TEST(SingleComponentView, Handle) {
     const auto entity = registry.create();
 
     auto view = registry.view<int>();
-    auto &&handle = view.handle();
+    auto *handle = view.handle();
 
-    ASSERT_TRUE(handle.empty());
-    ASSERT_FALSE(handle.contains(entity));
-    ASSERT_EQ(&handle, &view.handle());
+    ASSERT_NE(handle, nullptr);
+
+    ASSERT_TRUE(handle->empty());
+    ASSERT_FALSE(handle->contains(entity));
+    ASSERT_EQ(handle, view.handle());
 
     registry.emplace<int>(entity);
 
-    ASSERT_FALSE(handle.empty());
-    ASSERT_TRUE(handle.contains(entity));
-    ASSERT_EQ(&handle, &view.handle());
+    ASSERT_FALSE(handle->empty());
+    ASSERT_TRUE(handle->contains(entity));
+    ASSERT_EQ(handle, view.handle());
 }
 
 TEST(SingleComponentView, LazyTypeFromConstRegistry) {
@@ -639,12 +635,6 @@ TEST(MultiComponentView, InvalidView) {
     ASSERT_TRUE(view);
 }
 
-ENTT_DEBUG_TEST(MultiComponentViewDeathTest, InvalidView) {
-    entt::view<entt::get_t<int>, entt::exclude_t<char>> view{};
-
-    ASSERT_DEATH([[maybe_unused]] auto &&handle = view.handle(), "");
-}
-
 TEST(MultiComponentView, Constructors) {
     entt::storage<int> storage{};
 
@@ -656,7 +646,8 @@ TEST(MultiComponentView, Constructors) {
     ASSERT_TRUE(from_storage);
     ASSERT_TRUE(from_tuple);
 
-    ASSERT_EQ(&from_storage.handle(), &from_tuple.handle());
+    ASSERT_NE(from_storage.handle(), nullptr);
+    ASSERT_EQ(from_storage.handle(), from_tuple.handle());
 }
 
 TEST(MultiComponentView, Handle) {
@@ -664,30 +655,34 @@ TEST(MultiComponentView, Handle) {
     const auto entity = registry.create();
 
     auto view = registry.view<int, char>();
-    auto &&handle = view.handle();
+    auto *handle = view.handle();
 
-    ASSERT_TRUE(handle.empty());
-    ASSERT_FALSE(handle.contains(entity));
-    ASSERT_EQ(&handle, &view.handle());
+    ASSERT_NE(handle, nullptr);
+
+    ASSERT_TRUE(handle->empty());
+    ASSERT_FALSE(handle->contains(entity));
+    ASSERT_EQ(handle, view.handle());
 
     registry.emplace<int>(entity);
 
-    ASSERT_FALSE(handle.empty());
-    ASSERT_TRUE(handle.contains(entity));
-    ASSERT_EQ(&handle, &view.handle());
+    ASSERT_FALSE(handle->empty());
+    ASSERT_TRUE(handle->contains(entity));
+    ASSERT_EQ(handle, view.handle());
 
     view.refresh();
-    auto &&other = view.handle();
+    auto *other = view.handle();
+
+    ASSERT_NE(other, nullptr);
 
-    ASSERT_TRUE(other.empty());
-    ASSERT_FALSE(other.contains(entity));
-    ASSERT_EQ(&other, &view.handle());
-    ASSERT_NE(&handle, &other);
+    ASSERT_TRUE(other->empty());
+    ASSERT_FALSE(other->contains(entity));
+    ASSERT_EQ(other, view.handle());
+    ASSERT_NE(handle, other);
 
     view.use<int>();
 
-    ASSERT_NE(&other, &view.handle());
-    ASSERT_EQ(&handle, &view.handle());
+    ASSERT_NE(other, view.handle());
+    ASSERT_EQ(handle, view.handle());
 }
 
 TEST(MultiComponentView, LazyTypesFromConstRegistry) {
@@ -1352,11 +1347,11 @@ TEST(MultiComponentView, SameComponentTypes) {
         ASSERT_EQ(second, 9);
     }
 
-    ASSERT_EQ(&view.handle(), &storage);
+    ASSERT_EQ(view.handle(), &storage);
 
     view.use<1u>();
 
-    ASSERT_EQ(&view.handle(), &other);
+    ASSERT_EQ(view.handle(), &other);
 }
 
 TEST(MultiComponentView, Storage) {