14#define _LARGEFILE64_SOURCE
26#define PATH_SEPARATOR '\\'
27#define PATH_SEPARATOR_STR "\\"
29#define PATH_SEPARATOR '/'
30#define PATH_SEPARATOR_STR "/"
39 using sys_handle = HANDLE;
40 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
42 using sys_handle = int;
43 const sys_handle invalid_handle = (sys_handle)-1;
50 inline DWORD sys_error() {
return GetLastError(); }
52 inline int sys_error() {
return errno; }
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;
85 using sregex = std::basic_regex<stdex::schar_t>;
93 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
99 if (
this != std::addressof(other)) {
100 if (m_h != invalid_handle)
102 m_h = other.m_h != invalid_handle ?
duplicate(other.m_h,
false) : invalid_handle;
109 other.m_h = invalid_handle;
114 if (
this != std::addressof(other)) {
115 if (m_h != invalid_handle)
118 other.m_h = invalid_handle;
125 if (m_h != invalid_handle)
134 if (m_h != invalid_handle) {
136 m_h = invalid_handle;
143 inline operator bool() const noexcept {
return m_h != invalid_handle; }
148 inline sys_handle
get() const noexcept {
return m_h; }
154 static void close(_In_ sys_handle h)
157 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
159 throw std::system_error(GetLastError(), std::system_category(),
"CloseHandle failed");
161 if (
::close(h) >= 0 || errno == EBADF)
163 throw std::system_error(errno, std::system_category(),
"close failed");
170 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit)
174 HANDLE process = GetCurrentProcess();
175 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
177 throw std::system_error(GetLastError(), std::system_category(),
"DuplicateHandle failed");
179 _Unreferenced_(inherit);
180 if ((h_new = dup(h)) >= 0)
182 throw std::system_error(errno, std::system_category(),
"dup failed");
192 class safearray_accessor
195 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
197 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
199 throw std::system_error(hr, std::system_category(),
"SafeArrayAccessData failed");
202 ~safearray_accessor()
204 SafeArrayUnaccessData(m_sa);
207 T* data()
const {
return m_data; }
217 struct SafeArrayDestroy_delete
222 void operator()(_In_ LPSAFEARRAY sa)
const
224 SafeArrayDestroy(sa);
231 struct SysFreeString_delete
236 void operator()(_In_ BSTR sa)
const
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:91
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:148
virtual void close()
Closes object.
Definition system.hpp:132
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:170
static void close(sys_handle h)
Closes object.
Definition system.hpp:154