NexusForce 1.0.0
A rigorously engineered full-stack C++ backend library.
载入中...
搜索中...
未找到
error_code.hpp
浏览该文件的文档.
1#ifndef NEFORCE_CORE_EXCEPTION_ERROR_CODE_HPP__
2#define NEFORCE_CORE_EXCEPTION_ERROR_CODE_HPP__
3
11
13NEFORCE_BEGIN_NAMESPACE__
14
19
26class error_code : public icommon<error_code> {
27private:
28 int value_{0};
29 const error_category* category_{&system_category()};
30
31public:
37 error_code() noexcept = default;
38
44 error_code(int val, const error_category& cat) noexcept :
45 value_(val),
46 category_(&cat) {}
47
54 error_code(errc e) noexcept { *this = make_error_code(e); }
55
61 void assign(int val, const error_category& cat) noexcept {
62 value_ = val;
63 category_ = &cat;
64 }
65
71 void clear() noexcept {
72 value_ = 0;
73 category_ = &system_category();
74 }
75
80 NEFORCE_NODISCARD int value() const noexcept { return value_; }
81
86 NEFORCE_NODISCARD errc error() const noexcept { return static_cast<errc>(value_); }
87
92 NEFORCE_NODISCARD const error_category& category() const noexcept { return *category_; }
93
98 NEFORCE_NODISCARD error_condition default_error_condition() const noexcept {
99 return category_->default_error_condition(value_);
100 }
101
106 NEFORCE_NODISCARD string message() const { return category_->message(value_); }
107
112 explicit operator bool() const noexcept { return value_ != 0; }
113
114 NEFORCE_NODISCARD bool equal_to(const error_code& rhs) const noexcept {
115 return category_ == rhs.category_ && value_ == rhs.value_;
116 }
117 NEFORCE_NODISCARD bool less_than(const error_code& rhs) const noexcept {
118 if (*category_ < *rhs.category_) {
119 return true;
120 }
121 if (*rhs.category_ < *category_) {
122 return false;
123 }
124 return value_ < rhs.value_;
125 }
126
132 NEFORCE_NODISCARD bool operator==(const error_condition& cond) const noexcept {
133 return category_->equivalent(value_, cond) || cond.category().equivalent(*this, cond.value());
134 }
135
141 NEFORCE_NODISCARD bool operator!=(const error_condition& cond) const noexcept { return !(*this == cond); }
142
143 NEFORCE_NODISCARD size_t to_hash() const noexcept { return hash_combine_all(category_, value_); }
144};
145
151inline error_code make_error_code(errc e) noexcept { return {static_cast<int>(e), generic_category()}; }
152
157error_code NEFORCE_API last_error() noexcept;
158 // ErrorCode
160
161NEFORCE_END_NAMESPACE__
162#endif // NEFORCE_CORE_EXCEPTION_ERROR_CODE_HPP__
错误类别抽象基类
bool operator==(const error_condition &cond) const noexcept
与错误条件进行等价比较
string message() const
获取错误描述信息
int value() const noexcept
获取错误码值
bool operator!=(const error_condition &cond) const noexcept
与错误条件进行不等价比较
void clear() noexcept
清空错误码
void assign(int val, const error_category &cat) noexcept
赋值错误码值和类别
error_condition default_error_condition() const noexcept
获取对应的默认错误条件
const error_category & category() const noexcept
获取错误类别
error_code() noexcept=default
默认构造函数
errc error() const noexcept
获取对应 errc 枚举值
error_code(errc e) noexcept
从 errc 枚举构造
const error_category & generic_category() noexcept
获取通用错误类别单例
errc
系统错误码枚举
error_code last_error() noexcept
获取当前系统错误码
const error_category & system_category() noexcept
获取系统错误类别单例
error_code make_error_code(errc e) noexcept
从 errc 枚举创建错误码
constexpr size_t hash_combine_all(const Types &... values) noexcept
混合多个哈希值
相等比较仿函数
通用接口,同时具备可比较和可哈希功能