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;
71 using sys_char = schar_t;
76 using sstring = std::basic_string<stdex::schar_t>;
82 using sys_string = sstring;
87 using sstring_view = std::basic_string_view<stdex::schar_t, std::char_traits<stdex::schar_t>>;
92 using sregex = std::basic_regex<stdex::schar_t>;
99 static inline const sys_handle invalid_handle = stdex::invalid_handle;
104 static void close(_In_ sys_handle h)
107 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
109 throw std::system_error(GetLastError(), std::system_category(),
"CloseHandle failed");
111 if (
::close(h) >= 0 || errno == EBADF)
113 throw std::system_error(errno, std::system_category(),
"close failed");
120 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit =
false)
124 HANDLE process = GetCurrentProcess();
125 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
127 throw std::system_error(GetLastError(), std::system_category(),
"DuplicateHandle failed");
129 _Unreferenced_(inherit);
130 if ((h_new = dup(h)) >= 0)
132 throw std::system_error(errno, std::system_category(),
"dup failed");
140 template <
class T = sys_handle,
class TR = sys_
object_traits>
150 if (
this != std::addressof(other)) {
151 if (m_h != TR::invalid_handle)
153 m_h = other.m_h != TR::invalid_handle ? TR::duplicate(other.m_h) : TR::invalid_handle;
160 other.m_h = TR::invalid_handle;
165 if (
this != std::addressof(other)) {
166 if (m_h != TR::invalid_handle)
169 other.m_h = TR::invalid_handle;
176 if (m_h != TR::invalid_handle)
185 if (m_h != TR::invalid_handle) {
187 m_h = TR::invalid_handle;
194 operator bool() const noexcept {
return m_h != TR::invalid_handle; }
199 T
get() const noexcept {
return m_h; }
208 using sys_object = basic_sys_object<sys_handle, sys_object_traits>;
212 class safearray_accessor
215 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
217 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
219 throw std::system_error(hr, std::system_category(),
"SafeArrayAccessData failed");
222 ~safearray_accessor()
224 SafeArrayUnaccessData(m_sa);
227 T* data()
const {
return m_data; }
237 struct SafeArrayDestroy_delete
242 void operator()(_In_ LPSAFEARRAY sa)
const
244 SafeArrayDestroy(sa);
251 struct SysFreeString_delete
256 void operator()(_In_ BSTR sa)
const
Operating system object base class.
Definition system.hpp:142
virtual void close()
Closes object.
Definition system.hpp:183
T get() const noexcept
Returns object handle.
Definition system.hpp:199
System object operations.
Definition system.hpp:98
static sys_handle duplicate(sys_handle h, bool inherit=false)
Duplicates given object.
Definition system.hpp:120
static void close(sys_handle h)
Closes object.
Definition system.hpp:104