stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
system.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023 Amebis
4*/
5
6#pragma once
7
8#ifdef _WIN32
9#define NOMINMAX // Collides with std::min/max
10#include <windows.h>
11#include <intsafe.h>
12#include <oaidl.h>
13#include <tchar.h>
14#else
15#define _LARGEFILE64_SOURCE
16#include <sys/types.h>
17#include <unistd.h>
18#endif
19#include "compat.hpp"
20#include <assert.h>
21#include <stdexcept>
22#include <string>
23
24// In case somebody #included <windows.h> before us and didn't #define NOMINMAX
25#ifdef _WIN32
26#ifdef min
27#undef min
28#endif
29#ifdef max
30#undef max
31#endif
32#endif
33
34#if defined(_WIN32)
35#define PATH_SEPARATOR '\\'
36#define PATH_SEPARATOR_STR "\\"
37#else
38#define PATH_SEPARATOR '/'
39#define PATH_SEPARATOR_STR "/"
40#endif
41
42namespace stdex
43{
47#if defined(_WIN32)
48 using sys_handle = HANDLE;
49 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
50#else
51 using sys_handle = int;
52 const sys_handle invalid_handle = (sys_handle)-1;
53#endif
54
58#if defined(_WIN32)
59 using schar_t = TCHAR;
60#else
61 using schar_t = char;
62#define _T(x) x
63#endif
64
69 using sys_char = schar_t;
70
74 using sstring = std::basic_string<stdex::schar_t>;
75
80 using sys_string = sstring;
81
86 {
87 public:
88 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
89
90 sys_object(_In_ const sys_object& other) : m_h(other.m_h != invalid_handle ? duplicate(other.m_h, false) : invalid_handle) {}
91
92 sys_object& operator =(_In_ const sys_object& other)
93 {
94 if (this != std::addressof(other)) {
95 if (m_h != invalid_handle)
96 close(m_h);
97 m_h = other.m_h != invalid_handle ? duplicate(other.m_h, false) : invalid_handle;
98 }
99 return *this;
100 }
101
102 sys_object(_Inout_ sys_object&& other) noexcept : m_h(other.m_h)
103 {
104 other.m_h = invalid_handle;
105 }
106
107 sys_object& operator =(_Inout_ sys_object&& other) noexcept
108 {
109 if (this != std::addressof(other)) {
110 if (m_h != invalid_handle)
111 close(m_h);
112 m_h = other.m_h;
113 other.m_h = invalid_handle;
114 }
115 return *this;
116 }
117
118 virtual ~sys_object()
119 {
120 if (m_h != invalid_handle)
121 close(m_h);
122 }
123
127 virtual void close()
128 {
129 if (m_h != invalid_handle) {
130 close(m_h);
131 m_h = invalid_handle;
132 }
133 }
134
138 inline operator bool() const noexcept { return m_h != invalid_handle; }
139
143 inline sys_handle get() const noexcept { return m_h; }
144
145 protected:
149 static void close(_In_ sys_handle h)
150 {
151#ifdef _WIN32
152 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
153#else
154 if (::close(h) >= 0 || errno == EBADF)
155#endif
156 return;
157 throw std::runtime_error("failed to close handle");
158 }
159
163 static sys_handle duplicate(_In_ sys_handle h, _In_ bool inherit)
164 {
165 sys_handle h_new;
166#ifdef _WIN32
167 HANDLE process = GetCurrentProcess();
168 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
169#else
170 _Unreferenced_(inherit);
171 if ((h_new = dup(h)) >= 0)
172#endif
173 return h_new;
174 throw std::runtime_error("failed to duplicate handle");
175 }
176
177 protected:
178 sys_handle m_h;
179 };
180
181#ifdef _WIN32
182 template <class T>
183 class safearray_accessor
184 {
185 public:
186 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
187 {
188 HRESULT hr = SafeArrayAccessData(sa, reinterpret_cast<void HUGEP**>(&m_data));
189 if (FAILED(hr))
190 throw std::invalid_argument("SafeArrayAccessData failed");
191 }
192
193 ~safearray_accessor()
194 {
195 SafeArrayUnaccessData(m_sa);
196 }
197
198 T* data() const { return m_data; }
199
200 protected:
201 LPSAFEARRAY m_sa;
202 T* m_data;
203 };
204
208 struct SafeArrayDestroy_delete
209 {
213 void operator()(_In_ LPSAFEARRAY sa) const
214 {
215 SafeArrayDestroy(sa);
216 }
217 };
218
222 struct SysFreeString_delete
223 {
227 void operator()(_In_ BSTR sa) const
228 {
229 SysFreeString(sa);
230 }
231 };
232#endif
233}
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:86
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:143
virtual void close()
Closes object.
Definition system.hpp:127
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:163
static void close(sys_handle h)
Closes object.
Definition system.hpp:149