You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

182 lines
5.9 KiB

  1. /*
  2. * This program source code file is part of KiCad, a free EDA CAD application.
  3. *
  4. * Copyright (C) 2007-2016 SoftPLC Corporation, Dick Hollenbeck <dick@softplc.com>
  5. * Copyright (C) 2016 KiCad Developers, see AUTHORS.txt for contributors.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, you may find one here:
  19. * http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
  20. * or you may search the http://www.gnu.org website for the version 2 license,
  21. * or you may write to the Free Software Foundation, Inc.,
  22. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  23. */
  24. #ifndef KI_EXCEPTION_H_
  25. #define KI_EXCEPTION_H_
  26. #include <wx/string.h>
  27. /**
  28. * @ingroup exception_types
  29. * @{
  30. */
  31. /// macro which captures the "call site" values of __FILE_, __FUNCTION__ & __LINE__
  32. #define THROW_IO_ERROR( msg ) throw IO_ERROR( msg, __FILE__, __FUNCTION__, __LINE__ )
  33. /**
  34. * class KI_PARAM_ERROR
  35. * is a class used to hold a translatable error message and may be used when throwing exceptions
  36. * containing a translated error message.
  37. */
  38. class KI_PARAM_ERROR // similar to std::invalid_argument for instance
  39. {
  40. public:
  41. /**
  42. * Constructor
  43. */
  44. KI_PARAM_ERROR( const wxString& aMessage )
  45. {
  46. m_message = aMessage;
  47. }
  48. KI_PARAM_ERROR() {}
  49. const wxString What() const
  50. {
  51. return m_message;
  52. }
  53. virtual ~KI_PARAM_ERROR() throw () {}
  54. private:
  55. wxString m_message;
  56. };
  57. /**
  58. * Struct IO_ERROR
  59. * is a class used to hold an error message and may be used when throwing exceptions
  60. * containing meaningful error messages.
  61. * @author Dick Hollenbeck
  62. */
  63. class IO_ERROR // : std::exception
  64. {
  65. public:
  66. /**
  67. * Constructor
  68. *
  69. * @param aProblem is Problem() text.
  70. *
  71. * @param aThrowersFile is the __FILE__ preprocessor macro but generated
  72. * at the source file of thrower.
  73. *
  74. * @param aThrowersFunction is the function name at the throw site.
  75. * @param aThrowersLineNumber is the source code line number of the throw.
  76. *
  77. * Use macro THROW_IO_ERROR() to wrap a call to this constructor at the call site.
  78. */
  79. IO_ERROR( const wxString& aProblem, const char* aThrowersFile,
  80. const char* aThrowersFunction, int aThrowersLineNumber )
  81. {
  82. init( aProblem, aThrowersFile, aThrowersFunction, aThrowersLineNumber );
  83. }
  84. IO_ERROR() {}
  85. void init( const wxString& aProblem, const char* aThrowersFile,
  86. const char* aThrowersFunction, int aThrowersLineNumber );
  87. virtual const wxString Problem() const; ///< what was the problem?
  88. virtual const wxString Where() const; ///< where did the Problem() occur?
  89. virtual const wxString What() const; ///< A composite of Problem() and Where()
  90. virtual ~IO_ERROR() throw () {}
  91. protected:
  92. wxString problem;
  93. wxString where;
  94. };
  95. /**
  96. * Struct PARSE_ERROR
  97. * contains a filename or source description, a problem input line, a line number,
  98. * a byte offset, and an error message which contains the the caller's report and his
  99. * call site information: CPP source file, function, and line number.
  100. * @author Dick Hollenbeck
  101. */
  102. struct PARSE_ERROR : public IO_ERROR
  103. {
  104. int lineNumber; ///< at which line number, 1 based index.
  105. int byteIndex; ///< at which byte offset within the line, 1 based index
  106. /// problem line of input [say, from a LINE_READER].
  107. /// this is brought up in original byte format rather than wxString form, incase
  108. /// there was a problem with the encoding, in which case converting to wxString is
  109. /// not reliable in this context.
  110. std::string inputLine;
  111. /**
  112. * Constructor
  113. * which is normally called via the macro THROW_PARSE_ERROR so that
  114. * __FILE__ and __FUNCTION__ and __LINE__ can be captured from the call site.
  115. */
  116. PARSE_ERROR( const wxString& aProblem, const char* aThrowersFile,
  117. const char* aThrowersFunction, int aThrowersLineNumber,
  118. const wxString& aSource,
  119. const char* aInputLine,
  120. int aLineNumber, int aByteIndex ) :
  121. IO_ERROR()
  122. {
  123. init( aProblem, aThrowersFile, aThrowersFunction, aThrowersLineNumber,
  124. aSource, aInputLine, aLineNumber, aByteIndex );
  125. }
  126. void init( const wxString& aProblem, const char* aThrowersFile,
  127. const char* aThrowersFunction, int aThrowersLineNumber,
  128. const wxString& aSource, const char* aInputLine,
  129. int aLineNumber, int aByteIndex );
  130. ~PARSE_ERROR() throw () {}
  131. protected:
  132. PARSE_ERROR(): IO_ERROR(), lineNumber( 0 ), byteIndex( 0 ) {}
  133. };
  134. #define THROW_PARSE_ERROR( aProblem, aSource, aInputLine, aLineNumber, aByteIndex ) \
  135. throw PARSE_ERROR( aProblem, __FILE__, __FUNCTION__, __LINE__, aSource, aInputLine, aLineNumber, aByteIndex )
  136. /**
  137. * Struct FUTURE_FORMAT_ERROR
  138. * variant of PARSE_ERROR indicating that a syntax or related error was likely caused
  139. * by a file generated by a newer version of KiCad than this. Can be used to generate
  140. * more informative error messages.
  141. */
  142. struct FUTURE_FORMAT_ERROR : public PARSE_ERROR
  143. {
  144. wxString requiredVersion; ///< version or date of KiCad required to open file
  145. FUTURE_FORMAT_ERROR( const PARSE_ERROR& aParseError, const wxString& aRequiredVersion );
  146. ~FUTURE_FORMAT_ERROR() throw () {}
  147. };
  148. /** @} exception_types */
  149. #endif // KI_EXCEPTION_H_