Debian Codegraph @

libcxx/include/__cxx03/__system_error/error_condition.h

raw
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_CONDITION_H
11 #define _LIBCPP___CXX03___SYSTEM_ERROR_ERROR_CONDITION_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/cstddef>
19 #include <__cxx03/string>
20
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 # pragma GCC system_header
23 #endif
24
25 _LIBCPP_BEGIN_NAMESPACE_STD
26
27 template <class _Tp>
28 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum : public false_type {};
29
30 template <>
31 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc> : true_type {};
32
33 template <>
34 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc::__lx> : true_type {};
35
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_condition() = delete;
40 } // namespace __adl_only
41
42 class _LIBCPP_EXPORTED_FROM_ABI error_condition {
43 int __val_;
44 const error_category* __cat_;
45
46 public:
47 _LIBCPP_HIDE_FROM_ABI error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
48
49 _LIBCPP_HIDE_FROM_ABI error_condition(int __val, const error_category& __cat) _NOEXCEPT
50 : __val_(__val),
51 __cat_(&__cat) {}
52
53 template <class _Ep, __enable_if_t<is_error_condition_enum<_Ep>::value, int> = 0>
54 _LIBCPP_HIDE_FROM_ABI error_condition(_Ep __e) _NOEXCEPT {
55 using __adl_only::make_error_condition;
56 *this = make_error_condition(__e);
57 }
58
59 _LIBCPP_HIDE_FROM_ABI void assign(int __val, const error_category& __cat) _NOEXCEPT {
60 __val_ = __val;
61 __cat_ = &__cat;
62 }
63
64 template <class _Ep, __enable_if_t<is_error_condition_enum<_Ep>::value, int> = 0>
65 _LIBCPP_HIDE_FROM_ABI error_condition& operator=(_Ep __e) _NOEXCEPT {
66 using __adl_only::make_error_condition;
67 *this = make_error_condition(__e);
68 return *this;
69 }
70
71 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT {
72 __val_ = 0;
73 __cat_ = &generic_category();
74 }
75
76 _LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __val_; }
77
78 _LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; }
79 string message() const;
80
81 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __val_ != 0; }
82 };
83
84 inline _LIBCPP_HIDE_FROM_ABI error_condition make_error_condition(errc __e) _NOEXCEPT {
85 return error_condition(static_cast<int>(__e), generic_category());
86 }
87
88 inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT {
89 return __x.category() == __y.category() && __x.value() == __y.value();
90 }
91
92 inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT {
93 return !(__x == __y);
94 }
95
96 inline _LIBCPP_HIDE_FROM_ABI bool operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT {
97 return __x.category() < __y.category() || (__x.category() == __y.category() && __x.value() < __y.value());
98 }
99
100 template <>
101 struct _LIBCPP_TEMPLATE_VIS hash<error_condition> : public __unary_function<error_condition, size_t> {
102 _LIBCPP_HIDE_FROM_ABI size_t operator()(const error_condition& __ec) const _NOEXCEPT {
103 return static_cast<size_t>(__ec.value());
104 }
105 };
106
107 _LIBCPP_END_NAMESPACE_STD
108
109 #endif // _LIBCPP___CXX03___SYSTEM_ERROR_ERROR_CONDITION_H