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#include "compat.hpp"
9#ifdef _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>
26
27#if defined(_WIN32)
28#define PATH_SEPARATOR '\\'
29#define PATH_SEPARATOR_STR "\\"
30#else
31#define PATH_SEPARATOR '/'
32#define PATH_SEPARATOR_STR "/"
33#endif
34
35namespace stdex
36{
40#if defined(_WIN32)
41 using sys_handle = HANDLE;
42 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
43#else
44 using sys_handle = int;
45 const sys_handle invalid_handle = (sys_handle)-1;
46#endif
47
51#if defined(_WIN32)
52 inline DWORD sys_error() { return GetLastError(); }
53#else
54 inline int sys_error() { return errno; }
55#endif
56
60#if defined(_WIN32)
61 using schar_t = TCHAR;
62#else
63 using schar_t = char;
64#define _T(x) x
65#endif
66
71 using sys_char = schar_t;
72
76 using sstring = std::basic_string<stdex::schar_t>;
77
82 using sys_string = sstring;
83
87 using sregex = std::basic_regex<stdex::schar_t>;
88
93 {
94 public:
95 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
96
97 sys_object(_In_ const sys_object& other) : m_h(other.m_h != invalid_handle ? duplicate(other.m_h, false) : invalid_handle) {}
98
99 sys_object& operator =(_In_ const sys_object& other)
100 {
101 if (this != std::addressof(other)) {
102 if (m_h != invalid_handle)
103 close(m_h);
104 m_h = other.m_h != invalid_handle ? duplicate(other.m_h, false) : invalid_handle;
105 }
106 return *this;
107 }
108
109 sys_object(_Inout_ sys_object&& other) noexcept : m_h(other.m_h)
110 {
111 other.m_h = invalid_handle;
112 }
113
114 sys_object& operator =(_Inout_ sys_object&& other) noexcept
115 {
116 if (this != std::addressof(other)) {
117 if (m_h != invalid_handle)
118 close(m_h);
119 m_h = other.m_h;
120 other.m_h = invalid_handle;
121 }
122 return *this;
123 }
124
125 virtual ~sys_object() noexcept(false)
126 {
127 if (m_h != invalid_handle)
128 close(m_h);
129 }
130
134 virtual void close()
135 {
136 if (m_h != invalid_handle) {
137 close(m_h);
138 m_h = invalid_handle;
139 }
140 }
141
145 inline operator bool() const noexcept { return m_h != invalid_handle; }
146
150 inline sys_handle get() const noexcept { return m_h; }
151
152 protected:
156 static void close(_In_ sys_handle h)
157 {
158#ifdef _WIN32
159 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
160 return;
161 throw std::system_error(GetLastError(), std::system_category(), "CloseHandle failed");
162#else
163 if (::close(h) >= 0 || errno == EBADF)
164 return;
165 throw std::system_error(errno, std::system_category(), "close failed");
166#endif
167 }
168
172 static sys_handle duplicate(_In_ sys_handle h, _In_ bool inherit)
173 {
174 sys_handle h_new;
175#ifdef _WIN32
176 HANDLE process = GetCurrentProcess();
177 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
178 return h_new;
179 throw std::system_error(GetLastError(), std::system_category(), "DuplicateHandle failed");
180#else
181 _Unreferenced_(inherit);
182 if ((h_new = dup(h)) >= 0)
183 return h_new;
184 throw std::system_error(errno, std::system_category(), "dup failed");
185#endif
186 }
187
188 protected:
189 sys_handle m_h;
190 };
191
192#ifdef _WIN32
193 template <class T>
194 class safearray_accessor
195 {
196 public:
197 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
198 {
199 HRESULT hr = SafeArrayAccessData(sa, reinterpret_cast<void HUGEP**>(&m_data));
200 if (FAILED(hr))
201 throw std::system_error(hr, std::system_category(), "SafeArrayAccessData failed");
202 }
203
204 ~safearray_accessor()
205 {
206 SafeArrayUnaccessData(m_sa);
207 }
208
209 T* data() const { return m_data; }
210
211 protected:
212 LPSAFEARRAY m_sa;
213 T* m_data;
214 };
215
219 struct SafeArrayDestroy_delete
220 {
224 void operator()(_In_ LPSAFEARRAY sa) const
225 {
226 SafeArrayDestroy(sa);
227 }
228 };
229
233 struct SysFreeString_delete
234 {
238 void operator()(_In_ BSTR sa) const
239 {
240 SysFreeString(sa);
241 }
242 };
243#endif
244}
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:93
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:150
virtual void close()
Closes object.
Definition system.hpp:134
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:172
static void close(sys_handle h)
Closes object.
Definition system.hpp:156