-
Notifications
You must be signed in to change notification settings - Fork 541
/
Copy pathcpp-util.hh
272 lines (229 loc) · 7.49 KB
/
cpp-util.hh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#ifndef __CPP_UTIL_HH
#define __CPP_UTIL_HH
#include <array>
#include <cstdarg>
#include <cstdlib>
#include <cstdio>
#include <concepts>
#include <memory>
#include <ranges>
#include <source_location>
#include <string>
#include <string_view>
#include <type_traits>
#include <vector>
#include <semaphore.h>
#include <android/log.h>
#include "helpers.hh"
namespace xamarin::android::detail {
[[gnu::always_inline, gnu::flatten]]
static inline const char*
_format_message (const char *format, ...) noexcept
{
va_list ap;
va_start (ap, format);
char *message;
int ret = vasprintf (&message, format, ap);
va_end (ap);
return ret == -1 ? "Out of memory" : message;
}
[[gnu::always_inline]]
static inline std::string get_function_name (const char *signature)
{
using std::operator""sv;
std::string_view sig { signature };
if (sig.length () == 0) {
return "<unknown function>";
}
auto splitSignature = sig | std::views::split ("::"sv) | std::ranges::to<std::vector<std::string>> ();
std::string ret;
if (splitSignature.size () > 1) {
ret.append (splitSignature [splitSignature.size () - 2]);
ret.append ("::"sv);
}
std::string_view func_name { splitSignature[splitSignature.size () - 1] };
std::string_view::size_type args_pos = func_name.find ('(');
std::string_view::size_type name_start_pos = func_name.find (' ');
if (name_start_pos == std::string_view::npos) {
name_start_pos = 0;
} else {
name_start_pos++; // point to after the space which separates return type from name
if (name_start_pos >= func_name.length ()) [[unlikely]] {
name_start_pos = 0;
}
}
if (args_pos == std::string_view::npos) {
ret.append (func_name.substr (name_start_pos));
} else {
// If there's a snafu with positions, start from 0
if (name_start_pos >= args_pos || name_start_pos > func_name.length ()) [[unlikely]] {
name_start_pos = 0;
}
ret.append (func_name.substr (name_start_pos, args_pos - name_start_pos));
}
return ret;
}
}
template<std::invocable<> F>
[[gnu::always_inline, gnu::flatten]]
static inline void
abort_unless (bool condition, F&& get_message, std::source_location sloc = std::source_location::current ()) noexcept
{
static_assert (std::is_same<typename std::invoke_result<F>::type, const char*>::value, "get_message must return 'const char*'");
if (condition) [[likely]] {
return;
}
xamarin::android::Helpers::abort_application (std::invoke (get_message), true /* log_location */, sloc);
}
[[gnu::always_inline, gnu::flatten]]
static inline void
abort_unless (bool condition, const char *message, std::source_location sloc = std::source_location::current ()) noexcept
{
if (condition) [[likely]] {
return;
}
xamarin::android::Helpers::abort_application (message, true /* log_location */, sloc);
}
template<typename T>
[[gnu::always_inline, gnu::flatten]]
static inline void
abort_if_invalid_pointer_argument (T *ptr, const char *ptr_name, std::source_location sloc = std::source_location::current ()) noexcept
{
abort_unless (
ptr != nullptr,
[&ptr_name, &sloc] {
return xamarin::android::detail::_format_message (
"%s: parameter '%s' must be a valid pointer",
xamarin::android::detail::get_function_name (sloc.function_name ()).c_str (),
ptr_name
);
},
sloc
);
}
[[gnu::always_inline, gnu::flatten]]
static inline void
abort_if_negative_integer_argument (int arg, const char *arg_name, std::source_location sloc = std::source_location::current ()) noexcept
{
abort_unless (
arg > 0,
[&arg_name, &sloc] {
return xamarin::android::detail::_format_message (
"%s: parameter '%s' must be a valid pointer",
xamarin::android::detail::get_function_name (sloc.function_name ()).c_str (),
arg_name
);
},
sloc
);
}
// Helper to use in "printf debugging". Normally not used in code anywhere. No code should be shipped with any
// of the calls present.
force_inline inline void pd_log_location (std::source_location sloc = std::source_location::current ()) noexcept
{
log_info_nocheck (LOG_DEFAULT, "loc: {}:{} ('{}')", sloc.file_name (), sloc.line (), sloc.function_name ());
}
namespace xamarin::android
{
template <typename T>
struct CDeleter final
{
using UnderlyingType = std::remove_cv_t<T>;
void operator() (T* p)
{
UnderlyingType *ptr;
if constexpr (std::is_const_v<T>) {
ptr = const_cast<std::remove_const_t<T>*> (p);
} else {
ptr = p;
}
std::free (reinterpret_cast<void*>(ptr));
}
};
template <typename T>
using c_unique_ptr = std::unique_ptr<T, CDeleter<T>>;
template<size_t Size>
using char_array = std::array<char, Size>;
template<size_t ...Length>
constexpr auto concat_const (const char (&...parts)[Length])
{
// `parts` being constant string arrays, Length for each of them includes the trailing NUL byte, thus the
// `sizeof... (Length)` part which subtracts the number of template parameters - the amount of NUL bytes so that
// we don't waste space.
constexpr size_t total_length = (... + Length) - sizeof... (Length);
char_array<total_length + 1> ret; // lgtm [cpp/paddingbyteinformationdisclosure] the buffer is filled in the loop below
ret[total_length] = 0;
size_t i = 0uz;
for (char const* from : {parts...}) {
for (; *from != '\0'; i++) {
ret[i] = *from++;
}
}
return ret;
};
template<class T>
concept StringViewPart = std::is_same_v<T, std::string_view>;
template<size_t TotalLength, StringViewPart ...T>
consteval auto concat_string_views (T const&... parts)
{
std::array<char, TotalLength + 1> ret; // lgtm [cpp/paddingbyteinformationdisclosure] the buffer is filled in the loop below
ret[TotalLength] = 0;
size_t i = 0;
for (std::string_view const& sv : {parts...}) {
for (const char ch : sv) {
ret[i] = ch;
i++;
}
}
return ret;
}
consteval size_t calc_size (std::string_view const& sv1) noexcept
{
return sv1.size ();
}
template<StringViewPart ...T>
consteval size_t calc_size (std::string_view const& sv1, T const&... other_svs) noexcept
{
return sv1.size () + calc_size (other_svs...);
}
template <typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
constexpr TEnum operator & (TEnum l, TEnum r) noexcept
{
using etype = std::underlying_type_t<TEnum>;
return static_cast<TEnum>(static_cast<etype>(l) & static_cast<etype>(r));
}
template <typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
constexpr TEnum& operator &= (TEnum& l, TEnum r) noexcept
{
return l = (l & r);
}
template <typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
constexpr TEnum operator | (TEnum l, TEnum r) noexcept
{
using etype = std::underlying_type_t<TEnum>;
return static_cast<TEnum>(static_cast<etype>(l) | static_cast<etype>(r));
}
template <typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
constexpr TEnum& operator |= (TEnum& l, TEnum r) noexcept
{
return l = (l | r);
}
template <typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
constexpr TEnum operator ~ (TEnum r) noexcept
{
using etype = std::underlying_type_t<TEnum>;
return static_cast<TEnum> (~static_cast<etype>(r));
}
template <typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
constexpr TEnum operator ^ (TEnum l, TEnum r) noexcept
{
using etype = std::underlying_type_t<TEnum>;
return static_cast<TEnum>(static_cast<etype>(l) ^ static_cast<etype>(r));
}
template <typename TEnum, std::enable_if_t<std::is_enum_v<TEnum>, int> = 0>
constexpr TEnum& operator ^= (TEnum& l, TEnum r) noexcept
{
return l = (l ^ r);
}
}
#endif // !def __CPP_UTIL_HH