Browse Source

resource: added resource_handle::resource_type

Michele Caini 4 years ago
parent
commit
81ae2bed25
1 changed files with 22 additions and 20 deletions
  1. 22 20
      src/entt/resource/handle.hpp

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

@@ -30,6 +30,8 @@ class resource_handle {
 public:
 public:
     /*! @brief Unsigned integer type. */
     /*! @brief Unsigned integer type. */
     using size_type = long;
     using size_type = long;
+    /*! @brief Type of resources managed by a cache. */
+    using resource_type = Resource;
 
 
     /*! @brief Default constructor. */
     /*! @brief Default constructor. */
     resource_handle() ENTT_NOEXCEPT = default;
     resource_handle() ENTT_NOEXCEPT = default;
@@ -38,27 +40,27 @@ public:
      * @brief Creates a handle from a shared pointer, namely a resource.
      * @brief Creates a handle from a shared pointer, namely a resource.
      * @param res A pointer to a properly initialized resource.
      * @param res A pointer to a properly initialized resource.
      */
      */
-    resource_handle(std::shared_ptr<Resource> res) ENTT_NOEXCEPT
+    resource_handle(std::shared_ptr<resource_type> res) ENTT_NOEXCEPT
         : resource{std::move(res)} {}
         : resource{std::move(res)} {}
 
 
     /**
     /**
      * @brief Copy constructor.
      * @brief Copy constructor.
      * @param other The instance to copy from.
      * @param other The instance to copy from.
      */
      */
-    resource_handle(const resource_handle<Resource> &other) ENTT_NOEXCEPT = default;
+    resource_handle(const resource_handle &other) ENTT_NOEXCEPT = default;
 
 
     /**
     /**
      * @brief Move constructor.
      * @brief Move constructor.
      * @param other The instance to move from.
      * @param other The instance to move from.
      */
      */
-    resource_handle(resource_handle<Resource> &&other) ENTT_NOEXCEPT = default;
+    resource_handle(resource_handle &&other) ENTT_NOEXCEPT = default;
 
 
     /**
     /**
      * @brief Copy constructs a handle which shares ownership of the resource.
      * @brief Copy constructs a handle which shares ownership of the resource.
      * @tparam Other Type of resource managed by the received handle.
      * @tparam Other Type of resource managed by the received handle.
      * @param other The handle to copy from.
      * @param other The handle to copy from.
      */
      */
-    template<typename Other, typename = std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>>>
+    template<typename Other, typename = std::enable_if_t<!std::is_same_v<resource_type, Other> && std::is_base_of_v<resource_type, Other>>>
     resource_handle(const resource_handle<Other> &other) ENTT_NOEXCEPT
     resource_handle(const resource_handle<Other> &other) ENTT_NOEXCEPT
         : resource{other.resource} {}
         : resource{other.resource} {}
 
 
@@ -67,7 +69,7 @@ public:
      * @tparam Other Type of resource managed by the received handle.
      * @tparam Other Type of resource managed by the received handle.
      * @param other The handle to move from.
      * @param other The handle to move from.
      */
      */
-    template<typename Other, typename = std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>>>
+    template<typename Other, typename = std::enable_if_t<!std::is_same_v<resource_type, Other> && std::is_base_of_v<resource_type, Other>>>
     resource_handle(resource_handle<Other> &&other) ENTT_NOEXCEPT
     resource_handle(resource_handle<Other> &&other) ENTT_NOEXCEPT
         : resource{std::move(other.resource)} {}
         : resource{std::move(other.resource)} {}
 
 
@@ -76,14 +78,14 @@ public:
      * @param other The instance to copy from.
      * @param other The instance to copy from.
      * @return This resource handle.
      * @return This resource handle.
      */
      */
-    resource_handle &operator=(const resource_handle<Resource> &other) ENTT_NOEXCEPT = default;
+    resource_handle &operator=(const resource_handle &other) ENTT_NOEXCEPT = default;
 
 
     /**
     /**
      * @brief Move assignment operator.
      * @brief Move assignment operator.
      * @param other The instance to move from.
      * @param other The instance to move from.
      * @return This resource handle.
      * @return This resource handle.
      */
      */
-    resource_handle &operator=(resource_handle<Resource> &&other) ENTT_NOEXCEPT = default;
+    resource_handle &operator=(resource_handle &&other) ENTT_NOEXCEPT = default;
 
 
     /**
     /**
      * @brief Copy assignment operator from foreign handle.
      * @brief Copy assignment operator from foreign handle.
@@ -92,7 +94,7 @@ public:
      * @return This resource handle.
      * @return This resource handle.
      */
      */
     template<typename Other>
     template<typename Other>
-    std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>, resource_handle &>
+    std::enable_if_t<!std::is_same_v<resource_type, Other> && std::is_base_of_v<resource_type, Other>, resource_handle &>
     operator=(const resource_handle<Other> &other) ENTT_NOEXCEPT {
     operator=(const resource_handle<Other> &other) ENTT_NOEXCEPT {
         resource = other.resource;
         resource = other.resource;
         return *this;
         return *this;
@@ -105,7 +107,7 @@ public:
      * @return This resource handle.
      * @return This resource handle.
      */
      */
     template<typename Other>
     template<typename Other>
-    std::enable_if_t<!std::is_same_v<Other, Resource> && std::is_base_of_v<Resource, Other>, resource_handle &>
+    std::enable_if_t<!std::is_same_v<resource_type, Other> && std::is_base_of_v<resource_type, Other>, resource_handle &>
     operator=(resource_handle<Other> &&other) ENTT_NOEXCEPT {
     operator=(resource_handle<Other> &&other) ENTT_NOEXCEPT {
         resource = std::move(other.resource);
         resource = std::move(other.resource);
         return *this;
         return *this;
@@ -119,33 +121,33 @@ public:
      *
      *
      * @return A reference to the managed resource.
      * @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<bool>(resource), "Invalid resource");
         ENTT_ASSERT(static_cast<bool>(resource), "Invalid resource");
         return *resource;
         return *resource;
     }
     }
 
 
     /*! @copydoc get */
     /*! @copydoc get */
-    [[nodiscard]] Resource &get() ENTT_NOEXCEPT {
-        return const_cast<Resource &>(std::as_const(*this).get());
+    [[nodiscard]] resource_type &get() ENTT_NOEXCEPT {
+        return const_cast<resource_type &>(std::as_const(*this).get());
     }
     }
 
 
     /*! @copydoc get */
     /*! @copydoc get */
-    [[nodiscard]] operator const Resource &() const ENTT_NOEXCEPT {
+    [[nodiscard]] operator const resource_type &() const ENTT_NOEXCEPT {
         return get();
         return get();
     }
     }
 
 
     /*! @copydoc get */
     /*! @copydoc get */
-    [[nodiscard]] operator Resource &() ENTT_NOEXCEPT {
+    [[nodiscard]] operator resource_type &() ENTT_NOEXCEPT {
         return get();
         return get();
     }
     }
 
 
     /*! @copydoc get */
     /*! @copydoc get */
-    [[nodiscard]] const Resource &operator*() const ENTT_NOEXCEPT {
+    [[nodiscard]] const resource_type &operator*() const ENTT_NOEXCEPT {
         return get();
         return get();
     }
     }
 
 
     /*! @copydoc get */
     /*! @copydoc get */
-    [[nodiscard]] Resource &operator*() ENTT_NOEXCEPT {
+    [[nodiscard]] resource_type &operator*() ENTT_NOEXCEPT {
         return get();
         return get();
     }
     }
 
 
@@ -158,13 +160,13 @@ 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 *operator->() const ENTT_NOEXCEPT {
+    [[nodiscard]] const resource_type *operator->() const ENTT_NOEXCEPT {
         return resource.get();
         return resource.get();
     }
     }
 
 
     /*! @copydoc operator-> */
     /*! @copydoc operator-> */
-    [[nodiscard]] Resource *operator->() ENTT_NOEXCEPT {
-        return const_cast<Resource *>(std::as_const(*this).operator->());
+    [[nodiscard]] resource_type *operator->() ENTT_NOEXCEPT {
+        return resource.get();
     }
     }
 
 
     /**
     /**
@@ -184,7 +186,7 @@ public:
     }
     }
 
 
 private:
 private:
-    std::shared_ptr<Resource> resource;
+    std::shared_ptr<resource_type> resource;
 };
 };
 
 
 } // namespace entt
 } // namespace entt