system_error.hpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. //
  2. // system_error.hpp
  3. // ~~~~~~~~~~~~~~~~
  4. //
  5. // Copyright (c) 2003-2011 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/config.hpp"
  16. #include <boost/scoped_ptr.hpp>
  17. #include <cerrno>
  18. #include <exception>
  19. #include <string>
  20. #include "asio/error_code.hpp"
  21. #include "asio/detail/push_options.hpp"
  22. namespace asio {
  23. /// The system_error class is used to represent system conditions that
  24. /// prevent the library from operating correctly.
  25. class system_error
  26. : public std::exception
  27. {
  28. public:
  29. /// Construct with an error code.
  30. system_error(const error_code& code)
  31. : code_(code),
  32. context_()
  33. {
  34. }
  35. /// Construct with an error code and context.
  36. system_error(const error_code& code, const std::string& context)
  37. : code_(code),
  38. context_(context)
  39. {
  40. }
  41. /// Copy constructor.
  42. system_error(const system_error& other)
  43. : std::exception(other),
  44. code_(other.code_),
  45. context_(other.context_),
  46. what_()
  47. {
  48. }
  49. /// Destructor.
  50. virtual ~system_error() throw ()
  51. {
  52. }
  53. /// Assignment operator.
  54. system_error& operator=(const system_error& e)
  55. {
  56. context_ = e.context_;
  57. code_ = e.code_;
  58. what_.reset();
  59. return *this;
  60. }
  61. /// Get a string representation of the exception.
  62. virtual const char* what() const throw ()
  63. {
  64. #if !defined(BOOST_NO_EXCEPTIONS)
  65. try
  66. #endif // !defined(BOOST_NO_EXCEPTIONS)
  67. {
  68. if (!what_)
  69. {
  70. std::string tmp(context_);
  71. if (tmp.length())
  72. tmp += ": ";
  73. tmp += code_.message();
  74. what_.reset(new std::string(tmp));
  75. }
  76. return what_->c_str();
  77. }
  78. #if !defined(BOOST_NO_EXCEPTIONS)
  79. catch (std::exception&)
  80. {
  81. return "system_error";
  82. }
  83. #endif // !defined(BOOST_NO_EXCEPTIONS)
  84. }
  85. /// Get the error code associated with the exception.
  86. error_code code() const
  87. {
  88. return code_;
  89. }
  90. private:
  91. // The code associated with the error.
  92. error_code code_;
  93. // The context associated with the error.
  94. std::string context_;
  95. // The string representation of the error.
  96. mutable boost::scoped_ptr<std::string> what_;
  97. };
  98. } // namespace asio
  99. #include "asio/detail/pop_options.hpp"
  100. #endif // ASIO_SYSTEM_ERROR_HPP