NexusForce 1.0.0
A Modern C++ Library with extended functionality, web components, and utility libraries
载入中...
搜索中...
未找到
error_code.hpp
1#ifndef NEFORCE_CORE_EXCEPTION_ERROR_CODE_HPP__
2#define NEFORCE_CORE_EXCEPTION_ERROR_CODE_HPP__
3#include "NeForce/core/exception/error_condition.hpp"
4NEFORCE_BEGIN_NAMESPACE__
5
6class error_code : public icommon<error_code> {
7private:
8 int value_;
9 const error_category* category_;
10
11public:
12 error_code() noexcept :
13 value_(0),
14 category_(&system_category()) {}
15
16 error_code(int val, const error_category& cat) noexcept :
17 value_(val),
18 category_(&cat) {}
19
20 error_code(errc e) noexcept { *this = make_error_code(e); }
21
22 void assign(int val, const error_category& cat) noexcept {
23 value_ = val;
24 category_ = &cat;
25 }
26
27 void clear() noexcept {
28 value_ = 0;
29 category_ = &system_category();
30 }
31
32 NEFORCE_NODISCARD int value() const noexcept { return value_; }
33 NEFORCE_NODISCARD const error_category& category() const noexcept { return *category_; }
34
35 error_condition default_error_condition() const noexcept { return category_->default_error_condition(value_); }
36
37 NEFORCE_NODISCARD string message() const { return category_->message(value_); }
38
39 explicit operator bool() const noexcept { return value_ != 0; }
40
41 NEFORCE_NODISCARD bool operator==(const error_code& rhs) const noexcept {
42 return category_ == rhs.category_ && value_ == rhs.value_;
43 }
44 NEFORCE_NODISCARD bool operator<(const error_code& rhs) const noexcept {
45 if (*category_ < *rhs.category_) {
46 return true;
47 }
48 if (*rhs.category_ < *category_) {
49 return false;
50 }
51 return value_ < rhs.value_;
52 }
53
54 NEFORCE_NODISCARD bool operator==(const error_condition& cond) const noexcept {
55 return category_->equivalent(value_, cond) || cond.category().equivalent(*this, cond.value());
56 }
57 NEFORCE_NODISCARD bool operator!=(const error_condition& cond) const noexcept { return !(*this == cond); }
58
59 NEFORCE_NODISCARD size_t to_hash() const noexcept {
60 const size_t h1 = hash<const error_category*>{}(category_);
61 const size_t h2 = hash<int>{}(value_);
62 return h1 ^ (h2 << 32 | h2 >> 32);
63 }
64};
65
66inline error_code make_error_code(errc e) noexcept { return {static_cast<int>(e), generic_category()}; }
67
68error_code NEFORCE_API last_error() noexcept;
69
70NEFORCE_END_NAMESPACE__
71#endif // NEFORCE_CORE_EXCEPTION_ERROR_CODE_HPP__
errc
系统错误码枚举
通用接口,同时具备可比较和可哈希功能
NEFORCE_NODISCARD constexpr size_t to_hash() const noexcept(noexcept(derived().to_hash()))