1#ifndef NEFORCE_CORE_CONFIG_EXCEPTION_HPP__
2#define NEFORCE_CORE_CONFIG_EXCEPTION_HPP__
13NEFORCE_BEGIN_NAMESPACE__
21#define __NEFORCE_ERROR_CONSTRUCTOR(THIS, BASE, INFO) \
22 explicit THIS(const char* info = INFO, const char* type = static_type, const int code = 0) noexcept : \
23 BASE(info, type, code) {} \
25 explicit THIS(const exception& e) : \
28#define __NEFORCE_ERROR_DERIVED_DESTRUCTOR(CLASS) virtual ~CLASS() = default;
30#define __NEFORCE_ERROR_FINAL_DESTRUCTOR(CLASS) ~CLASS() override = default;
32#define __NEFORCE_ERROR_TYPE(CLASS) static constexpr auto static_type = #CLASS;
43#define NEFORCE_ERROR_BUILD_DERIVED_CLASS(THIS, BASE, INFO) \
44 struct THIS : BASE { \
45 __NEFORCE_ERROR_CONSTRUCTOR(THIS, BASE, INFO) \
46 __NEFORCE_ERROR_DERIVED_DESTRUCTOR(THIS) \
47 __NEFORCE_ERROR_TYPE(THIS) \
59#define NEFORCE_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO) \
60 struct THIS final : BASE { \
61 __NEFORCE_ERROR_CONSTRUCTOR(THIS, BASE, INFO) \
62 __NEFORCE_ERROR_FINAL_DESTRUCTOR(THIS) \
63 __NEFORCE_ERROR_TYPE(THIS) \
78 static constexpr size_t INFO_SIZE = 256;
79 static constexpr size_t TYPE_SIZE = 48;
81 char info_[INFO_SIZE];
82 char type_[TYPE_SIZE];
97 info_[INFO_SIZE - 1] =
'\0';
99 type_[TYPE_SIZE - 1] =
'\0';
133 other.info_[0] =
'\0';
134 other.type_[0] =
'\0';
150 other.info_[0] =
'\0';
151 other.type_[0] =
'\0';
164 template <
typename Error, enable_if_t<is_base_of_v<exception, Error>,
int> = 0>
177 NEFORCE_NODISCARD
const char*
what() const noexcept {
return info_; }
183 NEFORCE_NODISCARD
const char*
type() const noexcept {
return type_; }
189 NEFORCE_NODISCARD
int code() const noexcept {
return code_; }
276#if (defined(NEFORCE_STATE_DEBUG) || !defined(NDEBUG)) && 0
277# define NEFORCE_THROW_EXCEPTION(err) throw_with_stack(err)
279# define NEFORCE_THROW_EXCEPTION(err) throw err
282#define NEFORCE_ASSERTION(COND) \
284 _NEFORCE terminate(); \
289NEFORCE_END_NAMESPACE__
constexpr T * addressof(T &x) noexcept
获取对象的地址
#define NEFORCE_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO)
构建最终异常类宏
int uncaught_exceptions() noexcept
未捕获的异常数量
#define NEFORCE_ERROR_BUILD_DERIVED_CLASS(THIS, BASE, INFO)
构建可派生的异常类宏
NEFORCE_NORETURN void throw_with_stack(const exception &err)
抛出异常并打印堆栈信息
constexpr void * memory_copy(void *NEFORCE_RESTRICT dest, const void *NEFORCE_RESTRICT src, size_t count) noexcept
从源内存复制到目标内存
constexpr CharT * string_copy(CharT *NEFORCE_RESTRICT dest, const CharT *NEFORCE_RESTRICT src) noexcept
复制字符串
exception & operator=(const exception &other) noexcept
复制赋值运算符
exception(const char *info=static_type, const char *type=static_type, const int code=0)
构造函数
const char * what() const noexcept
获取错误信息
exception & operator=(exception &&other) noexcept
移动赋值运算符
const char * type() const noexcept
获取异常类型
virtual ~exception()=default
虚析构函数
int code() const noexcept
获取异常码
exception(const Error &error)
模板构造函数
exception(const exception &other) noexcept
复制构造函数
static constexpr auto static_type
静态类型字符串
exception(exception &&other) noexcept
移动构造函数