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>;
98 template <
class T = sys_handle,
class TR = sys_
object_traits>
108 if (
this != std::addressof(other)) {
109 if (m_h != TR::invalid_handle)
111 m_h = other.m_h != TR::invalid_handle ? TR::duplicate(other.m_h) : TR::invalid_handle;
118 other.m_h = TR::invalid_handle;
123 if (
this != std::addressof(other)) {
124 if (m_h != TR::invalid_handle)
127 other.m_h = TR::invalid_handle;
134 if (m_h != TR::invalid_handle)
143 if (m_h != TR::invalid_handle) {
145 m_h = TR::invalid_handle;
152 operator bool() const noexcept {
return m_h != TR::invalid_handle; }
157 T
get() const noexcept {
return m_h; }
168 static inline const sys_handle invalid_handle = stdex::invalid_handle;
173 static void close(_In_ sys_handle h)
176 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
178 throw std::system_error(GetLastError(), std::system_category(),
"CloseHandle failed");
180 if (
::close(h) >= 0 || errno == EBADF)
182 throw std::system_error(errno, std::system_category(),
"close failed");
189 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit =
false)
193 HANDLE process = GetCurrentProcess();
194 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
196 throw std::system_error(GetLastError(), std::system_category(),
"DuplicateHandle failed");
198 _Unreferenced_(inherit);
199 if ((h_new = dup(h)) >= 0)
201 throw std::system_error(errno, std::system_category(),
"dup failed");
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:100
virtual void close()
Closes object.
Definition system.hpp:141
T get() const noexcept
Returns object handle.
Definition system.hpp:157
System object operations.
Definition system.hpp:167
static sys_handle duplicate(sys_handle h, bool inherit=false)
Duplicates given object.
Definition system.hpp:189
static void close(sys_handle h)
Closes object.
Definition system.hpp:173