resource: added resource_handle::resource_type

This commit is contained in:
Michele Caini
2021-10-02 16:26:44 +02:00
parent 0ab71bc2e7
commit 81ae2bed25

View File

@@ -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<Resource> res) ENTT_NOEXCEPT
resource_handle(std::shared_ptr<resource_type> res) ENTT_NOEXCEPT
: resource{std::move(res)} {}
/**
* @brief Copy constructor.
* @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.
* @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.
* @tparam Other Type of resource managed by the received handle.
* @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{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<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{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<Resource> &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<Resource> &&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<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 {
resource = other.resource;
return *this;
@@ -105,7 +107,7 @@ public:
* @return This resource handle.
*/
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 {
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<bool>(resource), "Invalid resource");
return *resource;
}
/*! @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 */
[[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<Resource *>(std::as_const(*this).operator->());
[[nodiscard]] resource_type *operator->() ENTT_NOEXCEPT {
return resource.get();
}
/**
@@ -184,7 +186,7 @@ public:
}
private:
std::shared_ptr<Resource> resource;
std::shared_ptr<resource_type> resource;
};
} // namespace entt