system_error.cpp revision 278724
1281197Sandrew//===---------------------- system_error.cpp ------------------------------===// 2281197Sandrew// 3281197Sandrew// The LLVM Compiler Infrastructure 4281197Sandrew// 5281197Sandrew// This file is dual licensed under the MIT and the University of Illinois Open 6281197Sandrew// Source Licenses. See LICENSE.TXT for details. 7281197Sandrew// 8281197Sandrew//===----------------------------------------------------------------------===// 9281197Sandrew 10281197Sandrew#define _LIBCPP_BUILDING_SYSTEM_ERROR 11281197Sandrew#include "__config" 12281197Sandrew#include "system_error" 13281197Sandrew#include "string" 14281197Sandrew#include "cstring" 15281197Sandrew 16281197Sandrew_LIBCPP_BEGIN_NAMESPACE_STD 17281197Sandrew 18281197Sandrew// class error_category 19281197Sandrew 20281197Sandrewerror_category::error_category() _NOEXCEPT 21281197Sandrew{ 22281197Sandrew} 23281197Sandrew 24281197Sandrewerror_category::~error_category() _NOEXCEPT 25281197Sandrew{ 26281197Sandrew} 27281197Sandrew 28281197Sandrewerror_condition 29281197Sandrewerror_category::default_error_condition(int ev) const _NOEXCEPT 30281197Sandrew{ 31281197Sandrew return error_condition(ev, *this); 32297973Semaste} 33281197Sandrew 34281197Sandrewbool 35281197Sandrewerror_category::equivalent(int code, const error_condition& condition) const _NOEXCEPT 36281197Sandrew{ 37319195Sandrew return default_error_condition(code) == condition; 38281197Sandrew} 39281197Sandrew 40281197Sandrewbool 41281197Sandrewerror_category::equivalent(const error_code& code, int condition) const _NOEXCEPT 42281197Sandrew{ 43 return *this == code.category() && code.value() == condition; 44} 45 46string 47__do_message::message(int ev) const 48{ 49 return string(strerror(ev)); 50} 51 52class _LIBCPP_HIDDEN __generic_error_category 53 : public __do_message 54{ 55public: 56 virtual const char* name() const _NOEXCEPT; 57 virtual string message(int ev) const; 58}; 59 60const char* 61__generic_error_category::name() const _NOEXCEPT 62{ 63 return "generic"; 64} 65 66string 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 76const error_category& 77generic_category() _NOEXCEPT 78{ 79 static __generic_error_category s; 80 return s; 81} 82 83class _LIBCPP_HIDDEN __system_error_category 84 : public __do_message 85{ 86public: 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 92const char* 93__system_error_category::name() const _NOEXCEPT 94{ 95 return "system"; 96} 97 98string 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 108error_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 118const error_category& 119system_category() _NOEXCEPT 120{ 121 static __system_error_category s; 122 return s; 123} 124 125// error_condition 126 127string 128error_condition::message() const 129{ 130 return __cat_->message(__val_); 131} 132 133// error_code 134 135string 136error_code::message() const 137{ 138 return __cat_->message(__val_); 139} 140 141// system_error 142 143string 144system_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 155system_error::system_error(error_code ec, const string& what_arg) 156 : runtime_error(__init(ec, what_arg)), 157 __ec_(ec) 158{ 159} 160 161system_error::system_error(error_code ec, const char* what_arg) 162 : runtime_error(__init(ec, what_arg)), 163 __ec_(ec) 164{ 165} 166 167system_error::system_error(error_code ec) 168 : runtime_error(__init(ec, "")), 169 __ec_(ec) 170{ 171} 172 173system_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 179system_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 185system_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 191system_error::~system_error() _NOEXCEPT 192{ 193} 194 195void 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