EnTT  3.10.0
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 "../config/config.h"
8 #include "fwd.hpp"
9 
10 namespace entt {
11 
22 template<typename Type>
23 class resource {
25  template<typename>
26  friend class resource;
27 
28  template<typename Other>
29  static constexpr bool is_acceptable_v = !std::is_same_v<Type, Other> && std::is_constructible_v<Type &, Other &>;
30 
31 public:
33  resource() ENTT_NOEXCEPT
34  : value{} {}
35 
40  explicit resource(std::shared_ptr<Type> res) ENTT_NOEXCEPT
41  : value{std::move(res)} {}
42 
44  resource(const resource &) ENTT_NOEXCEPT = default;
45 
47  resource(resource &&) ENTT_NOEXCEPT = default;
48 
55  template<typename Other>
56  resource(const resource<Other> &other, Type &res) ENTT_NOEXCEPT
57  : value{other.value, std::addressof(res)} {}
58 
64  template<typename Other, typename = std::enable_if_t<is_acceptable_v<Other>>>
65  resource(const resource<Other> &other) ENTT_NOEXCEPT
66  : value{other.value} {}
67 
73  template<typename Other, typename = std::enable_if_t<is_acceptable_v<Other>>>
74  resource(resource<Other> &&other) ENTT_NOEXCEPT
75  : value{std::move(other.value)} {}
76 
81  resource &operator=(const resource &) ENTT_NOEXCEPT = default;
82 
87  resource &operator=(resource &&) ENTT_NOEXCEPT = default;
88 
95  template<typename Other>
96  std::enable_if_t<is_acceptable_v<Other>, resource &>
97  operator=(const resource<Other> &other) ENTT_NOEXCEPT {
98  value = other.value;
99  return *this;
100  }
101 
108  template<typename Other>
109  std::enable_if_t<is_acceptable_v<Other>, resource &>
110  operator=(resource<Other> &&other) ENTT_NOEXCEPT {
111  value = std::move(other.value);
112  return *this;
113  }
114 
123  [[nodiscard]] Type &operator*() const ENTT_NOEXCEPT {
124  return *value;
125  }
126 
128  [[nodiscard]] operator Type &() const ENTT_NOEXCEPT {
129  return *value;
130  }
131 
136  [[nodiscard]] Type *operator->() const ENTT_NOEXCEPT {
137  return value.get();
138  }
139 
144  [[nodiscard]] explicit operator bool() const ENTT_NOEXCEPT {
145  return static_cast<bool>(value);
146  }
147 
152  [[nodiscard]] long use_count() const ENTT_NOEXCEPT {
153  return value.use_count();
154  }
155 
156 private:
157  std::shared_ptr<Type> value;
158 };
159 
168 template<typename Res, typename Other>
169 [[nodiscard]] bool operator==(const resource<Res> &lhs, const resource<Other> &rhs) ENTT_NOEXCEPT {
170  return (std::addressof(*lhs) == std::addressof(*rhs));
171 }
172 
181 template<typename ILhs, typename IRhs>
182 [[nodiscard]] bool operator!=(const resource<ILhs> &lhs, const resource<IRhs> &rhs) ENTT_NOEXCEPT {
183  return !(lhs == rhs);
184 }
185 
186 } // namespace entt
187 
188 #endif
Basic resource handle.
Definition: resource.hpp:23
std::enable_if_t< is_acceptable_v< Other >, resource & > operator=(resource< Other > &&other)
Move assignment operator from foreign handle.
Definition: resource.hpp:110
long use_count() const
Returns the number of handles pointing the same resource.
Definition: resource.hpp:152
resource()
Default constructor.
Definition: resource.hpp:33
Type & operator*() const
Returns a reference to the managed resource.
Definition: resource.hpp:123
resource(std::shared_ptr< Type > res)
Creates a handle from a weak pointer, namely a resource.
Definition: resource.hpp:40
Type * operator->() const
Returns a pointer to the managed resource.
Definition: resource.hpp:136
resource(resource< Other > &&other)
Move constructs a handle which takes ownership of the resource.
Definition: resource.hpp:74
resource & operator=(resource &&)=default
Default move assignment operator.
resource(const resource &)=default
Default copy constructor.
resource(const resource< Other > &other)
Copy constructs a handle which shares ownership of the resource.
Definition: resource.hpp:65
resource(resource &&)=default
Default move constructor.
resource & operator=(const resource &)=default
Default copy assignment operator.
EnTT default namespace.
Definition: dense_map.hpp:22
constexpr bool operator==(const basic_hashed_string< Char > &lhs, const basic_hashed_string< Char > &rhs)
Compares two hashed strings.
bool operator!=(const basic_any< Len, Align > &lhs, const basic_any< Len, Align > &rhs)
Checks if two wrappers differ in their content.
Definition: any.hpp:402