Exception.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include <cppunit/Exception.h>
  2. CPPUNIT_NS_BEGIN
  3. #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
  4. /*!
  5. * \deprecated Use SourceLine::isValid() instead.
  6. */
  7. const std::string Exception::UNKNOWNFILENAME = "<unknown>";
  8. /*!
  9. * \deprecated Use SourceLine::isValid() instead.
  10. */
  11. const long Exception::UNKNOWNLINENUMBER = -1;
  12. #endif
  13. Exception::Exception( const Exception &other )
  14. : std::exception( other )
  15. {
  16. m_message = other.m_message;
  17. m_sourceLine = other.m_sourceLine;
  18. }
  19. Exception::Exception( const Message &message,
  20. const SourceLine &sourceLine )
  21. : m_message( message )
  22. , m_sourceLine( sourceLine )
  23. {
  24. }
  25. #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
  26. Exception::Exception( std::string message,
  27. long lineNumber,
  28. std::string fileName )
  29. : m_message( message )
  30. , m_sourceLine( fileName, lineNumber )
  31. {
  32. }
  33. #endif
  34. Exception::~Exception() throw()
  35. {
  36. }
  37. Exception &
  38. Exception::operator =( const Exception& other )
  39. {
  40. // Don't call superclass operator =(). VC++ STL implementation
  41. // has a bug. It calls the destructor and copy constructor of
  42. // std::exception() which reset the virtual table to std::exception.
  43. // SuperClass::operator =(other);
  44. if ( &other != this )
  45. {
  46. m_message = other.m_message;
  47. m_sourceLine = other.m_sourceLine;
  48. }
  49. return *this;
  50. }
  51. const char*
  52. Exception::what() const throw()
  53. {
  54. Exception *mutableThis = CPPUNIT_CONST_CAST( Exception *, this );
  55. mutableThis->m_whatMessage = m_message.shortDescription() + "\n" +
  56. m_message.details();
  57. return m_whatMessage.c_str();
  58. }
  59. SourceLine
  60. Exception::sourceLine() const
  61. {
  62. return m_sourceLine;
  63. }
  64. Message
  65. Exception::message() const
  66. {
  67. return m_message;
  68. }
  69. void
  70. Exception::setMessage( const Message &message )
  71. {
  72. m_message = message;
  73. }
  74. #ifdef CPPUNIT_ENABLE_SOURCELINE_DEPRECATED
  75. long
  76. Exception::lineNumber() const
  77. {
  78. return m_sourceLine.isValid() ? m_sourceLine.lineNumber() :
  79. UNKNOWNLINENUMBER;
  80. }
  81. std::string
  82. Exception::fileName() const
  83. {
  84. return m_sourceLine.isValid() ? m_sourceLine.fileName() :
  85. UNKNOWNFILENAME;
  86. }
  87. #endif
  88. Exception *
  89. Exception::clone() const
  90. {
  91. return new Exception( *this );
  92. }
  93. CPPUNIT_NS_END