EnTT 3.14.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
37 : value{} {}
38
44 : value{std::move(res)} {}
45
47 resource(const resource &) noexcept = default;
48
51
60 : value{other.value, std::addressof(res)} {}
61
67 template<typename Other, typename = std::enable_if_t<is_acceptable_v<Other>>>
69 : value{other.value} {}
70
76 template<typename Other, typename = std::enable_if_t<is_acceptable_v<Other>>>
78 : value{std::move(other.value)} {}
79
81 ~resource() = default;
82
87 resource &operator=(const resource &) noexcept = default;
88
94
101 template<typename Other, typename = std::enable_if_t<is_acceptable_v<Other>>>
103 value = other.value;
104 return *this;
105 }
106
113 template<typename Other, typename = std::enable_if_t<is_acceptable_v<Other>>>
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
137 return *value;
138 }
139
141 [[nodiscard]] operator element_type &() const noexcept {
142 return *value;
143 }
144
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
171 value = std::move(other);
172 }
173
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
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.
resource & operator=(resource< Other > &&other) noexcept
Move assignment operator from foreign handle.
Definition resource.hpp:114
element_type & operator*() const noexcept
Returns a reference to the managed resource.
Definition resource.hpp:136
const handle_type & handle() const noexcept
Returns the underlying resource handle.
Definition resource.hpp:178
resource & operator=(const resource &) noexcept=default
Default copy assignment operator.
element_type * operator->() const noexcept
Returns a pointer to the managed resource.
Definition resource.hpp:149
void reset(handle_type other)
Replaces the managed resource.
Definition resource.hpp:170
void reset()
Releases the ownership of the managed resource.
Definition resource.hpp:162
resource & operator=(resource &&) noexcept=default
Default move assignment operator.
resource(handle_type res) noexcept
Creates a new resource handle.
Definition resource.hpp:43
void swap(resource &other) noexcept
Exchanges the content with that of a given resource.
Definition resource.hpp:123
resource() noexcept
Default constructor.
Definition resource.hpp:36
~resource()=default
Default destructor.
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:22
constexpr Type make_obj_using_allocator(const Allocator &allocator, Args &&...args)
Uses-allocator construction utility (waiting for C++20).
Definition memory.hpp:219
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.