y3c-stl 0.3.3
Friendly C++ STL wrapper with automatic stacktrace
Loading...
Searching...
No Matches
terminate.h
Go to the documentation of this file.
1#pragma once
2#ifdef Y3C_MESON
3#include "y3c-config.h"
4#else
5#include "y3c/y3c-config.h"
6#endif
7#include "y3c/what.h"
8#include <stdexcept>
9#include <string>
10#include <memory>
11#include <unordered_map>
12#include <atomic>
13
14namespace y3c {
15namespace internal {
16
17Y3C_DLL void Y3C_CALL link() noexcept;
18
28
29enum class terminate_type {
40};
41
44 const char *e_class;
45 std::string func;
46 std::string what;
47 std::shared_ptr<void> raw_trace;
48
50 std::string &&func, std::string &&what,
51 skip_trace_tag = {});
53 std::string &&what, skip_trace_tag = {})
54 : terminate_detail(type, nullptr, std::move(func), std::move(what)) {}
55};
56
62[[noreturn]] Y3C_DLL void Y3C_CALL handle_final_terminate_message() noexcept;
69[[noreturn]] Y3C_DLL void Y3C_CALL do_terminate_with(terminate_detail &&detail);
70
79 int id;
80
81 protected:
82 std::string what;
83
84 public:
85 static Y3C_DLL std::atomic<int> last_exception_id;
86 static Y3C_DLL std::unordered_map<int, terminate_detail> exceptions;
87
88 exception_base(const char *e_class, std::string &&func, std::string &&what,
89 skip_trace_tag = {})
90 : id(++last_exception_id), what(what) {
91 exceptions.emplace(id,
93 std::move(func), std::move(what)));
94 }
95 exception_base(const exception_base &) = delete;
98 : id(other.id), what(std::move(other.what)) {
99 other.id = -1;
100 }
102 ~exception_base() { exceptions.erase(id); }
103};
104
115extern Y3C_DLL bool throw_on_terminate;
116
117#define define_terminate_func(ub_name) \
118 class ub_name : public std::exception { \
119 public: \
120 ub_name() = default; \
121 const char *what() const noexcept override { return #ub_name; } \
122 }; \
123 template <typename... Args, typename = skip_trace_tag> \
124 [[noreturn]] void terminate_##ub_name(std::string func, Args &&...args) { \
125 if (throw_on_terminate) { \
126 throw ub_name(); \
127 } \
128 do_terminate_with({terminate_type::ub_name, std::move(func), \
129 what::ub_name(std::forward<Args>(args)...)}); \
130 }
131
139
140#undef define_terminate_func
141
142[[noreturn]] inline void terminate_internal(std::string func, std::string what,
143 skip_trace_tag = {}) {
145 {terminate_type::internal, std::move(func), std::move(what)});
146}
147
148#define y3c_assert_internal(cond) \
149 if (!(cond)) { \
150 y3c::internal::terminate_internal(__func__, \
151 "asserion '" #cond "' failed"); \
152 }
153
154} // namespace internal
155
167inline void link() { internal::link(); }
168
178class out_of_range final : public std::out_of_range,
180 public:
181 out_of_range(std::string func, std::size_t size, std::ptrdiff_t index,
183 : std::out_of_range(""),
184 internal::exception_base(
185 "y3c::out_of_range", std::move(func),
186 internal::what::ub_out_of_range(size, index)) {}
187
188 const char *what() const noexcept override {
189 return this->internal::exception_base::what.c_str();
190 }
191};
192
193} // namespace y3c
y3cの例外クラスのベース。
Definition terminate.h:78
exception_base & operator=(exception_base &&)=delete
static std::unordered_map< int, terminate_detail > exceptions
Definition terminate.h:86
static std::atomic< int > last_exception_id
Definition terminate.h:85
exception_base(exception_base &&other)
Definition terminate.h:97
exception_base & operator=(const exception_base &)=delete
std::string what
Definition terminate.h:82
exception_base(const char *e_class, std::string &&func, std::string &&what, skip_trace_tag={})
Definition terminate.h:88
~exception_base()
Definition terminate.h:102
exception_base(const exception_base &)=delete
Definition terminate.h:134
Definition terminate.h:133
Definition terminate.h:136
Definition terminate.h:137
Definition terminate.h:138
Definition terminate.h:132
Definition terminate.h:135
Definition terminate.h:179
out_of_range(std::string func, std::size_t size, std::ptrdiff_t index, internal::skip_trace_tag={})
Definition terminate.h:181
const char * what() const noexcept override
Definition terminate.h:188
void link() noexcept
Definition terminate_detail.cc:7
void do_terminate_with(terminate_detail &&detail)
例外を表示して強制終了する
Definition final_message.cc:177
bool throw_on_terminate
Definition terminate_detail.cc:18
void terminate_internal(std::string func, std::string what, skip_trace_tag={})
Definition terminate.h:142
terminate_type
Definition terminate.h:29
void handle_final_terminate_message() noexcept
Definition final_message.cc:147
Definition array.h:8
void link()
Definition terminate.h:167
Definition terminate.h:27
Definition terminate.h:42
std::shared_ptr< void > raw_trace
Definition terminate.h:47
terminate_type type
Definition terminate.h:43
const char * e_class
Definition terminate.h:44
std::string what
Definition terminate.h:46
terminate_detail(terminate_type type, std::string &&func, std::string &&what, skip_trace_tag={})
Definition terminate.h:52
std::string func
Definition terminate.h:45
#define define_terminate_func(ub_name)
Definition terminate.h:117
#define Y3C_CALL
Definition y3c-config.h:34
#define Y3C_DLL
Definition y3c-config.h:20