Просмотр исходного кода

resource: handle as value type, const correctness review

Michele Caini 4 лет назад
Родитель
Сommit
a05e7a84c4
2 измененных файлов с 24 добавлено и 25 удалено
  1. 4 25
      src/entt/resource/handle.hpp
  2. 20 0
      test/entt/resource/resource.cpp

+ 4 - 25
src/entt/resource/handle.hpp

@@ -121,33 +121,17 @@ public:
      *
      *
      * @return A reference to the managed resource.
      * @return A reference to the managed resource.
      */
      */
-    [[nodiscard]] const resource_type &get() const ENTT_NOEXCEPT {
-        ENTT_ASSERT(static_cast<bool>(resource), "Invalid resource");
+    [[nodiscard]] resource_type &get() const ENTT_NOEXCEPT {
         return *resource;
         return *resource;
     }
     }
 
 
     /*! @copydoc get */
     /*! @copydoc get */
-    [[nodiscard]] resource_type &get() ENTT_NOEXCEPT {
-        return const_cast<resource_type &>(std::as_const(*this).get());
-    }
-
-    /*! @copydoc get */
-    [[nodiscard]] operator const resource_type &() const ENTT_NOEXCEPT {
-        return get();
-    }
-
-    /*! @copydoc get */
-    [[nodiscard]] operator resource_type &() ENTT_NOEXCEPT {
+    [[nodiscard]] operator resource_type &() const ENTT_NOEXCEPT {
         return get();
         return get();
     }
     }
 
 
     /*! @copydoc get */
     /*! @copydoc get */
-    [[nodiscard]] const resource_type &operator*() const ENTT_NOEXCEPT {
-        return get();
-    }
-
-    /*! @copydoc get */
-    [[nodiscard]] resource_type &operator*() ENTT_NOEXCEPT {
+    [[nodiscard]] resource_type &operator*() const ENTT_NOEXCEPT {
         return get();
         return get();
     }
     }
 
 
@@ -160,12 +144,7 @@ public:
      * @return A pointer to the managed resource or `nullptr` if the handle
      * @return A pointer to the managed resource or `nullptr` if the handle
      * contains no resource at all.
      * contains no resource at all.
      */
      */
-    [[nodiscard]] const resource_type *operator->() const ENTT_NOEXCEPT {
-        return resource.get();
-    }
-
-    /*! @copydoc operator-> */
-    [[nodiscard]] resource_type *operator->() ENTT_NOEXCEPT {
+    [[nodiscard]] resource_type *operator->() const ENTT_NOEXCEPT {
         return resource.get();
         return resource.get();
     }
     }
 
 

+ 20 - 0
test/entt/resource/resource.cpp

@@ -113,6 +113,26 @@ TEST(Resource, Functionalities) {
     ASSERT_TRUE(std::is_move_assignable_v<entt::resource_handle<resource>>);
     ASSERT_TRUE(std::is_move_assignable_v<entt::resource_handle<resource>>);
 }
 }
 
 
+TEST(Resource, ConstNonConstHandle) {
+    entt::resource_cache<resource> cache;
+
+    entt::resource_handle<resource> handle = cache.temp<loader<resource>>(42);
+    entt::resource_handle<const resource> chandle = handle;
+
+    static_assert(std::is_same_v<decltype(handle.get()), resource &>);
+    static_assert(std::is_same_v<decltype(chandle.get()), const resource &>);
+    static_assert(std::is_same_v<decltype(std::as_const(handle).get()), resource &>);
+
+    ASSERT_TRUE(chandle);
+    ASSERT_EQ(handle.use_count(), 2u);
+    ASSERT_EQ(chandle->value, 42);
+
+    chandle = {};
+
+    ASSERT_FALSE(chandle);
+    ASSERT_EQ(handle.use_count(), 1u);
+}
+
 TEST(Resource, MutableHandle) {
 TEST(Resource, MutableHandle) {
     entt::resource_cache<resource> cache;
     entt::resource_cache<resource> cache;