NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
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, const char* type = static_type, const int code = 0) noexcept : \
23 BASE(info, type, code) {} \
24 \
25 explicit THIS(const exception& e) : \
26 BASE(e) {}
27
28#define __NEFORCE_ERROR_DERIVED_DESTRUCTOR(CLASS) virtual ~CLASS() = default;
29
30#define __NEFORCE_ERROR_FINAL_DESTRUCTOR(CLASS) ~CLASS() override = default;
31
32#define __NEFORCE_ERROR_TYPE(CLASS) static constexpr auto static_type = #CLASS;
33
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) \
48 };
49
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) \
64 };
65
71
76struct exception {
77private:
78 static constexpr size_t INFO_SIZE = 256; // 异常信息长度
79 static constexpr size_t TYPE_SIZE = 48; // 类型名称长度
80
81 char info_[INFO_SIZE]; // 异常信息
82 char type_[TYPE_SIZE]; // 异常类型
83 int code_{0};
84
85public:
92 explicit exception(const char* info = static_type, const char* type = static_type, const int code = 0) :
93 code_(code) {
94 string_copy(info_, info, INFO_SIZE - 1);
95 string_copy(type_, type, TYPE_SIZE - 1);
96 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
97 info_[INFO_SIZE - 1] = '\0';
98 // NOLINTNEXTLINE(clang-analyzer-core.NullDereference)
99 type_[TYPE_SIZE - 1] = '\0';
100 }
101
105 exception(const exception& other) noexcept {
106 memory_copy(info_, other.info_, INFO_SIZE);
107 memory_copy(type_, other.type_, TYPE_SIZE);
108 code_ = other.code_;
109 }
110
114 exception& operator=(const exception& other) noexcept {
115 if (_NEFORCE addressof(other) == this) {
116 return *this;
117 }
118
119 memory_copy(info_, other.info_, INFO_SIZE);
120 memory_copy(type_, other.type_, TYPE_SIZE);
121 code_ = other.code_;
122
123 return *this;
124 }
125
129 exception(exception&& other) noexcept :
130 code_(other.code_) {
131 memory_copy(info_, other.info_, INFO_SIZE);
132 memory_copy(type_, other.type_, TYPE_SIZE);
133 other.info_[0] = '\0';
134 other.type_[0] = '\0';
135 other.code_ = 0;
136 }
137
141 exception& operator=(exception&& other) noexcept {
142 if (_NEFORCE addressof(other) == this) {
143 return *this;
144 }
145
146 memory_copy(info_, other.info_, INFO_SIZE);
147 memory_copy(type_, other.type_, TYPE_SIZE);
148 code_ = other.code_;
149
150 other.info_[0] = '\0';
151 other.type_[0] = '\0';
152 other.code_ = 0;
153
154 return *this;
155 }
156
164 template <typename Error, enable_if_t<is_base_of_v<exception, Error>, int> = 0>
165 explicit exception(const Error& error) :
166 exception(error.what(), error.type(), error.code()) {}
167
171 virtual ~exception() = default;
172
177 NEFORCE_NODISCARD const char* what() const noexcept { return info_; }
178
183 NEFORCE_NODISCARD const char* type() const noexcept { return type_; }
184
189 NEFORCE_NODISCARD int code() const noexcept { return code_; }
190
191 static constexpr auto static_type = "exception";
192};
193
198NEFORCE_ERROR_BUILD_DERIVED_CLASS(memory_exception, exception, "Memory Operation Failed.")
199
200
204NEFORCE_ERROR_BUILD_DERIVED_CLASS(system_exception, exception, "System Access Failed.")
205
210NEFORCE_ERROR_BUILD_FINAL_CLASS(iterator_exception, memory_exception, "Iterator or Pointer Access Invalid.")
211
216NEFORCE_ERROR_BUILD_DERIVED_CLASS(typecast_exception, memory_exception, "Type Cast Mismatch.")
217
222NEFORCE_ERROR_BUILD_DERIVED_CLASS(value_exception, exception, "Variable Operation Invalid.")
223
228NEFORCE_ERROR_BUILD_DERIVED_CLASS(device_exception, system_exception, "Device Operation Failed.")
229
234NEFORCE_ERROR_BUILD_FINAL_CLASS(file_exception, system_exception, "File Operation Failed.")
235
240NEFORCE_ERROR_BUILD_FINAL_CLASS(math_exception, value_exception, "Math Calculation Invalid.")
241
246NEFORCE_ERROR_BUILD_DERIVED_CLASS(thirdparty_exception, exception, "ThirdParty Operation Invalid.")
247
252NEFORCE_ERROR_BUILD_DERIVED_CLASS(database_exception, thirdparty_exception, "Database Operation Failed.")
253
258NEFORCE_ERROR_BUILD_DERIVED_CLASS(network_exception, exception, "Network Operation or Action Failed.")
259 // Exceptions
261
268int NEFORCE_API uncaught_exceptions() noexcept;
269
274NEFORCE_NORETURN void NEFORCE_API throw_with_stack(const exception& err);
275
276#if (defined(NEFORCE_STATE_DEBUG) || !defined(NDEBUG)) && 0 // 0 switch
277# define NEFORCE_THROW_EXCEPTION(err) throw_with_stack(err)
278#else
279# define NEFORCE_THROW_EXCEPTION(err) throw err
280#endif
281
282#define NEFORCE_ASSERTION(COND) \
283 if (!(COND)) { \
284 _NEFORCE terminate(); \
285 }
286 // ExceptionHandling
288
289NEFORCE_END_NAMESPACE__
290#endif // NEFORCE_CORE_CONFIG_EXCEPTION_HPP__
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
移动构造函数