17#define _LARGEFILE64_SOURCE
37#define PATH_SEPARATOR '\\'
38#define PATH_SEPARATOR_STR "\\"
40#define PATH_SEPARATOR '/'
41#define PATH_SEPARATOR_STR "/"
50 using sys_handle = HANDLE;
51 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
53 using sys_handle = int;
54 const sys_handle invalid_handle = (sys_handle)-1;
61 using schar_t = TCHAR;
71 using sys_char = schar_t;
76 using sstring = std::basic_string<stdex::schar_t>;
82 using sys_string = sstring;
90 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
96 if (
this != std::addressof(other)) {
97 if (m_h != invalid_handle)
99 m_h = other.m_h != invalid_handle ?
duplicate(other.m_h,
false) : invalid_handle;
106 other.m_h = invalid_handle;
111 if (
this != std::addressof(other)) {
112 if (m_h != invalid_handle)
115 other.m_h = invalid_handle;
122 if (m_h != invalid_handle)
131 if (m_h != invalid_handle) {
133 m_h = invalid_handle;
140 inline operator bool() const noexcept {
return m_h != invalid_handle; }
145 inline sys_handle
get() const noexcept {
return m_h; }
151 static void close(_In_ sys_handle h)
154 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
156 if (
::close(h) >= 0 || errno == EBADF)
159 throw std::runtime_error(
"failed to close handle");
165 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit)
169 HANDLE process = GetCurrentProcess();
170 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
172 _Unreferenced_(inherit);
173 if ((h_new = dup(h)) >= 0)
176 throw std::runtime_error(
"failed to duplicate handle");
185 class safearray_accessor
188 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
190 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
192 throw std::invalid_argument(
"SafeArrayAccessData failed");
195 ~safearray_accessor()
197 SafeArrayUnaccessData(m_sa);
200 T* data()
const {
return m_data; }
210 struct SafeArrayDestroy_delete
215 void operator()(_In_ LPSAFEARRAY sa)
const
217 SafeArrayDestroy(sa);
224 struct SysFreeString_delete
229 void operator()(_In_ BSTR sa)
const
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:88
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:145
virtual void close()
Closes object.
Definition system.hpp:129
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:165
static void close(sys_handle h)
Closes object.
Definition system.hpp:151