ctkException.h 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. /*=============================================================================
  2. Library: CTK
  3. Copyright (c) German Cancer Research Center,
  4. Division of Medical and Biological Informatics
  5. Licensed under the Apache License, Version 2.0 (the "License");
  6. you may not use this file except in compliance with the License.
  7. You may obtain a copy of the License at
  8. http://www.apache.org/licenses/LICENSE-2.0
  9. Unless required by applicable law or agreed to in writing, software
  10. distributed under the License is distributed on an "AS IS" BASIS,
  11. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. See the License for the specific language governing permissions and
  13. limitations under the License.
  14. =============================================================================*/
  15. #ifndef __ctkException_h
  16. #define __ctkException_h
  17. // Qt includes
  18. #include <QString>
  19. // CTK includes
  20. #include <ctkCoreExport.h>
  21. //---------------------------------------------------------------------------
  22. /**
  23. * \ingroup Core
  24. *
  25. * \brief The base class for all exceptions defined in CTK.
  26. *
  27. * This exception class takes a QString object as the message text and can
  28. * optionally store another ctkException instance which caused this exception.
  29. *
  30. * ctkException classes can be copied, saved, and rethrown.
  31. */
  32. class CTK_CORE_EXPORT ctkException : public std::exception
  33. {
  34. public:
  35. /**
  36. * @brief Create a new ctkException.
  37. * @param msg The exception message.
  38. */
  39. ctkException(const QString& msg);
  40. /**
  41. * @brief Create a new ctkException containing another exception as the cause.
  42. * @param msg The exception message.
  43. * @param cause The nested exception causing this exception.
  44. */
  45. ctkException(const QString& msg, const ctkException& cause);
  46. /**
  47. * @brief Copy constructor.
  48. * @param o The exception to copy.
  49. */
  50. ctkException(const ctkException& o);
  51. /**
  52. * @brief Assignment operator.
  53. * @param o The exception to assign to this exception.
  54. * @return
  55. */
  56. ctkException& operator=(const ctkException& o);
  57. ~ctkException() throw();
  58. /**
  59. * @brief Gets the nested exception which caused this exception.
  60. * @return The nested exception, or <code>NULL</code> if there is none.
  61. */
  62. const ctkException* cause() const throw();
  63. /**
  64. * @brief Sets the cause for this exception.
  65. * @param cause The exception causing this exception.
  66. */
  67. void setCause(const ctkException& cause);
  68. /**
  69. * @brief Returns the name for this exception.
  70. * @return The exception name.
  71. */
  72. virtual const char* name() const throw();
  73. /**
  74. * @brief Returns the class name for this exception.
  75. * @return The exception class name.
  76. */
  77. virtual const char* className() const throw();
  78. /**
  79. * @brief Returns a static string describing this exception.
  80. * @return The exception description.
  81. */
  82. virtual const char* what() const throw();
  83. /**
  84. * @brief Creates a copy of this exception. Use rethrow() to throw the
  85. * copy again.
  86. * @return A copy of this exception.
  87. */
  88. virtual ctkException* clone() const;
  89. /**
  90. * @brief (Re)Throws this exception.
  91. */
  92. void rethrow() const;
  93. private:
  94. QString Msg;
  95. ctkException* NestedException;
  96. };
  97. //---------------------------------------------------------------------------
  98. /**
  99. * \ingroup Core
  100. */
  101. CTK_CORE_EXPORT QDebug operator<<(QDebug dbg, const ctkException& exc);
  102. //---------------------------------------------------------------------------
  103. /**
  104. * \ingroup Core
  105. *
  106. * \brief Quickly declare a ctkException sub-class.
  107. * \param API The export macro.
  108. * \param CLS The class name for the ctkException sub-class.
  109. * \param BASE The class name of the actual super class.
  110. */
  111. #define CTK_DECLARE_EXCEPTION(API, CLS, BASE) \
  112. class API CLS : public BASE \
  113. { \
  114. public: \
  115. CLS(const QString& msg); \
  116. CLS(const QString& msg, const ctkException& exc); \
  117. CLS(const CLS& exc); \
  118. ~CLS() throw(); \
  119. CLS& operator = (const CLS& exc); \
  120. const char* name() const throw(); \
  121. CLS* clone() const; \
  122. };
  123. //---------------------------------------------------------------------------
  124. /**
  125. * \ingroup Core
  126. *
  127. * \brief Quickly implement a ctkException sub-class
  128. * \param CLS The class name for the ctkException sub-class.
  129. * \param BASE The class name of the actual super class.
  130. * \param NAME A human-readable name for this exception class.
  131. */
  132. #define CTK_IMPLEMENT_EXCEPTION(CLS, BASE, NAME) \
  133. CLS::CLS(const QString& msg) : BASE(msg) \
  134. { } \
  135. CLS::CLS(const QString& msg, const ctkException& exc) : BASE(msg, exc) \
  136. { } \
  137. CLS::CLS(const CLS& exc) : BASE(exc) \
  138. { } \
  139. CLS::~CLS() throw() \
  140. { } \
  141. CLS& CLS::operator = (const CLS& exc) \
  142. { \
  143. BASE::operator = (exc); \
  144. return *this; \
  145. } \
  146. const char* CLS::name() const throw() \
  147. { \
  148. return NAME; \
  149. } \
  150. ctkException* CLS::clone() const \
  151. { \
  152. return new CLS(*this); \
  153. }
  154. #endif // __ctkException_h