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, __enable_if_t<is_error_code_enum<_Ep>::value, int> = 0> 53 _LIBCPP_HIDE_FROM_ABI error_code(_Ep __e) _NOEXCEPT { 54 using __adl_only::make_error_code; 55 *this = make_error_code(__e); 56 } 57 58 _LIBCPP_HIDE_FROM_ABI void assign(int __val, const error_category& __cat) _NOEXCEPT { 59 __val_ = __val; 60 __cat_ = &__cat; 61 } 62 63 template <class _Ep, __enable_if_t<is_error_code_enum<_Ep>::value, int> = 0> 64 _LIBCPP_HIDE_FROM_ABI error_code& operator=(_Ep __e) _NOEXCEPT { 65 using __adl_only::make_error_code; 66 *this = make_error_code(__e); 67 return *this; 68 } 69 70 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { 71 __val_ = 0; 72 __cat_ = &system_category(); 73 } 74 75 _LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __val_; } 76 77 _LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; } 78 79 _LIBCPP_HIDE_FROM_ABI error_condition default_error_condition() const _NOEXCEPT { 80 return __cat_->default_error_condition(__val_); 81 } 82 83 string message() const; 84 85 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __val_ != 0; } 86 }; 87 88 inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(errc __e) _NOEXCEPT { 89 return error_code(static_cast<int>(__e), generic_category()); 90 } 91 92 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, const error_code& __y) _NOEXCEPT { 93 return __x.category() == __y.category() && __x.value() == __y.value(); 94 } 95 96 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT { 97 return __x.category().equivalent(__x.value(), __y) || __y.category().equivalent(__x, __y.value()); 98 } 99 100 #if _LIBCPP_STD_VER <= 17 101 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT { 102 return __y == __x; 103 } 104 #endif 105 106 #if _LIBCPP_STD_VER <= 17 107 108 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT { 109 return !(__x == __y); 110 } 111 112 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT { 113 return !(__x == __y); 114 } 115 116 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT { 117 return !(__x == __y); 118 } 119 120 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const error_code& __x, const error_code& __y) _NOEXCEPT { 121 return __x.category() < __y.category() || (__x.category() == __y.category() && __x.value() < __y.value()); 122 } 123 124 #else // _LIBCPP_STD_VER <= 17 125 126 inline _LIBCPP_HIDE_FROM_ABI strong_ordering operator<=>(const error_code& __x, const error_code& __y) noexcept { 127 if (auto __c = __x.category() <=> __y.category(); __c != 0) 128 return __c; 129 return __x.value() <=> __y.value(); 130 } 131 132 #endif // _LIBCPP_STD_VER <= 17 133 134 template <> 135 struct _LIBCPP_TEMPLATE_VIS hash<error_code> : public __unary_function<error_code, size_t> { 136 _LIBCPP_HIDE_FROM_ABI size_t operator()(const error_code& __ec) const _NOEXCEPT { 137 return static_cast<size_t>(__ec.value()); 138 } 139 }; 140 141 _LIBCPP_END_NAMESPACE_STD 142 143 #endif // _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H 144