Howard Hinnant | 3e51952 | 2010-05-11 19:42:16 +0000 | [diff] [blame^] | 1 | //===---------------------- system_error.cpp ------------------------------===// |
| 2 | // |
| 3 | // ÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊÊThe LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 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 | |
| 18 | error_category::error_category() |
| 19 | { |
| 20 | } |
| 21 | |
| 22 | error_category::~error_category() |
| 23 | { |
| 24 | } |
| 25 | |
| 26 | error_condition |
| 27 | error_category::default_error_condition(int ev) const |
| 28 | { |
| 29 | return error_condition(ev, *this); |
| 30 | } |
| 31 | |
| 32 | bool |
| 33 | error_category::equivalent(int code, const error_condition& condition) const |
| 34 | { |
| 35 | return default_error_condition(code) == condition; |
| 36 | } |
| 37 | |
| 38 | bool |
| 39 | error_category::equivalent(const error_code& code, int condition) const |
| 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: |
| 54 | virtual const char* name() const; |
| 55 | virtual string message(int ev) const; |
| 56 | }; |
| 57 | |
| 58 | const char* |
| 59 | __generic_error_category::name() const |
| 60 | { |
| 61 | return "generic"; |
| 62 | } |
| 63 | |
| 64 | string |
| 65 | __generic_error_category::message(int ev) const |
| 66 | { |
| 67 | if (ev <= ELAST) |
| 68 | return __do_message::message(ev); |
| 69 | return string("unspecified generic_category error"); |
| 70 | } |
| 71 | |
| 72 | const error_category& |
| 73 | generic_category() |
| 74 | { |
| 75 | static __generic_error_category s; |
| 76 | return s; |
| 77 | } |
| 78 | |
| 79 | class _LIBCPP_HIDDEN __system_error_category |
| 80 | : public __do_message |
| 81 | { |
| 82 | public: |
| 83 | virtual const char* name() const; |
| 84 | virtual string message(int ev) const; |
| 85 | virtual error_condition default_error_condition(int ev) const; |
| 86 | }; |
| 87 | |
| 88 | const char* |
| 89 | __system_error_category::name() const |
| 90 | { |
| 91 | return "system"; |
| 92 | } |
| 93 | |
| 94 | string |
| 95 | __system_error_category::message(int ev) const |
| 96 | { |
| 97 | if (ev <= ELAST) |
| 98 | return __do_message::message(ev); |
| 99 | return string("unspecified system_category error"); |
| 100 | } |
| 101 | |
| 102 | error_condition |
| 103 | __system_error_category::default_error_condition(int ev) const |
| 104 | { |
| 105 | if (ev <= ELAST) |
| 106 | return error_condition(ev, generic_category()); |
| 107 | return error_condition(ev, system_category()); |
| 108 | } |
| 109 | |
| 110 | const error_category& |
| 111 | system_category() |
| 112 | { |
| 113 | static __system_error_category s; |
| 114 | return s; |
| 115 | } |
| 116 | |
| 117 | // error_condition |
| 118 | |
| 119 | string |
| 120 | error_condition::message() const |
| 121 | { |
| 122 | return __cat_->message(__val_); |
| 123 | } |
| 124 | |
| 125 | // error_code |
| 126 | |
| 127 | string |
| 128 | error_code::message() const |
| 129 | { |
| 130 | return __cat_->message(__val_); |
| 131 | } |
| 132 | |
| 133 | // system_error |
| 134 | |
| 135 | string |
| 136 | system_error::__init(const error_code& ec, string what_arg) |
| 137 | { |
| 138 | if (ec) |
| 139 | { |
| 140 | if (!what_arg.empty()) |
| 141 | what_arg += ": "; |
| 142 | what_arg += ec.message(); |
| 143 | } |
| 144 | return _STD::move(what_arg); |
| 145 | } |
| 146 | |
| 147 | system_error::system_error(error_code ec, const string& what_arg) |
| 148 | : runtime_error(__init(ec, what_arg)), |
| 149 | __ec_(ec) |
| 150 | { |
| 151 | } |
| 152 | |
| 153 | system_error::system_error(error_code ec, const char* what_arg) |
| 154 | : runtime_error(__init(ec, what_arg)), |
| 155 | __ec_(ec) |
| 156 | { |
| 157 | } |
| 158 | |
| 159 | system_error::system_error(error_code ec) |
| 160 | : runtime_error(__init(ec, "")), |
| 161 | __ec_(ec) |
| 162 | { |
| 163 | } |
| 164 | |
| 165 | system_error::system_error(int ev, const error_category& ecat, const string& what_arg) |
| 166 | : runtime_error(__init(error_code(ev, ecat), what_arg)), |
| 167 | __ec_(error_code(ev, ecat)) |
| 168 | { |
| 169 | } |
| 170 | |
| 171 | system_error::system_error(int ev, const error_category& ecat, const char* 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) |
| 178 | : runtime_error(__init(error_code(ev, ecat), "")), |
| 179 | __ec_(error_code(ev, ecat)) |
| 180 | { |
| 181 | } |
| 182 | |
| 183 | system_error::~system_error() throw() |
| 184 | { |
| 185 | } |
| 186 | |
| 187 | void |
| 188 | __throw_system_error(int ev, const char* what_arg) |
| 189 | { |
| 190 | throw system_error(error_code(ev, system_category()), what_arg); |
| 191 | } |
| 192 | |
| 193 | _LIBCPP_END_NAMESPACE_STD |