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