4#ifndef GREM_CORE_DATA_UNIQUE_HANDLE_HPP
5#define GREM_CORE_DATA_UNIQUE_HANDLE_HPP
25template <
typename Handle,
typename Deleter, Handle NullHandle = Handle{}>
40 constexpr explicit
UniqueHandle(Handle handle, Deleter deleter = Deleter()) noexcept
42 , deleter(std::
move(deleter)) {}
65 : handle(other.release())
66 , deleter(std::move(other.get_deleter())) {}
88 reset(other.release());
89 deleter = std::move(other.get_deleter());
99 constexpr explicit operator bool() const noexcept {
100 return get() != NullHandle;
115 return get() == other.get();
129 constexpr void reset(Handle newHandle = NullHandle)
noexcept {
130 deleter(std::exchange(handle, newHandle));
153 return std::exchange(handle, NullHandle);
164 [[nodiscard]]
constexpr Handle
get() const noexcept {
182 [[nodiscard]]
constexpr const Deleter&
get_deleter() const noexcept {
187 Handle handle = NullHandle;
188 [[no_unique_address]] Deleter deleter;
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.
constexpr auto move(input_range auto &&range, auto outputIterator)
Definition algorithms.hpp:669