Răsfoiți Sursa

mutable handle

Michele Caini 6 ani în urmă
părinte
comite
d4a64e93e0
3 a modificat fișierele cu 38 adăugiri și 21 ștergeri
  1. 1 0
      AUTHORS
  2. 20 20
      src/entt/resource/handle.hpp
  3. 17 1
      test/entt/resource/resource.cpp

+ 1 - 0
AUTHORS

@@ -26,6 +26,7 @@ Kerndog73
 Paolo-Oliverio
 pgruenbacher
 prowolf
+The5-1
 vblanco20-1
 willtunnels
 WizardIke

+ 20 - 20
src/entt/resource/handle.hpp

@@ -51,30 +51,25 @@ public:
         return *resource;
     }
 
-    /**
-     * @brief Casts a handle and gets a reference to the managed resource.
-     *
-     * @warning
-     * The behavior is undefined if the handle doesn't contain a resource.<br/>
-     * An assertion will abort the execution at runtime in debug mode if the
-     * handle is empty.
-     */
-    inline operator const Resource &() const ENTT_NOEXCEPT { return get(); }
+    /*! @copydoc get */
+    Resource & get() ENTT_NOEXCEPT {
+        return const_cast<Resource &>(std::as_const(*this).get());
+    }
 
-    /**
-     * @brief Dereferences a handle to obtain the managed resource.
-     *
-     * @warning
-     * The behavior is undefined if the handle doesn't contain a resource.<br/>
-     * An assertion will abort the execution at runtime in debug mode if the
-     * handle is empty.
-     *
-     * @return A reference to the managed resource.
-     */
+    /*! @copydoc get */
+    inline operator const Resource & () const ENTT_NOEXCEPT { return get(); }
+
+    /*! @copydoc get */
+    inline operator Resource & () ENTT_NOEXCEPT { return get(); }
+
+    /*! @copydoc get */
     inline const Resource & operator *() const ENTT_NOEXCEPT { return get(); }
 
+    /*! @copydoc get */
+    inline Resource & operator *() ENTT_NOEXCEPT { return get(); }
+
     /**
-     * @brief Gets a pointer to the managed resource from a handle.
+     * @brief Gets a pointer to the managed resource.
      *
      * @warning
      * The behavior is undefined if the handle doesn't contain a resource.<br/>
@@ -89,6 +84,11 @@ public:
         return resource.get();
     }
 
+    /*! @copydoc operator-> */
+    inline Resource * operator->() ENTT_NOEXCEPT {
+        return const_cast<Resource *>(std::as_const(*this).operator->());
+    }
+
     /**
      * @brief Returns true if a handle contains a resource, false otherwise.
      * @return True if the handle contains a resource, false otherwise.

+ 17 - 1
test/entt/resource/resource.cpp

@@ -2,7 +2,7 @@
 #include <gtest/gtest.h>
 #include <entt/resource/cache.hpp>
 
-struct resource { const int value; };
+struct resource { int value; };
 
 struct loader: entt::resource_loader<loader, resource> {
     std::shared_ptr<resource> load(int value) const {
@@ -91,3 +91,19 @@ TEST(Resource, Functionalities) {
     ASSERT_TRUE(std::is_copy_assignable_v<entt::resource_handle<resource>>);
     ASSERT_TRUE(std::is_move_assignable_v<entt::resource_handle<resource>>);
 }
+
+TEST(Resource, MutableHandle) {
+    entt::resource_cache<resource> cache;
+
+    constexpr auto hs = entt::hashed_string{"res"};
+    auto handle = cache.load<loader>(hs, 0);
+
+    ASSERT_TRUE(handle);
+
+    ++handle.get().value;
+    ++static_cast<resource &>(handle).value;
+    ++(*handle).value;
+    ++handle->value;
+
+    ASSERT_EQ(cache.handle(hs)->value, 4);
+}