Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 1 | //===---------------------- system_error.cpp ------------------------------===// |
| 2 | // |
Howard Hinnant | f5256e1 | 2010-05-11 21:36:01 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 4 | // |
Howard Hinnant | b64f8b0 | 2010-11-16 22:09:02 +0000 | [diff] [blame] | 5 | // This file is dual licensed under the MIT and the University of Illinois Open |
| 6 | // Source Licenses. See LICENSE.TXT for details. |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "system_error" |
| 11 | #include "string" |
| 12 | #include "cstring" |
| 13 | |
| 14 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 15 | |
| 16 | // class error_category |
| 17 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 18 | error_category::error_category() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 19 | { |
| 20 | } |
| 21 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 22 | error_category::~error_category() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 23 | { |
| 24 | } |
| 25 | |
| 26 | error_condition |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 27 | error_category::default_error_condition(int ev) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | { |
| 29 | return error_condition(ev, *this); |
| 30 | } |
| 31 | |
| 32 | bool |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 33 | error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 34 | { |
| 35 | return default_error_condition(code) == condition; |
| 36 | } |
| 37 | |
| 38 | bool |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 39 | error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 40 | { |
| 41 | return *this == code.category() && code.value() == condition; |
| 42 | } |
| 43 | |
| 44 | string |
| 45 | __do_message::message(int ev) const |
| 46 | { |
| 47 | return string(strerror(ev)); |
| 48 | } |
| 49 | |
| 50 | class _LIBCPP_HIDDEN __generic_error_category |
| 51 | : public __do_message |
| 52 | { |
| 53 | public: |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 54 | virtual const char* name() const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 55 | virtual string message(int ev) const; |
| 56 | }; |
| 57 | |
| 58 | const char* |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 59 | __generic_error_category::name() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | { |
| 61 | return "generic"; |
| 62 | } |
| 63 | |
| 64 | string |
| 65 | __generic_error_category::message(int ev) const |
| 66 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 67 | #ifdef ELAST |
| 68 | if (ev > ELAST) |
| 69 | return string("unspecified generic_category error"); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 70 | #endif // ELAST |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 71 | return __do_message::message(ev); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | const error_category& |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 75 | generic_category() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 76 | { |
| 77 | static __generic_error_category s; |
| 78 | return s; |
| 79 | } |
| 80 | |
| 81 | class _LIBCPP_HIDDEN __system_error_category |
| 82 | : public __do_message |
| 83 | { |
| 84 | public: |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 85 | virtual const char* name() const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 86 | virtual string message(int ev) const; |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 87 | virtual error_condition default_error_condition(int ev) const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 88 | }; |
| 89 | |
| 90 | const char* |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 91 | __system_error_category::name() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 92 | { |
| 93 | return "system"; |
| 94 | } |
| 95 | |
| 96 | string |
| 97 | __system_error_category::message(int ev) const |
| 98 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 99 | #ifdef ELAST |
| 100 | if (ev > ELAST) |
| 101 | return string("unspecified system_category error"); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 102 | #endif // ELAST |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 103 | return __do_message::message(ev); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | error_condition |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 107 | __system_error_category::default_error_condition(int ev) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 108 | { |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 109 | #ifdef ELAST |
| 110 | if (ev > ELAST) |
| 111 | return error_condition(ev, system_category()); |
Howard Hinnant | 16e6e1d | 2010-08-22 00:03:27 +0000 | [diff] [blame] | 112 | #endif // ELAST |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 113 | return error_condition(ev, generic_category()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | const error_category& |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 117 | system_category() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 118 | { |
| 119 | static __system_error_category s; |
| 120 | return s; |
| 121 | } |
| 122 | |
| 123 | // error_condition |
| 124 | |
| 125 | string |
| 126 | error_condition::message() const |
| 127 | { |
| 128 | return __cat_->message(__val_); |
| 129 | } |
| 130 | |
| 131 | // error_code |
| 132 | |
| 133 | string |
| 134 | error_code::message() const |
| 135 | { |
| 136 | return __cat_->message(__val_); |
| 137 | } |
| 138 | |
| 139 | // system_error |
| 140 | |
| 141 | string |
| 142 | system_error::__init(const error_code& ec, string what_arg) |
| 143 | { |
| 144 | if (ec) |
| 145 | { |
| 146 | if (!what_arg.empty()) |
| 147 | what_arg += ": "; |
| 148 | what_arg += ec.message(); |
| 149 | } |
| 150 | return _STD::move(what_arg); |
| 151 | } |
| 152 | |
| 153 | system_error::system_error(error_code ec, const string& what_arg) |
| 154 | : runtime_error(__init(ec, what_arg)), |
| 155 | __ec_(ec) |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | system_error::system_error(error_code ec, const char* what_arg) |
| 160 | : runtime_error(__init(ec, what_arg)), |
| 161 | __ec_(ec) |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | system_error::system_error(error_code ec) |
| 166 | : runtime_error(__init(ec, "")), |
| 167 | __ec_(ec) |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | system_error::system_error(int ev, const error_category& ecat, const string& what_arg) |
| 172 | : runtime_error(__init(error_code(ev, ecat), what_arg)), |
| 173 | __ec_(error_code(ev, ecat)) |
| 174 | { |
| 175 | } |
| 176 | |
| 177 | system_error::system_error(int ev, const error_category& ecat, const char* what_arg) |
| 178 | : runtime_error(__init(error_code(ev, ecat), what_arg)), |
| 179 | __ec_(error_code(ev, ecat)) |
| 180 | { |
| 181 | } |
| 182 | |
| 183 | system_error::system_error(int ev, const error_category& ecat) |
| 184 | : runtime_error(__init(error_code(ev, ecat), "")), |
| 185 | __ec_(error_code(ev, ecat)) |
| 186 | { |
| 187 | } |
| 188 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame^] | 189 | system_error::~system_error() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 190 | { |
| 191 | } |
| 192 | |
| 193 | void |
| 194 | __throw_system_error(int ev, const char* what_arg) |
| 195 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 196 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 197 | throw system_error(error_code(ev, system_category()), what_arg); |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 198 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | _LIBCPP_END_NAMESPACE_STD |