18#define _LARGEFILE64_SOURCE
38#define PATH_SEPARATOR '\\'
39#define PATH_SEPARATOR_STR "\\"
41#define PATH_SEPARATOR '/'
42#define PATH_SEPARATOR_STR "/"
51 using sys_handle = HANDLE;
52 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
54 using sys_handle = int;
55 const sys_handle invalid_handle = (sys_handle)-1;
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;
91 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
97 if (
this != std::addressof(other)) {
98 if (m_h != invalid_handle)
100 m_h = other.m_h != invalid_handle ?
duplicate(other.m_h,
false) : invalid_handle;
107 other.m_h = invalid_handle;
112 if (
this != std::addressof(other)) {
113 if (m_h != invalid_handle)
116 other.m_h = invalid_handle;
123 if (m_h != invalid_handle)
132 if (m_h != invalid_handle) {
134 m_h = invalid_handle;
141 inline operator bool() const noexcept {
return m_h != invalid_handle; }
146 inline sys_handle
get() const noexcept {
return m_h; }
152 static void close(_In_ sys_handle h)
155 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
157 if (
::close(h) >= 0 || errno == EBADF)
160 throw std::runtime_error(
"failed to close handle");
166 static sys_handle
duplicate(_In_ sys_handle h, _In_
bool inherit)
170 HANDLE process = GetCurrentProcess();
171 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
173 _Unreferenced_(inherit);
174 if ((h_new = dup(h)) >= 0)
177 throw std::runtime_error(
"failed to duplicate handle");
186 class safearray_accessor
189 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
191 HRESULT hr = SafeArrayAccessData(sa,
reinterpret_cast<void HUGEP**
>(&m_data));
193 throw std::invalid_argument(
"SafeArrayAccessData failed");
196 ~safearray_accessor()
198 SafeArrayUnaccessData(m_sa);
201 T* data()
const {
return m_data; }
211 struct SafeArrayDestroy_delete
216 void operator()(_In_ LPSAFEARRAY sa)
const
218 SafeArrayDestroy(sa);
225 struct SysFreeString_delete
230 void operator()(_In_ BSTR sa)
const
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:89
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:146
virtual void close()
Closes object.
Definition system.hpp:130
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:166
static void close(sys_handle h)
Closes object.
Definition system.hpp:152