EnTT 3.13.0
Loading...
Searching...
No Matches
resource.hpp
1#ifndef ENTT_RESOURCE_RESOURCE_HPP
2#define ENTT_RESOURCE_RESOURCE_HPP
3
4#include <memory>
5#include <type_traits>
6#include <utility>
7#include "fwd.hpp"
8
9namespace entt {
10
21template<typename Type>
22class resource {
23 template<typename>
24 friend class resource;
25
26 template<typename Other>
27 static constexpr bool is_acceptable_v = !std::is_same_v<Type, Other> && std::is_constructible_v<Type &, Other &>;
28
29public:
31 using element_type = Type;
33 using handle_type = std::shared_ptr<element_type>;
34
36 resource() noexcept
37 : value{} {}
38
43 explicit resource(handle_type res) noexcept
44 : value{std::move(res)} {}
45
47 resource(const resource &) noexcept = default;
48
50 resource(resource &&) noexcept = default;
51
58 template<typename Other>
59 resource(const resource<Other> &other, element_type &res) noexcept
60 : value{other.value, std::addressof(res)} {}
61
67 template<typename Other, typename = std::enable_if_t<is_acceptable_v<Other>>>
68 resource(const resource<Other> &other) noexcept
69 : value{other.value} {}
70
76 template<typename Other, typename = std::enable_if_t<is_acceptable_v<Other>>>
77 resource(resource<Other> &&other) noexcept
78 : value{std::move(other.value)} {}
79
84 resource &operator=(const resource &) noexcept = default;
85
90 resource &operator=(resource &&) noexcept = default;
91
98 template<typename Other>
99 std::enable_if_t<is_acceptable_v<Other>, resource &>
100 operator=(const resource<Other> &other) noexcept {
101 value = other.value;
102 return *this;
103 }
104
111 template<typename Other>
112 std::enable_if_t<is_acceptable_v<Other>, resource &>
113 operator=(resource<Other> &&other) noexcept {
114 value = std::move(other.value);
115 return *this;
116 }
117
126 [[nodiscard]] element_type &operator*() const noexcept {
127 return *value;
128 }
129
131 [[nodiscard]] operator element_type &() const noexcept {
132 return *value;
133 }
134
139 [[nodiscard]] element_type *operator->() const noexcept {
140 return value.get();
141 }
142
147 [[nodiscard]] explicit operator bool() const noexcept {
148 return static_cast<bool>(value);
149 }
150
155 [[nodiscard]] const handle_type &handle() const noexcept {
156 return value;
157 }
158
159private:
160 handle_type value;
161};
162
171template<typename Lhs, typename Rhs>
172[[nodiscard]] bool operator==(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
173 return (std::addressof(*lhs) == std::addressof(*rhs));
174}
175
184template<typename Lhs, typename Rhs>
185[[nodiscard]] bool operator!=(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
186 return !(lhs == rhs);
187}
188
197template<typename Lhs, typename Rhs>
198[[nodiscard]] bool operator<(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
199 return (std::addressof(*lhs) < std::addressof(*rhs));
200}
201
210template<typename Lhs, typename Rhs>
211[[nodiscard]] bool operator>(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
212 return rhs < lhs;
213}
214
224template<typename Lhs, typename Rhs>
225[[nodiscard]] bool operator<=(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
226 return !(lhs > rhs);
227}
228
238template<typename Lhs, typename Rhs>
239[[nodiscard]] bool operator>=(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
240 return !(lhs < rhs);
241}
242
243} // namespace entt
244
245#endif
Basic resource handle.
Definition resource.hpp:22
resource(const resource< Other > &other) noexcept
Copy constructs a handle which shares ownership of the resource.
Definition resource.hpp:68
Type element_type
Resource type.
Definition resource.hpp:31
resource(const resource &) noexcept=default
Default copy constructor.
element_type & operator*() const noexcept
Returns a reference to the managed resource.
Definition resource.hpp:126
const handle_type & handle() const noexcept
Returns the underlying resource handle.
Definition resource.hpp:155
resource & operator=(const resource &) noexcept=default
Default copy assignment operator.
std::enable_if_t< is_acceptable_v< Other >, resource & > operator=(resource< Other > &&other) noexcept
Move assignment operator from foreign handle.
Definition resource.hpp:113
element_type * operator->() const noexcept
Returns a pointer to the managed resource.
Definition resource.hpp:139
resource & operator=(resource &&) noexcept=default
Default move assignment operator.
resource(handle_type res) noexcept
Creates a handle from a weak pointer, namely a resource.
Definition resource.hpp:43
resource() noexcept
Default constructor.
Definition resource.hpp:36
std::shared_ptr< element_type > handle_type
Handle type.
Definition resource.hpp:33
resource(resource< Other > &&other) noexcept
Move constructs a handle which takes ownership of the resource.
Definition resource.hpp:77
resource(resource &&) noexcept=default
Default move constructor.
EnTT default namespace.
Definition dense_map.hpp:21
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.