-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpanic.h
67 lines (58 loc) · 2.28 KB
/
panic.h
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
#pragma once
#include <concepts>
#include <format>
#include <source_location>
#include <string_view>
#include <type_traits>
namespace bbai::baser {
//--------------------------------------------------------------------------------------------------
// panic_dynamic_string_view
//--------------------------------------------------------------------------------------------------
struct panic_dynamic_string_view {
template <class T>
requires std::constructible_from<std::string_view, T>
panic_dynamic_string_view(
const T &s,
std::source_location loc = std::source_location::current()) noexcept
: s{s}, loc{loc} {}
std::string_view s;
std::source_location loc;
};
//--------------------------------------------------------------------------------------------------
// panic_format
//--------------------------------------------------------------------------------------------------
template <class... Args>
struct panic_format {
template <class T>
consteval panic_format(
const T &s,
std::source_location loc = std::source_location::current()) noexcept
: fmt{s}, loc{loc} {}
std::format_string<Args...> fmt;
std::source_location loc;
};
//--------------------------------------------------------------------------------------------------
// panic_impl
//--------------------------------------------------------------------------------------------------
[[noreturn]] void panic_impl(const char* s) noexcept;
//--------------------------------------------------------------------------------------------------
// panic
//--------------------------------------------------------------------------------------------------
[[noreturn]] inline void panic(panic_dynamic_string_view s) noexcept {
auto msg =
std::format("{}:{} panic: {}\n", s.loc.file_name(), s.loc.line(), s.s);
panic_impl(msg.c_str());
}
template <class... Args>
[[noreturn]] void panic(panic_format<std::type_identity_t<Args>...> fmt,
Args &&...args) noexcept
requires (sizeof ...(Args) > 0)
{
auto msg = std::format("{}:{} panic: {}\n", fmt.loc.file_name(), fmt.loc.line(),
std::format(fmt.fmt, std::forward<Args>(args)...));
panic_impl(msg.c_str());
}
} // namespace bbai::baser
namespace bbai {
using bbai::baser::panic;
} // namespace bbai