| 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___CXX03___SYSTEM_ERROR_ERROR_CODE_H |
| 11 |
#define _LIBCPP___CXX03___SYSTEM_ERROR_ERROR_CODE_H |
| 12 |
|
| 13 |
#include <__cxx03/__config> |
| 14 |
#include <__cxx03/__functional/hash.h> |
| 15 |
#include <__cxx03/__functional/unary_function.h> |
| 16 |
#include <__cxx03/__system_error/errc.h> |
| 17 |
#include <__cxx03/__system_error/error_category.h> |
| 18 |
#include <__cxx03/__system_error/error_condition.h> |
| 19 |
#include <__cxx03/cstddef> |
| 20 |
#include <__cxx03/string> |
| 21 |
|
| 22 |
#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 23 |
# pragma GCC system_header |
| 24 |
#endif |
| 25 |
|
| 26 |
_LIBCPP_BEGIN_NAMESPACE_STD |
| 27 |
|
| 28 |
template <class _Tp> |
| 29 |
struct _LIBCPP_TEMPLATE_VIS is_error_code_enum : public false_type {}; |
| 30 |
|
| 31 |
namespace __adl_only { |
| 32 |
// Those cause ADL to trigger but they are not viable candidates, |
| 33 |
// so they are never actually selected. |
| 34 |
void make_error_code() = delete; |
| 35 |
} // namespace __adl_only |
| 36 |
|
| 37 |
class _LIBCPP_EXPORTED_FROM_ABI error_code { |
| 38 |
int __val_; |
| 39 |
const error_category* __cat_; |
| 40 |
|
| 41 |
public: |
| 42 |
_LIBCPP_HIDE_FROM_ABI error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {} |
| 43 |
|
| 44 |
_LIBCPP_HIDE_FROM_ABI error_code(int __val, const error_category& __cat) _NOEXCEPT : __val_(__val), __cat_(&__cat) {} |
| 45 |
|
| 46 |
template <class _Ep, __enable_if_t<is_error_code_enum<_Ep>::value, int> = 0> |
| 47 |
_LIBCPP_HIDE_FROM_ABI error_code(_Ep __e) _NOEXCEPT { |
| 48 |
using __adl_only::make_error_code; |
| 49 |
*this = make_error_code(__e); |
| 50 |
} |
| 51 |
|
| 52 |
_LIBCPP_HIDE_FROM_ABI void assign(int __val, const error_category& __cat) _NOEXCEPT { |
| 53 |
__val_ = __val; |
| 54 |
__cat_ = &__cat; |
| 55 |
} |
| 56 |
|
| 57 |
template <class _Ep, __enable_if_t<is_error_code_enum<_Ep>::value, int> = 0> |
| 58 |
_LIBCPP_HIDE_FROM_ABI error_code& operator=(_Ep __e) _NOEXCEPT { |
| 59 |
using __adl_only::make_error_code; |
| 60 |
*this = make_error_code(__e); |
| 61 |
return *this; |
| 62 |
} |
| 63 |
|
| 64 |
_LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT { |
| 65 |
__val_ = 0; |
| 66 |
__cat_ = &system_category(); |
| 67 |
} |
| 68 |
|
| 69 |
_LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __val_; } |
| 70 |
|
| 71 |
_LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; } |
| 72 |
|
| 73 |
_LIBCPP_HIDE_FROM_ABI error_condition default_error_condition() const _NOEXCEPT { |
| 74 |
return __cat_->default_error_condition(__val_); |
| 75 |
} |
| 76 |
|
| 77 |
string message() const; |
| 78 |
|
| 79 |
_LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __val_ != 0; } |
| 80 |
}; |
| 81 |
|
| 82 |
inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(errc __e) _NOEXCEPT { |
| 83 |
return error_code(static_cast<int>(__e), generic_category()); |
| 84 |
} |
| 85 |
|
| 86 |
inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, const error_code& __y) _NOEXCEPT { |
| 87 |
return __x.category() == __y.category() && __x.value() == __y.value(); |
| 88 |
} |
| 89 |
|
| 90 |
inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT { |
| 91 |
return __x.category().equivalent(__x.value(), __y) || __y.category().equivalent(__x, __y.value()); |
| 92 |
} |
| 93 |
|
| 94 |
inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT { |
| 95 |
return __y == __x; |
| 96 |
} |
| 97 |
|
| 98 |
inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT { |
| 99 |
return !(__x == __y); |
| 100 |
} |
| 101 |
|
| 102 |
inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT { |
| 103 |
return !(__x == __y); |
| 104 |
} |
| 105 |
|
| 106 |
inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT { |
| 107 |
return !(__x == __y); |
| 108 |
} |
| 109 |
|
| 110 |
inline _LIBCPP_HIDE_FROM_ABI bool operator<(const error_code& __x, const error_code& __y) _NOEXCEPT { |
| 111 |
return __x.category() < __y.category() || (__x.category() == __y.category() && __x.value() < __y.value()); |
| 112 |
} |
| 113 |
|
| 114 |
template <> |
| 115 |
struct _LIBCPP_TEMPLATE_VIS hash<error_code> : public __unary_function<error_code, size_t> { |
| 116 |
_LIBCPP_HIDE_FROM_ABI size_t operator()(const error_code& __ec) const _NOEXCEPT { |
| 117 |
return static_cast<size_t>(__ec.value()); |
| 118 |
} |
| 119 |
}; |
| 120 |
|
| 121 |
_LIBCPP_END_NAMESPACE_STD |
| 122 |
|
| 123 |
#endif // _LIBCPP___CXX03___SYSTEM_ERROR_ERROR_CODE_H |