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_CONDITION_H
11 #define _LIBCPP___SYSTEM_ERROR_ERROR_CONDITION_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>
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_condition_enum
: public false_type
{};
31 #if _LIBCPP_STD_VER >= 17
33 inline constexpr bool is_error_condition_enum_v
= is_error_condition_enum
<_Tp
>::value
;
37 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
<errc
> : true_type
{};
39 #ifdef _LIBCPP_CXX03_LANG
41 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
<errc::__lx
> : true_type
{};
44 namespace __adl_only
{
45 // Those cause ADL to trigger but they are not viable candidates,
46 // so they are never actually selected.
47 void make_error_condition() = delete;
48 } // namespace __adl_only
50 class _LIBCPP_EXPORTED_FROM_ABI error_condition
{
52 const error_category
* __cat_
;
55 _LIBCPP_HIDE_FROM_ABI
error_condition() _NOEXCEPT
: __val_(0), __cat_(&generic_category()) {}
57 _LIBCPP_HIDE_FROM_ABI
error_condition(int __val
, const error_category
& __cat
) _NOEXCEPT
61 template <class _Ep
, __enable_if_t
<is_error_condition_enum
<_Ep
>::value
, int> = 0>
62 _LIBCPP_HIDE_FROM_ABI
error_condition(_Ep __e
) _NOEXCEPT
{
63 using __adl_only::make_error_condition
;
64 *this = make_error_condition(__e
);
67 _LIBCPP_HIDE_FROM_ABI
void assign(int __val
, const error_category
& __cat
) _NOEXCEPT
{
72 template <class _Ep
, __enable_if_t
<is_error_condition_enum
<_Ep
>::value
, int> = 0>
73 _LIBCPP_HIDE_FROM_ABI error_condition
& operator=(_Ep __e
) _NOEXCEPT
{
74 using __adl_only::make_error_condition
;
75 *this = make_error_condition(__e
);
79 _LIBCPP_HIDE_FROM_ABI
void clear() _NOEXCEPT
{
81 __cat_
= &generic_category();
84 _LIBCPP_HIDE_FROM_ABI
int value() const _NOEXCEPT
{ return __val_
; }
86 _LIBCPP_HIDE_FROM_ABI
const error_category
& category() const _NOEXCEPT
{ return *__cat_
; }
87 string
message() const;
89 _LIBCPP_HIDE_FROM_ABI
explicit operator bool() const _NOEXCEPT
{ return __val_
!= 0; }
92 inline _LIBCPP_HIDE_FROM_ABI error_condition
make_error_condition(errc __e
) _NOEXCEPT
{
93 return error_condition(static_cast<int>(__e
), generic_category());
96 inline _LIBCPP_HIDE_FROM_ABI
bool operator==(const error_condition
& __x
, const error_condition
& __y
) _NOEXCEPT
{
97 return __x
.category() == __y
.category() && __x
.value() == __y
.value();
100 #if _LIBCPP_STD_VER <= 17
102 inline _LIBCPP_HIDE_FROM_ABI
bool operator!=(const error_condition
& __x
, const error_condition
& __y
) _NOEXCEPT
{
103 return !(__x
== __y
);
106 inline _LIBCPP_HIDE_FROM_ABI
bool operator<(const error_condition
& __x
, const error_condition
& __y
) _NOEXCEPT
{
107 return __x
.category() < __y
.category() || (__x
.category() == __y
.category() && __x
.value() < __y
.value());
110 #else // _LIBCPP_STD_VER <= 17
112 inline _LIBCPP_HIDE_FROM_ABI strong_ordering
113 operator<=>(const error_condition
& __x
, const error_condition
& __y
) noexcept
{
114 if (auto __c
= __x
.category() <=> __y
.category(); __c
!= 0)
116 return __x
.value() <=> __y
.value();
119 #endif // _LIBCPP_STD_VER <= 17
122 struct _LIBCPP_TEMPLATE_VIS hash
<error_condition
> : public __unary_function
<error_condition
, size_t> {
123 _LIBCPP_HIDE_FROM_ABI
size_t operator()(const error_condition
& __ec
) const _NOEXCEPT
{
124 return static_cast<size_t>(__ec
.value());
128 _LIBCPP_END_NAMESPACE_STD
130 #endif // _LIBCPP___SYSTEM_ERROR_ERROR_CONDITION_H