14#ifndef _LARGEFILE64_SOURCE
15#define _LARGEFILE64_SOURCE
28#define PATH_SEPARATOR '\\'
29#define PATH_SEPARATOR_STR "\\"
31#define PATH_SEPARATOR '/'
32#define PATH_SEPARATOR_STR "/"
41 using sys_handle = HANDLE;
42 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
44 using sys_handle = int;
45 const sys_handle invalid_handle = (sys_handle)-1;
52 inline DWORD sys_error() {
return GetLastError(); }
54 inline int sys_error() {
return errno; }
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;
87 using sregex = std::basic_regex<stdex::schar_t>;
95 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
101 if (
this != std::addressof(other)) {
102 if (m_h != invalid_handle)
104 m_h = other.m_h != invalid_handle ?
duplicate(other.m_h,
false) : invalid_handle;
111 other.m_h = invalid_handle;
116 if (
this != std::addressof(other)) {
117 if (m_h != invalid_handle)
120 other.m_h = invalid_handle;
127 if (m_h != invalid_handle)
136 if (m_h != invalid_handle) {
138 m_h = invalid_handle;
145 inline operator bool() const noexcept {
return m_h != invalid_handle; }
150 inline sys_handle
get() const noexcept {
return m_h; }
156 static void close(_In_ sys_handle h)
159 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
161 throw std::system_error(GetLastError(), std::system_category(),
"CloseHandle failed");
163 if (
::close(h) >= 0 || errno == EBADF)
165 throw std::system_error(errno, std::system_category(),
"close failed");
172 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit)
176 HANDLE process = GetCurrentProcess();
177 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
179 throw std::system_error(GetLastError(), std::system_category(),
"DuplicateHandle failed");
181 _Unreferenced_(inherit);
182 if ((h_new = dup(h)) >= 0)
184 throw std::system_error(errno, std::system_category(),
"dup failed");
194 class safearray_accessor
197 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
199 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
201 throw std::system_error(hr, std::system_category(),
"SafeArrayAccessData failed");
204 ~safearray_accessor()
206 SafeArrayUnaccessData(m_sa);
209 T* data()
const {
return m_data; }
219 struct SafeArrayDestroy_delete
224 void operator()(_In_ LPSAFEARRAY sa)
const
226 SafeArrayDestroy(sa);
233 struct SysFreeString_delete
238 void operator()(_In_ BSTR sa)
const
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:93
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:150
virtual void close()
Closes object.
Definition system.hpp:134
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:172
static void close(sys_handle h)
Closes object.
Definition system.hpp:156