-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy patherrors.hpp
More file actions
55 lines (44 loc) · 1.36 KB
/
Copy patherrors.hpp
File metadata and controls
55 lines (44 loc) · 1.36 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
#ifndef CAV_INCLUDE_UTILS_NOEXCEPTIONS_HPP
#define CAV_INCLUDE_UTILS_NOEXCEPTIONS_HPP
#include <fmt/core.h>
#include <cassert>
#include <source_location>
#include <type_traits>
#include "../comptime/test.hpp"
#if __cpp_exceptions
#define CAV_TRY try
#define CAV_CATCH(X) catch (X)
#define CAV_THROW(E) throw E
#else
#include <fmt/core.h>
#define CAV_TRY if constexpr (true)
#define CAV_CATCH(X) if constexpr (false)
template <typename Exception>
consteval void CAV_THROW(Exception e) noexcept {
fmt::print(stderr, "An exception has occurred:\n");
if constexpr (std::is_fundamental_v<Exception>)
fmt::print(stderr, "Exception value: {}\n", e);
else
fmt::print(stderr, "{}\n", e.what());
abort();
} // namespace
#endif
namespace cav {
template <typename... Ts>
[[noreturn]] static void throw_with_message(fmt::format_string<Ts...> fmt, Ts&&... args) {
auto msg = fmt::format(fmt, FWD(args)...);
CAV_THROW(std::runtime_error(msg));
}
inline void failed_assert() {
assert(!"Debug: syntetic fail.");
}
template <typename... Ts>
[[noreturn]] constexpr void exit_with_message(::fmt::format_string<Ts...> fmt, Ts&&... args) {
std::fflush(stdout);
fmt::print(stderr, fmt, FWD(args)...);
std::fflush(stderr);
failed_assert();
exit(EXIT_FAILURE);
}
} // namespace cav
#endif /* CAV_INCLUDE_UTILS_NOEXCEPTIONS_HPP */