2 //===----------------------------------------------------------------------===//
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
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H
11 #define _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H
13 #include <__compare/ordering.h>
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>
22 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23 # pragma GCC system_header
26 _LIBCPP_BEGIN_NAMESPACE_STD
29 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum
: public false_type
{};
31 #if _LIBCPP_STD_VER >= 17
33 inline constexpr bool is_error_code_enum_v
= is_error_code_enum
<_Tp
>::value
;
36 namespace __adl_only
{
37 // Those cause ADL to trigger but they are not viable candidates,
38 // so they are never actually selected.
39 void make_error_code() = delete;
40 } // namespace __adl_only
42 class _LIBCPP_EXPORTED_FROM_ABI error_code
{
44 const error_category
* __cat_
;
47 _LIBCPP_HIDE_FROM_ABI
error_code() _NOEXCEPT
: __val_(0), __cat_(&system_category()) {}
49 _LIBCPP_HIDE_FROM_ABI
error_code(int __val
, const error_category
& __cat
) _NOEXCEPT
: __val_(__val
), __cat_(&__cat
) {}
51 template <class _Ep
, __enable_if_t
<is_error_code_enum
<_Ep
>::value
, int> = 0>
52 _LIBCPP_HIDE_FROM_ABI
error_code(_Ep __e
) _NOEXCEPT
{
53 using __adl_only::make_error_code
;
54 *this = make_error_code(__e
);
57 _LIBCPP_HIDE_FROM_ABI
void assign(int __val
, const error_category
& __cat
) _NOEXCEPT
{
62 template <class _Ep
, __enable_if_t
<is_error_code_enum
<_Ep
>::value
, int> = 0>
63 _LIBCPP_HIDE_FROM_ABI error_code
& operator=(_Ep __e
) _NOEXCEPT
{
64 using __adl_only::make_error_code
;
65 *this = make_error_code(__e
);
69 _LIBCPP_HIDE_FROM_ABI
void clear() _NOEXCEPT
{
71 __cat_
= &system_category();
74 _LIBCPP_HIDE_FROM_ABI
int value() const _NOEXCEPT
{ return __val_
; }
76 _LIBCPP_HIDE_FROM_ABI
const error_category
& category() const _NOEXCEPT
{ return *__cat_
; }
78 _LIBCPP_HIDE_FROM_ABI error_condition
default_error_condition() const _NOEXCEPT
{
79 return __cat_
->default_error_condition(__val_
);
82 string
message() const;
84 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const _NOEXCEPT
{ return __val_
!= 0; }
87 inline _LIBCPP_HIDE_FROM_ABI error_code
make_error_code(errc __e
) _NOEXCEPT
{
88 return error_code(static_cast<int>(__e
), generic_category());
91 inline _LIBCPP_HIDE_FROM_ABI
bool operator==(const error_code
& __x
, const error_code
& __y
) _NOEXCEPT
{
92 return __x
.category() == __y
.category() && __x
.value() == __y
.value();
95 inline _LIBCPP_HIDE_FROM_ABI
bool operator==(const error_code
& __x
, const error_condition
& __y
) _NOEXCEPT
{
96 return __x
.category().equivalent(__x
.value(), __y
) || __y
.category().equivalent(__x
, __y
.value());
99 #if _LIBCPP_STD_VER <= 17
100 inline _LIBCPP_HIDE_FROM_ABI
bool operator==(const error_condition
& __x
, const error_code
& __y
) _NOEXCEPT
{
105 #if _LIBCPP_STD_VER <= 17
107 inline _LIBCPP_HIDE_FROM_ABI
bool operator!=(const error_code
& __x
, const error_code
& __y
) _NOEXCEPT
{
108 return !(__x
== __y
);
111 inline _LIBCPP_HIDE_FROM_ABI
bool operator!=(const error_code
& __x
, const error_condition
& __y
) _NOEXCEPT
{
112 return !(__x
== __y
);
115 inline _LIBCPP_HIDE_FROM_ABI
bool operator!=(const error_condition
& __x
, const error_code
& __y
) _NOEXCEPT
{
116 return !(__x
== __y
);
119 inline _LIBCPP_HIDE_FROM_ABI
bool operator<(const error_code
& __x
, const error_code
& __y
) _NOEXCEPT
{
120 return __x
.category() < __y
.category() || (__x
.category() == __y
.category() && __x
.value() < __y
.value());
123 #else // _LIBCPP_STD_VER <= 17
125 inline _LIBCPP_HIDE_FROM_ABI strong_ordering
operator<=>(const error_code
& __x
, const error_code
& __y
) noexcept
{
126 if (auto __c
= __x
.category() <=> __y
.category(); __c
!= 0)
128 return __x
.value() <=> __y
.value();
131 #endif // _LIBCPP_STD_VER <= 17
134 struct _LIBCPP_TEMPLATE_VIS hash
<error_code
> : public __unary_function
<error_code
, size_t> {
135 _LIBCPP_HIDE_FROM_ABI
size_t operator()(const error_code
& __ec
) const _NOEXCEPT
{
136 return static_cast<size_t>(__ec
.value());
140 _LIBCPP_END_NAMESPACE_STD
142 #endif // _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H