00001 // 00002 // system_error.hpp 00003 // ~~~~~~~~~~~~~~~~ 00004 // 00005 // Copyright (c) 2003-2008 Christopher M. Kohlhoff (chris at kohlhoff dot com) 00006 // 00007 // Distributed under the Boost Software License, Version 1.0. (See accompanying 00008 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) 00009 // 00010 00011 #ifndef ASIO_SYSTEM_ERROR_HPP 00012 #define ASIO_SYSTEM_ERROR_HPP 00013 00014 #if defined(_MSC_VER) && (_MSC_VER >= 1200) 00015 # pragma once 00016 #endif // defined(_MSC_VER) && (_MSC_VER >= 1200) 00017 00018 #include "asio/detail/push_options.hpp" 00019 00020 #include "asio/detail/push_options.hpp" 00021 #include <boost/config.hpp> 00022 #include <boost/scoped_ptr.hpp> 00023 #include <cerrno> 00024 #include <exception> 00025 #include <string> 00026 #include "asio/detail/pop_options.hpp" 00027 00028 #include "asio/error_code.hpp" 00029 00030 namespace asio { 00031 00034 class system_error 00035 : public std::exception 00036 { 00037 public: 00039 system_error(const error_code& code) 00040 : code_(code), 00041 context_() 00042 { 00043 } 00044 00046 system_error(const error_code& code, const std::string& context) 00047 : code_(code), 00048 context_(context) 00049 { 00050 } 00051 00053 system_error(const system_error& other) 00054 : std::exception(other), 00055 code_(other.code_), 00056 context_(other.context_), 00057 what_() 00058 { 00059 } 00060 00062 virtual ~system_error() throw () 00063 { 00064 } 00065 00067 system_error& operator=(const system_error& e) 00068 { 00069 context_ = e.context_; 00070 code_ = e.code_; 00071 what_.reset(); 00072 return *this; 00073 } 00074 00076 virtual const char* what() const throw () 00077 { 00078 try 00079 { 00080 if (!what_) 00081 { 00082 std::string tmp(context_); 00083 if (tmp.length()) 00084 tmp += ": "; 00085 tmp += code_.message(); 00086 what_.reset(new std::string(tmp)); 00087 } 00088 return what_->c_str(); 00089 } 00090 catch (std::exception&) 00091 { 00092 return "system_error"; 00093 } 00094 } 00095 00097 error_code code() const 00098 { 00099 return code_; 00100 } 00101 00102 private: 00103 // The code associated with the error. 00104 error_code code_; 00105 00106 // The context associated with the error. 00107 std::string context_; 00108 00109 // The string representation of the error. 00110 mutable boost::scoped_ptr<std::string> what_; 00111 }; 00112 00113 } // namespace asio 00114 00115 #include "asio/detail/pop_options.hpp" 00116 00117 #endif // ASIO_SYSTEM_ERROR_HPP