File indexing completed on 2026-05-03 08:13:43
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___CXX03___SYSTEM_ERROR_ERROR_CODE_H
0011 #define _LIBCPP___CXX03___SYSTEM_ERROR_ERROR_CODE_H
0012
0013 #include <__cxx03/__compare/ordering.h>
0014 #include <__cxx03/__config>
0015 #include <__cxx03/__functional/hash.h>
0016 #include <__cxx03/__functional/unary_function.h>
0017 #include <__cxx03/__system_error/errc.h>
0018 #include <__cxx03/__system_error/error_category.h>
0019 #include <__cxx03/__system_error/error_condition.h>
0020 #include <__cxx03/cstddef>
0021 #include <__cxx03/string>
0022
0023 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0024 # pragma GCC system_header
0025 #endif
0026
0027 _LIBCPP_BEGIN_NAMESPACE_STD
0028
0029 template <class _Tp>
0030 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum : public false_type {};
0031
0032 #if _LIBCPP_STD_VER >= 17
0033 template <class _Tp>
0034 inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value;
0035 #endif
0036
0037 namespace __adl_only {
0038
0039
0040 void make_error_code() = delete;
0041 }
0042
0043 class _LIBCPP_EXPORTED_FROM_ABI error_code {
0044 int __val_;
0045 const error_category* __cat_;
0046
0047 public:
0048 _LIBCPP_HIDE_FROM_ABI error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
0049
0050 _LIBCPP_HIDE_FROM_ABI error_code(int __val, const error_category& __cat) _NOEXCEPT : __val_(__val), __cat_(&__cat) {}
0051
0052 template <class _Ep, __enable_if_t<is_error_code_enum<_Ep>::value, int> = 0>
0053 _LIBCPP_HIDE_FROM_ABI error_code(_Ep __e) _NOEXCEPT {
0054 using __adl_only::make_error_code;
0055 *this = make_error_code(__e);
0056 }
0057
0058 _LIBCPP_HIDE_FROM_ABI void assign(int __val, const error_category& __cat) _NOEXCEPT {
0059 __val_ = __val;
0060 __cat_ = &__cat;
0061 }
0062
0063 template <class _Ep, __enable_if_t<is_error_code_enum<_Ep>::value, int> = 0>
0064 _LIBCPP_HIDE_FROM_ABI error_code& operator=(_Ep __e) _NOEXCEPT {
0065 using __adl_only::make_error_code;
0066 *this = make_error_code(__e);
0067 return *this;
0068 }
0069
0070 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT {
0071 __val_ = 0;
0072 __cat_ = &system_category();
0073 }
0074
0075 _LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __val_; }
0076
0077 _LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; }
0078
0079 _LIBCPP_HIDE_FROM_ABI error_condition default_error_condition() const _NOEXCEPT {
0080 return __cat_->default_error_condition(__val_);
0081 }
0082
0083 string message() const;
0084
0085 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __val_ != 0; }
0086 };
0087
0088 inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(errc __e) _NOEXCEPT {
0089 return error_code(static_cast<int>(__e), generic_category());
0090 }
0091
0092 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, const error_code& __y) _NOEXCEPT {
0093 return __x.category() == __y.category() && __x.value() == __y.value();
0094 }
0095
0096 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT {
0097 return __x.category().equivalent(__x.value(), __y) || __y.category().equivalent(__x, __y.value());
0098 }
0099
0100 #if _LIBCPP_STD_VER <= 17
0101 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT {
0102 return __y == __x;
0103 }
0104 #endif
0105
0106 #if _LIBCPP_STD_VER <= 17
0107
0108 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT {
0109 return !(__x == __y);
0110 }
0111
0112 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT {
0113 return !(__x == __y);
0114 }
0115
0116 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT {
0117 return !(__x == __y);
0118 }
0119
0120 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const error_code& __x, const error_code& __y) _NOEXCEPT {
0121 return __x.category() < __y.category() || (__x.category() == __y.category() && __x.value() < __y.value());
0122 }
0123
0124 #else
0125
0126 inline _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(const error_code& __x, const error_code& __y) noexcept {
0127 if (auto __c = __x.category() <=> __y.category(); __c != 0)
0128 return __c;
0129 return __x.value() <=> __y.value();
0130 }
0131
0132 #endif
0133
0134 template <>
0135 struct _LIBCPP_TEMPLATE_VIS hash<error_code> : public __unary_function<error_code, size_t> {
0136 _LIBCPP_HIDE_FROM_ABI size_t operator()(const error_code& __ec) const _NOEXCEPT {
0137 return static_cast<size_t>(__ec.value());
0138 }
0139 };
0140
0141 _LIBCPP_END_NAMESPACE_STD
0142
0143 #endif