EnTT: src/entt/resource/resource.hpp Source File

EnTT 3.15.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>
22 class resource {
23 template<typename>
24 friend class resource;
25
26 template<typename Other>
27 static constexpr bool is_acceptable = !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<Other>>>
68 resource(const resource<Other> &other) noexcept
69 : value{other.value} {}
70
76 template<typename Other, typename = std::enable_if_t<is_acceptable<Other>>>
77 resource(resource<Other> &&other) noexcept
78 : value{std::move(other.value)} {}
79
81 ~resource() = default;
82
87 resource &operator=(const resource &) noexcept = default;
88
93 resource &operator=(resource &&) noexcept = default;
94
101 template<typename Other, typename = std::enable_if_t<is_acceptable<Other>>>
102 resource &operator=(const resource<Other> &other) noexcept {
103 value = other.value;
104 return *this;
105 }
106
113 template<typename Other, typename = std::enable_if_t<is_acceptable<Other>>>
114 resource &operator=(resource<Other> &&other) noexcept {
115 value = std::move(other.value);
116 return *this;
117 }
118
123 void swap(resource &other) noexcept {
124 using std::swap;
125 swap(value, other.value);
126 }
127
136 [[nodiscard]] element_type &operator*() const noexcept {
137 return *value;
138 }
139
141 [[nodiscard]] operator element_type &() const noexcept {
142 return *value;
143 }
144
149 [[nodiscard]] element_type *operator->() const noexcept {
150 return value.get();
151 }
152
157 [[nodiscard]] explicit operator bool() const noexcept {
158 return static_cast<bool>(value);
159 }
160
162 void reset() {
163 value.reset();
164 }
165
170 void reset(handle_type other) {
171 value = std::move(other);
172 }
173
178 [[nodiscard]] const handle_type &handle() const noexcept {
179 return value;
180 }
181
182private:
183 handle_type value;
184};
185
194template<typename Lhs, typename Rhs>
195[[nodiscard]] bool operator==(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
196 return (std::addressof(*lhs) == std::addressof(*rhs));
197}
198
207template<typename Lhs, typename Rhs>
208[[nodiscard]] bool operator!=(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
209 return !(lhs == rhs);
210}
211
220template<typename Lhs, typename Rhs>
221[[nodiscard]] bool operator<(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
222 return (std::addressof(*lhs) < std::addressof(*rhs));
223}
224
233template<typename Lhs, typename Rhs>
234[[nodiscard]] bool operator>(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
235 return rhs < lhs;
236}
237
247template<typename Lhs, typename Rhs>
248[[nodiscard]] bool operator<=(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
249 return !(lhs > rhs);
250}
251
261template<typename Lhs, typename Rhs>
262[[nodiscard]] bool operator>=(const resource<Lhs> &lhs, const resource<Rhs> &rhs) noexcept {
263 return !(lhs < rhs);
264}
265
266} // namespace entt
267
268#endif
entt::resource
Basic resource handle.
Definition resource.hpp:22
entt::resource::resource
resource(const resource< Other > &other) noexcept
Copy constructs a handle which shares ownership of the resource.
Definition resource.hpp:68
entt::resource::element_type
Type element_type
Resource type.
Definition resource.hpp:31
entt::resource::resource
resource(const resource &) noexcept=default
Default copy constructor.
entt::resource::operator=
resource & operator=(resource< Other > &&other) noexcept
Move assignment operator from foreign handle.
Definition resource.hpp:114
entt::resource::operator*
element_type & operator*() const noexcept
Returns a reference to the managed resource.
Definition resource.hpp:136
entt::resource::handle
const handle_type & handle() const noexcept
Returns the underlying resource handle.
Definition resource.hpp:178
entt::resource::operator=
resource & operator=(const resource &) noexcept=default
Default copy assignment operator.
entt::resource::operator->
element_type * operator->() const noexcept
Returns a pointer to the managed resource.
Definition resource.hpp:149
entt::resource::reset
void reset(handle_type other)
Replaces the managed resource.
Definition resource.hpp:170
entt::resource::reset
void reset()
Releases the ownership of the managed resource.
Definition resource.hpp:162
entt::resource::operator=
resource & operator=(resource &&) noexcept=default
Default move assignment operator.
entt::resource::resource
resource(handle_type res) noexcept
Creates a new resource handle.
Definition resource.hpp:43
entt::resource::swap
void swap(resource &other) noexcept
Exchanges the content with that of a given resource.
Definition resource.hpp:123
entt::resource::resource
resource() noexcept
Default constructor.
Definition resource.hpp:36
entt::resource::~resource
~resource()=default
Default destructor.
entt::resource::handle_type
std::shared_ptr< element_type > handle_type
Handle type.
Definition resource.hpp:33
entt::resource::resource
resource(resource< Other > &&other) noexcept
Move constructs a handle which takes ownership of the resource.
Definition resource.hpp:77
entt::resource::resource
resource(resource &&) noexcept=default
Default move constructor.
entt
EnTT default namespace.
Definition dense_map.hpp:22
entt::operator<=
constexpr bool operator<=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:257
entt::operator<
constexpr bool operator<(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:244
entt::operator!=
constexpr bool operator!=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:232
entt::operator>=
constexpr bool operator>=(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:283
entt::operator>
constexpr bool operator>(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:270
entt::operator==
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs) noexcept
Compares two hashed strings.
Definition hashed_string.hpp:220