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 {};
53 error_code() noexcept;
54 error_code(int val, const error_category& cat) noexcept;
55 template <class ErrorCodeEnum>
56 error_code(ErrorCodeEnum e) noexcept;
59 void assign(int val, const error_category& cat) noexcept;
60 template <class ErrorCodeEnum>
61 error_code& operator=(ErrorCodeEnum e) noexcept;
62 void clear() noexcept;
65 int value() const noexcept;
66 const error_category& category() const noexcept;
67 error_condition default_error_condition() const noexcept;
68 string message() const;
69 explicit operator bool() const noexcept;
72 // non-member functions:
73 bool operator<(const error_code& lhs, const error_code& rhs) noexcept;
74 template <class charT, class traits>
75 basic_ostream<charT,traits>&
76 operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
82 error_condition() noexcept;
83 error_condition(int val, const error_category& cat) noexcept;
84 template <class ErrorConditionEnum>
85 error_condition(ErrorConditionEnum e) noexcept;
88 void assign(int val, const error_category& cat) noexcept;
89 template <class ErrorConditionEnum>
90 error_condition& operator=(ErrorConditionEnum e) noexcept;
91 void clear() noexcept;
94 int value() const noexcept;
95 const error_category& category() const noexcept;
96 string message() const noexcept;
97 explicit operator bool() const noexcept;
100 bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;
103 : public runtime_error
106 system_error(error_code ec, const string& what_arg);
107 system_error(error_code ec, const char* what_arg);
108 system_error(error_code ec);
109 system_error(int ev, const error_category& ecat, const string& what_arg);
110 system_error(int ev, const error_category& ecat, const char* what_arg);
111 system_error(int ev, const error_category& ecat);
113 const error_code& code() const noexcept;
114 const char* what() const noexcept;
119 address_family_not_supported, // EAFNOSUPPORT
120 address_in_use, // EADDRINUSE
121 address_not_available, // EADDRNOTAVAIL
122 already_connected, // EISCONN
123 argument_list_too_long, // E2BIG
124 argument_out_of_domain, // EDOM
125 bad_address, // EFAULT
126 bad_file_descriptor, // EBADF
127 bad_message, // EBADMSG
128 broken_pipe, // EPIPE
129 connection_aborted, // ECONNABORTED
130 connection_already_in_progress, // EALREADY
131 connection_refused, // ECONNREFUSED
132 connection_reset, // ECONNRESET
133 cross_device_link, // EXDEV
134 destination_address_required, // EDESTADDRREQ
135 device_or_resource_busy, // EBUSY
136 directory_not_empty, // ENOTEMPTY
137 executable_format_error, // ENOEXEC
138 file_exists, // EEXIST
139 file_too_large, // EFBIG
140 filename_too_long, // ENAMETOOLONG
141 function_not_supported, // ENOSYS
142 host_unreachable, // EHOSTUNREACH
143 identifier_removed, // EIDRM
144 illegal_byte_sequence, // EILSEQ
145 inappropriate_io_control_operation, // ENOTTY
146 interrupted, // EINTR
147 invalid_argument, // EINVAL
148 invalid_seek, // ESPIPE
150 is_a_directory, // EISDIR
151 message_size, // EMSGSIZE
152 network_down, // ENETDOWN
153 network_reset, // ENETRESET
154 network_unreachable, // ENETUNREACH
155 no_buffer_space, // ENOBUFS
156 no_child_process, // ECHILD
158 no_lock_available, // ENOLCK
159 no_message_available, // ENODATA
160 no_message, // ENOMSG
161 no_protocol_option, // ENOPROTOOPT
162 no_space_on_device, // ENOSPC
163 no_stream_resources, // ENOSR
164 no_such_device_or_address, // ENXIO
165 no_such_device, // ENODEV
166 no_such_file_or_directory, // ENOENT
167 no_such_process, // ESRCH
168 not_a_directory, // ENOTDIR
169 not_a_socket, // ENOTSOCK
170 not_a_stream, // ENOSTR
171 not_connected, // ENOTCONN
172 not_enough_memory, // ENOMEM
173 not_supported, // ENOTSUP
174 operation_canceled, // ECANCELED
175 operation_in_progress, // EINPROGRESS
176 operation_not_permitted, // EPERM
177 operation_not_supported, // EOPNOTSUPP
178 operation_would_block, // EWOULDBLOCK
179 owner_dead, // EOWNERDEAD
180 permission_denied, // EACCES
181 protocol_error, // EPROTO
182 protocol_not_supported, // EPROTONOSUPPORT
183 read_only_file_system, // EROFS
184 resource_deadlock_would_occur, // EDEADLK
185 resource_unavailable_try_again, // EAGAIN
186 result_out_of_range, // ERANGE
187 state_not_recoverable, // ENOTRECOVERABLE
188 stream_timeout, // ETIME
189 text_file_busy, // ETXTBSY
190 timed_out, // ETIMEDOUT
191 too_many_files_open_in_system, // ENFILE
192 too_many_files_open, // EMFILE
193 too_many_links, // EMLINK
194 too_many_symbolic_link_levels, // ELOOP
195 value_too_large, // EOVERFLOW
196 wrong_protocol_type // EPROTOTYPE
199 template <> struct is_error_condition_enum<errc>
202 error_code make_error_code(errc e) noexcept;
203 error_condition make_error_condition(errc e) noexcept;
205 // Comparison operators:
206 bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
207 bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
208 bool operator==(const error_condition& lhs, const error_code& rhs) noexcept;
209 bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
210 bool operator!=(const error_code& lhs, const error_code& rhs) noexcept;
211 bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;
212 bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;
213 bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;
215 template <> struct hash<std::error_code>;
223 #include <type_traits>
225 #include <__functional_base>
227 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
228 #pragma GCC system_header
231 _LIBCPP_BEGIN_NAMESPACE_STD
233 // is_error_code_enum
236 struct _LIBCPP_TYPE_VIS_ONLY is_error_code_enum
237 : public false_type {};
239 // is_error_condition_enum
242 struct _LIBCPP_TYPE_VIS_ONLY is_error_condition_enum
243 : public false_type {};
245 // Some error codes are not present on all platforms, so we provide equivalents
249 _LIBCPP_DECLARE_STRONG_ENUM(errc)
251 address_family_not_supported = EAFNOSUPPORT,
252 address_in_use = EADDRINUSE,
253 address_not_available = EADDRNOTAVAIL,
254 already_connected = EISCONN,
255 argument_list_too_long = E2BIG,
256 argument_out_of_domain = EDOM,
257 bad_address = EFAULT,
258 bad_file_descriptor = EBADF,
259 bad_message = EBADMSG,
261 connection_aborted = ECONNABORTED,
262 connection_already_in_progress = EALREADY,
263 connection_refused = ECONNREFUSED,
264 connection_reset = ECONNRESET,
265 cross_device_link = EXDEV,
266 destination_address_required = EDESTADDRREQ,
267 device_or_resource_busy = EBUSY,
268 directory_not_empty = ENOTEMPTY,
269 executable_format_error = ENOEXEC,
270 file_exists = EEXIST,
271 file_too_large = EFBIG,
272 filename_too_long = ENAMETOOLONG,
273 function_not_supported = ENOSYS,
274 host_unreachable = EHOSTUNREACH,
275 identifier_removed = EIDRM,
276 illegal_byte_sequence = EILSEQ,
277 inappropriate_io_control_operation = ENOTTY,
279 invalid_argument = EINVAL,
280 invalid_seek = ESPIPE,
282 is_a_directory = EISDIR,
283 message_size = EMSGSIZE,
284 network_down = ENETDOWN,
285 network_reset = ENETRESET,
286 network_unreachable = ENETUNREACH,
287 no_buffer_space = ENOBUFS,
288 no_child_process = ECHILD,
290 no_lock_available = ENOLCK,
292 no_message_available = ENODATA,
294 no_message_available = ENOMSG,
297 no_protocol_option = ENOPROTOOPT,
298 no_space_on_device = ENOSPC,
300 no_stream_resources = ENOSR,
302 no_stream_resources = ENOMEM,
304 no_such_device_or_address = ENXIO,
305 no_such_device = ENODEV,
306 no_such_file_or_directory = ENOENT,
307 no_such_process = ESRCH,
308 not_a_directory = ENOTDIR,
309 not_a_socket = ENOTSOCK,
311 not_a_stream = ENOSTR,
313 not_a_stream = EINVAL,
315 not_connected = ENOTCONN,
316 not_enough_memory = ENOMEM,
317 not_supported = ENOTSUP,
318 operation_canceled = ECANCELED,
319 operation_in_progress = EINPROGRESS,
320 operation_not_permitted = EPERM,
321 operation_not_supported = EOPNOTSUPP,
322 operation_would_block = EWOULDBLOCK,
323 owner_dead = EOWNERDEAD,
324 permission_denied = EACCES,
325 protocol_error = EPROTO,
326 protocol_not_supported = EPROTONOSUPPORT,
327 read_only_file_system = EROFS,
328 resource_deadlock_would_occur = EDEADLK,
329 resource_unavailable_try_again = EAGAIN,
330 result_out_of_range = ERANGE,
331 state_not_recoverable = ENOTRECOVERABLE,
333 stream_timeout = ETIME,
335 stream_timeout = ETIMEDOUT,
337 text_file_busy = ETXTBSY,
338 timed_out = ETIMEDOUT,
339 too_many_files_open_in_system = ENFILE,
340 too_many_files_open = EMFILE,
341 too_many_links = EMLINK,
342 too_many_symbolic_link_levels = ELOOP,
343 value_too_large = EOVERFLOW,
344 wrong_protocol_type = EPROTOTYPE
346 _LIBCPP_DECLARE_STRONG_ENUM_EPILOG(errc)
349 struct _LIBCPP_TYPE_VIS_ONLY is_error_condition_enum<errc>
352 #ifdef _LIBCPP_HAS_NO_STRONG_ENUMS
354 struct _LIBCPP_TYPE_VIS_ONLY is_error_condition_enum<errc::__lx>
358 class _LIBCPP_TYPE_VIS error_condition;
359 class _LIBCPP_TYPE_VIS error_code;
361 // class error_category
363 class _LIBCPP_HIDDEN __do_message;
365 class _LIBCPP_TYPE_VIS error_category
368 virtual ~error_category() _NOEXCEPT;
370 #ifdef _LIBCPP_BUILDING_SYSTEM_ERROR
371 error_category() _NOEXCEPT;
373 _LIBCPP_ALWAYS_INLINE
374 _LIBCPP_CONSTEXPR_AFTER_CXX11 error_category() _NOEXCEPT _LIBCPP_DEFAULT
377 error_category(const error_category&);// = delete;
378 error_category& operator=(const error_category&);// = delete;
381 virtual const char* name() const _NOEXCEPT = 0;
382 virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
383 virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
384 virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
385 virtual string message(int __ev) const = 0;
387 _LIBCPP_ALWAYS_INLINE
388 bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
390 _LIBCPP_ALWAYS_INLINE
391 bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
393 _LIBCPP_ALWAYS_INLINE
394 bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
396 friend class _LIBCPP_HIDDEN __do_message;
399 class _LIBCPP_HIDDEN __do_message
400 : public error_category
403 virtual string message(int ev) const;
406 _LIBCPP_FUNC_VIS const error_category& generic_category() _NOEXCEPT;
407 _LIBCPP_FUNC_VIS const error_category& system_category() _NOEXCEPT;
409 class _LIBCPP_TYPE_VIS error_condition
412 const error_category* __cat_;
414 _LIBCPP_ALWAYS_INLINE
415 error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
417 _LIBCPP_ALWAYS_INLINE
418 error_condition(int __val, const error_category& __cat) _NOEXCEPT
419 : __val_(__val), __cat_(&__cat) {}
422 _LIBCPP_ALWAYS_INLINE
423 error_condition(_Ep __e,
424 typename enable_if<is_error_condition_enum<_Ep>::value>::type* = 0
426 {*this = make_error_condition(__e);}
428 _LIBCPP_ALWAYS_INLINE
429 void assign(int __val, const error_category& __cat) _NOEXCEPT
436 _LIBCPP_ALWAYS_INLINE
439 is_error_condition_enum<_Ep>::value,
442 operator=(_Ep __e) _NOEXCEPT
443 {*this = make_error_condition(__e); return *this;}
445 _LIBCPP_ALWAYS_INLINE
446 void clear() _NOEXCEPT
449 __cat_ = &generic_category();
452 _LIBCPP_ALWAYS_INLINE
453 int value() const _NOEXCEPT {return __val_;}
455 _LIBCPP_ALWAYS_INLINE
456 const error_category& category() const _NOEXCEPT {return *__cat_;}
457 string message() const;
459 _LIBCPP_ALWAYS_INLINE
461 operator bool() const _NOEXCEPT {return __val_ != 0;}
464 inline _LIBCPP_INLINE_VISIBILITY
466 make_error_condition(errc __e) _NOEXCEPT
468 return error_condition(static_cast<int>(__e), generic_category());
471 inline _LIBCPP_INLINE_VISIBILITY
473 operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
475 return __x.category() < __y.category()
476 || (__x.category() == __y.category() && __x.value() < __y.value());
481 class _LIBCPP_TYPE_VIS error_code
484 const error_category* __cat_;
486 _LIBCPP_ALWAYS_INLINE
487 error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
489 _LIBCPP_ALWAYS_INLINE
490 error_code(int __val, const error_category& __cat) _NOEXCEPT
491 : __val_(__val), __cat_(&__cat) {}
494 _LIBCPP_ALWAYS_INLINE
496 typename enable_if<is_error_code_enum<_Ep>::value>::type* = 0
498 {*this = make_error_code(__e);}
500 _LIBCPP_ALWAYS_INLINE
501 void assign(int __val, const error_category& __cat) _NOEXCEPT
508 _LIBCPP_ALWAYS_INLINE
511 is_error_code_enum<_Ep>::value,
514 operator=(_Ep __e) _NOEXCEPT
515 {*this = make_error_code(__e); return *this;}
517 _LIBCPP_ALWAYS_INLINE
518 void clear() _NOEXCEPT
521 __cat_ = &system_category();
524 _LIBCPP_ALWAYS_INLINE
525 int value() const _NOEXCEPT {return __val_;}
527 _LIBCPP_ALWAYS_INLINE
528 const error_category& category() const _NOEXCEPT {return *__cat_;}
530 _LIBCPP_ALWAYS_INLINE
531 error_condition default_error_condition() const _NOEXCEPT
532 {return __cat_->default_error_condition(__val_);}
534 string message() const;
536 _LIBCPP_ALWAYS_INLINE
538 operator bool() const _NOEXCEPT {return __val_ != 0;}
541 inline _LIBCPP_INLINE_VISIBILITY
543 make_error_code(errc __e) _NOEXCEPT
545 return error_code(static_cast<int>(__e), generic_category());
548 inline _LIBCPP_INLINE_VISIBILITY
550 operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
552 return __x.category() < __y.category()
553 || (__x.category() == __y.category() && __x.value() < __y.value());
556 inline _LIBCPP_INLINE_VISIBILITY
558 operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
560 return __x.category() == __y.category() && __x.value() == __y.value();
563 inline _LIBCPP_INLINE_VISIBILITY
565 operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
567 return __x.category().equivalent(__x.value(), __y)
568 || __y.category().equivalent(__x, __y.value());
571 inline _LIBCPP_INLINE_VISIBILITY
573 operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
578 inline _LIBCPP_INLINE_VISIBILITY
580 operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
582 return __x.category() == __y.category() && __x.value() == __y.value();
585 inline _LIBCPP_INLINE_VISIBILITY
587 operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
588 {return !(__x == __y);}
590 inline _LIBCPP_INLINE_VISIBILITY
592 operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
593 {return !(__x == __y);}
595 inline _LIBCPP_INLINE_VISIBILITY
597 operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
598 {return !(__x == __y);}
600 inline _LIBCPP_INLINE_VISIBILITY
602 operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
603 {return !(__x == __y);}
606 struct _LIBCPP_TYPE_VIS_ONLY hash<error_code>
607 : public unary_function<error_code, size_t>
609 _LIBCPP_INLINE_VISIBILITY
610 size_t operator()(const error_code& __ec) const _NOEXCEPT
612 return static_cast<size_t>(__ec.value());
618 class _LIBCPP_TYPE_VIS system_error
619 : public runtime_error
623 system_error(error_code __ec, const string& __what_arg);
624 system_error(error_code __ec, const char* __what_arg);
625 system_error(error_code __ec);
626 system_error(int __ev, const error_category& __ecat, const string& __what_arg);
627 system_error(int __ev, const error_category& __ecat, const char* __what_arg);
628 system_error(int __ev, const error_category& __ecat);
629 ~system_error() _NOEXCEPT;
631 _LIBCPP_ALWAYS_INLINE
632 const error_code& code() const _NOEXCEPT {return __ec_;}
635 static string __init(const error_code&, string);
638 _LIBCPP_FUNC_VIS void __throw_system_error(int ev, const char* what_arg);
640 _LIBCPP_END_NAMESPACE_STD
642 #endif // _LIBCPP_SYSTEM_ERROR