1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H 11 #define _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H 12 13 #include <__compare/ordering.h> 14 #include <__config> 15 #include <__functional/hash.h> 16 #include <__functional/unary_function.h> 17 #include <__system_error/errc.h> 18 #include <__system_error/error_category.h> 19 #include <__system_error/error_condition.h> 20 #include <cstddef> 21 #include <string> 22 23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 24 # pragma GCC system_header 25 #endif 26 27 _LIBCPP_BEGIN_NAMESPACE_STD 28 29 template <class _Tp> 30 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum : public false_type {}; 31 32 #if _LIBCPP_STD_VER >= 17 33 template <class _Tp> 34 inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value; 35 #endif 36 37 namespace __adl_only { 38 // Those cause ADL to trigger but they are not viable candidates, 39 // so they are never actually selected. 40 void make_error_code() = delete; 41 } // namespace __adl_only 42 43 class _LIBCPP_EXPORTED_FROM_ABI error_code { 44 int __val_; 45 const error_category* __cat_; 46 47 public: 48 _LIBCPP_HIDE_FROM_ABI error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {} 49 50 _LIBCPP_HIDE_FROM_ABI error_code(int __val, const error_category& __cat) _NOEXCEPT : __val_(__val), __cat_(&__cat) {} 51 52 template <class _Ep> 53 _LIBCPP_HIDE_FROM_ABI 54 error_code(_Ep __e, typename enable_if<is_error_code_enum<_Ep>::value>::type* = nullptr) _NOEXCEPT { 55 using __adl_only::make_error_code; 56 *this = make_error_code(__e); 57 } 58 59 _LIBCPP_HIDE_FROM_ABI void assign(int __val, const error_category& __cat) _NOEXCEPT { 60 __val_ = __val; 61 __cat_ = &__cat; 62 } 63 64 template <class _Ep> 65 _LIBCPP_HIDE_FROM_ABI typename enable_if< is_error_code_enum<_Ep>::value, error_code& >::type 66 operator=(_Ep __e) _NOEXCEPT { 67 using __adl_only::make_error_code; 68 *this = make_error_code(__e); 69 return *this; 70 } 71 72 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { 73 __val_ = 0; 74 __cat_ = &system_category(); 75 } 76 77 _LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __val_; } 78 79 _LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; } 80 81 _LIBCPP_HIDE_FROM_ABI error_condition default_error_condition() const _NOEXCEPT { 82 return __cat_->default_error_condition(__val_); 83 } 84 85 string message() const; 86 87 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __val_ != 0; } 88 }; 89 90 inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(errc __e) _NOEXCEPT { 91 return error_code(static_cast<int>(__e), generic_category()); 92 } 93 94 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, const error_code& __y) _NOEXCEPT { 95 return __x.category() == __y.category() && __x.value() == __y.value(); 96 } 97 98 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT { 99 return __x.category().equivalent(__x.value(), __y) || __y.category().equivalent(__x, __y.value()); 100 } 101 102 #if _LIBCPP_STD_VER <= 17 103 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT { 104 return __y == __x; 105 } 106 #endif 107 108 #if _LIBCPP_STD_VER <= 17 109 110 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT { 111 return !(__x == __y); 112 } 113 114 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT { 115 return !(__x == __y); 116 } 117 118 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT { 119 return !(__x == __y); 120 } 121 122 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const error_code& __x, const error_code& __y) _NOEXCEPT { 123 return __x.category() < __y.category() || (__x.category() == __y.category() && __x.value() < __y.value()); 124 } 125 126 #else // _LIBCPP_STD_VER <= 17 127 128 inline _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(const error_code& __x, const error_code& __y) noexcept { 129 if (auto __c = __x.category() <=> __y.category(); __c != 0) 130 return __c; 131 return __x.value() <=> __y.value(); 132 } 133 134 #endif // _LIBCPP_STD_VER <= 17 135 136 template <> 137 struct _LIBCPP_TEMPLATE_VIS hash<error_code> : public __unary_function<error_code, size_t> { 138 _LIBCPP_HIDE_FROM_ABI size_t operator()(const error_code& __ec) const _NOEXCEPT { 139 return static_cast<size_t>(__ec.value()); 140 } 141 }; 142 143 _LIBCPP_END_NAMESPACE_STD 144 145 #endif // _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H 146