15#define _LARGEFILE64_SOURCE
40 using sys_handle = HANDLE;
41 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
43 using sys_handle = int;
44 const sys_handle invalid_handle = (sys_handle)-1;
51 using schar_t = TCHAR;
61 using sys_char = schar_t;
66 using sstring = std::basic_string<stdex::schar_t>;
72 using sys_string = sstring;
80 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
86 if (
this != std::addressof(other)) {
87 if (m_h != invalid_handle)
89 m_h = other.m_h != invalid_handle ?
duplicate(other.m_h,
false) : invalid_handle;
96 other.m_h = invalid_handle;
101 if (
this != std::addressof(other)) {
102 if (m_h != invalid_handle)
105 other.m_h = invalid_handle;
112 if (m_h != invalid_handle)
121 if (m_h != invalid_handle) {
123 m_h = invalid_handle;
130 inline operator bool() const noexcept {
return m_h != invalid_handle; }
135 inline sys_handle
get() const noexcept {
return m_h; }
141 static void close(_In_ sys_handle h)
144 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
146 if (
::close(h) >= 0 || errno == EBADF)
149 throw std::runtime_error(
"failed to close handle");
155 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit)
159 HANDLE process = GetCurrentProcess();
160 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
162 _Unreferenced_(inherit);
163 if ((h_new = dup(h)) >= 0)
166 throw std::runtime_error(
"failed to duplicate handle");
175 class safearray_accessor
178 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
180 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
182 throw std::invalid_argument(
"SafeArrayAccessData failed");
185 ~safearray_accessor()
187 SafeArrayUnaccessData(m_sa);
190 T* data()
const {
return m_data; }
200 struct SafeArrayDestroy_delete
205 void operator()(_In_ LPSAFEARRAY sa)
const
207 SafeArrayDestroy(sa);
214 struct SysFreeString_delete
219 void operator()(_In_ BSTR sa)
const
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:78
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:135
virtual void close()
Closes object.
Definition system.hpp:119
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:155
static void close(sys_handle h)
Closes object.
Definition system.hpp:141