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 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 {};
52 error_code() noexcept;
53 error_code(int val, const error_category& cat) noexcept;
54 template <class ErrorCodeEnum>
55 error_code(ErrorCodeEnum e) noexcept;
58 void assign(int val, const error_category& cat) noexcept;
59 template <class ErrorCodeEnum>
60 error_code& operator=(ErrorCodeEnum e) noexcept;
61 void clear() noexcept;
64 int value() const noexcept;
65 const error_category& category() const noexcept;
66 error_condition default_error_condition() const noexcept;
67 string message() const;
68 explicit operator bool() const noexcept;
71 // non-member functions:
72 bool operator<(const error_code& lhs, const error_code& rhs) noexcept;
73 template <class charT, class traits>
74 basic_ostream<charT,traits>&
75 operator<<(basic_ostream<charT,traits>& os, const error_code& ec);
81 error_condition() noexcept;
82 error_condition(int val, const error_category& cat) noexcept;
83 template <class ErrorConditionEnum>
84 error_condition(ErrorConditionEnum e) noexcept;
87 void assign(int val, const error_category& cat) noexcept;
88 template <class ErrorConditionEnum>
89 error_condition& operator=(ErrorConditionEnum e) noexcept;
90 void clear() noexcept;
93 int value() const noexcept;
94 const error_category& category() const noexcept;
95 string message() const noexcept;
96 explicit operator bool() const noexcept;
99 bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept;
102 : public runtime_error
105 system_error(error_code ec, const string& what_arg);
106 system_error(error_code ec, const char* what_arg);
107 system_error(error_code ec);
108 system_error(int ev, const error_category& ecat, const string& what_arg);
109 system_error(int ev, const error_category& ecat, const char* what_arg);
110 system_error(int ev, const error_category& ecat);
112 const error_code& code() const noexcept;
113 const char* what() const noexcept;
118 address_family_not_supported, // EAFNOSUPPORT
119 address_in_use, // EADDRINUSE
120 address_not_available, // EADDRNOTAVAIL
121 already_connected, // EISCONN
122 argument_list_too_long, // E2BIG
123 argument_out_of_domain, // EDOM
124 bad_address, // EFAULT
125 bad_file_descriptor, // EBADF
126 bad_message, // EBADMSG
127 broken_pipe, // EPIPE
128 connection_aborted, // ECONNABORTED
129 connection_already_in_progress, // EALREADY
130 connection_refused, // ECONNREFUSED
131 connection_reset, // ECONNRESET
132 cross_device_link, // EXDEV
133 destination_address_required, // EDESTADDRREQ
134 device_or_resource_busy, // EBUSY
135 directory_not_empty, // ENOTEMPTY
136 executable_format_error, // ENOEXEC
137 file_exists, // EEXIST
138 file_too_large, // EFBIG
139 filename_too_long, // ENAMETOOLONG
140 function_not_supported, // ENOSYS
141 host_unreachable, // EHOSTUNREACH
142 identifier_removed, // EIDRM
143 illegal_byte_sequence, // EILSEQ
144 inappropriate_io_control_operation, // ENOTTY
145 interrupted, // EINTR
146 invalid_argument, // EINVAL
147 invalid_seek, // ESPIPE
149 is_a_directory, // EISDIR
150 message_size, // EMSGSIZE
151 network_down, // ENETDOWN
152 network_reset, // ENETRESET
153 network_unreachable, // ENETUNREACH
154 no_buffer_space, // ENOBUFS
155 no_child_process, // ECHILD
157 no_lock_available, // ENOLCK
158 no_message_available, // ENODATA
159 no_message, // ENOMSG
160 no_protocol_option, // ENOPROTOOPT
161 no_space_on_device, // ENOSPC
162 no_stream_resources, // ENOSR
163 no_such_device_or_address, // ENXIO
164 no_such_device, // ENODEV
165 no_such_file_or_directory, // ENOENT
166 no_such_process, // ESRCH
167 not_a_directory, // ENOTDIR
168 not_a_socket, // ENOTSOCK
169 not_a_stream, // ENOSTR
170 not_connected, // ENOTCONN
171 not_enough_memory, // ENOMEM
172 not_supported, // ENOTSUP
173 operation_canceled, // ECANCELED
174 operation_in_progress, // EINPROGRESS
175 operation_not_permitted, // EPERM
176 operation_not_supported, // EOPNOTSUPP
177 operation_would_block, // EWOULDBLOCK
178 owner_dead, // EOWNERDEAD
179 permission_denied, // EACCES
180 protocol_error, // EPROTO
181 protocol_not_supported, // EPROTONOSUPPORT
182 read_only_file_system, // EROFS
183 resource_deadlock_would_occur, // EDEADLK
184 resource_unavailable_try_again, // EAGAIN
185 result_out_of_range, // ERANGE
186 state_not_recoverable, // ENOTRECOVERABLE
187 stream_timeout, // ETIME
188 text_file_busy, // ETXTBSY
189 timed_out, // ETIMEDOUT
190 too_many_files_open_in_system, // ENFILE
191 too_many_files_open, // EMFILE
192 too_many_links, // EMLINK
193 too_many_symbolic_link_levels, // ELOOP
194 value_too_large, // EOVERFLOW
195 wrong_protocol_type // EPROTOTYPE
198 template <> struct is_error_condition_enum<errc>
201 error_code make_error_code(errc e) noexcept;
202 error_condition make_error_condition(errc e) noexcept;
204 // Comparison operators:
205 bool operator==(const error_code& lhs, const error_code& rhs) noexcept;
206 bool operator==(const error_code& lhs, const error_condition& rhs) noexcept;
207 bool operator==(const error_condition& lhs, const error_code& rhs) noexcept;
208 bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept;
209 bool operator!=(const error_code& lhs, const error_code& rhs) noexcept;
210 bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept;
211 bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept;
212 bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept;
214 template <> struct hash<std::error_code>;
222 #include <type_traits>
224 #include <__functional_base>
226 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
227 #pragma GCC system_header
230 _LIBCPP_BEGIN_NAMESPACE_STD
232 // is_error_code_enum
235 struct _LIBCPP_VISIBLE is_error_code_enum
236 : public false_type {};
238 // is_error_condition_enum
241 struct _LIBCPP_VISIBLE is_error_condition_enum
242 : public false_type {};
244 // Some error codes are not present on all platforms, so we provide equivalents
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
349 _LIBCPP_ALWAYS_INLINE
350 errc(_ __v) : __v_(__v) {}
351 _LIBCPP_ALWAYS_INLINE
352 operator int() const {return __v_;}
357 struct _LIBCPP_VISIBLE is_error_condition_enum<errc>
361 struct _LIBCPP_VISIBLE is_error_condition_enum<errc::_>
364 class error_condition;
367 // class error_category
371 class _LIBCPP_VISIBLE error_category
374 virtual ~error_category() _NOEXCEPT;
377 error_category() _NOEXCEPT;
378 error_category(const error_category&);// = delete;
379 error_category& operator=(const error_category&);// = delete;
382 virtual const char* name() const _NOEXCEPT = 0;
383 virtual error_condition default_error_condition(int __ev) const _NOEXCEPT;
384 virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT;
385 virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT;
386 virtual string message(int __ev) const = 0;
388 _LIBCPP_ALWAYS_INLINE
389 bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;}
391 _LIBCPP_ALWAYS_INLINE
392 bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);}
394 _LIBCPP_ALWAYS_INLINE
395 bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;}
397 friend class __do_message;
400 class _LIBCPP_HIDDEN __do_message
401 : public error_category
404 virtual string message(int ev) const;
407 const error_category& generic_category() _NOEXCEPT;
408 const error_category& system_category() _NOEXCEPT;
410 class _LIBCPP_VISIBLE error_condition
413 const error_category* __cat_;
415 _LIBCPP_ALWAYS_INLINE
416 error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {}
418 _LIBCPP_ALWAYS_INLINE
419 error_condition(int __val, const error_category& __cat) _NOEXCEPT
420 : __val_(__val), __cat_(&__cat) {}
423 _LIBCPP_ALWAYS_INLINE
424 error_condition(_E __e,
425 typename enable_if<is_error_condition_enum<_E>::value>::type* = 0
427 {*this = make_error_condition(__e);}
429 _LIBCPP_ALWAYS_INLINE
430 void assign(int __val, const error_category& __cat) _NOEXCEPT
437 _LIBCPP_ALWAYS_INLINE
440 is_error_condition_enum<_E>::value,
443 operator=(_E __e) _NOEXCEPT
444 {*this = make_error_condition(__e); return *this;}
446 _LIBCPP_ALWAYS_INLINE
447 void clear() _NOEXCEPT
450 __cat_ = &generic_category();
453 _LIBCPP_ALWAYS_INLINE
454 int value() const _NOEXCEPT {return __val_;}
456 _LIBCPP_ALWAYS_INLINE
457 const error_category& category() const _NOEXCEPT {return *__cat_;}
458 string message() const;
460 _LIBCPP_ALWAYS_INLINE
462 operator bool() const _NOEXCEPT {return __val_ != 0;}
465 inline _LIBCPP_INLINE_VISIBILITY
467 make_error_condition(errc __e) _NOEXCEPT
469 return error_condition(static_cast<int>(__e), generic_category());
472 inline _LIBCPP_INLINE_VISIBILITY
474 operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT
476 return __x.category() < __y.category()
477 || __x.category() == __y.category() && __x.value() < __y.value();
482 class _LIBCPP_VISIBLE error_code
485 const error_category* __cat_;
487 _LIBCPP_ALWAYS_INLINE
488 error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {}
490 _LIBCPP_ALWAYS_INLINE
491 error_code(int __val, const error_category& __cat) _NOEXCEPT
492 : __val_(__val), __cat_(&__cat) {}
495 _LIBCPP_ALWAYS_INLINE
497 typename enable_if<is_error_code_enum<_E>::value>::type* = 0
499 {*this = make_error_code(__e);}
501 _LIBCPP_ALWAYS_INLINE
502 void assign(int __val, const error_category& __cat) _NOEXCEPT
509 _LIBCPP_ALWAYS_INLINE
512 is_error_code_enum<_E>::value,
515 operator=(_E __e) _NOEXCEPT
516 {*this = make_error_code(__e); return *this;}
518 _LIBCPP_ALWAYS_INLINE
519 void clear() _NOEXCEPT
522 __cat_ = &system_category();
525 _LIBCPP_ALWAYS_INLINE
526 int value() const _NOEXCEPT {return __val_;}
528 _LIBCPP_ALWAYS_INLINE
529 const error_category& category() const _NOEXCEPT {return *__cat_;}
531 _LIBCPP_ALWAYS_INLINE
532 error_condition default_error_condition() const _NOEXCEPT
533 {return __cat_->default_error_condition(__val_);}
535 string message() const;
537 _LIBCPP_ALWAYS_INLINE
539 operator bool() const _NOEXCEPT {return __val_ != 0;}
542 inline _LIBCPP_INLINE_VISIBILITY
544 make_error_code(errc __e) _NOEXCEPT
546 return error_code(static_cast<int>(__e), generic_category());
549 inline _LIBCPP_INLINE_VISIBILITY
551 operator<(const error_code& __x, const error_code& __y) _NOEXCEPT
553 return __x.category() < __y.category()
554 || __x.category() == __y.category() && __x.value() < __y.value();
557 inline _LIBCPP_INLINE_VISIBILITY
559 operator==(const error_code& __x, const error_code& __y) _NOEXCEPT
561 return __x.category() == __y.category() && __x.value() == __y.value();
564 inline _LIBCPP_INLINE_VISIBILITY
566 operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT
568 return __x.category().equivalent(__x.value(), __y)
569 || __y.category().equivalent(__x, __y.value());
572 inline _LIBCPP_INLINE_VISIBILITY
574 operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT
579 inline _LIBCPP_INLINE_VISIBILITY
581 operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT
583 return __x.category() == __y.category() && __x.value() == __y.value();
586 inline _LIBCPP_INLINE_VISIBILITY
588 operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT
589 {return !(__x == __y);}
591 inline _LIBCPP_INLINE_VISIBILITY
593 operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT
594 {return !(__x == __y);}
596 inline _LIBCPP_INLINE_VISIBILITY
598 operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT
599 {return !(__x == __y);}
601 inline _LIBCPP_INLINE_VISIBILITY
603 operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT
604 {return !(__x == __y);}
607 struct _LIBCPP_VISIBLE hash<error_code>
608 : public unary_function<error_code, size_t>
610 _LIBCPP_INLINE_VISIBILITY
611 size_t operator()(const error_code& __ec) const _NOEXCEPT
613 return static_cast<size_t>(__ec.value());
619 class _LIBCPP_VISIBLE system_error
620 : public runtime_error
624 system_error(error_code __ec, const string& __what_arg);
625 system_error(error_code __ec, const char* __what_arg);
626 system_error(error_code __ec);
627 system_error(int __ev, const error_category& __ecat, const string& __what_arg);
628 system_error(int __ev, const error_category& __ecat, const char* __what_arg);
629 system_error(int __ev, const error_category& __ecat);
630 ~system_error() _NOEXCEPT;
632 _LIBCPP_ALWAYS_INLINE
633 const error_code& code() const _NOEXCEPT {return __ec_;}
636 static string __init(const error_code&, string);
639 void __throw_system_error(int ev, const char* what_arg);
641 _LIBCPP_END_NAMESPACE_STD
643 #endif // _LIBCPP_SYSTEM_ERROR