Warning: This file is not a C or C++ file. It does not have highlighting.

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___SYSTEM_ERROR_ERROR_CODE_H
11#define _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H
12
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 <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
28template <class _Tp>
29struct is_error_code_enum : public false_type {};
30
31#if _LIBCPP_STD_VER >= 17
32template <class _Tp>
33inline constexpr bool is_error_code_enum_v = is_error_code_enum<_Tp>::value;
34#endif
35
36namespace __adl_only {
37// Those cause ADL to trigger but they are not viable candidates,
38// so they are never actually selected.
39void make_error_code() = delete;
40} // namespace __adl_only
41
42class _LIBCPP_EXPORTED_FROM_ABI error_code {
43 int __val_;
44 const error_category* __cat_;
45
46public:
47 _LIBCPP_HIDE_FROM_ABI error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
48
49 _LIBCPP_HIDE_FROM_ABI error_code(int __val, const error_category& __cat) _NOEXCEPT : __val_(__val), __cat_(&__cat) {}
50
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);
55 }
56
57 _LIBCPP_HIDE_FROM_ABI void assign(int __val, const error_category& __cat) _NOEXCEPT {
58 __val_ = __val;
59 __cat_ = &__cat;
60 }
61
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);
66 return *this;
67 }
68
69 _LIBCPP_HIDE_FROM_ABI void clear() _NOEXCEPT {
70 __val_ = 0;
71 __cat_ = &system_category();
72 }
73
74 _LIBCPP_HIDE_FROM_ABI int value() const _NOEXCEPT { return __val_; }
75
76 _LIBCPP_HIDE_FROM_ABI const error_category& category() const _NOEXCEPT { return *__cat_; }
77
78 _LIBCPP_HIDE_FROM_ABI error_condition default_error_condition() const _NOEXCEPT {
79 return __cat_->default_error_condition(__val_);
80 }
81
82 string message() const;
83
84 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const _NOEXCEPT { return __val_ != 0; }
85};
86
87inline _LIBCPP_HIDE_FROM_ABI error_code make_error_code(errc __e) _NOEXCEPT {
88 return error_code(static_cast<int>(__e), generic_category());
89}
90
91inline _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();
93}
94
95inline _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());
97}
98
99#if _LIBCPP_STD_VER <= 17
100inline _LIBCPP_HIDE_FROM_ABI bool operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT {
101 return __y == __x;
102}
103#endif
104
105#if _LIBCPP_STD_VER <= 17
106
107inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT {
108 return !(__x == __y);
109}
110
111inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT {
112 return !(__x == __y);
113}
114
115inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT {
116 return !(__x == __y);
117}
118
119inline _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());
121}
122
123#else // _LIBCPP_STD_VER <= 17
124
125inline _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)
127 return __c;
128 return __x.value() <=> __y.value();
129}
130
131#endif // _LIBCPP_STD_VER <= 17
132
133template <>
134struct 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());
137 }
138};
139
140_LIBCPP_END_NAMESPACE_STD
141
142#endif // _LIBCPP___SYSTEM_ERROR_ERROR_CODE_H
143

Warning: This file is not a C or C++ file. It does not have highlighting.

source code of libcxx/include/__system_error/error_code.h