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.

184 lines
5.8 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-2020 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. * Hold a translatable error message and may be used when throwing exceptions containing a
  35. * translated error message.
  36. */
  37. class KI_PARAM_ERROR // similar to std::invalid_argument for instance
  38. {
  39. public:
  40. /**
  41. * Constructor
  42. */
  43. KI_PARAM_ERROR( const wxString& aMessage )
  44. {
  45. m_message = aMessage;
  46. }
  47. KI_PARAM_ERROR() {}
  48. const wxString What() const
  49. {
  50. return m_message;
  51. }
  52. virtual ~KI_PARAM_ERROR() throw () {}
  53. private:
  54. wxString m_message;
  55. };
  56. /**
  57. * Hold an error message and may be used when throwing exceptions containing meaningful
  58. * error messages.
  59. *
  60. * @author Dick Hollenbeck
  61. */
  62. class IO_ERROR // : std::exception
  63. {
  64. public:
  65. /**
  66. * Use macro #THROW_IO_ERROR() to wrap a call to this constructor at the call site.
  67. *
  68. * @param aProblem is Problem() text.
  69. * @param aThrowersFile is the __FILE__ preprocessor macro but generated at the source
  70. * file of thrower.
  71. * @param aThrowersFunction is the function name at the throw site.
  72. * @param aThrowersLineNumber is the source code line number of the throw.
  73. */
  74. IO_ERROR( const wxString& aProblem, const char* aThrowersFile,
  75. const char* aThrowersFunction, int aThrowersLineNumber )
  76. {
  77. init( aProblem, aThrowersFile, aThrowersFunction, aThrowersLineNumber );
  78. }
  79. IO_ERROR() {}
  80. void init( const wxString& aProblem, const char* aThrowersFile,
  81. const char* aThrowersFunction, int aThrowersLineNumber );
  82. virtual const wxString Problem() const; ///< what was the problem?
  83. virtual const wxString Where() const; ///< where did the Problem() occur?
  84. virtual const wxString What() const; ///< A composite of Problem() and Where()
  85. virtual ~IO_ERROR() throw () {}
  86. protected:
  87. wxString problem;
  88. wxString where;
  89. };
  90. /**
  91. * A filename or source description, a problem input line, a line number, a byte
  92. * offset, and an error message which contains the the caller's report and his call
  93. * site information: CPP source file, function, and line number.
  94. *
  95. * @author Dick Hollenbeck
  96. */
  97. struct PARSE_ERROR : public IO_ERROR
  98. {
  99. int lineNumber; ///< at which line number, 1 based index.
  100. int byteIndex; ///< at which byte offset within the line, 1 based index
  101. /// problem line of input [say, from a LINE_READER].
  102. /// this is brought up in original byte format rather than wxString form, incase
  103. /// there was a problem with the encoding, in which case converting to wxString is
  104. /// not reliable in this context.
  105. std::string inputLine;
  106. /**
  107. * Normally called via the macro #THROW_PARSE_ERROR so that __FILE__, __FUNCTION__,
  108. * and __LINE__ can be captured from the call site.
  109. */
  110. PARSE_ERROR( const wxString& aProblem, const char* aThrowersFile,
  111. const char* aThrowersFunction, int aThrowersLineNumber,
  112. const wxString& aSource, const char* aInputLine,
  113. int aLineNumber, int aByteIndex ) :
  114. IO_ERROR()
  115. {
  116. init( aProblem, aThrowersFile, aThrowersFunction, aThrowersLineNumber,
  117. aSource, aInputLine, aLineNumber, aByteIndex );
  118. }
  119. void init( const wxString& aProblem, const char* aThrowersFile,
  120. const char* aThrowersFunction, int aThrowersLineNumber,
  121. const wxString& aSource, const char* aInputLine,
  122. int aLineNumber, int aByteIndex );
  123. ~PARSE_ERROR() throw () {}
  124. const wxString ParseProblem() { return parseProblem; }
  125. protected:
  126. PARSE_ERROR() :
  127. IO_ERROR(),
  128. lineNumber( 0 ),
  129. byteIndex( 0 )
  130. {}
  131. protected:
  132. wxString parseProblem;
  133. };
  134. #define THROW_PARSE_ERROR( aProblem, aSource, aInputLine, aLineNumber, aByteIndex ) \
  135. throw PARSE_ERROR( aProblem, __FILE__, __FUNCTION__, __LINE__, aSource, aInputLine, \
  136. aLineNumber, aByteIndex )
  137. /**
  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. 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_