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 errc error() const noexcept { return static_cast<errc>(value_); }
34 NEFORCE_NODISCARD const error_category& category() const noexcept { return *category_; }
35
36 NEFORCE_NODISCARD error_condition default_error_condition() const noexcept {
37 return category_->default_error_condition(value_);
38 }
39
40 NEFORCE_NODISCARD string message() const { return category_->message(value_); }
41
42 explicit operator bool() const noexcept { return value_ != 0; }
43
44 NEFORCE_NODISCARD bool equal_to(const error_code& rhs) const noexcept {
45 return category_ == rhs.category_ && value_ == rhs.value_;
46 }
47 NEFORCE_NODISCARD bool less_than(const error_code& rhs) const noexcept {
48 if (*category_ < *rhs.category_) {
49 return true;
50 }
51 if (*rhs.category_ < *category_) {
52 return false;
53 }
54 return value_ < rhs.value_;
55 }
56
57 NEFORCE_NODISCARD bool operator==(const error_condition& cond) const noexcept {
58 return category_->equivalent(value_, cond) || cond.category().equivalent(*this, cond.value());
59 }
60 NEFORCE_NODISCARD bool operator!=(const error_condition& cond) const noexcept { return !(*this == cond); }
61
62 NEFORCE_NODISCARD size_t to_hash() const noexcept {
63 const size_t h1 = hash<const error_category*>{}(category_);
64 const size_t h2 = hash<int>{}(value_);
65#ifdef NEFORCE_ARCH_BITS_64
66 return h1 ^ (h2 << 32 | h2 >> 32);
67#else
68 return h1 ^ h2;
69#endif
70 }
71};
72
73inline error_code make_error_code(errc e) noexcept { return {static_cast<int>(e), generic_category()}; }
74
75error_code NEFORCE_API last_error() noexcept;
76
77NEFORCE_END_NAMESPACE__
78#endif // NEFORCE_CORE_EXCEPTION_ERROR_CODE_HPP__
errc
系统错误码枚举
通用接口,同时具备可比较和可哈希功能