MSTL 1.4.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
exception.hpp
浏览该文件的文档.
1#ifndef MSTL_CORE_CONFIG_EXCEPTION_HPP__
2#define MSTL_CORE_CONFIG_EXCEPTION_HPP__
3
11
14
20
21#define __MSTL_ERROR_CONSTRUCTOR(THIS, BASE, INFO) \
22 explicit THIS(const char* info = INFO, const char* type = static_type) noexcept \
23 : BASE(info, type) {} \
24 explicit THIS(const exception& e) : BASE(e) {}
25
26#define __MSTL_ERROR_DERIVED_DESTRUCTOR(CLASS) \
27 virtual ~CLASS() = default;
28
29#define __MSTL_ERROR_FINAL_DESTRUCTOR(CLASS) \
30 ~CLASS() override = default;
31
32#define __MSTL_ERROR_TYPE(CLASS) \
33 static constexpr auto static_type = #CLASS;
34
44#define MSTL_ERROR_BUILD_DERIVED_CLASS(THIS, BASE, INFO) \
45 struct MSTL_API THIS : BASE { \
46 __MSTL_ERROR_CONSTRUCTOR(THIS, BASE, INFO) \
47 __MSTL_ERROR_DERIVED_DESTRUCTOR(THIS) \
48 __MSTL_ERROR_TYPE(THIS) \
49 };
50
60#define MSTL_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO) \
61 struct MSTL_API THIS final : BASE { \
62 __MSTL_ERROR_CONSTRUCTOR(THIS, BASE, INFO) \
63 __MSTL_ERROR_FINAL_DESTRUCTOR(THIS) \
64 __MSTL_ERROR_TYPE(THIS) \
65 };
66
72
77struct MSTL_API exception {
78private:
79 static constexpr size_t INFO_SIZE = 256; // 异常信息长度
80 static constexpr size_t TYPE_SIZE = 48; // 类型名称长度
81
82 char info_[INFO_SIZE]; // 异常信息
83 char type_[TYPE_SIZE]; // 异常类型
84 int code_{0};
85
86public:
93 explicit exception(
94 const char* info = static_type,
95 const char* type = static_type,
96 const int code = 0) : code_(code) {
97 string_copy(info_, info, INFO_SIZE - 1);
98 string_copy(type_, type, TYPE_SIZE - 1);
99 info_[INFO_SIZE - 1] = '\0';
100 type_[TYPE_SIZE - 1] = '\0';
101 }
102
106 exception(const exception& other) noexcept {
107 memory_copy(info_, other.info_, INFO_SIZE);
108 memory_copy(type_, other.type_, TYPE_SIZE);
109 code_ = other.code_;
110 }
111
115 exception& operator =(const exception& other) noexcept {
116 if (_MSTL addressof(other) == this) return *this;
117
118 memory_copy(info_, other.info_, INFO_SIZE);
119 memory_copy(type_, other.type_, TYPE_SIZE);
120 code_ = other.code_;
121
122 return *this;
123 }
124
128 exception(exception&& other) noexcept {
129 memory_copy(this, &other);
130 other.info_[0] = '\0';
131 other.type_[0] = '\0';
132 other.code_ = 0;
133 }
134
138 exception& operator =(exception&& other) noexcept {
139 if (_MSTL addressof(other) == this) return *this;
140
141 memory_copy(info_, other.info_, INFO_SIZE);
142 memory_copy(type_, other.type_, TYPE_SIZE);
143 other.info_[0] = '\0';
144 other.type_[0] = '\0';
145 other.code_ = 0;
146
147 return *this;
148 }
149
157 template <typename Error>
158 explicit exception(const Error& error)
159 : exception(error.what(), error.type(), error.code()) {}
160
164 virtual ~exception() = default;
165
170 MSTL_NODISCARD const char* what() const noexcept { return info_; }
171
176 MSTL_NODISCARD const char* type() const noexcept { return type_; }
177
182 MSTL_NODISCARD int code() const noexcept { return code_; }
183
184 __MSTL_ERROR_TYPE(exception)
185};
186
191MSTL_ERROR_BUILD_DERIVED_CLASS(memory_exception, exception, "Memory Operation Failed.")
192
193
197MSTL_ERROR_BUILD_DERIVED_CLASS(system_exception, exception, "System Access Failed.")
198
203MSTL_ERROR_BUILD_FINAL_CLASS(iterator_exception, memory_exception, "Iterator or Pointer Access Invalid.")
204
209MSTL_ERROR_BUILD_DERIVED_CLASS(typecast_exception, memory_exception, "Type Cast Mismatch.")
210
215MSTL_ERROR_BUILD_DERIVED_CLASS(value_exception, exception, "Variable Operation Invalid.")
216
221MSTL_ERROR_BUILD_DERIVED_CLASS(device_exception, system_exception, "Device Operation Failed.")
222
227MSTL_ERROR_BUILD_FINAL_CLASS(file_exception, system_exception, "File Operation Failed.")
228
233MSTL_ERROR_BUILD_FINAL_CLASS(math_exception, value_exception, "Math Calculation Invalid.")
234
239MSTL_ERROR_BUILD_DERIVED_CLASS(database_exception, system_exception, "Database Operation Failed.")
240 // Exceptions
242
247void MSTL_API throw_with_stack(const exception& err);
248
249#if defined(MSTL_STATE_DEBUG__) || !defined(NDEBUG)
250#define throw_exception(err) throw_with_stack(err)
251#else
252#define throw_exception(err) throw err
253#endif
254 // ExceptionHandling
256
258#endif // MSTL_CORE_CONFIG_EXCEPTION_HPP__
MSTL_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
#define MSTL_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO)
构建最终异常类宏
#define MSTL_ERROR_BUILD_DERIVED_CLASS(THIS, BASE, INFO)
构建可派生的异常类宏
void MSTL_API throw_with_stack(const exception &err)
抛出异常并打印堆栈信息
MSTL_CONSTEXPR14 void * memory_copy(void *MSTL_RESTRICT dest, const void *MSTL_RESTRICT src, size_t count) noexcept
从源内存复制到目标内存
#define _MSTL
全局命名空间MSTL前缀
#define MSTL_END_NAMESPACE__
结束全局命名空间MSTL
#define MSTL_BEGIN_NAMESPACE__
开始全局命名空间MSTL
constexpr CharT * string_copy(CharT *MSTL_RESTRICT dest, const CharT *MSTL_RESTRICT src) noexcept
复制字符串
MSTL内存操作函数
异常基类
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
virtual ~exception()=default
虚析构函数
MSTL_NODISCARD const char * type() const noexcept
获取异常类型
MSTL_NODISCARD const char * what() const noexcept
获取错误信息
MSTL_NODISCARD int code() const noexcept
获取异常码
exception(const exception &other) noexcept
复制构造函数
static constexpr auto static_type
静态类型字符串
exception(const Error &error)
模板构造函数
exception(exception &&other) noexcept
移动构造函数