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(); 24 25 error_category(const error_category&) = delete; 26 error_category& operator=(const error_category&) = delete; 27 28 virtual const char* name() const = 0; 29 virtual error_condition default_error_condition(int ev) const; 30 virtual bool equivalent(int code, const error_condition& condition) const; 31 virtual bool equivalent(const error_code& code, int condition) const; 32 virtual string message(int ev) const = 0; 33 34 bool operator==(const error_category& rhs) const; 35 bool operator!=(const error_category& rhs) const; 36 bool operator<(const error_category& rhs) const; 37}; 38 39const error_category& generic_category(); 40const error_category& system_category(); 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(); 53 error_code(int val, const error_category& cat); 54 template <class ErrorCodeEnum> 55 error_code(ErrorCodeEnum e); 56 57 // modifiers: 58 void assign(int val, const error_category& cat); 59 template <class ErrorCodeEnum> 60 error_code& operator=(ErrorCodeEnum e); 61 void clear(); 62 63 // observers: 64 int value() const; 65 const error_category& category() const; 66 error_condition default_error_condition() const; 67 string message() const; 68 explicit operator bool() const; 69}; 70 71// non-member functions: 72bool operator<(const error_code& lhs, const error_code& rhs); 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(); 82 error_condition(int val, const error_category& cat); 83 template <class ErrorConditionEnum> 84 error_condition(ErrorConditionEnum e); 85 86 // modifiers: 87 void assign(int val, const error_category& cat); 88 template <class ErrorConditionEnum> 89 error_condition& operator=(ErrorConditionEnum e); 90 void clear(); 91 92 // observers: 93 int value() const; 94 const error_category& category() const; 95 string message() const; 96 explicit operator bool() const; 97}; 98 99bool operator<(const error_condition& lhs, const error_condition& rhs); 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 throw(); 113 const char* what() const throw(); 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); 202error_condition make_error_condition(errc e); 203 204// Comparison operators: 205bool operator==(const error_code& lhs, const error_code& rhs); 206bool operator==(const error_code& lhs, const error_condition& rhs); 207bool operator==(const error_condition& lhs, const error_code& rhs); 208bool operator==(const error_condition& lhs, const error_condition& rhs); 209bool operator!=(const error_code& lhs, const error_code& rhs); 210bool operator!=(const error_code& lhs, const error_condition& rhs); 211bool operator!=(const error_condition& lhs, const error_code& rhs); 212bool operator!=(const error_condition& lhs, const error_condition& rhs); 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(); 373 374private: 375 error_category(); 376 error_category(const error_category&);// = delete; 377 error_category& operator=(const error_category&);// = delete; 378 379public: 380 virtual const char* name() const = 0; 381 virtual error_condition default_error_condition(int __ev) const; 382 virtual bool equivalent(int __code, const error_condition& __condition) const; 383 virtual bool equivalent(const error_code& __code, int __condition) const; 384 virtual string message(int __ev) const = 0; 385 386 _LIBCPP_ALWAYS_INLINE 387 bool operator==(const error_category& __rhs) const {return this == &__rhs;} 388 389 _LIBCPP_ALWAYS_INLINE 390 bool operator!=(const error_category& __rhs) const {return !(*this == __rhs);} 391 392 _LIBCPP_ALWAYS_INLINE 393 bool operator< (const error_category& __rhs) const {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(); 406const error_category& system_category(); 407 408class _LIBCPP_VISIBLE error_condition 409{ 410 int __val_; 411 const error_category* __cat_; 412public: 413 _LIBCPP_ALWAYS_INLINE 414 error_condition() : __val_(0), __cat_(&generic_category()) {} 415 416 _LIBCPP_ALWAYS_INLINE 417 error_condition(int __val, const error_category& __cat) 418 : __val_(__val), __cat_(&__cat) {} 419 420 template <class _E> 421 _LIBCPP_ALWAYS_INLINE 422 error_condition(_E __e, typename enable_if<is_error_condition_enum<_E>::value>::type* = 0) 423 {*this = make_error_condition(__e);} 424 425 _LIBCPP_ALWAYS_INLINE 426 void assign(int __val, const error_category& __cat) 427 { 428 __val_ = __val; 429 __cat_ = &__cat; 430 } 431 432 template <class _E> 433 _LIBCPP_ALWAYS_INLINE 434 typename enable_if 435 < 436 is_error_condition_enum<_E>::value, 437 error_condition& 438 >::type 439 operator=(_E __e) 440 {*this = make_error_condition(__e); return *this;} 441 442 _LIBCPP_ALWAYS_INLINE 443 void clear() 444 { 445 __val_ = 0; 446 __cat_ = &generic_category(); 447 } 448 449 _LIBCPP_ALWAYS_INLINE 450 int value() const {return __val_;} 451 452 _LIBCPP_ALWAYS_INLINE 453 const error_category& category() const {return *__cat_;} 454 string message() const; 455 456 _LIBCPP_ALWAYS_INLINE 457 //explicit 458 operator bool() const {return __val_ != 0;} 459}; 460 461inline _LIBCPP_INLINE_VISIBILITY 462error_condition 463make_error_condition(errc __e) 464{ 465 return error_condition(static_cast<int>(__e), generic_category()); 466} 467 468inline _LIBCPP_INLINE_VISIBILITY 469bool 470operator<(const error_condition& __x, const error_condition& __y) 471{ 472 return __x.category() < __y.category() 473 || __x.category() == __y.category() && __x.value() < __y.value(); 474} 475 476// error_code 477 478class _LIBCPP_VISIBLE error_code 479{ 480 int __val_; 481 const error_category* __cat_; 482public: 483 _LIBCPP_ALWAYS_INLINE 484 error_code() : __val_(0), __cat_(&system_category()) {} 485 486 _LIBCPP_ALWAYS_INLINE 487 error_code(int __val, const error_category& __cat) 488 : __val_(__val), __cat_(&__cat) {} 489 490 template <class _E> 491 _LIBCPP_ALWAYS_INLINE 492 error_code(_E __e, typename enable_if<is_error_code_enum<_E>::value>::type* = 0) 493 {*this = make_error_code(__e);} 494 495 _LIBCPP_ALWAYS_INLINE 496 void assign(int __val, const error_category& __cat) 497 { 498 __val_ = __val; 499 __cat_ = &__cat; 500 } 501 502 template <class _E> 503 _LIBCPP_ALWAYS_INLINE 504 typename enable_if 505 < 506 is_error_code_enum<_E>::value, 507 error_code& 508 >::type 509 operator=(_E __e) 510 {*this = make_error_code(__e); return *this;} 511 512 _LIBCPP_ALWAYS_INLINE 513 void clear() 514 { 515 __val_ = 0; 516 __cat_ = &system_category(); 517 } 518 519 _LIBCPP_ALWAYS_INLINE 520 int value() const {return __val_;} 521 522 _LIBCPP_ALWAYS_INLINE 523 const error_category& category() const {return *__cat_;} 524 525 _LIBCPP_ALWAYS_INLINE 526 error_condition default_error_condition() const 527 {return __cat_->default_error_condition(__val_);} 528 529 string message() const; 530 531 _LIBCPP_ALWAYS_INLINE 532 //explicit 533 operator bool() const {return __val_ != 0;} 534}; 535 536inline _LIBCPP_INLINE_VISIBILITY 537error_code 538make_error_code(errc __e) 539{ 540 return error_code(static_cast<int>(__e), generic_category()); 541} 542 543inline _LIBCPP_INLINE_VISIBILITY 544bool 545operator<(const error_code& __x, const error_code& __y) 546{ 547 return __x.category() < __y.category() 548 || __x.category() == __y.category() && __x.value() < __y.value(); 549} 550 551inline _LIBCPP_INLINE_VISIBILITY 552bool 553operator==(const error_code& __x, const error_code& __y) 554{ 555 return __x.category() == __y.category() && __x.value() == __y.value(); 556} 557 558inline _LIBCPP_INLINE_VISIBILITY 559bool 560operator==(const error_code& __x, const error_condition& __y) 561{ 562 return __x.category().equivalent(__x.value(), __y) 563 || __y.category().equivalent(__x, __y.value()); 564} 565 566inline _LIBCPP_INLINE_VISIBILITY 567bool 568operator==(const error_condition& __x, const error_code& __y) 569{ 570 return __y == __x; 571} 572 573inline _LIBCPP_INLINE_VISIBILITY 574bool 575operator==(const error_condition& __x, const error_condition& __y) 576{ 577 return __x.category() == __y.category() && __x.value() == __y.value(); 578} 579 580inline _LIBCPP_INLINE_VISIBILITY 581bool 582operator!=(const error_code& __x, const error_code& __y) {return !(__x == __y);} 583 584inline _LIBCPP_INLINE_VISIBILITY 585bool 586operator!=(const error_code& __x, const error_condition& __y) {return !(__x == __y);} 587 588inline _LIBCPP_INLINE_VISIBILITY 589bool 590operator!=(const error_condition& __x, const error_code& __y) {return !(__x == __y);} 591 592inline _LIBCPP_INLINE_VISIBILITY 593bool 594operator!=(const error_condition& __x, const error_condition& __y) {return !(__x == __y);} 595 596template <> 597struct _LIBCPP_VISIBLE hash<error_code> 598 : public unary_function<error_code, size_t> 599{ 600 _LIBCPP_INLINE_VISIBILITY 601 size_t operator()(const error_code& __ec) const 602 { 603 return static_cast<size_t>(__ec.value()); 604 } 605}; 606 607// system_error 608 609class _LIBCPP_VISIBLE system_error 610 : public runtime_error 611{ 612 error_code __ec_; 613public: 614 system_error(error_code __ec, const string& __what_arg); 615 system_error(error_code __ec, const char* __what_arg); 616 system_error(error_code __ec); 617 system_error(int __ev, const error_category& __ecat, const string& __what_arg); 618 system_error(int __ev, const error_category& __ecat, const char* __what_arg); 619 system_error(int __ev, const error_category& __ecat); 620 ~system_error() throw(); 621 622 _LIBCPP_ALWAYS_INLINE 623 const error_code& code() const throw() {return __ec_;} 624 625private: 626 static string __init(const error_code&, string); 627}; 628 629void __throw_system_error(int ev, const char* what_arg); 630 631_LIBCPP_END_NAMESPACE_STD 632 633#endif // _LIBCPP_SYSTEM_ERROR 634