2 //===---------------------------- system_error ----------------------------===//
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
11 #define _LIBCPP_SYSTEM_ERROR
22 virtual ~error_category() noexcept;
24 constexpr error_category();
25 error_category(const error_category&) = delete;
26 error_category& operator=(const error_category&) = delete;
28 virtual const char* name() const noexcept = 0;
29 virtual error_condition default_error_condition(int ev) const noexcept;
30 virtual bool equivalent(int code, const error_condition& condition) const noexcept;
31 virtual bool equivalent(const error_code& code, int condition) const noexcept;
32 virtual string message(int ev) const = 0;
34 bool operator==(const error_category& rhs) const noexcept;
35 bool operator!=(const error_category& rhs) const noexcept;
36 bool operator<(const error_category& rhs) const noexcept;
39 const error_category& generic_category() noexcept;
40 const error_category& system_category() noexcept;
42 template <class T> struct is_error_code_enum
43 : public false_type {};
45 template <class T> struct is_error_condition_enum
46 : public false_type {};
49 inline constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
52 inline constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value; // C++17
58 error_code() noexcept;
59 error_code(int val, const error_category& cat) noexcept;
60 template <class ErrorCodeEnum>
61 error_code(ErrorCodeEnum e) noexcept;
64 void assign(int val, const error_category& cat) noexcept;
65 template <class ErrorCodeEnum>
66 error_code& operator=(ErrorCodeEnum e) noexcept;
67 void clear() noexcept;
70 int value() const noexcept;
71 const error_category& category() const noexcept;
72 error_condition default_error_condition() const noexcept;
73 string message() const;
74 explicit operator bool() const noexcept;
77 // non-member functions:
78 bool operator<(const error_code& lhs, const error_code& rhs) noexcept;
79 template <class charT, class traits>
80 basic_ostream<charT,traits>&
81 operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
87 error_condition() noexcept;
88 error_condition(int val, const error_category& cat) noexcept;
89 template <class ErrorConditionEnum>
90 error_condition(ErrorConditionEnum e) noexcept;
93 void assign(int val, const error_category& cat) noexcept;
94 template <class ErrorConditionEnum>
95 error_condition& operator=(ErrorConditionEnum e) noexcept;
96 void clear() noexcept;
99 int value() const noexcept;
100 const error_category& category() const noexcept;
101 string message() const noexcept;
102 explicit operator bool() const noexcept;
105 bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;
108 : public runtime_error
111 system_error(error_code ec, const string& what_arg);
112 system_error(error_code ec, const char* what_arg);
113 system_error(error_code ec);
114 system_error(int ev, const error_category& ecat, const string& what_arg);
115 system_error(int ev, const error_category& ecat, const char* what_arg);
116 system_error(int ev, const error_category& ecat);
118 const error_code& code() const noexcept;
119 const char* what() const noexcept;
122 template <> struct is_error_condition_enum<errc>
125 error_code make_error_code(errc e) noexcept;
126 error_condition make_error_condition(errc e) noexcept;
128 // Comparison operators:
129 bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
130 bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
131 bool operator==(const error_condition& lhs, const error_code& rhs) noexcept;
132 bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
133 bool operator!=(const error_code& lhs, const error_code& rhs) noexcept;
134 bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;
135 bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;
136 bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;
138 template <> struct hash<std::error_code>;
139 template <> struct hash<std::error_condition>;
146 #include <type_traits>
148 #include <__functional_base>
151 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
152 #pragma GCC system_header
155 _LIBCPP_BEGIN_NAMESPACE_STD
157 // is_error_code_enum
160 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum
161 : public false_type {};
163 #if _LIBCPP_STD_VER > 14
165 _LIBCPP_INLINE_VAR constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value;
168 // is_error_condition_enum
171 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
172 : public false_type {};
174 #if _LIBCPP_STD_VER > 14
176 _LIBCPP_INLINE_VAR constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
180 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc>
183 #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
185 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc::__lx>
189 class _LIBCPP_TYPE_VIS error_condition;
190 class _LIBCPP_TYPE_VIS error_code;
192 // class error_category
194 class _LIBCPP_HIDDEN __do_message;
196 class _LIBCPP_TYPE_VIS error_category
199 virtual ~error_category() _NOEXCEPT;
201 #if defined(_LIBCPP_BUILDING_LIBRARY) && \
202 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
203 error_category() _NOEXCEPT;
205 _LIBCPP_INLINE_VISIBILITY
206 _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT _LIBCPP_DEFAULT
209 error_category(const error_category&);// = delete;
210 error_category& operator=(const error_category&);// = delete;
213 virtual const char* name() const _NOEXCEPT = 0;
214 virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
215 virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
216 virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
217 virtual string message(int __ev) const = 0;
219 _LIBCPP_INLINE_VISIBILITY
220 bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
222 _LIBCPP_INLINE_VISIBILITY
223 bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
225 _LIBCPP_INLINE_VISIBILITY
226 bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
228 friend class _LIBCPP_HIDDEN __do_message;
231 class _LIBCPP_HIDDEN __do_message
232 : public error_category
235 virtual string message(int ev) const;
238 _LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
239 _LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
241 class _LIBCPP_TYPE_VIS error_condition
244 const error_category* __cat_;
246 _LIBCPP_INLINE_VISIBILITY
247 error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
249 _LIBCPP_INLINE_VISIBILITY
250 error_condition(int __val, const error_category& __cat) _NOEXCEPT
251 : __val_(__val), __cat_(&__cat) {}
254 _LIBCPP_INLINE_VISIBILITY
255 error_condition(_Ep __e,
256 typename enable_if<is_error_condition_enum<_Ep>::value>::type* = 0
258 {*this = make_error_condition(__e);}
260 _LIBCPP_INLINE_VISIBILITY
261 void assign(int __val, const error_category& __cat) _NOEXCEPT
268 _LIBCPP_INLINE_VISIBILITY
271 is_error_condition_enum<_Ep>::value,
274 operator=(_Ep __e) _NOEXCEPT
275 {*this = make_error_condition(__e); return *this;}
277 _LIBCPP_INLINE_VISIBILITY
278 void clear() _NOEXCEPT
281 __cat_ = &generic_category();
284 _LIBCPP_INLINE_VISIBILITY
285 int value() const _NOEXCEPT {return __val_;}
287 _LIBCPP_INLINE_VISIBILITY
288 const error_category& category() const _NOEXCEPT {return *__cat_;}
289 string message() const;
291 _LIBCPP_INLINE_VISIBILITY
293 operator bool() const _NOEXCEPT {return __val_ != 0;}
296 inline _LIBCPP_INLINE_VISIBILITY
298 make_error_condition(errc __e) _NOEXCEPT
300 return error_condition(static_cast<int>(__e), generic_category());
303 inline _LIBCPP_INLINE_VISIBILITY
305 operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
307 return __x.category() < __y.category()
308 || (__x.category() == __y.category() && __x.value() < __y.value());
313 class _LIBCPP_TYPE_VIS error_code
316 const error_category* __cat_;
318 _LIBCPP_INLINE_VISIBILITY
319 error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
321 _LIBCPP_INLINE_VISIBILITY
322 error_code(int __val, const error_category& __cat) _NOEXCEPT
323 : __val_(__val), __cat_(&__cat) {}
326 _LIBCPP_INLINE_VISIBILITY
328 typename enable_if<is_error_code_enum<_Ep>::value>::type* = 0
330 {*this = make_error_code(__e);}
332 _LIBCPP_INLINE_VISIBILITY
333 void assign(int __val, const error_category& __cat) _NOEXCEPT
340 _LIBCPP_INLINE_VISIBILITY
343 is_error_code_enum<_Ep>::value,
346 operator=(_Ep __e) _NOEXCEPT
347 {*this = make_error_code(__e); return *this;}
349 _LIBCPP_INLINE_VISIBILITY
350 void clear() _NOEXCEPT
353 __cat_ = &system_category();
356 _LIBCPP_INLINE_VISIBILITY
357 int value() const _NOEXCEPT {return __val_;}
359 _LIBCPP_INLINE_VISIBILITY
360 const error_category& category() const _NOEXCEPT {return *__cat_;}
362 _LIBCPP_INLINE_VISIBILITY
363 error_condition default_error_condition() const _NOEXCEPT
364 {return __cat_->default_error_condition(__val_);}
366 string message() const;
368 _LIBCPP_INLINE_VISIBILITY
370 operator bool() const _NOEXCEPT {return __val_ != 0;}
373 inline _LIBCPP_INLINE_VISIBILITY
375 make_error_code(errc __e) _NOEXCEPT
377 return error_code(static_cast<int>(__e), generic_category());
380 inline _LIBCPP_INLINE_VISIBILITY
382 operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
384 return __x.category() < __y.category()
385 || (__x.category() == __y.category() && __x.value() < __y.value());
388 inline _LIBCPP_INLINE_VISIBILITY
390 operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
392 return __x.category() == __y.category() && __x.value() == __y.value();
395 inline _LIBCPP_INLINE_VISIBILITY
397 operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
399 return __x.category().equivalent(__x.value(), __y)
400 || __y.category().equivalent(__x, __y.value());
403 inline _LIBCPP_INLINE_VISIBILITY
405 operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
410 inline _LIBCPP_INLINE_VISIBILITY
412 operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
414 return __x.category() == __y.category() && __x.value() == __y.value();
417 inline _LIBCPP_INLINE_VISIBILITY
419 operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
420 {return !(__x == __y);}
422 inline _LIBCPP_INLINE_VISIBILITY
424 operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
425 {return !(__x == __y);}
427 inline _LIBCPP_INLINE_VISIBILITY
429 operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
430 {return !(__x == __y);}
432 inline _LIBCPP_INLINE_VISIBILITY
434 operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
435 {return !(__x == __y);}
438 struct _LIBCPP_TEMPLATE_VIS hash<error_code>
439 : public unary_function<error_code, size_t>
441 _LIBCPP_INLINE_VISIBILITY
442 size_t operator()(const error_code& __ec) const _NOEXCEPT
444 return static_cast<size_t>(__ec.value());
449 struct _LIBCPP_TEMPLATE_VIS hash<error_condition>
450 : public unary_function<error_condition, size_t>
452 _LIBCPP_INLINE_VISIBILITY
453 size_t operator()(const error_condition& __ec) const _NOEXCEPT
455 return static_cast<size_t>(__ec.value());
461 class _LIBCPP_TYPE_VIS system_error
462 : public runtime_error
466 system_error(error_code __ec, const string& __what_arg);
467 system_error(error_code __ec, const char* __what_arg);
468 system_error(error_code __ec);
469 system_error(int __ev, const error_category& __ecat, const string& __what_arg);
470 system_error(int __ev, const error_category& __ecat, const char* __what_arg);
471 system_error(int __ev, const error_category& __ecat);
472 ~system_error() _NOEXCEPT;
474 _LIBCPP_INLINE_VISIBILITY
475 const error_code& code() const _NOEXCEPT {return __ec_;}
478 static string __init(const error_code&, string);
481 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS
482 void __throw_system_error(int ev, const char* what_arg);
484 _LIBCPP_END_NAMESPACE_STD
486 #endif // _LIBCPP_SYSTEM_ERROR