15#define _LARGEFILE64_SOURCE
35#define PATH_SEPARATOR '\\'
36#define PATH_SEPARATOR_STR "\\"
38#define PATH_SEPARATOR '/'
39#define PATH_SEPARATOR_STR "/"
48 using sys_handle = HANDLE;
49 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
51 using sys_handle = int;
52 const sys_handle invalid_handle = (sys_handle)-1;
59 using schar_t = TCHAR;
69 using sys_char = schar_t;
74 using sstring = std::basic_string<stdex::schar_t>;
80 using sys_string = sstring;
88 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
94 if (
this != std::addressof(other)) {
95 if (m_h != invalid_handle)
97 m_h = other.m_h != invalid_handle ?
duplicate(other.m_h,
false) : invalid_handle;
104 other.m_h = invalid_handle;
109 if (
this != std::addressof(other)) {
110 if (m_h != invalid_handle)
113 other.m_h = invalid_handle;
120 if (m_h != invalid_handle)
129 if (m_h != invalid_handle) {
131 m_h = invalid_handle;
138 inline operator bool() const noexcept {
return m_h != invalid_handle; }
143 inline sys_handle
get() const noexcept {
return m_h; }
149 static void close(_In_ sys_handle h)
152 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
154 if (
::close(h) >= 0 || errno == EBADF)
157 throw std::runtime_error(
"failed to close handle");
163 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit)
167 HANDLE process = GetCurrentProcess();
168 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
170 _Unreferenced_(inherit);
171 if ((h_new = dup(h)) >= 0)
174 throw std::runtime_error(
"failed to duplicate handle");
183 class safearray_accessor
186 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
188 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
190 throw std::invalid_argument(
"SafeArrayAccessData failed");
193 ~safearray_accessor()
195 SafeArrayUnaccessData(m_sa);
198 T* data()
const {
return m_data; }
208 struct SafeArrayDestroy_delete
213 void operator()(_In_ LPSAFEARRAY sa)
const
215 SafeArrayDestroy(sa);
222 struct SysFreeString_delete
227 void operator()(_In_ BSTR sa)
const
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:86
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:143
virtual void close()
Closes object.
Definition system.hpp:127
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:163
static void close(sys_handle h)
Closes object.
Definition system.hpp:149