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 | |
Jonathan Roelofs | b942093 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 10 | #include "__config" |
Dan Albert | 656850f | 2015-01-06 17:34:51 +0000 | [diff] [blame] | 11 | |
| 12 | #define _LIBCPP_BUILDING_SYSTEM_ERROR |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 13 | #include "system_error" |
Dan Albert | 656850f | 2015-01-06 17:34:51 +0000 | [diff] [blame] | 14 | |
| 15 | #include "config_elast.h" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 16 | #include "cstring" |
Dan Albert | 656850f | 2015-01-06 17:34:51 +0000 | [diff] [blame] | 17 | #include "string" |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 18 | |
| 19 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 20 | |
| 21 | // class error_category |
| 22 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 23 | error_category::error_category() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 24 | { |
| 25 | } |
| 26 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 27 | error_category::~error_category() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 28 | { |
| 29 | } |
| 30 | |
| 31 | error_condition |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 32 | error_category::default_error_condition(int ev) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 33 | { |
| 34 | return error_condition(ev, *this); |
| 35 | } |
| 36 | |
| 37 | bool |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 38 | error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 39 | { |
| 40 | return default_error_condition(code) == condition; |
| 41 | } |
| 42 | |
| 43 | bool |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 44 | error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 45 | { |
| 46 | return *this == code.category() && code.value() == condition; |
| 47 | } |
| 48 | |
| 49 | string |
| 50 | __do_message::message(int ev) const |
| 51 | { |
| 52 | return string(strerror(ev)); |
| 53 | } |
| 54 | |
| 55 | class _LIBCPP_HIDDEN __generic_error_category |
| 56 | : public __do_message |
| 57 | { |
| 58 | public: |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 59 | virtual const char* name() const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 60 | virtual string message(int ev) const; |
| 61 | }; |
| 62 | |
| 63 | const char* |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 64 | __generic_error_category::name() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 65 | { |
| 66 | return "generic"; |
| 67 | } |
| 68 | |
| 69 | string |
| 70 | __generic_error_category::message(int ev) const |
| 71 | { |
Jonathan Roelofs | b942093 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 72 | #ifdef _LIBCPP_ELAST |
| 73 | if (ev > _LIBCPP_ELAST) |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 74 | return string("unspecified generic_category error"); |
Jonathan Roelofs | b942093 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 75 | #endif // _LIBCPP_ELAST |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 76 | return __do_message::message(ev); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | const error_category& |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 80 | generic_category() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 81 | { |
| 82 | static __generic_error_category s; |
| 83 | return s; |
| 84 | } |
| 85 | |
| 86 | class _LIBCPP_HIDDEN __system_error_category |
| 87 | : public __do_message |
| 88 | { |
| 89 | public: |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 90 | virtual const char* name() const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 91 | virtual string message(int ev) const; |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 92 | virtual error_condition default_error_condition(int ev) const _NOEXCEPT; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 93 | }; |
| 94 | |
| 95 | const char* |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 96 | __system_error_category::name() const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 97 | { |
| 98 | return "system"; |
| 99 | } |
| 100 | |
| 101 | string |
| 102 | __system_error_category::message(int ev) const |
| 103 | { |
Jonathan Roelofs | b942093 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 104 | #ifdef _LIBCPP_ELAST |
| 105 | if (ev > _LIBCPP_ELAST) |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 106 | return string("unspecified system_category error"); |
Jonathan Roelofs | b942093 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 107 | #endif // _LIBCPP_ELAST |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 108 | return __do_message::message(ev); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | error_condition |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 112 | __system_error_category::default_error_condition(int ev) const _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 113 | { |
Jonathan Roelofs | b942093 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 114 | #ifdef _LIBCPP_ELAST |
| 115 | if (ev > _LIBCPP_ELAST) |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 116 | return error_condition(ev, system_category()); |
Jonathan Roelofs | b942093 | 2014-09-02 20:34:23 +0000 | [diff] [blame] | 117 | #endif // _LIBCPP_ELAST |
Howard Hinnant | adff489 | 2010-05-24 17:49:41 +0000 | [diff] [blame] | 118 | return error_condition(ev, generic_category()); |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | const error_category& |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 122 | system_category() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 123 | { |
| 124 | static __system_error_category s; |
| 125 | return s; |
| 126 | } |
| 127 | |
| 128 | // error_condition |
| 129 | |
| 130 | string |
| 131 | error_condition::message() const |
| 132 | { |
| 133 | return __cat_->message(__val_); |
| 134 | } |
| 135 | |
| 136 | // error_code |
| 137 | |
| 138 | string |
| 139 | error_code::message() const |
| 140 | { |
| 141 | return __cat_->message(__val_); |
| 142 | } |
| 143 | |
| 144 | // system_error |
| 145 | |
| 146 | string |
| 147 | system_error::__init(const error_code& ec, string what_arg) |
| 148 | { |
| 149 | if (ec) |
| 150 | { |
| 151 | if (!what_arg.empty()) |
| 152 | what_arg += ": "; |
| 153 | what_arg += ec.message(); |
| 154 | } |
Richard Trieu | 14dbb25 | 2015-04-30 21:47:28 +0000 | [diff] [blame] | 155 | return what_arg; |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | system_error::system_error(error_code ec, const string& what_arg) |
| 159 | : runtime_error(__init(ec, what_arg)), |
| 160 | __ec_(ec) |
| 161 | { |
| 162 | } |
| 163 | |
| 164 | system_error::system_error(error_code ec, const char* what_arg) |
| 165 | : runtime_error(__init(ec, what_arg)), |
| 166 | __ec_(ec) |
| 167 | { |
| 168 | } |
| 169 | |
| 170 | system_error::system_error(error_code ec) |
| 171 | : runtime_error(__init(ec, "")), |
| 172 | __ec_(ec) |
| 173 | { |
| 174 | } |
| 175 | |
| 176 | system_error::system_error(int ev, const error_category& ecat, const string& what_arg) |
| 177 | : runtime_error(__init(error_code(ev, ecat), what_arg)), |
| 178 | __ec_(error_code(ev, ecat)) |
| 179 | { |
| 180 | } |
| 181 | |
| 182 | system_error::system_error(int ev, const error_category& ecat, const char* what_arg) |
| 183 | : runtime_error(__init(error_code(ev, ecat), what_arg)), |
| 184 | __ec_(error_code(ev, ecat)) |
| 185 | { |
| 186 | } |
| 187 | |
| 188 | system_error::system_error(int ev, const error_category& ecat) |
| 189 | : runtime_error(__init(error_code(ev, ecat), "")), |
| 190 | __ec_(error_code(ev, ecat)) |
| 191 | { |
| 192 | } |
| 193 | |
Howard Hinnant | 1e15fd1 | 2011-05-26 19:48:01 +0000 | [diff] [blame] | 194 | system_error::~system_error() _NOEXCEPT |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 195 | { |
| 196 | } |
| 197 | |
| 198 | void |
| 199 | __throw_system_error(int ev, const char* what_arg) |
| 200 | { |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 201 | #ifndef _LIBCPP_NO_EXCEPTIONS |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 202 | throw system_error(error_code(ev, system_category()), what_arg); |
Howard Hinnant | db4d478 | 2013-03-28 18:56:26 +0000 | [diff] [blame] | 203 | #else |
| 204 | (void)ev; |
| 205 | (void)what_arg; |
Howard Hinnant | d444470 | 2010-08-11 17:04:31 +0000 | [diff] [blame] | 206 | #endif |
Howard Hinnant | bc8d3f9 | 2010-05-11 19:42:16 +0000 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | _LIBCPP_END_NAMESPACE_STD |