36 using sys_handle = HANDLE;
37 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
39 using sys_handle = int;
40 const sys_handle invalid_handle = (sys_handle)-1;
47 using sys_char = TCHAR;
49 using sys_char = char;
59 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
65 if (
this != std::addressof(other)) {
66 if (m_h != invalid_handle)
68 m_h = other.m_h != invalid_handle ?
duplicate(other.m_h,
false) : invalid_handle;
75 other.m_h = invalid_handle;
80 if (
this != std::addressof(other)) {
81 if (m_h != invalid_handle)
84 other.m_h = invalid_handle;
91 if (m_h != invalid_handle)
100 if (m_h != invalid_handle) {
102 m_h = invalid_handle;
109 inline operator bool() const noexcept {
return m_h != invalid_handle; }
114 inline sys_handle
get() const noexcept {
return m_h; }
123 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
125 if (
close(h) >= 0 || errno == EBADF)
128 throw std::runtime_error(
"failed to close handle");
134 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit)
138 HANDLE process = GetCurrentProcess();
139 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
141 UNREFERENCED_PARAMETER(inherit);
142 if ((h_new = dup(h)) >= 0)
145 throw std::runtime_error(
"failed to duplicate handle");
154 class safearray_accessor
157 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
159 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
161 throw std::invalid_argument(
"SafeArrayAccessData failed");
164 ~safearray_accessor()
166 SafeArrayUnaccessData(m_sa);
169 T* data()
const {
return m_data; }
179 struct SafeArrayDestroy_delete
184 void operator()(_In_ LPSAFEARRAY sa)
const
186 SafeArrayDestroy(sa);
193 struct SysFreeString_delete
198 void operator()(_In_ BSTR sa)
const
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:57
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:114
virtual void close()
Closes object.
Definition system.hpp:98
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:134
static void close(sys_handle h)
Closes object.
Definition system.hpp:120