stdex
Additional custom or not Standard C++ covered algorithms
Loading...
Searching...
No Matches
system.hpp
1/*
2 SPDX-License-Identifier: MIT
3 Copyright © 2023-2024 Amebis
4*/
5
6#pragma once
7
8#include "compat.hpp"
9#if defined(_WIN32)
10#include "windows.h"
11#include <oaidl.h>
12#include <tchar.h>
13#else
14#ifndef _LARGEFILE64_SOURCE
15#define _LARGEFILE64_SOURCE // TODO: Make this -D compile-time project setting
16#endif
17#include <grp.h>
18#include <pwd.h>
19#include <string.h>
20#include <sys/types.h>
21#include <unistd.h>
22#endif
23#include <regex>
24#include <stdexcept>
25#include <string_view>
26#include <string>
27
28#if defined(_WIN32)
29#define PATH_SEPARATOR '\\'
30#define PATH_SEPARATOR_STR "\\"
31#else
32#define PATH_SEPARATOR '/'
33#define PATH_SEPARATOR_STR "/"
34#endif
35
36namespace stdex
37{
41#if defined(_WIN32)
42 using sys_handle = HANDLE;
43 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
44#else
45 using sys_handle = int;
46 const sys_handle invalid_handle = (sys_handle)-1;
47#endif
48
52#if defined(_WIN32)
53 inline DWORD sys_error() { return GetLastError(); }
54#else
55 inline int sys_error() { return errno; }
56#endif
57
61#if defined(_WIN32)
62 using schar_t = TCHAR;
63#else
64 using schar_t = char;
65#define _T(x) x
66#endif
67
72 using sys_char = schar_t;
73
77 using sstring = std::basic_string<stdex::schar_t>;
78
83 using sys_string = sstring;
84
88 using sstring_view = std::basic_string_view<stdex::schar_t, std::char_traits<stdex::schar_t>>;
89
93 using sregex = std::basic_regex<stdex::schar_t>;
94
98 template <class T = sys_handle, class TR = sys_object_traits>
100 {
101 public:
102 basic_sys_object(_In_opt_ T h = TR::invalid_handle) : m_h(h) {}
103
104 basic_sys_object(_In_ const basic_sys_object<T, TR>& other) : m_h(other.m_h != TR::invalid_handle ? TR::duplicate(other.m_h) : TR::invalid_handle) {}
105
106 basic_sys_object& operator =(_In_ const basic_sys_object<T, TR>& other)
107 {
108 if (this != std::addressof(other)) {
109 if (m_h != TR::invalid_handle)
110 TR::close(m_h);
111 m_h = other.m_h != TR::invalid_handle ? TR::duplicate(other.m_h) : TR::invalid_handle;
112 }
113 return *this;
114 }
115
116 basic_sys_object(_Inout_ basic_sys_object<T, TR>&& other) noexcept : m_h(other.m_h)
117 {
118 other.m_h = TR::invalid_handle;
119 }
120
121 basic_sys_object& operator =(_Inout_ basic_sys_object<T, TR>&& other) noexcept
122 {
123 if (this != std::addressof(other)) {
124 if (m_h != TR::invalid_handle)
125 TR::close(m_h);
126 m_h = other.m_h;
127 other.m_h = TR::invalid_handle;
128 }
129 return *this;
130 }
131
132 virtual ~basic_sys_object() noexcept(false)
133 {
134 if (m_h != TR::invalid_handle)
135 TR::close(m_h);
136 }
137
141 virtual void close()
142 {
143 if (m_h != TR::invalid_handle) {
144 TR::close(m_h);
145 m_h = TR::invalid_handle;
146 }
147 }
148
152 operator bool() const noexcept { return m_h != TR::invalid_handle; }
153
157 T get() const noexcept { return m_h; }
158
159 protected:
160 T m_h;
161 };
162
167 {
168 static inline const sys_handle invalid_handle = stdex::invalid_handle;
169
173 static void close(_In_ sys_handle h)
174 {
175#if defined(_WIN32)
176 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
177 return;
178 throw std::system_error(GetLastError(), std::system_category(), "CloseHandle failed");
179#else
180 if (::close(h) >= 0 || errno == EBADF)
181 return;
182 throw std::system_error(errno, std::system_category(), "close failed");
183#endif
184 }
185
189 static sys_handle duplicate(_In_ sys_handle h, _In_ bool inherit = false)
190 {
191 sys_handle h_new;
192#if defined(_WIN32)
193 HANDLE process = GetCurrentProcess();
194 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
195 return h_new;
196 throw std::system_error(GetLastError(), std::system_category(), "DuplicateHandle failed");
197#else
198 _Unreferenced_(inherit);
199 if ((h_new = dup(h)) >= 0)
200 return h_new;
201 throw std::system_error(errno, std::system_category(), "dup failed");
202#endif
203 }
204 };
205
209 using sys_object = basic_sys_object<sys_handle, sys_object_traits>;
210
211#ifdef _WIN32
212 template <class T>
213 class safearray_accessor
214 {
215 public:
216 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
217 {
218 HRESULT hr = SafeArrayAccessData(sa, reinterpret_cast<void HUGEP**>(&m_data));
219 if (FAILED(hr))
220 throw std::system_error(hr, std::system_category(), "SafeArrayAccessData failed");
221 }
222
223 ~safearray_accessor()
224 {
225 SafeArrayUnaccessData(m_sa);
226 }
227
228 T* data() const { return m_data; }
229
230 protected:
231 LPSAFEARRAY m_sa;
232 T* m_data;
233 };
234
238 struct SafeArrayDestroy_delete
239 {
243 void operator()(_In_ LPSAFEARRAY sa) const
244 {
245 SafeArrayDestroy(sa);
246 }
247 };
248
252 struct SysFreeString_delete
253 {
257 void operator()(_In_ BSTR sa) const
258 {
259 SysFreeString(sa);
260 }
261 };
262#endif
263}
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