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__
6class error_code :
public icommon<error_code> {
9 const error_category* category_;
12 error_code() noexcept :
14 category_(&system_category()) {}
16 error_code(
int val,
const error_category& cat) noexcept :
20 error_code(
errc e)
noexcept { *
this = make_error_code(e); }
22 void assign(
int val,
const error_category& cat)
noexcept {
27 void clear() noexcept {
29 category_ = &system_category();
32 NEFORCE_NODISCARD
int value() const noexcept {
return value_; }
33 NEFORCE_NODISCARD
const error_category& category() const noexcept {
return *category_; }
35 error_condition default_error_condition() const noexcept {
return category_->default_error_condition(value_); }
37 NEFORCE_NODISCARD
string message()
const {
return category_->message(value_); }
39 explicit operator bool() const noexcept {
return value_ != 0; }
41 NEFORCE_NODISCARD
bool operator==(
const error_code& rhs)
const noexcept {
42 return category_ == rhs.category_ && value_ == rhs.value_;
44 NEFORCE_NODISCARD
bool operator<(
const error_code& rhs)
const noexcept {
45 if (*category_ < *rhs.category_) {
48 if (*rhs.category_ < *category_) {
51 return value_ < rhs.value_;
54 NEFORCE_NODISCARD
bool operator==(
const error_condition& cond)
const noexcept {
55 return category_->equivalent(value_, cond) || cond.category().equivalent(*
this, cond.value());
57 NEFORCE_NODISCARD
bool operator!=(
const error_condition& cond)
const noexcept {
return !(*
this == cond); }
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);
66inline error_code make_error_code(
errc e)
noexcept {
return {
static_cast<int>(e), generic_category()}; }
68error_code NEFORCE_API last_error() noexcept;
70NEFORCE_END_NAMESPACE__
NEFORCE_NODISCARD constexpr size_t to_hash() const noexcept(noexcept(derived().to_hash()))