From 81ae2bed25b40f9a17bd91676aee6a62813fe113 Mon Sep 17 00:00:00 2001 From: Michele Caini Date: Sat, 2 Oct 2021 16:26:44 +0200 Subject: [PATCH] resource: added resource_handle::resource_type --- src/entt/resource/handle.hpp | 42 +++++++++++++++++++----------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/entt/resource/handle.hpp b/src/entt/resource/handle.hpp index 24d5b73cd..4efea0166 100644 --- a/src/entt/resource/handle.hpp +++ b/src/entt/resource/handle.hpp @@ -30,6 +30,8 @@ class resource_handle { public: /*! @brief Unsigned integer type. */ using size_type = long; + /*! @brief Type of resources managed by a cache. */ + using resource_type = Resource; /*! @brief Default constructor. */ resource_handle() ENTT_NOEXCEPT = default; @@ -38,27 +40,27 @@ public: * @brief Creates a handle from a shared pointer, namely a resource. * @param res A pointer to a properly initialized resource. */ - resource_handle(std::shared_ptr res) ENTT_NOEXCEPT + resource_handle(std::shared_ptr res) ENTT_NOEXCEPT : resource{std::move(res)} {} /** * @brief Copy constructor. * @param other The instance to copy from. */ - resource_handle(const resource_handle &other) ENTT_NOEXCEPT = default; + resource_handle(const resource_handle &other) ENTT_NOEXCEPT = default; /** * @brief Move constructor. * @param other The instance to move from. */ - resource_handle(resource_handle &&other) ENTT_NOEXCEPT = default; + resource_handle(resource_handle &&other) ENTT_NOEXCEPT = default; /** * @brief Copy constructs a handle which shares ownership of the resource. * @tparam Other Type of resource managed by the received handle. * @param other The handle to copy from. */ - template && std::is_base_of_v>> + template && std::is_base_of_v>> resource_handle(const resource_handle &other) ENTT_NOEXCEPT : resource{other.resource} {} @@ -67,7 +69,7 @@ public: * @tparam Other Type of resource managed by the received handle. * @param other The handle to move from. */ - template && std::is_base_of_v>> + template && std::is_base_of_v>> resource_handle(resource_handle &&other) ENTT_NOEXCEPT : resource{std::move(other.resource)} {} @@ -76,14 +78,14 @@ public: * @param other The instance to copy from. * @return This resource handle. */ - resource_handle &operator=(const resource_handle &other) ENTT_NOEXCEPT = default; + resource_handle &operator=(const resource_handle &other) ENTT_NOEXCEPT = default; /** * @brief Move assignment operator. * @param other The instance to move from. * @return This resource handle. */ - resource_handle &operator=(resource_handle &&other) ENTT_NOEXCEPT = default; + resource_handle &operator=(resource_handle &&other) ENTT_NOEXCEPT = default; /** * @brief Copy assignment operator from foreign handle. @@ -92,7 +94,7 @@ public: * @return This resource handle. */ template - std::enable_if_t && std::is_base_of_v, resource_handle &> + std::enable_if_t && std::is_base_of_v, resource_handle &> operator=(const resource_handle &other) ENTT_NOEXCEPT { resource = other.resource; return *this; @@ -105,7 +107,7 @@ public: * @return This resource handle. */ template - std::enable_if_t && std::is_base_of_v, resource_handle &> + std::enable_if_t && std::is_base_of_v, resource_handle &> operator=(resource_handle &&other) ENTT_NOEXCEPT { resource = std::move(other.resource); return *this; @@ -119,33 +121,33 @@ public: * * @return A reference to the managed resource. */ - [[nodiscard]] const Resource &get() const ENTT_NOEXCEPT { + [[nodiscard]] const resource_type &get() const ENTT_NOEXCEPT { ENTT_ASSERT(static_cast(resource), "Invalid resource"); return *resource; } /*! @copydoc get */ - [[nodiscard]] Resource &get() ENTT_NOEXCEPT { - return const_cast(std::as_const(*this).get()); + [[nodiscard]] resource_type &get() ENTT_NOEXCEPT { + return const_cast(std::as_const(*this).get()); } /*! @copydoc get */ - [[nodiscard]] operator const Resource &() const ENTT_NOEXCEPT { + [[nodiscard]] operator const resource_type &() const ENTT_NOEXCEPT { return get(); } /*! @copydoc get */ - [[nodiscard]] operator Resource &() ENTT_NOEXCEPT { + [[nodiscard]] operator resource_type &() ENTT_NOEXCEPT { return get(); } /*! @copydoc get */ - [[nodiscard]] const Resource &operator*() const ENTT_NOEXCEPT { + [[nodiscard]] const resource_type &operator*() const ENTT_NOEXCEPT { return get(); } /*! @copydoc get */ - [[nodiscard]] Resource &operator*() ENTT_NOEXCEPT { + [[nodiscard]] resource_type &operator*() ENTT_NOEXCEPT { return get(); } @@ -158,13 +160,13 @@ public: * @return A pointer to the managed resource or `nullptr` if the handle * contains no resource at all. */ - [[nodiscard]] const Resource *operator->() const ENTT_NOEXCEPT { + [[nodiscard]] const resource_type *operator->() const ENTT_NOEXCEPT { return resource.get(); } /*! @copydoc operator-> */ - [[nodiscard]] Resource *operator->() ENTT_NOEXCEPT { - return const_cast(std::as_const(*this).operator->()); + [[nodiscard]] resource_type *operator->() ENTT_NOEXCEPT { + return resource.get(); } /** @@ -184,7 +186,7 @@ public: } private: - std::shared_ptr resource; + std::shared_ptr resource; }; } // namespace entt