4#include "../Foundation/TypeTraits.h"
65template <
typename Definition>
68 using Handle =
typename Definition::Handle;
71 static constexpr Handle Invalid = Definition::Invalid;
77 UniqueHandle(
const Handle& externalHandle) : handle(externalHandle) {}
85 if (other.handle == handle)
86 return CloseReturnType(
false);
89 handle = other.handle;
91 return CloseReturnType(
true);
93 return CloseReturnType(
false);
99 [[nodiscard]] CloseReturnType
assign(
const Handle& externalHandle)
101 if (handle == externalHandle)
102 return CloseReturnType(
false);
105 handle = externalHandle;
106 return CloseReturnType(
true);
108 return CloseReturnType(
false);
113 (void)(
assign(forward<UniqueHandle>(other)));
119 [[nodiscard]]
bool isValid()
const {
return handle != Invalid; }
128 [[nodiscard]] CloseReturnType
get(Handle& outHandle, CloseReturnType invalidReturnType)
const
133 return CloseReturnType(
true);
135 return invalidReturnType;
140 [[nodiscard]] CloseReturnType
close()
144 Handle handleCopy = handle;
146 return Definition::releaseHandle(handleCopy);
148 return CloseReturnType(
true);
152 Handle handle = Invalid;
ReturnType extracts the return type from different forms of function types.
Definition: TypeTraits.h:50
Move only handle that has a special tag value flagging its invalid state.
Definition: UniqueHandle.h:67
CloseReturnType get(Handle &outHandle, CloseReturnType invalidReturnType) const
Extracts the native operating system handle out.
Definition: UniqueHandle.h:128
CloseReturnType assign(const Handle &externalHandle)
Copy assigns another UniqueHandle to this object, eventually closing existing handle.
Definition: UniqueHandle.h:99
CloseReturnType assign(UniqueHandle &&other)
Move assigns another UniqueHandle to this object, eventually closing existing handle.
Definition: UniqueHandle.h:83
CloseReturnType close()
Closes the handle by calling its OS specific close function.
Definition: UniqueHandle.h:140
void detach()
Detaches (sets to invalid) current handle, without closing it.
Definition: UniqueHandle.h:122
bool isValid() const
Check if current handle is valid.
Definition: UniqueHandle.h:119