2 //===---------------------------- system_error ----------------------------===//
4 // The LLVM Compiler Infrastructure
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
9 //===----------------------------------------------------------------------===//
11 #ifndef _LIBCPP_SYSTEM_ERROR
12 #define _LIBCPP_SYSTEM_ERROR
23 virtual ~error_category() noexcept;
25 constexpr error_category();
26 error_category(const error_category&) = delete;
27 error_category& operator=(const error_category&) = delete;
29 virtual const char* name() const noexcept = 0;
30 virtual error_condition default_error_condition(int ev) const noexcept;
31 virtual bool equivalent(int code, const error_condition& condition) const noexcept;
32 virtual bool equivalent(const error_code& code, int condition) const noexcept;
33 virtual string message(int ev) const = 0;
35 bool operator==(const error_category& rhs) const noexcept;
36 bool operator!=(const error_category& rhs) const noexcept;
37 bool operator<(const error_category& rhs) const noexcept;
40 const error_category& generic_category() noexcept;
41 const error_category& system_category() noexcept;
43 template <class T> struct is_error_code_enum
44 : public false_type {};
46 template <class T> struct is_error_condition_enum
47 : public false_type {};
50 inline constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value; // C++17
53 inline constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value; // C++17
59 error_code() noexcept;
60 error_code(int val, const error_category& cat) noexcept;
61 template <class ErrorCodeEnum>
62 error_code(ErrorCodeEnum e) noexcept;
65 void assign(int val, const error_category& cat) noexcept;
66 template <class ErrorCodeEnum>
67 error_code& operator=(ErrorCodeEnum e) noexcept;
68 void clear() noexcept;
71 int value() const noexcept;
72 const error_category& category() const noexcept;
73 error_condition default_error_condition() const noexcept;
74 string message() const;
75 explicit operator bool() const noexcept;
78 // non-member functions:
79 bool operator<(const error_code& lhs, const error_code& rhs) noexcept;
80 template <class charT, class traits>
81 basic_ostream<charT,traits>&
82 operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
88 error_condition() noexcept;
89 error_condition(int val, const error_category& cat) noexcept;
90 template <class ErrorConditionEnum>
91 error_condition(ErrorConditionEnum e) noexcept;
94 void assign(int val, const error_category& cat) noexcept;
95 template <class ErrorConditionEnum>
96 error_condition& operator=(ErrorConditionEnum e) noexcept;
97 void clear() noexcept;
100 int value() const noexcept;
101 const error_category& category() const noexcept;
102 string message() const noexcept;
103 explicit operator bool() const noexcept;
106 bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;
109 : public runtime_error
112 system_error(error_code ec, const string& what_arg);
113 system_error(error_code ec, const char* what_arg);
114 system_error(error_code ec);
115 system_error(int ev, const error_category& ecat, const string& what_arg);
116 system_error(int ev, const error_category& ecat, const char* what_arg);
117 system_error(int ev, const error_category& ecat);
119 const error_code& code() const noexcept;
120 const char* what() const noexcept;
123 template <> struct is_error_condition_enum<errc>
126 error_code make_error_code(errc e) noexcept;
127 error_condition make_error_condition(errc e) noexcept;
129 // Comparison operators:
130 bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
131 bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
132 bool operator==(const error_condition& lhs, const error_code& rhs) noexcept;
133 bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
134 bool operator!=(const error_code& lhs, const error_code& rhs) noexcept;
135 bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;
136 bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;
137 bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;
139 template <> struct hash<std::error_code>;
140 template <> struct hash<std::error_condition>;
147 #include <type_traits>
149 #include <__functional_base>
152 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
153 #pragma GCC system_header
156 _LIBCPP_BEGIN_NAMESPACE_STD
158 // is_error_code_enum
161 struct _LIBCPP_TEMPLATE_VIS is_error_code_enum
162 : public false_type {};
164 #if _LIBCPP_STD_VER > 14
166 _LIBCPP_INLINE_VAR constexpr size_t is_error_code_enum_v = is_error_code_enum<_Tp>::value;
169 // is_error_condition_enum
172 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum
173 : public false_type {};
175 #if _LIBCPP_STD_VER > 14
177 _LIBCPP_INLINE_VAR constexpr size_t is_error_condition_enum_v = is_error_condition_enum<_Tp>::value;
181 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc>
184 #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
186 struct _LIBCPP_TEMPLATE_VIS is_error_condition_enum<errc::__lx>
190 class _LIBCPP_TYPE_VIS error_condition;
191 class _LIBCPP_TYPE_VIS error_code;
193 // class error_category
195 class _LIBCPP_HIDDEN __do_message;
197 class _LIBCPP_TYPE_VIS error_category
200 virtual ~error_category() _NOEXCEPT;
202 #if defined(_LIBCPP_BUILDING_LIBRARY) && \
203 defined(_LIBCPP_DEPRECATED_ABI_LEGACY_LIBRARY_DEFINITIONS_FOR_INLINE_FUNCTIONS)
204 error_category() _NOEXCEPT;
206 _LIBCPP_INLINE_VISIBILITY
207 _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT _LIBCPP_DEFAULT
210 error_category(const error_category&);// = delete;
211 error_category& operator=(const error_category&);// = delete;
214 virtual const char* name() const _NOEXCEPT = 0;
215 virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
216 virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
217 virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
218 virtual string message(int __ev) const = 0;
220 _LIBCPP_INLINE_VISIBILITY
221 bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
223 _LIBCPP_INLINE_VISIBILITY
224 bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
226 _LIBCPP_INLINE_VISIBILITY
227 bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
229 friend class _LIBCPP_HIDDEN __do_message;
232 class _LIBCPP_HIDDEN __do_message
233 : public error_category
236 virtual string message(int ev) const;
239 _LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
240 _LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
242 class _LIBCPP_TYPE_VIS error_condition
245 const error_category* __cat_;
247 _LIBCPP_INLINE_VISIBILITY
248 error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
250 _LIBCPP_INLINE_VISIBILITY
251 error_condition(int __val, const error_category& __cat) _NOEXCEPT
252 : __val_(__val), __cat_(&__cat) {}
255 _LIBCPP_INLINE_VISIBILITY
256 error_condition(_Ep __e,
257 typename enable_if<is_error_condition_enum<_Ep>::value>::type* = 0
259 {*this = make_error_condition(__e);}
261 _LIBCPP_INLINE_VISIBILITY
262 void assign(int __val, const error_category& __cat) _NOEXCEPT
269 _LIBCPP_INLINE_VISIBILITY
272 is_error_condition_enum<_Ep>::value,
275 operator=(_Ep __e) _NOEXCEPT
276 {*this = make_error_condition(__e); return *this;}
278 _LIBCPP_INLINE_VISIBILITY
279 void clear() _NOEXCEPT
282 __cat_ = &generic_category();
285 _LIBCPP_INLINE_VISIBILITY
286 int value() const _NOEXCEPT {return __val_;}
288 _LIBCPP_INLINE_VISIBILITY
289 const error_category& category() const _NOEXCEPT {return *__cat_;}
290 string message() const;
292 _LIBCPP_INLINE_VISIBILITY
294 operator bool() const _NOEXCEPT {return __val_ != 0;}
297 inline _LIBCPP_INLINE_VISIBILITY
299 make_error_condition(errc __e) _NOEXCEPT
301 return error_condition(static_cast<int>(__e), generic_category());
304 inline _LIBCPP_INLINE_VISIBILITY
306 operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
308 return __x.category() < __y.category()
309 || (__x.category() == __y.category() && __x.value() < __y.value());
314 class _LIBCPP_TYPE_VIS error_code
317 const error_category* __cat_;
319 _LIBCPP_INLINE_VISIBILITY
320 error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
322 _LIBCPP_INLINE_VISIBILITY
323 error_code(int __val, const error_category& __cat) _NOEXCEPT
324 : __val_(__val), __cat_(&__cat) {}
327 _LIBCPP_INLINE_VISIBILITY
329 typename enable_if<is_error_code_enum<_Ep>::value>::type* = 0
331 {*this = make_error_code(__e);}
333 _LIBCPP_INLINE_VISIBILITY
334 void assign(int __val, const error_category& __cat) _NOEXCEPT
341 _LIBCPP_INLINE_VISIBILITY
344 is_error_code_enum<_Ep>::value,
347 operator=(_Ep __e) _NOEXCEPT
348 {*this = make_error_code(__e); return *this;}
350 _LIBCPP_INLINE_VISIBILITY
351 void clear() _NOEXCEPT
354 __cat_ = &system_category();
357 _LIBCPP_INLINE_VISIBILITY
358 int value() const _NOEXCEPT {return __val_;}
360 _LIBCPP_INLINE_VISIBILITY
361 const error_category& category() const _NOEXCEPT {return *__cat_;}
363 _LIBCPP_INLINE_VISIBILITY
364 error_condition default_error_condition() const _NOEXCEPT
365 {return __cat_->default_error_condition(__val_);}
367 string message() const;
369 _LIBCPP_INLINE_VISIBILITY
371 operator bool() const _NOEXCEPT {return __val_ != 0;}
374 inline _LIBCPP_INLINE_VISIBILITY
376 make_error_code(errc __e) _NOEXCEPT
378 return error_code(static_cast<int>(__e), generic_category());
381 inline _LIBCPP_INLINE_VISIBILITY
383 operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
385 return __x.category() < __y.category()
386 || (__x.category() == __y.category() && __x.value() < __y.value());
389 inline _LIBCPP_INLINE_VISIBILITY
391 operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
393 return __x.category() == __y.category() && __x.value() == __y.value();
396 inline _LIBCPP_INLINE_VISIBILITY
398 operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
400 return __x.category().equivalent(__x.value(), __y)
401 || __y.category().equivalent(__x, __y.value());
404 inline _LIBCPP_INLINE_VISIBILITY
406 operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
411 inline _LIBCPP_INLINE_VISIBILITY
413 operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
415 return __x.category() == __y.category() && __x.value() == __y.value();
418 inline _LIBCPP_INLINE_VISIBILITY
420 operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
421 {return !(__x == __y);}
423 inline _LIBCPP_INLINE_VISIBILITY
425 operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
426 {return !(__x == __y);}
428 inline _LIBCPP_INLINE_VISIBILITY
430 operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
431 {return !(__x == __y);}
433 inline _LIBCPP_INLINE_VISIBILITY
435 operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
436 {return !(__x == __y);}
439 struct _LIBCPP_TEMPLATE_VIS hash<error_code>
440 : public unary_function<error_code, size_t>
442 _LIBCPP_INLINE_VISIBILITY
443 size_t operator()(const error_code& __ec) const _NOEXCEPT
445 return static_cast<size_t>(__ec.value());
450 struct _LIBCPP_TEMPLATE_VIS hash<error_condition>
451 : public unary_function<error_condition, size_t>
453 _LIBCPP_INLINE_VISIBILITY
454 size_t operator()(const error_condition& __ec) const _NOEXCEPT
456 return static_cast<size_t>(__ec.value());
462 class _LIBCPP_TYPE_VIS system_error
463 : public runtime_error
467 system_error(error_code __ec, const string& __what_arg);
468 system_error(error_code __ec, const char* __what_arg);
469 system_error(error_code __ec);
470 system_error(int __ev, const error_category& __ecat, const string& __what_arg);
471 system_error(int __ev, const error_category& __ecat, const char* __what_arg);
472 system_error(int __ev, const error_category& __ecat);
473 ~system_error() _NOEXCEPT;
475 _LIBCPP_INLINE_VISIBILITY
476 const error_code& code() const _NOEXCEPT {return __ec_;}
479 static string __init(const error_code&, string);
482 _LIBCPP_NORETURN _LIBCPP_FUNC_VIS
483 void __throw_system_error(int ev, const char* what_arg);
485 _LIBCPP_END_NAMESPACE_STD
487 #endif // _LIBCPP_SYSTEM_ERROR