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>;
100 static inline const sys_handle invalid_handle = stdex::invalid_handle;
105 static void close(_In_ sys_handle h)
108 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
110 throw std::system_error(GetLastError(), std::system_category(),
"CloseHandle failed");
112 if (
::close(h) >= 0 || errno == EBADF)
114 throw std::system_error(errno, std::system_category(),
"close failed");
121 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit =
false)
125 HANDLE process = GetCurrentProcess();
126 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
128 throw std::system_error(GetLastError(), std::system_category(),
"DuplicateHandle failed");
130 _Unreferenced_(inherit);
131 if ((h_new = dup(h)) >= 0)
133 throw std::system_error(errno, std::system_category(),
"dup failed");
141 template <
class T = sys_handle,
class TR = sys_
object_traits>
151 if (
this != std::addressof(other)) {
152 if (m_h != TR::invalid_handle)
154 m_h = other.m_h != TR::invalid_handle ? TR::duplicate(other.m_h) : TR::invalid_handle;
161 other.m_h = TR::invalid_handle;
166 if (
this != std::addressof(other)) {
167 if (m_h != TR::invalid_handle)
170 other.m_h = TR::invalid_handle;
177 if (m_h != TR::invalid_handle)
186 if (m_h != TR::invalid_handle) {
188 m_h = TR::invalid_handle;
195 operator bool() const noexcept {
return m_h != TR::invalid_handle; }
200 T
get() const noexcept {
return m_h; }
209 using sys_object = basic_sys_object<sys_handle, sys_object_traits>;
213 class safearray_accessor
216 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
218 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
220 throw std::system_error(hr, std::system_category(),
"SafeArrayAccessData failed");
223 ~safearray_accessor()
225 SafeArrayUnaccessData(m_sa);
228 T* data()
const {
return m_data; }
238 struct SafeArrayDestroy_delete
243 void operator()(_In_ LPSAFEARRAY sa)
const
245 SafeArrayDestroy(sa);
252 struct SysFreeString_delete
257 void operator()(_In_ BSTR sa)
const
Operating system object base class.
Definition system.hpp:143
virtual void close()
Closes object.
Definition system.hpp:184
T get() const noexcept
Returns object handle.
Definition system.hpp:200
System object operations.
Definition system.hpp:99
static sys_handle duplicate(sys_handle h, bool inherit=false)
Duplicates given object.
Definition system.hpp:121
static void close(sys_handle h)
Closes object.
Definition system.hpp:105