1// -*- C++ -*- 2//===---------------------------- system_error ----------------------------===// 3// 4// The LLVM Compiler Infrastructure 5// 6// This file is dual licensed under the MIT and the University of Illinois Open 7// Source Licenses. See LICENSE.TXT for details. 8// 9//===----------------------------------------------------------------------===// 10 11#ifndef _LIBCPP_SYSTEM_ERROR 12#define _LIBCPP_SYSTEM_ERROR 13 14/* 15 system_error synopsis 16 17namespace std 18{ 19 20class error_category 21{ 22public: 23 virtual ~error_category() noexcept; 24 25 error_category(const error_category&) = delete; 26 error_category& operator=(const error_category&) = delete; 27 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; 33 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; 37}; 38 39const error_category& generic_category() noexcept; 40const error_category& system_category() noexcept; 41 42template <class T> struct is_error_code_enum 43 : public false_type {}; 44 45template <class T> struct is_error_condition_enum 46 : public false_type {}; 47 48class error_code 49{ 50public: 51 // constructors: 52 error_code() noexcept; 53 error_code(int val, const error_category& cat) noexcept; 54 template <class ErrorCodeEnum> 55 error_code(ErrorCodeEnum e) noexcept; 56 57 // modifiers: 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; 62 63 // observers: 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; 69}; 70 71// non-member functions: 72bool operator<(const error_code& lhs, const error_code& rhs) noexcept; 73template <class charT, class traits> 74 basic_ostream<charT,traits>& 75 operator<<(basic_ostream<charT,traits>& os, const error_code& ec); 76 77class error_condition 78{ 79public: 80 // constructors: 81 error_condition() noexcept; 82 error_condition(int val, const error_category& cat) noexcept; 83 template <class ErrorConditionEnum> 84 error_condition(ErrorConditionEnum e) noexcept; 85 86 // modifiers: 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; 91 92 // observers: 93 int value() const noexcept; 94 const error_category& category() const noexcept; 95 string message() const noexcept; 96 explicit operator bool() const noexcept; 97}; 98 99bool operator<(const error_condition& lhs, const error_condition& rhs) noexcept; 100 101class system_error 102 : public runtime_error 103{ 104public: 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); 111 112 const error_code& code() const noexcept; 113 const char* what() const noexcept; 114}; 115 116enum class errc 117{ 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 148 io_error, // EIO 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 156 no_link, // ENOLINK 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 196}; 197 198template <> struct is_error_condition_enum<errc> 199 : true_type { } 200 201error_code make_error_code(errc e) noexcept; 202error_condition make_error_condition(errc e) noexcept; 203 204// Comparison operators: 205bool operator==(const error_code& lhs, const error_code& rhs) noexcept; 206bool operator==(const error_code& lhs, const error_condition& rhs) noexcept; 207bool operator==(const error_condition& lhs, const error_code& rhs) noexcept; 208bool operator==(const error_condition& lhs, const error_condition& rhs) noexcept; 209bool operator!=(const error_code& lhs, const error_code& rhs) noexcept; 210bool operator!=(const error_code& lhs, const error_condition& rhs) noexcept; 211bool operator!=(const error_condition& lhs, const error_code& rhs) noexcept; 212bool operator!=(const error_condition& lhs, const error_condition& rhs) noexcept; 213 214template <> struct hash<std::error_code>; 215 216} // std 217 218*/ 219 220#include <__config> 221#include <cerrno> 222#include <type_traits> 223#include <stdexcept> 224#include <__functional_base> 225 226#pragma GCC system_header 227 228_LIBCPP_BEGIN_NAMESPACE_STD 229 230// is_error_code_enum 231 232template <class _Tp> 233struct _LIBCPP_VISIBLE is_error_code_enum 234 : public false_type {}; 235 236// is_error_condition_enum 237 238template <class _Tp> 239struct _LIBCPP_VISIBLE is_error_condition_enum 240 : public false_type {}; 241 242// Some error codes are not present on all platforms, so we provide equivalents 243// for them: 244 245//enum class errc 246struct errc 247{ 248enum _ { 249 address_family_not_supported = EAFNOSUPPORT, 250 address_in_use = EADDRINUSE, 251 address_not_available = EADDRNOTAVAIL, 252 already_connected = EISCONN, 253 argument_list_too_long = E2BIG, 254 argument_out_of_domain = EDOM, 255 bad_address = EFAULT, 256 bad_file_descriptor = EBADF, 257 bad_message = EBADMSG, 258 broken_pipe = EPIPE, 259 connection_aborted = ECONNABORTED, 260 connection_already_in_progress = EALREADY, 261 connection_refused = ECONNREFUSED, 262 connection_reset = ECONNRESET, 263 cross_device_link = EXDEV, 264 destination_address_required = EDESTADDRREQ, 265 device_or_resource_busy = EBUSY, 266 directory_not_empty = ENOTEMPTY, 267 executable_format_error = ENOEXEC, 268 file_exists = EEXIST, 269 file_too_large = EFBIG, 270 filename_too_long = ENAMETOOLONG, 271 function_not_supported = ENOSYS, 272 host_unreachable = EHOSTUNREACH, 273 identifier_removed = EIDRM, 274 illegal_byte_sequence = EILSEQ, 275 inappropriate_io_control_operation = ENOTTY, 276 interrupted = EINTR, 277 invalid_argument = EINVAL, 278 invalid_seek = ESPIPE, 279 io_error = EIO, 280 is_a_directory = EISDIR, 281 message_size = EMSGSIZE, 282 network_down = ENETDOWN, 283 network_reset = ENETRESET, 284 network_unreachable = ENETUNREACH, 285 no_buffer_space = ENOBUFS, 286 no_child_process = ECHILD, 287 no_link = ENOLINK, 288 no_lock_available = ENOLCK, 289#ifdef ENODATA 290 no_message_available = ENODATA, 291#else 292 no_message_available = ENOMSG, 293#endif 294 no_message = ENOMSG, 295 no_protocol_option = ENOPROTOOPT, 296 no_space_on_device = ENOSPC, 297#ifdef ENOSR 298 no_stream_resources = ENOSR, 299#else 300 no_stream_resources = ENOMEM, 301#endif 302 no_such_device_or_address = ENXIO, 303 no_such_device = ENODEV, 304 no_such_file_or_directory = ENOENT, 305 no_such_process = ESRCH, 306 not_a_directory = ENOTDIR, 307 not_a_socket = ENOTSOCK, 308#ifdef ENOSTR 309 not_a_stream = ENOSTR, 310#else 311 not_a_stream = EINVAL, 312#endif 313 not_connected = ENOTCONN, 314 not_enough_memory = ENOMEM, 315 not_supported = ENOTSUP, 316 operation_canceled = ECANCELED, 317 operation_in_progress = EINPROGRESS, 318 operation_not_permitted = EPERM, 319 operation_not_supported = EOPNOTSUPP, 320 operation_would_block = EWOULDBLOCK, 321 owner_dead = EOWNERDEAD, 322 permission_denied = EACCES, 323 protocol_error = EPROTO, 324 protocol_not_supported = EPROTONOSUPPORT, 325 read_only_file_system = EROFS, 326 resource_deadlock_would_occur = EDEADLK, 327 resource_unavailable_try_again = EAGAIN, 328 result_out_of_range = ERANGE, 329 state_not_recoverable = ENOTRECOVERABLE, 330#ifdef ETIME 331 stream_timeout = ETIME, 332#else 333 stream_timeout = ETIMEDOUT, 334#endif 335 text_file_busy = ETXTBSY, 336 timed_out = ETIMEDOUT, 337 too_many_files_open_in_system = ENFILE, 338 too_many_files_open = EMFILE, 339 too_many_links = EMLINK, 340 too_many_symbolic_link_levels = ELOOP, 341 value_too_large = EOVERFLOW, 342 wrong_protocol_type = EPROTOTYPE 343}; 344 345 _ __v_; 346 347 _LIBCPP_ALWAYS_INLINE 348 errc(_ __v) : __v_(__v) {} 349 _LIBCPP_ALWAYS_INLINE 350 operator int() const {return __v_;} 351 352}; 353 354template <> 355struct _LIBCPP_VISIBLE is_error_condition_enum<errc> 356 : true_type { }; 357 358template <> 359struct _LIBCPP_VISIBLE is_error_condition_enum<errc::_> 360 : true_type { }; 361 362class error_condition; 363class error_code; 364 365// class error_category 366 367class __do_message; 368 369class _LIBCPP_VISIBLE error_category 370{ 371public: 372 virtual ~error_category() _NOEXCEPT; 373 374private: 375 error_category() _NOEXCEPT; 376 error_category(const error_category&);// = delete; 377 error_category& operator=(const error_category&);// = delete; 378 379public: 380 virtual const char* name() const _NOEXCEPT = 0; 381 virtual error_condition default_error_condition(int __ev) const _NOEXCEPT; 382 virtual bool equivalent(int __code, const error_condition& __condition) const _NOEXCEPT; 383 virtual bool equivalent(const error_code& __code, int __condition) const _NOEXCEPT; 384 virtual string message(int __ev) const = 0; 385 386 _LIBCPP_ALWAYS_INLINE 387 bool operator==(const error_category& __rhs) const _NOEXCEPT {return this == &__rhs;} 388 389 _LIBCPP_ALWAYS_INLINE 390 bool operator!=(const error_category& __rhs) const _NOEXCEPT {return !(*this == __rhs);} 391 392 _LIBCPP_ALWAYS_INLINE 393 bool operator< (const error_category& __rhs) const _NOEXCEPT {return this < &__rhs;} 394 395 friend class __do_message; 396}; 397 398class _LIBCPP_HIDDEN __do_message 399 : public error_category 400{ 401public: 402 virtual string message(int ev) const; 403}; 404 405const error_category& generic_category() _NOEXCEPT; 406const error_category& system_category() _NOEXCEPT; 407 408class _LIBCPP_VISIBLE error_condition 409{ 410 int __val_; 411 const error_category* __cat_; 412public: 413 _LIBCPP_ALWAYS_INLINE 414 error_condition() _NOEXCEPT : __val_(0), __cat_(&generic_category()) {} 415 416 _LIBCPP_ALWAYS_INLINE 417 error_condition(int __val, const error_category& __cat) _NOEXCEPT 418 : __val_(__val), __cat_(&__cat) {} 419 420 template <class _E> 421 _LIBCPP_ALWAYS_INLINE 422 error_condition(_E __e, 423 typename enable_if<is_error_condition_enum<_E>::value>::type* = 0 424 ) _NOEXCEPT 425 {*this = make_error_condition(__e);} 426 427 _LIBCPP_ALWAYS_INLINE 428 void assign(int __val, const error_category& __cat) _NOEXCEPT 429 { 430 __val_ = __val; 431 __cat_ = &__cat; 432 } 433 434 template <class _E> 435 _LIBCPP_ALWAYS_INLINE 436 typename enable_if 437 < 438 is_error_condition_enum<_E>::value, 439 error_condition& 440 >::type 441 operator=(_E __e) _NOEXCEPT 442 {*this = make_error_condition(__e); return *this;} 443 444 _LIBCPP_ALWAYS_INLINE 445 void clear() _NOEXCEPT 446 { 447 __val_ = 0; 448 __cat_ = &generic_category(); 449 } 450 451 _LIBCPP_ALWAYS_INLINE 452 int value() const _NOEXCEPT {return __val_;} 453 454 _LIBCPP_ALWAYS_INLINE 455 const error_category& category() const _NOEXCEPT {return *__cat_;} 456 string message() const; 457 458 _LIBCPP_ALWAYS_INLINE 459 //explicit 460 operator bool() const _NOEXCEPT {return __val_ != 0;} 461}; 462 463inline _LIBCPP_INLINE_VISIBILITY 464error_condition 465make_error_condition(errc __e) _NOEXCEPT 466{ 467 return error_condition(static_cast<int>(__e), generic_category()); 468} 469 470inline _LIBCPP_INLINE_VISIBILITY 471bool 472operator<(const error_condition& __x, const error_condition& __y) _NOEXCEPT 473{ 474 return __x.category() < __y.category() 475 || __x.category() == __y.category() && __x.value() < __y.value(); 476} 477 478// error_code 479 480class _LIBCPP_VISIBLE error_code 481{ 482 int __val_; 483 const error_category* __cat_; 484public: 485 _LIBCPP_ALWAYS_INLINE 486 error_code() _NOEXCEPT : __val_(0), __cat_(&system_category()) {} 487 488 _LIBCPP_ALWAYS_INLINE 489 error_code(int __val, const error_category& __cat) _NOEXCEPT 490 : __val_(__val), __cat_(&__cat) {} 491 492 template <class _E> 493 _LIBCPP_ALWAYS_INLINE 494 error_code(_E __e, 495 typename enable_if<is_error_code_enum<_E>::value>::type* = 0 496 ) _NOEXCEPT 497 {*this = make_error_code(__e);} 498 499 _LIBCPP_ALWAYS_INLINE 500 void assign(int __val, const error_category& __cat) _NOEXCEPT 501 { 502 __val_ = __val; 503 __cat_ = &__cat; 504 } 505 506 template <class _E> 507 _LIBCPP_ALWAYS_INLINE 508 typename enable_if 509 < 510 is_error_code_enum<_E>::value, 511 error_code& 512 >::type 513 operator=(_E __e) _NOEXCEPT 514 {*this = make_error_code(__e); return *this;} 515 516 _LIBCPP_ALWAYS_INLINE 517 void clear() _NOEXCEPT 518 { 519 __val_ = 0; 520 __cat_ = &system_category(); 521 } 522 523 _LIBCPP_ALWAYS_INLINE 524 int value() const _NOEXCEPT {return __val_;} 525 526 _LIBCPP_ALWAYS_INLINE 527 const error_category& category() const _NOEXCEPT {return *__cat_;} 528 529 _LIBCPP_ALWAYS_INLINE 530 error_condition default_error_condition() const _NOEXCEPT 531 {return __cat_->default_error_condition(__val_);} 532 533 string message() const; 534 535 _LIBCPP_ALWAYS_INLINE 536 //explicit 537 operator bool() const _NOEXCEPT {return __val_ != 0;} 538}; 539 540inline _LIBCPP_INLINE_VISIBILITY 541error_code 542make_error_code(errc __e) _NOEXCEPT 543{ 544 return error_code(static_cast<int>(__e), generic_category()); 545} 546 547inline _LIBCPP_INLINE_VISIBILITY 548bool 549operator<(const error_code& __x, const error_code& __y) _NOEXCEPT 550{ 551 return __x.category() < __y.category() 552 || __x.category() == __y.category() && __x.value() < __y.value(); 553} 554 555inline _LIBCPP_INLINE_VISIBILITY 556bool 557operator==(const error_code& __x, const error_code& __y) _NOEXCEPT 558{ 559 return __x.category() == __y.category() && __x.value() == __y.value(); 560} 561 562inline _LIBCPP_INLINE_VISIBILITY 563bool 564operator==(const error_code& __x, const error_condition& __y) _NOEXCEPT 565{ 566 return __x.category().equivalent(__x.value(), __y) 567 || __y.category().equivalent(__x, __y.value()); 568} 569 570inline _LIBCPP_INLINE_VISIBILITY 571bool 572operator==(const error_condition& __x, const error_code& __y) _NOEXCEPT 573{ 574 return __y == __x; 575} 576 577inline _LIBCPP_INLINE_VISIBILITY 578bool 579operator==(const error_condition& __x, const error_condition& __y) _NOEXCEPT 580{ 581 return __x.category() == __y.category() && __x.value() == __y.value(); 582} 583 584inline _LIBCPP_INLINE_VISIBILITY 585bool 586operator!=(const error_code& __x, const error_code& __y) _NOEXCEPT 587{return !(__x == __y);} 588 589inline _LIBCPP_INLINE_VISIBILITY 590bool 591operator!=(const error_code& __x, const error_condition& __y) _NOEXCEPT 592{return !(__x == __y);} 593 594inline _LIBCPP_INLINE_VISIBILITY 595bool 596operator!=(const error_condition& __x, const error_code& __y) _NOEXCEPT 597{return !(__x == __y);} 598 599inline _LIBCPP_INLINE_VISIBILITY 600bool 601operator!=(const error_condition& __x, const error_condition& __y) _NOEXCEPT 602{return !(__x == __y);} 603 604template <> 605struct _LIBCPP_VISIBLE hash<error_code> 606 : public unary_function<error_code, size_t> 607{ 608 _LIBCPP_INLINE_VISIBILITY 609 size_t operator()(const error_code& __ec) const _NOEXCEPT 610 { 611 return static_cast<size_t>(__ec.value()); 612 } 613}; 614 615// system_error 616 617class _LIBCPP_VISIBLE system_error 618 : public runtime_error 619{ 620 error_code __ec_; 621public: 622 system_error(error_code __ec, const string& __what_arg); 623 system_error(error_code __ec, const char* __what_arg); 624 system_error(error_code __ec); 625 system_error(int __ev, const error_category& __ecat, const string& __what_arg); 626 system_error(int __ev, const error_category& __ecat, const char* __what_arg); 627 system_error(int __ev, const error_category& __ecat); 628 ~system_error() _NOEXCEPT; 629 630 _LIBCPP_ALWAYS_INLINE 631 const error_code& code() const _NOEXCEPT {return __ec_;} 632 633private: 634 static string __init(const error_code&, string); 635}; 636 637void __throw_system_error(int ev, const char* what_arg); 638 639_LIBCPP_END_NAMESPACE_STD 640 641#endif // _LIBCPP_SYSTEM_ERROR 642