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 info_[INFO_SIZE - 1] = '\0';
97 type_[TYPE_SIZE - 1] = '\0';
98 }
99
103 exception(const exception& other) noexcept {
104 memory_copy(info_, other.info_, INFO_SIZE);
105 memory_copy(type_, other.type_, TYPE_SIZE);
106 code_ = other.code_;
107 }
108
112 exception& operator=(const exception& other) noexcept {
113 if (_NEFORCE addressof(other) == this) {
114 return *this;
115 }
116
117 memory_copy(info_, other.info_, INFO_SIZE);
118 memory_copy(type_, other.type_, TYPE_SIZE);
119 code_ = other.code_;
120
121 return *this;
122 }
123
127 exception(exception&& other) noexcept {
128 memory_copy(this, &other);
129 other.info_[0] = '\0';
130 other.type_[0] = '\0';
131 other.code_ = 0;
132 }
133
137 exception& operator=(exception&& other) noexcept {
138 if (_NEFORCE addressof(other) == this) {
139 return *this;
140 }
141
142 memory_copy(info_, other.info_, INFO_SIZE);
143 memory_copy(type_, other.type_, TYPE_SIZE);
144 other.info_[0] = '\0';
145 other.type_[0] = '\0';
146 other.code_ = 0;
147
148 return *this;
149 }
150
158 template <typename Error>
159 explicit exception(const Error& error) :
160 exception(error.what(), error.type(), error.code()) {}
161
165 virtual ~exception() = default;
166
171 NEFORCE_NODISCARD const char* what() const noexcept { return info_; }
172
177 NEFORCE_NODISCARD const char* type() const noexcept { return type_; }
178
183 NEFORCE_NODISCARD int code() const noexcept { return code_; }
184
185 static constexpr auto static_type = "exception";
186};
187
192NEFORCE_ERROR_BUILD_DERIVED_CLASS(memory_exception, exception, "Memory Operation Failed.")
193
194
198NEFORCE_ERROR_BUILD_DERIVED_CLASS(system_exception, exception, "System Access Failed.")
199
204NEFORCE_ERROR_BUILD_FINAL_CLASS(iterator_exception, memory_exception, "Iterator or Pointer Access Invalid.")
205
210NEFORCE_ERROR_BUILD_DERIVED_CLASS(typecast_exception, memory_exception, "Type Cast Mismatch.")
211
216NEFORCE_ERROR_BUILD_DERIVED_CLASS(value_exception, exception, "Variable Operation Invalid.")
217
222NEFORCE_ERROR_BUILD_DERIVED_CLASS(device_exception, system_exception, "Device Operation Failed.")
223
228NEFORCE_ERROR_BUILD_FINAL_CLASS(file_exception, system_exception, "File Operation Failed.")
229
234NEFORCE_ERROR_BUILD_FINAL_CLASS(math_exception, value_exception, "Math Calculation Invalid.")
235
240NEFORCE_ERROR_BUILD_DERIVED_CLASS(thirdparty_exception, exception, "ThirdParty Operation Invalid.")
241
246NEFORCE_ERROR_BUILD_DERIVED_CLASS(database_exception, thirdparty_exception, "Database Operation Failed.")
247
252NEFORCE_ERROR_BUILD_DERIVED_CLASS(network_exception, exception, "Network Operation or Action Failed.")
253 // Exceptions
255
262int NEFORCE_API uncaught_exceptions() noexcept NEFORCE_PURE_FUNCTION;
263
268void NEFORCE_API throw_with_stack(const exception& err);
269
270#if defined(NEFORCE_STATE_DEBUG) || !defined(NDEBUG)
271# define NEFORCE_THROW_EXCEPTION(err) throw_with_stack(err)
272#else
273# define NEFORCE_THROW_EXCEPTION(err) throw err
274#endif
275
276#define NEFORCE_ASSERTION(COND) \
277 if (!(COND)) { \
278 _NEFORCE terminate(); \
279 }
280 // ExceptionHandling
282
283NEFORCE_END_NAMESPACE__
284#endif // NEFORCE_CORE_CONFIG_EXCEPTION_HPP__
NEFORCE_NODISCARD constexpr T * addressof(T &x) noexcept
获取对象的地址
#define NEFORCE_ERROR_BUILD_FINAL_CLASS(THIS, BASE, INFO)
构建最终异常类宏
#define NEFORCE_ERROR_BUILD_DERIVED_CLASS(THIS, BASE, INFO)
构建可派生的异常类宏
int NEFORCE_API uncaught_exceptions() noexcept NEFORCE_PURE_FUNCTION
未捕获的异常数量
void NEFORCE_API throw_with_stack(const exception &err)
抛出异常并打印堆栈信息
NEFORCE_CONSTEXPR14 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)
构造函数
NEFORCE_NODISCARD int code() const noexcept
获取异常码
NEFORCE_NODISCARD const char * type() const noexcept
获取异常类型
exception & operator=(exception &&other) noexcept
移动赋值运算符
virtual ~exception()=default
虚析构函数
NEFORCE_NODISCARD const char * what() const noexcept
获取错误信息
exception(const exception &other) noexcept
复制构造函数
static constexpr auto static_type
静态类型字符串
exception(const Error &error)
模板构造函数
exception(exception &&other) noexcept
移动构造函数