From a05e7a84c45e176f26f4cee1d1cc2a8740c03bf8 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Sat, 2 Oct 2021 23:52:22 +0200 Subject: [PATCH] resource: handle as value type, const correctness review --- src/entt/resource/handle.hpp | 29 ++++------------------------- test/entt/resource/resource.cpp | 20 ++++++++++++++++++++ 2 files changed, 24 insertions(+), 25 deletions(-) diff --git a/src/entt/resource/handle.hpp b/src/entt/resource/handle.hpp index 4efea0166..77a8f65f2 100644 --- a/src/entt/resource/handle.hpp +++ b/src/entt/resource/handle.hpp @@ -121,33 +121,17 @@ public: * * @return A reference to the managed resource. */ - [[nodiscard]] const resource_type &get() const ENTT_NOEXCEPT { - ENTT_ASSERT(static_cast(resource), "Invalid resource"); + [[nodiscard]] resource_type &get() const ENTT_NOEXCEPT { return *resource; } /*! @copydoc get */ - [[nodiscard]] resource_type &get() ENTT_NOEXCEPT { - return const_cast(std::as_const(*this).get()); - } - - /*! @copydoc get */ - [[nodiscard]] operator const resource_type &() const ENTT_NOEXCEPT { + [[nodiscard]] operator resource_type &() const ENTT_NOEXCEPT { return get(); } /*! @copydoc get */ - [[nodiscard]] operator resource_type &() ENTT_NOEXCEPT { - return 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(); } @@ -160,12 +144,7 @@ public: * @return A pointer to the managed resource or `nullptr` if the handle * 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(); } diff --git a/test/entt/resource/resource.cpp b/test/entt/resource/resource.cpp index ed2561c47..281abd787 100644 --- a/test/entt/resource/resource.cpp +++ b/test/entt/resource/resource.cpp @@ -113,6 +113,26 @@ TEST(Resource, Functionalities) { ASSERT_TRUE(std::is_move_assignable_v>); } +TEST(Resource, ConstNonConstHandle) { + entt::resource_cache cache; + + entt::resource_handle handle = cache.temp>(42); + entt::resource_handle chandle = handle; + + static_assert(std::is_same_v); + static_assert(std::is_same_v); + static_assert(std::is_same_v); + + 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) { entt::resource_cache cache;