system_error.hpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. //
  2. // system_error.hpp
  3. // ~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2010 Christopher M. Kohlhoff (chris at kohlhoff dot com)
  6. //
  7. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  8. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. //
  10. #ifndef ASIO_SYSTEM_ERROR_HPP
  11. #define ASIO_SYSTEM_ERROR_HPP
  12. #if defined(_MSC_VER) && (_MSC_VER >= 1200)
  13. # pragma once
  14. #endif // defined(_MSC_VER) && (_MSC_VER >= 1200)
  15. #include "asio/detail/push_options.hpp"
  16. #include "asio/detail/push_options.hpp"
  17. #include <boost/config.hpp>
  18. #include <boost/scoped_ptr.hpp>
  19. #include <cerrno>
  20. #include <exception>
  21. #include <string>
  22. #include "asio/detail/pop_options.hpp"
  23. #include "asio/error_code.hpp"
  24. namespace asio {
  25. /// The system_error class is used to represent system conditions that
  26. /// prevent the library from operating correctly.
  27. class system_error
  28. : public std::exception
  29. {
  30. public:
  31. /// Construct with an error code.
  32. system_error(const error_code& code)
  33. : code_(code),
  34. context_()
  35. {
  36. }
  37. /// Construct with an error code and context.
  38. system_error(const error_code& code, const std::string& context)
  39. : code_(code),
  40. context_(context)
  41. {
  42. }
  43. /// Copy constructor.
  44. system_error(const system_error& other)
  45. : std::exception(other),
  46. code_(other.code_),
  47. context_(other.context_),
  48. what_()
  49. {
  50. }
  51. /// Destructor.
  52. virtual ~system_error() throw ()
  53. {
  54. }
  55. /// Assignment operator.
  56. system_error& operator=(const system_error& e)
  57. {
  58. context_ = e.context_;
  59. code_ = e.code_;
  60. what_.reset();
  61. return *this;
  62. }
  63. /// Get a string representation of the exception.
  64. virtual const char* what() const throw ()
  65. {
  66. #if !defined(BOOST_NO_EXCEPTIONS)
  67. try
  68. #endif // !defined(BOOST_NO_EXCEPTIONS)
  69. {
  70. if (!what_)
  71. {
  72. std::string tmp(context_);
  73. if (tmp.length())
  74. tmp += ": ";
  75. tmp += code_.message();
  76. what_.reset(new std::string(tmp));
  77. }
  78. return what_->c_str();
  79. }
  80. #if !defined(BOOST_NO_EXCEPTIONS)
  81. catch (std::exception&)
  82. {
  83. return "system_error";
  84. }
  85. #endif // !defined(BOOST_NO_EXCEPTIONS)
  86. }
  87. /// Get the error code associated with the exception.
  88. error_code code() const
  89. {
  90. return code_;
  91. }
  92. private:
  93. // The code associated with the error.
  94. error_code code_;
  95. // The context associated with the error.
  96. std::string context_;
  97. // The string representation of the error.
  98. mutable boost::scoped_ptr<std::string> what_;
  99. };
  100. } // namespace asio
  101. #include "asio/detail/pop_options.hpp"
  102. #endif // ASIO_SYSTEM_ERROR_HPP