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 "__config" 12 #include "system_error" 13 #include "string" 14 #include "cstring" 15 16 _LIBCPP_BEGIN_NAMESPACE_STD 17 18 // class error_category 19 20 error_category::error_category() _NOEXCEPT 21 { 22 } 23 24 error_category::~error_category() _NOEXCEPT 25 { 26 } 27 28 error_condition 29 error_category::default_error_condition(int ev) const _NOEXCEPT 30 { 31 return error_condition(ev, *this); 32 } 33 34 bool 35 error_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT 36 { 37 return default_error_condition(code) == condition; 38 } 39 40 bool 41 error_category::equivalent(const error_code& code, int condition) const _NOEXCEPT 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: 56 virtual const char* name() const _NOEXCEPT; 57 virtual string message(int ev) const; 58 }; 59 60 const char* 61 __generic_error_category::name() const _NOEXCEPT 62 { 63 return "generic"; 64 } 65 66 string 67 __generic_error_category::message(int ev) const 68 { 69 #ifdef _LIBCPP_ELAST 70 if (ev > _LIBCPP_ELAST) 71 return string("unspecified generic_category error"); 72 #endif // _LIBCPP_ELAST 73 return __do_message::message(ev); 74 } 75 76 const error_category& 77 generic_category() _NOEXCEPT 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: 87 virtual const char* name() const _NOEXCEPT; 88 virtual string message(int ev) const; 89 virtual error_condition default_error_condition(int ev) const _NOEXCEPT; 90 }; 91 92 const char* 93 __system_error_category::name() const _NOEXCEPT 94 { 95 return "system"; 96 } 97 98 string 99 __system_error_category::message(int ev) const 100 { 101 #ifdef _LIBCPP_ELAST 102 if (ev > _LIBCPP_ELAST) 103 return string("unspecified system_category error"); 104 #endif // _LIBCPP_ELAST 105 return __do_message::message(ev); 106 } 107 108 error_condition 109 __system_error_category::default_error_condition(int ev) const _NOEXCEPT 110 { 111 #ifdef _LIBCPP_ELAST 112 if (ev > _LIBCPP_ELAST) 113 return error_condition(ev, system_category()); 114 #endif // _LIBCPP_ELAST 115 return error_condition(ev, generic_category()); 116 } 117 118 const error_category& 119 system_category() _NOEXCEPT 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 } 152 return _VSTD::move(what_arg); 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 191 system_error::~system_error() _NOEXCEPT 192 { 193 } 194 195 void 196 __throw_system_error(int ev, const char* what_arg) 197 { 198 #ifndef _LIBCPP_NO_EXCEPTIONS 199 throw system_error(error_code(ev, system_category()), what_arg); 200 #else 201 (void)ev; 202 (void)what_arg; 203 #endif 204 } 205 206 _LIBCPP_END_NAMESPACE_STD 207