NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
exception.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_CONFIG_EXCEPTION_HPP__
2#define NEFORCE_CORE_CONFIG_EXCEPTION_HPP__
3
11
13NEFORCE_BEGIN_NAMESPACE__
14
20
21#define __NEFORCE_ERROR_CONSTRUCTOR(THIS, BASE, INFO) \
22 explicit THIS(const char* info = INFO) noexcept : \
23 BASE(info) {} \
24 \
25 explicit THIS(const exception& e) : \
26 BASE(e) {}
27
28#define __NEFORCE_ERROR_DERIVED_DESTRUCTOR(CLASS) ~CLASS() override = default;
29
30#define __NEFORCE_ERROR_FINAL_DESTRUCTOR(CLASS) ~CLASS() override = default;
31
32#define __NEFORCE_ERROR_TYPE(CLASS) \
33 NEFORCE_NODISCARD const char* type() const noexcept override { return #CLASS; }
34
44#define NEFORCE_ERROR_BUILD_DERIVED_CLASS(THIS, BASE, INFO) \
45 struct THIS : BASE { \
46 __NEFORCE_ERROR_CONSTRUCTOR(THIS, BASE, INFO) \
47 __NEFORCE_ERROR_DERIVED_DESTRUCTOR(THIS) \
48 __NEFORCE_ERROR_TYPE(THIS) \
49 };
50
60#define NEFORCE_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO) \
61 struct THIS final : BASE { \
62 __NEFORCE_ERROR_CONSTRUCTOR(THIS, BASE, INFO) \
63 __NEFORCE_ERROR_FINAL_DESTRUCTOR(THIS) \
64 __NEFORCE_ERROR_TYPE(THIS) \
65 };
66
72
77struct exception {
78private:
79 static constexpr size_t INFO_SIZE = 256; // 异常信息长度
80
81 char info_[INFO_SIZE]; // 异常信息
82
83public:
88 explicit exception(const char* info = "") {
89 string_copy(info_, info, INFO_SIZE - 1);
90 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
91 info_[INFO_SIZE - 1] = '\0';
92 }
93
97 exception(const exception& other) noexcept { memory_copy(info_, other.info_, INFO_SIZE); }
98
102 exception& operator=(const exception& other) noexcept {
103 if (_NEFORCE addressof(other) == this) {
104 return *this;
105 }
106
107 memory_copy(info_, other.info_, INFO_SIZE);
108
109 return *this;
110 }
111
115 exception(exception&& other) noexcept {
116 memory_copy(info_, other.info_, INFO_SIZE);
117 other.info_[0] = '\0';
118 }
119
123 exception& operator=(exception&& other) noexcept {
124 if (_NEFORCE addressof(other) == this) {
125 return *this;
126 }
127
128 memory_copy(info_, other.info_, INFO_SIZE);
129
130 other.info_[0] = '\0';
131
132 return *this;
133 }
134
142 template <typename Error, enable_if_t<is_base_of_v<exception, Error>, int> = 0>
143 explicit exception(const Error& error) :
144 exception(error.what(), error.type(), error.code()) {}
145
149 virtual ~exception() = default;
150
155 NEFORCE_NODISCARD virtual const char* what() const noexcept { return info_; }
156
161 NEFORCE_NODISCARD virtual const char* type() const noexcept { return "exception"; }
162};
163
168NEFORCE_ERROR_BUILD_DERIVED_CLASS(memory_exception, exception, "Memory Operation Failed.")
169
170
174NEFORCE_ERROR_BUILD_DERIVED_CLASS(allocate_exception, memory_exception, "Memory Allocation Failed.")
175
180NEFORCE_ERROR_BUILD_FINAL_CLASS(iterator_exception, memory_exception, "Iterator or Pointer Access Invalid.")
181
186NEFORCE_ERROR_BUILD_DERIVED_CLASS(typecast_exception, memory_exception, "Type Cast Mismatch.")
187
192NEFORCE_ERROR_BUILD_DERIVED_CLASS(value_exception, exception, "Variable Operation Invalid.")
193
198NEFORCE_ERROR_BUILD_FINAL_CLASS(math_exception, value_exception, "Math Calculation Invalid.")
199
204NEFORCE_ERROR_BUILD_DERIVED_CLASS(thirdparty_exception, exception, "ThirdParty Operation Invalid.")
205
210NEFORCE_ERROR_BUILD_DERIVED_CLASS(database_exception, thirdparty_exception, "Database Operation Failed.")
211 // Exceptions
213
220int NEFORCE_API uncaught_exceptions() noexcept;
221
226NEFORCE_NORETURN void NEFORCE_API throw_with_stack(const exception& err);
227
228#if (defined(NEFORCE_STATE_DEBUG) || !defined(NDEBUG)) && 0 // 0 switch
229# define NEFORCE_THROW_EXCEPTION(err) throw_with_stack(err)
230#else
231# define NEFORCE_THROW_EXCEPTION(err) throw err
232#endif
233
234#define NEFORCE_ASSERTION(COND) \
235 if (!(COND)) { \
236 _NEFORCE terminate(); \
237 }
238 // ExceptionHandling
240
241NEFORCE_END_NAMESPACE__
242#endif // NEFORCE_CORE_CONFIG_EXCEPTION_HPP__
constexpr T * addressof(T &x) noexcept
获取对象的地址
int uncaught_exceptions() noexcept
未捕获的异常数量
void throw_with_stack(const exception &err)
抛出异常并打印堆栈信息
#define NEFORCE_ERROR_BUILD_DERIVED_CLASS(THIS, BASE, INFO)
构建可派生的异常类宏
constexpr void * memory_copy(void *__restrict dest, const void *__restrict src, size_t count) noexcept
从源内存复制到目标内存
constexpr CharT * string_copy(CharT *__restrict dest, const CharT *__restrict src) noexcept
复制字符串
内存操作函数
exception & operator=(const exception &other) noexcept
复制赋值运算符
exception(exception &&other) noexcept
移动构造函数
virtual const char * what() const noexcept
获取错误信息
virtual const char * type() const noexcept
获取异常类型
exception(const exception &other) noexcept
复制构造函数
exception & operator=(exception &&other) noexcept
移动赋值运算符
virtual ~exception()=default
虚析构函数
exception(const Error &error)
模板构造函数
exception(const char *info="")
构造函数