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