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
34namespace stdex
35{
39#if defined(_WIN32)
40 using sys_handle = HANDLE;
41 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
42#else
43 using sys_handle = int;
44 const sys_handle invalid_handle = (sys_handle)-1;
45#endif
46
50#if defined(_WIN32)
51 using schar_t = TCHAR;
52#else
53 using schar_t = char;
54#define _T(x) x
55#endif
56
61 using sys_char = schar_t;
62
66 using sstring = std::basic_string<stdex::schar_t>;
67
72 using sys_string = sstring;
73
78 {
79 public:
80 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
81
82 sys_object(_In_ const sys_object& other) : m_h(other.m_h != invalid_handle ? duplicate(other.m_h, false) : invalid_handle) {}
83
84 sys_object& operator =(_In_ const sys_object& other)
85 {
86 if (this != std::addressof(other)) {
87 if (m_h != invalid_handle)
88 close(m_h);
89 m_h = other.m_h != invalid_handle ? duplicate(other.m_h, false) : invalid_handle;
90 }
91 return *this;
92 }
93
94 sys_object(_Inout_ sys_object&& other) noexcept : m_h(other.m_h)
95 {
96 other.m_h = invalid_handle;
97 }
98
99 sys_object& operator =(_Inout_ sys_object&& other) noexcept
100 {
101 if (this != std::addressof(other)) {
102 if (m_h != invalid_handle)
103 close(m_h);
104 m_h = other.m_h;
105 other.m_h = invalid_handle;
106 }
107 return *this;
108 }
109
110 virtual ~sys_object()
111 {
112 if (m_h != invalid_handle)
113 close(m_h);
114 }
115
119 virtual void close()
120 {
121 if (m_h != invalid_handle) {
122 close(m_h);
123 m_h = invalid_handle;
124 }
125 }
126
130 inline operator bool() const noexcept { return m_h != invalid_handle; }
131
135 inline sys_handle get() const noexcept { return m_h; }
136
137 protected:
141 static void close(_In_ sys_handle h)
142 {
143#ifdef _WIN32
144 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
145#else
146 if (::close(h) >= 0 || errno == EBADF)
147#endif
148 return;
149 throw std::runtime_error("failed to close handle");
150 }
151
155 static sys_handle duplicate(_In_ sys_handle h, _In_ bool inherit)
156 {
157 sys_handle h_new;
158#ifdef _WIN32
159 HANDLE process = GetCurrentProcess();
160 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
161#else
162 _Unreferenced_(inherit);
163 if ((h_new = dup(h)) >= 0)
164#endif
165 return h_new;
166 throw std::runtime_error("failed to duplicate handle");
167 }
168
169 protected:
170 sys_handle m_h;
171 };
172
173#ifdef _WIN32
174 template <class T>
175 class safearray_accessor
176 {
177 public:
178 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
179 {
180 HRESULT hr = SafeArrayAccessData(sa, reinterpret_cast<void HUGEP**>(&m_data));
181 if (FAILED(hr))
182 throw std::invalid_argument("SafeArrayAccessData failed");
183 }
184
185 ~safearray_accessor()
186 {
187 SafeArrayUnaccessData(m_sa);
188 }
189
190 T* data() const { return m_data; }
191
192 protected:
193 LPSAFEARRAY m_sa;
194 T* m_data;
195 };
196
200 struct SafeArrayDestroy_delete
201 {
205 void operator()(_In_ LPSAFEARRAY sa) const
206 {
207 SafeArrayDestroy(sa);
208 }
209 };
210
214 struct SysFreeString_delete
215 {
219 void operator()(_In_ BSTR sa) const
220 {
221 SysFreeString(sa);
222 }
223 };
224#endif
225}
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:78
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:135
virtual void close()
Closes object.
Definition system.hpp:119
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:155
static void close(sys_handle h)
Closes object.
Definition system.hpp:141