EnTT 4.0.0
Loading...
Searching...
No Matches
resource.hpp
1#ifndef ENTT_RESOURCE_RESOURCE_HPP
2#define ENTT_RESOURCE_RESOURCE_HPP
3
4#include <compare>
5#include "../stl/concepts.hpp"
6#include "../stl/memory.hpp"
7#include "../stl/utility.hpp"
8#include "fwd.hpp"
9
10namespace entt {
11
22template<typename Type>
23class resource {
24 template<typename>
25 friend class resource;
26
27public:
29 using element_type = Type;
31 using handle_type = stl::shared_ptr<element_type>;
32
34 resource() noexcept
35 : value{} {}
36
41 explicit resource(handle_type res) noexcept
42 : value{stl::move(res)} {}
43
45 resource(const resource &) noexcept = default;
46
48 resource(resource &&) noexcept = default;
49
56 template<typename Other>
57 resource(const resource<Other> &other, element_type &res) noexcept
58 : value{other.value, stl::addressof(res)} {}
59
65 template<typename Other>
66 requires (!stl::same_as<Type, Other> && stl::constructible_from<Type &, Other &>)
67 resource(const resource<Other> &other) noexcept
68 : value{other.value} {}
69
75 template<typename Other>
76 requires (!stl::same_as<Type, Other> && stl::constructible_from<Type &, Other &>)
77 resource(resource<Other> &&other) noexcept
78 : value{stl::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>
102 requires (!stl::same_as<Type, Other> && stl::constructible_from<Type &, Other &>)
103 resource &operator=(const resource<Other> &other) noexcept {
104 value = other.value;
105 return *this;
106 }
107
114 template<typename Other>
115 requires (!stl::same_as<Type, Other> && stl::constructible_from<Type &, Other &>)
116 resource &operator=(resource<Other> &&other) noexcept {
117 value = stl::move(other.value);
118 return *this;
119 }
120
125 void swap(resource &other) noexcept {
126 using stl::swap;
127 swap(value, other.value);
128 }
129
138 [[nodiscard]] element_type &operator*() const noexcept {
139 return *value;
140 }
141
143 [[nodiscard]] operator element_type &() const noexcept {
144 return *value;
145 }
146
151 [[nodiscard]] element_type *operator->() const noexcept {
152 return value.get();
153 }
154
159 [[nodiscard]] explicit operator bool() const noexcept {
160 return static_cast<bool>(value);
161 }
162
169 template<typename Other>
170 [[nodiscard]] bool operator==(const resource<Other> &other) const noexcept {
171 return (value == other.value);
172 }
173
180 template<typename Other>
181 [[nodiscard]] auto operator<=>(const resource<Other> &other) const noexcept {
182 return (value <=> other.value);
183 }
184
186 void reset() {
187 value.reset();
188 }
189
194 void reset(handle_type other) {
195 value = stl::move(other);
196 }
197
202 [[nodiscard]] handle_type handle() const noexcept {
203 return value;
204 }
205
206private:
207 handle_type value;
208};
209
210} // namespace entt
211
212#endif
Type element_type
Resource type.
Definition resource.hpp:29
resource(const resource &) noexcept=default
Default copy constructor.
auto operator<=>(const resource< Other > &other) const noexcept
Lexicographically compares two handles.
Definition resource.hpp:181
element_type & operator*() const noexcept
Returns a reference to the managed resource.
Definition resource.hpp:138
resource(const resource< Other > &other) noexcept
Copy constructs a handle which shares ownership of the resource.
Definition resource.hpp:67
stl::shared_ptr< element_type > handle_type
Handle type.
Definition resource.hpp:31
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:151
void reset(handle_type other)
Replaces the managed resource.
Definition resource.hpp:194
void reset()
Releases the ownership of the managed resource.
Definition resource.hpp:186
handle_type handle() const noexcept
Returns the underlying resource handle.
Definition resource.hpp:202
resource & operator=(resource &&) noexcept=default
Default move assignment operator.
resource & operator=(resource< Other > &&other) noexcept
Move assignment operator from foreign handle.
Definition resource.hpp:116
resource(handle_type res) noexcept
Creates a new resource handle.
Definition resource.hpp:41
bool operator==(const resource< Other > &other) const noexcept
Compares two handles.
Definition resource.hpp:170
void swap(resource &other) noexcept
Exchanges the content with that of a given resource.
Definition resource.hpp:125
resource() noexcept
Default constructor.
Definition resource.hpp:34
~resource()=default
Default destructor.
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.
Custom EnTT namespace for the standard template library.
Definition entt.hpp:5
EnTT default namespace.
Definition dense_map.hpp:25