Repository navigation
Expand file tree
/
Copy pathlxwil.h
More file actions
524 lines (437 loc) · 13.4 KB
/
Copy pathlxwil.h
File metadata and controls
524 lines (437 loc) · 13.4 KB
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.
#pragma once
namespace wil
{
#define FAIL_FAST() raise(SIGABRT);
#define FAIL_FAST_CAUGHT_EXCEPTION() FAIL_FAST()
#define FAIL_FAST_IF(condition) if ((condition)) { FAIL_FAST() }
typedef void LogFunction(const char *message, const char *exceptionDescription) noexcept;
inline LogFunction *g_LogExceptionCallback{};
namespace details
{
struct FailureInfo
{
const char *File;
int Line;
const char *Function;
};
}
class ResultException : public std::exception
{
public:
ResultException(int result, details::FailureInfo info) noexcept
: m_Result{ result }, m_Info{ info }
{
}
~ResultException() noexcept
{
delete[] m_What;
}
const char *what() const noexcept override
{
constexpr size_t bufferSize = 4096;
if (m_What == nullptr)
{
m_What = new(std::nothrow) char[bufferSize]{};
if (m_What == nullptr)
{
return strerror(m_Result);
}
snprintf(m_What, bufferSize, "%s @%s:%d (%s)\n", strerror(m_Result), m_Info.File, m_Info.Line, m_Info.Function);
}
return m_What;
}
int GetErrorCode() const noexcept
{
return m_Result;
}
private:
mutable char *m_What{};
int m_Result;
details::FailureInfo m_Info;
};
namespace details
{
template <typename TLambda>
class lambda_call
{
public:
lambda_call(const lambda_call&) = delete;
lambda_call& operator=(const lambda_call&) = delete;
lambda_call& operator=(lambda_call&& other) = delete;
explicit lambda_call(TLambda&& lambda) noexcept : m_lambda(std::move(lambda))
{
static_assert(std::is_same<decltype(lambda()), void>::value, "scope_exit lambdas must not have a return value");
static_assert(!std::is_lvalue_reference<TLambda>::value && !std::is_rvalue_reference<TLambda>::value,
"scope_exit should only be directly used with a lambda");
}
lambda_call(lambda_call&& other) noexcept : m_lambda(std::move(other.m_lambda)), m_call(other.m_call)
{
other.m_call = false;
}
~lambda_call() noexcept
{
reset();
}
// Ensures the scope_exit lambda will not be called
void release() noexcept
{
m_call = false;
}
// Executes the scope_exit lambda immediately if not yet run; ensures it will not run again
void reset() noexcept
{
if (m_call)
{
m_call = false;
m_lambda();
}
}
// Returns true if the scope_exit lambda is still going to be executed
explicit operator bool() const noexcept
{
return m_call;
}
protected:
TLambda m_lambda;
bool m_call = true;
};
inline void ThrowErrorIf(bool condition, int error, FailureInfo info)
{
if (condition)
{
throw ::wil::ResultException(error, info);
}
}
inline void LogFailure(const char *message, const char *exceptionDescription) noexcept
{
auto callback = g_LogExceptionCallback;
if (callback != nullptr)
{
callback(message, exceptionDescription);
}
else
{
if (message != nullptr)
{
fputs(message, stderr);
fputs("\n", stderr);
}
if (exceptionDescription != nullptr)
{
fputs("Exception: ", stderr);
fputs(exceptionDescription, stderr);
fputs("\n", stderr);
}
}
}
inline void LogCaughtException(const char *message)
{
try
{
throw;
}
catch (const std::exception &ex)
{
LogFailure(message, ex.what());
}
catch (...)
{
LogFailure(message, nullptr);
}
}
}
inline int ResultFromCaughtException()
{
try
{
throw;
}
catch (wil::ResultException &ex)
{
return ex.GetErrorCode();
}
catch (std::bad_alloc &)
{
return ENOMEM;
}
catch (...)
{
}
// Unknown exception type.
return EINVAL;
}
#define __WIL_ERROR_INFO { __FILE__, __LINE__, __FUNCTION__ }
#define THROW_ERRNO(Error) throw ::wil::ResultException(Error, __WIL_ERROR_INFO)
#define THROW_ERRNO_IF(Error, Condition) ::wil::details::ThrowErrorIf((Condition), (Error), __WIL_ERROR_INFO)
#define THROW_ERRNO_IF_FALSE(Error, Condition) ::wil::details::ThrowErrorIf(!(Condition), (Error), __WIL_ERROR_INFO)
#define THROW_LAST_ERROR_IF(Condition) THROW_ERRNO_IF(errno, (Condition));
#define THROW_LAST_ERROR_IF_FALSE(Condition) THROW_ERRNO_IF_FALSE(errno, (Condition))
#define THROW_INVALID() THROW_ERRNO(EINVAL)
#define THROW_INVALID_IF(Condition) THROW_ERRNO_IF(EINVAL, (Condition))
#define THROW_UNEXPECTED_IF(Condition) THROW_ERRNO_IF(EINVAL, (Condition))
#define LOG_CAUGHT_EXCEPTION() ::wil::details::LogCaughtException(nullptr);
#define LOG_CAUGHT_EXCEPTION_MSG(msg) ::wil::details::LogCaughtException(msg);
#define RETURN_CAUGHT_EXCEPTION() return -::wil::ResultFromCaughtException()
#define CATCH_RETURN() catch (...) { RETURN_CAUGHT_EXCEPTION(); }
#define CATCH_RETURN_ERRNO() \
catch (...) \
{ \
LOG_CAUGHT_EXCEPTION(); \
errno = ::wil::ResultFromCaughtException(); \
return -1; \
}
#define CATCH_LOG() catch (...) { LOG_CAUGHT_EXCEPTION(); }
#define CATCH_LOG_MSG(msg) catch (...) { LOG_CAUGHT_EXCEPTION_MSG(msg); }
class unique_dir
{
public:
static constexpr DIR* invalid_dir = nullptr;
unique_dir(DIR* dir = invalid_dir) noexcept
: m_Dir{ dir }
{
}
~unique_dir() noexcept
{
reset();
}
unique_dir(const unique_dir &) = delete;
unique_dir& operator=(const unique_dir &) = delete;
unique_dir(unique_dir &&other) noexcept
: m_Dir{ other.m_Dir }
{
other.m_Dir = invalid_dir;
}
unique_dir& operator=(unique_dir &&other) noexcept
{
std::swap(m_Dir, other.m_Dir);
return *this;
}
explicit operator bool() const noexcept
{
return m_Dir != invalid_dir;
}
DIR* get() const noexcept
{
return m_Dir;
}
void reset(DIR *dir = invalid_dir) noexcept
{
if (m_Dir != invalid_dir)
{
closedir(m_Dir);
}
m_Dir = dir;
}
DIR* release() noexcept
{
DIR *dir = m_Dir;
8000
m_Dir = invalid_dir;
return dir;
}
friend void swap(unique_dir &dir1, unique_dir &dir2)
{
std::swap(dir1.m_Dir, dir2.m_Dir);
}
private:
DIR *m_Dir;
};
class unique_fd
{
public:
static constexpr int invalid_fd = -1;
unique_fd(int fd = invalid_fd) noexcept
: m_Fd{ fd }
{
}
~unique_fd() noexcept
{
reset();
}
unique_fd(const unique_fd &) = delete;
unique_fd& operator=(const unique_fd &) = delete;
unique_fd(unique_fd &&other) noexcept
: m_Fd{ other.m_Fd }
{
other.m_Fd = invalid_fd;
}
unique_fd& operator=(unique_fd &&other) noexcept
{
std::swap(m_Fd, other.m_Fd);
return *this;
}
explicit operator bool() const noexcept
{
return m_Fd >= 0;
}
int get() const noexcept
{
return m_Fd;
}
void reset(int fd = invalid_fd) noexcept
{
if (m_Fd >= 0)
{
close(m_Fd);
}
m_Fd = fd;
}
int release() noexcept
{
int fd = m_Fd;
m_Fd = invalid_fd;
return fd;
}
friend void swap(unique_fd &fd1, unique_fd &fd2)
{
std::swap(fd1.m_Fd, fd2.m_Fd);
}
private:
int m_Fd;
};
class unique_file
{
public:
static constexpr FILE* invalid_file = nullptr;
unique_file(FILE* file = invalid_file) noexcept
: m_File{ file }
{
}
~unique_file() noexcept
{
reset();
}
unique_file(const unique_file &) = delete;
unique_file& operator=(const unique_file &) = delete;
unique_file(unique_file &&other) noexcept
: m_File{ other.m_File }
{
other.m_File = invalid_file;
}
unique_file& operator=(unique_file &&other) noexcept
{
std::swap(m_File, other.m_File);
return *this;
}
explicit operator bool() const noexcept
{
return m_File != invalid_file;
}
FILE* get() const noexcept
{
return m_File;
}
void reset(FILE *file = invalid_file) noexcept
{
if (m_File != invalid_file)
{
fclose(m_File);
}
m_File = file;
}
FILE* release() noexcept
{
FILE *file = m_File;
m_File = invalid_file;
return file;
}
frie
8000
nd void swap(unique_file &file1, unique_file &file2)
{
std::swap(file1.m_File, file2.m_File);
}
private:
FILE *m_File;
};
/** Returns an object that executes the given lambda when destroyed.
Capture the object with 'auto'; use reset() to execute the lambda early or release() to avoid
execution. Exceptions thrown in the lambda will fail-fast; use scope_exit_log to avoid. */
template <typename TLambda>
[[nodiscard]] inline auto scope_exit(TLambda&& lambda) noexcept
{
return details::lambda_call<TLambda>(std::forward<TLambda>(lambda));
}
namespace details
{
template <unsigned long long flag>
struct verify_single_flag_helper
{
static_assert((flag != 0) && ((flag & (flag - 1)) == 0), "Single flag expected, zero or multiple flags found");
static const unsigned long long value = flag;
};
// Use size-specific casts to avoid sign extending numbers -- avoid warning C4310: cast truncates constant value
#define __WI_MAKE_UNSIGNED(val) \
(sizeof(val) == 1 ? static_cast<unsigned char>(val) : \
sizeof(val) == 2 ? static_cast<unsigned short>(val) : \
sizeof(val) == 4 ? static_cast<unsigned long>(val) : \
static_cast<unsigned long long>(val))
#define __WI_IS_UNSIGNED_SINGLE_FLAG_SET(val) ((val) && !((val) & ((val) - 1)))
#define __WI_IS_SINGLE_FLAG_SET(val) __WI_IS_UNSIGNED_SINGLE_FLAG_SET(__WI_MAKE_UNSIGNED(val))
template <typename TVal, typename TFlags>
inline constexpr bool AreAllFlagsSetHelper(TVal val, TFlags flags)
{
return ((val & flags) == static_cast<decltype(val & flags)>(flags));
}
template <typename TVal>
inline constexpr bool IsSingleFlagSetHelper(TVal val)
{
return __WI_IS_SINGLE_FLAG_SET(val);
}
template <typename TVal>
inline constexpr bool IsClearOrSingleFlagSetHelper(TVal val)
{
return ((val == static_cast<std::remove_reference_t<TVal>>(0)) || IsSingleFlagSetHelper(val));
}
template <typename TVal, typename TMask, typename TFlags>
inline constexpr void UpdateFlagsInMaskHelper(TVal& val, TMask mask, TFlags flags)
{
val = static_cast<std::remove_reference_t<TVal>>((val & ~mask) | (flags & mask));
}
template <long>
struct variable_size;
template <>
struct variable_size<1>
{
typedef unsigned char type;
};
template <>
struct variable_size<2>
{
typedef unsigned short type;
};
template <>
struct variable_size<4>
{
typedef unsigned long type;
};
template <>
struct variable_size<8>
{
typedef unsigned long long type;
};
template <typename T>
struct variable_size_mapping
{
typedef typename variable_size<sizeof(T)>::type type;
};
}
/** Defines the unsigned type of the same width (1, 2, 4, or 8 bytes) as the given type.
This allows code to generically convert any enum class to it's corresponding underlying type. */
template <typename T>
using integral_from_enum = typename details::variable_size_mapping<T>::type;
#define WI_StaticAssertSingleBitSet(flag) static_cast<decltype(flag)>(::wil::details::verify_single_flag_helper<static_cast<unsigned long long>(WI_EnumValue(flag))>::value)
#define WI_IsAnyFlagSet(val, flags) (static_cast<decltype((val) & (flags))>(WI_EnumValue(val) & WI_EnumValue(flags)) != static_cast<decltype((val) & (flags))>(0))
#define WI_IsFlagSet(val, flag) WI_IsAnyFlagSet(val, WI_StaticAssertSingleBitSet(flag))
#define WI_EnumValue(val) static_cast<::wil::integral_from_enum<decltype(val)>>(val)
//! Evaluates as true if every bitflag specified in `flags` is set within `val`.
#define WI_AreAllFlagsSet(val, flags) wil::details::AreAllFlagsSetHelper(val, flags)
//! Set zero or more bitflags specified by `flags` in the variable `var`.
#define WI_SetAllFlags(var, flags) ((var) |= (flags))
//! Set a single compile-time constant `flag` in the variable `var`.
#define WI_SetFlag(var, flag) WI_SetAllFlags(var, WI_StaticAssertSingleBitSet(flag))
//! Clear zero or more bitflags specified by `flags` from the variable `var`.
#define WI_ClearAllFlags(var, flags) ((var) &= ~(flags))
//! Clear a single compile-time constant `flag` from the variable `var`.
#define WI_ClearFlag(var, flag) WI_ClearAllFlags(var, WI_StaticAssertSingleBitSet(flag))
#define WI_ASSERT(condition) assert(condition)
}