diff --git a/AUTHORS b/AUTHORS index 26a9eaa5f..63c8b7c01 100644 --- a/AUTHORS +++ b/AUTHORS @@ -26,6 +26,7 @@ Kerndog73 Paolo-Oliverio pgruenbacher prowolf +The5-1 vblanco20-1 willtunnels WizardIke diff --git a/src/entt/resource/handle.hpp b/src/entt/resource/handle.hpp index 78017a461..a19d9f695 100644 --- a/src/entt/resource/handle.hpp +++ b/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.
- * 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(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.
- * 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.
@@ -89,6 +84,11 @@ public: return resource.get(); } + /*! @copydoc operator-> */ + inline Resource * operator->() ENTT_NOEXCEPT { + return const_cast(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. diff --git a/test/entt/resource/resource.cpp b/test/entt/resource/resource.cpp index cf6d9166a..ee26b7d65 100644 --- a/test/entt/resource/resource.cpp +++ b/test/entt/resource/resource.cpp @@ -2,7 +2,7 @@ #include #include -struct resource { const int value; }; +struct resource { int value; }; struct loader: entt::resource_loader { std::shared_ptr load(int value) const { @@ -91,3 +91,19 @@ TEST(Resource, Functionalities) { ASSERT_TRUE(std::is_copy_assignable_v>); ASSERT_TRUE(std::is_move_assignable_v>); } + +TEST(Resource, MutableHandle) { + entt::resource_cache cache; + + constexpr auto hs = entt::hashed_string{"res"}; + auto handle = cache.load(hs, 0); + + ASSERT_TRUE(handle); + + ++handle.get().value; + ++static_cast(handle).value; + ++(*handle).value; + ++handle->value; + + ASSERT_EQ(cache.handle(hs)->value, 4); +}