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