namespace bastion class OperationCanceled template class bool was_canc

 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
namespace bastion {
class OperationCanceled;
template<class T>
bool was_canceled(Result<T> const &r);
[[noreturn]] inline static void throwSystemError(std::string const &message) {
throw std::system_error(errno, std::generic_category(), message);
}
[[noreturn]] inline static void throwSystemError(std::string const &message, std::error_code code) {
throw std::system_error(code.value(), code.category(), message);
}
template<typename ...Args>
[[noreturn]] inline static void throwSystemError(std::string const &format, Args &&...args) {
throwSystemError(fmt::sprintf(format, std::forward<Args>(args)...));
}
template<typename T>
inline static int
checkCall(Result<T> &&result, char const *call, char const *function, char const *file, int line) {
if (was_canceled(result)) {
throw OperationCanceled(
fmt::sprintf("Operation was canceled in function %s at %s (file %s, line %d)", function, call, file,
line));
}
if (!result.value()) {
throw std::system_error(result.error(),
fmt::sprintf("Error in function %s at %s (file %s, line %d)", function, call, file,
line));
}
return std::move(result.value());
}
template<typename T>
inline static T checkCall(T result, char const *call, char const *function, char const *file, int line) {
static_assert(std::is_integral_v<T>);
if (result < 0)
throwSystemError("Error in function %s at %s (file %s, line %d)", function, call, file, line);
return result;
}
}
#define CHECKED(call) bastion::checkCall(call, #call, __PRETTY_FUNCTION__, __FILE__, __LINE__)