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#define _LARGEFILE64_SOURCE // TODO: Make this -D compile-time project setting
15#include <grp.h>
16#include <pwd.h>
17#include <string.h>
18#include <sys/types.h>
19#include <unistd.h>
20#endif
21#include <regex>
22#include <stdexcept>
23#include <string>
24
25#if defined(_WIN32)
26#define PATH_SEPARATOR '\\'
27#define PATH_SEPARATOR_STR "\\"
28#else
29#define PATH_SEPARATOR '/'
30#define PATH_SEPARATOR_STR "/"
31#endif
32
33namespace stdex
34{
38#if defined(_WIN32)
39 using sys_handle = HANDLE;
40 const sys_handle invalid_handle = INVALID_HANDLE_VALUE;
41#else
42 using sys_handle = int;
43 const sys_handle invalid_handle = (sys_handle)-1;
44#endif
45
49#if defined(_WIN32)
50 inline DWORD sys_error() { return GetLastError(); }
51#else
52 inline int sys_error() { return errno; }
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
85 using sregex = std::basic_regex<stdex::schar_t>;
86
91 {
92 public:
93 sys_object(_In_opt_ sys_handle h = invalid_handle) : m_h(h) {}
94
95 sys_object(_In_ const sys_object& other) : m_h(other.m_h != invalid_handle ? duplicate(other.m_h, false) : invalid_handle) {}
96
97 sys_object& operator =(_In_ const sys_object& other)
98 {
99 if (this != std::addressof(other)) {
100 if (m_h != invalid_handle)
101 close(m_h);
102 m_h = other.m_h != invalid_handle ? duplicate(other.m_h, false) : invalid_handle;
103 }
104 return *this;
105 }
106
107 sys_object(_Inout_ sys_object&& other) noexcept : m_h(other.m_h)
108 {
109 other.m_h = invalid_handle;
110 }
111
112 sys_object& operator =(_Inout_ sys_object&& other) noexcept
113 {
114 if (this != std::addressof(other)) {
115 if (m_h != invalid_handle)
116 close(m_h);
117 m_h = other.m_h;
118 other.m_h = invalid_handle;
119 }
120 return *this;
121 }
122
123 virtual ~sys_object() noexcept(false)
124 {
125 if (m_h != invalid_handle)
126 close(m_h);
127 }
128
132 virtual void close()
133 {
134 if (m_h != invalid_handle) {
135 close(m_h);
136 m_h = invalid_handle;
137 }
138 }
139
143 inline operator bool() const noexcept { return m_h != invalid_handle; }
144
148 inline sys_handle get() const noexcept { return m_h; }
149
150 protected:
154 static void close(_In_ sys_handle h)
155 {
156#ifdef _WIN32
157 if (CloseHandle(h) || GetLastError() == ERROR_INVALID_HANDLE)
158 return;
159 throw std::system_error(GetLastError(), std::system_category(), "CloseHandle failed");
160#else
161 if (::close(h) >= 0 || errno == EBADF)
162 return;
163 throw std::system_error(errno, std::system_category(), "close failed");
164#endif
165 }
166
170 static sys_handle duplicate(_In_ sys_handle h, _In_ bool inherit)
171 {
172 sys_handle h_new;
173#ifdef _WIN32
174 HANDLE process = GetCurrentProcess();
175 if (DuplicateHandle(process, h, process, &h_new, 0, inherit, DUPLICATE_SAME_ACCESS))
176 return h_new;
177 throw std::system_error(GetLastError(), std::system_category(), "DuplicateHandle failed");
178#else
179 _Unreferenced_(inherit);
180 if ((h_new = dup(h)) >= 0)
181 return h_new;
182 throw std::system_error(errno, std::system_category(), "dup failed");
183#endif
184 }
185
186 protected:
187 sys_handle m_h;
188 };
189
190#ifdef _WIN32
191 template <class T>
192 class safearray_accessor
193 {
194 public:
195 safearray_accessor(_In_ LPSAFEARRAY sa) : m_sa(sa)
196 {
197 HRESULT hr = SafeArrayAccessData(sa, reinterpret_cast<void HUGEP**>(&m_data));
198 if (FAILED(hr))
199 throw std::system_error(hr, std::system_category(), "SafeArrayAccessData failed");
200 }
201
202 ~safearray_accessor()
203 {
204 SafeArrayUnaccessData(m_sa);
205 }
206
207 T* data() const { return m_data; }
208
209 protected:
210 LPSAFEARRAY m_sa;
211 T* m_data;
212 };
213
217 struct SafeArrayDestroy_delete
218 {
222 void operator()(_In_ LPSAFEARRAY sa) const
223 {
224 SafeArrayDestroy(sa);
225 }
226 };
227
231 struct SysFreeString_delete
232 {
236 void operator()(_In_ BSTR sa) const
237 {
238 SysFreeString(sa);
239 }
240 };
241#endif
242}
Operating system object (file, pipe, anything with an OS handle etc.)
Definition system.hpp:91
sys_handle get() const noexcept
Returns object handle.
Definition system.hpp:148
virtual void close()
Closes object.
Definition system.hpp:132
static sys_handle duplicate(sys_handle h, bool inherit)
Duplicates given object.
Definition system.hpp:170
static void close(sys_handle h)
Closes object.
Definition system.hpp:154