1#ifndef MSTL_CORE_CONFIG_EXCEPTION_HPP__
2#define MSTL_CORE_CONFIG_EXCEPTION_HPP__
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) {}
26#define __MSTL_ERROR_DERIVED_DESTRUCTOR(CLASS) \
27 virtual ~CLASS() = default;
29#define __MSTL_ERROR_FINAL_DESTRUCTOR(CLASS) \
30 ~CLASS() override = default;
32#define __MSTL_ERROR_TYPE(CLASS) \
33 static constexpr auto static_type = #CLASS;
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) \
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) \
79 static constexpr size_t INFO_SIZE = 256;
80 static constexpr size_t TYPE_SIZE = 48;
82 char info_[INFO_SIZE];
83 char type_[TYPE_SIZE];
99 info_[INFO_SIZE - 1] =
'\0';
100 type_[TYPE_SIZE - 1] =
'\0';
130 other.info_[0] =
'\0';
131 other.type_[0] =
'\0';
143 other.info_[0] =
'\0';
144 other.type_[0] =
'\0';
157 template <
typename Error>
170 MSTL_NODISCARD
const char*
what() const noexcept {
return info_; }
176 MSTL_NODISCARD
const char*
type() const noexcept {
return type_; }
182 MSTL_NODISCARD
int code() const noexcept {
return code_; }
249#if defined(MSTL_STATE_DEBUG__) || !defined(NDEBUG)
250#define throw_exception(err) throw_with_stack(err)
252#define throw_exception(err) throw err
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
复制字符串
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
移动构造函数