14#ifndef _LARGEFILE64_SOURCE
15#define _LARGEFILE64_SOURCE
29#define PATH_SEPARATOR '\\'
30#define PATH_SEPARATOR_STR "\\"
32#define PATH_SEPARATOR '/'
33#define PATH_SEPARATOR_STR "/"
42 using sys_handle = HANDLE;
43 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
45 using sys_handle = int;
46 const sys_handle invalid_handle = (sys_handle)-1;
53 inline DWORD sys_error() {
return GetLastError(); }
55 inline int sys_error() {
return errno; }
62 using schar_t = TCHAR;
72 using sys_char = schar_t;
77 using sstring = std::basic_string<stdex::schar_t>;
83 using sys_string = sstring;
88 using sstring_view = std::basic_string_view<stdex::schar_t, std::char_traits<stdex::schar_t>>;
93 using sregex = std::basic_regex<stdex::schar_t>;
101 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
107 if (
this != std::addressof(other)) {
108 if (m_h != invalid_handle)
110 m_h = other.m_h != invalid_handle ?
duplicate(other.m_h,
false) : invalid_handle;
117 other.m_h = invalid_handle;
122 if (
this != std::addressof(other)) {
123 if (m_h != invalid_handle)
126 other.m_h = invalid_handle;
133 if (m_h != invalid_handle)
142 if (m_h != invalid_handle) {
144 m_h = invalid_handle;
151 operator bool() const noexcept {
return m_h != invalid_handle; }
156 sys_handle
get() const noexcept {
return m_h; }
162 static void close(_In_ sys_handle h)
165 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
167 throw std::system_error(GetLastError(), std::system_category(),
"CloseHandle failed");
169 if (
::close(h) >= 0 || errno == EBADF)
171 throw std::system_error(errno, std::system_category(),
"close failed");
178 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit)
182 HANDLE process = GetCurrentProcess();
183 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
185 throw std::system_error(GetLastError(), std::system_category(),
"DuplicateHandle failed");
187 _Unreferenced_(inherit);
188 if ((h_new = dup(h)) >= 0)
190 throw std::system_error(errno, std::system_category(),
"dup failed");
200 class safearray_accessor
203 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
205 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
207 throw std::system_error(hr, std::system_category(),
"SafeArrayAccessData failed");
210 ~safearray_accessor()
212 SafeArrayUnaccessData(m_sa);
215 T* data()
const {
return m_data; }
225 struct SafeArrayDestroy_delete
230 void operator()(_In_ LPSAFEARRAY sa)
const
232 SafeArrayDestroy(sa);
239 struct SysFreeString_delete
244 void operator()(_In_ BSTR sa)
const
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:99
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:156
virtual void close()
Closes object.
Definition system.hpp:140
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:178
static void close(sys_handle h)
Closes object.
Definition system.hpp:162