Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / libcxx / include / __system_error / error_code.h
blob475f2bb96a56d93597d2b67eeb0b3305aef13f97
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 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H
11 #define _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H
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>
23 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
24 # pragma GCC system_header
25 #endif
27 _LIBCPP_BEGIN_NAMESPACE_STD
29 template <class _Tp>
30 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum : public false_type {};
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
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
43 class _LIBCPP_EXPORTED_FROM_ABI error_code {
44 int __val_;
45 const error_category* __cat_;
47 public:
48 _LIBCPP_HIDE_FROM_ABI error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
50 _LIBCPP_HIDE_FROM_ABI error_code(int __val, const error_category& __cat) _NOEXCEPT : __val_(__val), __cat_(&__cat) {}
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);
58 _LIBCPP_HIDE_FROM_ABI void assign(int __val, const error_category& __cat) _NOEXCEPT {
59 __val_ = __val;
60 __cat_ = &__cat;
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;
70 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT {
71 __val_ = 0;
72 __cat_ = &system_category();
75 _LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __val_; }
77 _LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; }
79 _LIBCPP_HIDE_FROM_ABI error_condition default_error_condition() const _NOEXCEPT {
80 return __cat_->default_error_condition(__val_);
83 string message() const;
85 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __val_ != 0; }
88 inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(errc __e) _NOEXCEPT {
89 return error_code(static_cast<int>(__e), generic_category());
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();
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());
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;
104 #endif
106 #if _LIBCPP_STD_VER <= 17
108 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT {
109 return !(__x == __y);
112 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT {
113 return !(__x == __y);
116 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT {
117 return !(__x == __y);
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());
124 #else // _LIBCPP_STD_VER <= 17
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();
132 #endif // _LIBCPP_STD_VER <= 17
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());
141 _LIBCPP_END_NAMESPACE_STD
143 #endif // _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H