GREM 3.12.0
Game/Rendering Engine Modules for C++
Loading...
Searching...
No Matches
UniqueHandle.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2026 Ivar Härnqvist
2// SPDX-License-Identifier: MIT
3
4#ifndef GREM_CORE_DATA_UNIQUE_HANDLE_HPP
5#define GREM_CORE_DATA_UNIQUE_HANDLE_HPP
6
8
9#include <utility> // std::move, std::exchange
10
11namespace grem {
12
25template <typename Handle, typename Deleter, Handle NullHandle = Handle{}>
27public:
31 constexpr UniqueHandle() noexcept = default;
32
40 constexpr explicit UniqueHandle(Handle handle, Deleter deleter = Deleter()) noexcept
41 : handle(handle)
42 , deleter(std::move(deleter)) {}
43
48 deleter(handle);
49 }
50
54 UniqueHandle(const UniqueHandle&) = delete;
55
64 constexpr UniqueHandle(UniqueHandle&& other) noexcept
65 : handle(other.release())
66 , deleter(std::move(other.get_deleter())) {}
67
72
84 constexpr UniqueHandle& operator=(UniqueHandle&& other) noexcept {
85 if (this == &other) {
86 return *this;
87 }
88 reset(other.release());
89 deleter = std::move(other.get_deleter());
90 return *this;
91 }
92
99 constexpr explicit operator bool() const noexcept {
100 return get() != NullHandle;
101 }
102
114 [[nodiscard]] constexpr bool operator==(const UniqueHandle& other) const noexcept {
115 return get() == other.get();
116 }
117
129 constexpr void reset(Handle newHandle = NullHandle) noexcept {
130 deleter(std::exchange(handle, newHandle));
131 }
132
152 constexpr Handle release() noexcept {
153 return std::exchange(handle, NullHandle);
154 }
155
164 [[nodiscard]] constexpr Handle get() const noexcept {
165 return handle;
166 }
167
173 [[nodiscard]] constexpr Deleter& get_deleter() noexcept {
174 return deleter;
175 }
176
182 [[nodiscard]] constexpr const Deleter& get_deleter() const noexcept {
183 return deleter;
184 }
185
186private:
187 Handle handle = NullHandle;
188 [[no_unique_address]] Deleter deleter;
189};
190
191} // namespace grem
192
193#endif
UniqueHandle & operator=(const UniqueHandle &)=delete
Copying a resource handle is disallowed to enforce exclusive ownership.
constexpr UniqueHandle(UniqueHandle &&other) noexcept
Move constructor.
Definition UniqueHandle.hpp:64
constexpr Handle get() const noexcept
Get the value of the underlying resource handle.
Definition UniqueHandle.hpp:164
constexpr const Deleter & get_deleter() const noexcept
Get the current deleter.
Definition UniqueHandle.hpp:182
UniqueHandle(const UniqueHandle &)=delete
Copying a resource handle is disallowed to enforce exclusive ownership.
constexpr bool operator==(const UniqueHandle &other) const noexcept
Compare this resource handle against another for equality of the underlying handle value.
Definition UniqueHandle.hpp:114
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:129
~UniqueHandle()
Destroy the handle and its associated resource if it has one.
Definition UniqueHandle.hpp:47
constexpr Handle release() noexcept
Relinquish ownership of the associated resource.
Definition UniqueHandle.hpp:152
constexpr Deleter & get_deleter() noexcept
Get the current deleter.
Definition UniqueHandle.hpp:173
constexpr UniqueHandle & operator=(UniqueHandle &&other) noexcept
Move assignment.
Definition UniqueHandle.hpp:84
constexpr UniqueHandle() noexcept=default
Construct a null handle without an associated resource.
Definition aliases.hpp:9
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669