libdonut  2.3.2
Application framework for cross-platform game development in C++20
UniqueHandle.hpp
Go to the documentation of this file.
1 #ifndef DONUT_UNIQUE_HANDLE_HPP
2 #define DONUT_UNIQUE_HANDLE_HPP
3 
4 #include <utility> // std::exchange
5 
6 namespace donut {
7 
20 template <typename Handle, typename Deleter, Handle NullHandle = Handle{}>
21 class UniqueHandle {
22 public:
26  constexpr UniqueHandle() noexcept = default;
27 
34  constexpr explicit UniqueHandle(Handle handle) noexcept
35  : handle(handle) {}
36 
41  Deleter{}(handle);
42  }
43 
47  UniqueHandle(const UniqueHandle&) = delete;
48 
57  constexpr UniqueHandle(UniqueHandle&& other) noexcept
58  : handle(other.release()) {}
59 
63  UniqueHandle& operator=(const UniqueHandle&) = delete;
64 
76  constexpr UniqueHandle& operator=(UniqueHandle&& other) noexcept {
77  reset(other.release());
78  return *this;
79  }
80 
87  [[nodiscard]] constexpr explicit operator bool() const noexcept {
88  return get() != NullHandle;
89  }
90 
102  [[nodiscard]] constexpr bool operator==(const UniqueHandle& other) const noexcept {
103  return get() == other.get();
104  }
105 
117  constexpr void reset(Handle newHandle = NullHandle) noexcept {
118  Deleter{}(std::exchange(handle, newHandle));
119  }
120 
140  constexpr Handle release() noexcept {
141  return std::exchange(handle, NullHandle);
142  }
143 
152  [[nodiscard]] constexpr Handle get() const noexcept {
153  return handle;
154  }
155 
156 private:
157  Handle handle = NullHandle;
158 };
159 
160 } // namespace donut
161 
162 #endif
Generic nullable RAII resource handle with exclusive ownership of a resource that is automatically de...
Definition: UniqueHandle.hpp:21
UniqueHandle(const UniqueHandle &)=delete
Copying a resource handle is disallowed to enforce exclusive ownership.
constexpr Handle get() const noexcept
Get the value of the underlying resource handle.
Definition: UniqueHandle.hpp:152
~UniqueHandle()
Destroy the handle and its associated resource if it has one.
Definition: UniqueHandle.hpp:40
UniqueHandle & operator=(const UniqueHandle &)=delete
Copying a resource handle is disallowed to enforce exclusive ownership.
constexpr void reset(Handle newHandle=NullHandle) noexcept
Destroy the resource associated with this handle, if any, and take ownership of a new resource handle...
Definition: UniqueHandle.hpp:117
constexpr UniqueHandle(UniqueHandle &&other) noexcept
Move constructor.
Definition: UniqueHandle.hpp:57
constexpr bool operator==(const UniqueHandle &other) const noexcept
Compare this resource handle against another for equality of the underlying handle value.
Definition: UniqueHandle.hpp:102
constexpr UniqueHandle() noexcept=default
Construct a null handle without an associated resource.
constexpr UniqueHandle & operator=(UniqueHandle &&other) noexcept
Move assignment.
Definition: UniqueHandle.hpp:76
constexpr Handle release() noexcept
Relinquish ownership of the associated resource.
Definition: UniqueHandle.hpp:140
std::uint32_t Handle
Generic GPU resource handle.
Definition: Handle.hpp:11
Definition: Application.hpp:9