Sane C++ Libraries
C++ Platform Abstraction Libraries
Loading...
Searching...
No Matches
UniqueHandle.h
1// Copyright (c) Stefano Cristiano
2// SPDX-License-Identifier: MIT
3#pragma once
4#include "../Foundation/Result.h"
5
6namespace SC
7{
10
25template <typename Definition>
27{
28 using Handle = typename Definition::Handle;
29
30 static constexpr Handle Invalid = Definition::Invalid;
31
32 UniqueHandle() = default;
33 UniqueHandle(const UniqueHandle& v) = delete;
34 UniqueHandle& operator=(const UniqueHandle& other) = delete;
35 UniqueHandle(UniqueHandle&& v) : handle(v.handle) { v.detach(); }
36 UniqueHandle(const Handle& externalHandle) : handle(externalHandle) {}
37 ~UniqueHandle() { (void)close(); }
38
42 [[nodiscard]] Result assign(UniqueHandle&& other)
43 {
44 if (other.handle == handle)
45 return Result(false);
46 if (close())
47 {
48 handle = other.handle;
49 other.detach();
50 return Result(true);
51 }
52 return Result(false);
53 }
54
58 [[nodiscard]] Result assign(const Handle& externalHandle)
59 {
60 if (handle == externalHandle)
61 return Result(false);
62 if (close())
63 {
64 handle = externalHandle;
65 return Result(true);
66 }
67 return Result(false);
68 }
69
70 UniqueHandle& operator=(UniqueHandle&& other)
71 {
72 (void)(assign(forward<UniqueHandle>(other)));
73 return *this;
74 }
75
78 [[nodiscard]] bool isValid() const { return handle != Invalid; }
79
81 void detach() { handle = Invalid; }
82
87 [[nodiscard]] Result get(Handle& outHandle, Result invalidReturnType) const
88 {
89 if (isValid())
90 {
91 outHandle = handle;
92 return Result(true);
93 }
94 return invalidReturnType;
95 }
96
99 [[nodiscard]] Result close()
100 {
101 if (isValid())
102 {
103 Handle handleCopy = handle;
104 detach();
105 return Definition::releaseHandle(handleCopy);
106 }
107 return Result(true);
108 }
109
110 protected:
111 Handle handle = Invalid;
112};
113
115
116} // namespace SC
constexpr T && forward(typename TypeTraits::RemoveReference< T >::type &value)
Forwards an lvalue or an rvalue as an rvalue reference.
Definition Compiler.h:260
An ascii string used as boolean result. SC_TRY macro forwards errors to caller.
Definition Result.h:12
Move only handle that has a special tag value flagging its invalid state.
Definition UniqueHandle.h:27
Result assign(const Handle &externalHandle)
Copy assigns another UniqueHandle to this object, eventually closing existing handle.
Definition UniqueHandle.h:58
void detach()
Detaches (sets to invalid) current handle, without closing it.
Definition UniqueHandle.h:81
Result get(Handle &outHandle, Result invalidReturnType) const
Extracts the native operating system handle out.
Definition UniqueHandle.h:87
bool isValid() const
Check if current handle is valid.
Definition UniqueHandle.h:78
Result close()
Closes the handle by calling its OS specific close function.
Definition UniqueHandle.h:99
Result assign(UniqueHandle &&other)
Move assigns another UniqueHandle to this object, eventually closing existing handle.
Definition UniqueHandle.h:42