gcsx_exception.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /* GCSx
  2. ** EXCEPTION.H
  3. **
  4. ** GCSx exception classes
  5. */
  6. /*****************************************************************************
  7. ** Copyright (C) 2003-2006 Janson
  8. **
  9. ** This program is free software; you can redistribute it and/or modify
  10. ** it under the terms of the GNU General Public License as published by
  11. ** the Free Software Foundation; either version 2 of the License, or
  12. ** (at your option) any later version.
  13. **
  14. ** This program is distributed in the hope that it will be useful,
  15. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. ** GNU General Public License for more details.
  18. **
  19. ** You should have received a copy of the GNU General Public License
  20. ** along with this program; if not, write to the Free Software
  21. ** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
  22. *****************************************************************************/
  23. #ifndef __GCSx_EXCEPTION_H_
  24. #define __GCSx_EXCEPTION_H_
  25. // Exceptions are only used for non-fatal errors; fatal errors simply
  26. // abort gracelessly (i.e. system error box) for simplicity
  27. class BaseException {
  28. protected:
  29. void setDetails(const char* base, va_list arglist);
  30. enum {
  31. DETAILS_SIZE = 160,
  32. };
  33. public:
  34. char details[DETAILS_SIZE];
  35. BaseException();
  36. };
  37. // Non-fatal video execptions only occur during setting/changing video modes or
  38. // resolutions, etc.- video problems during normal graphic activity are fatal
  39. class VideoException : public BaseException {
  40. public:
  41. VideoException(const char* base, ...);
  42. };
  43. // File exceptions should cause an abort of whatever was being loaded
  44. class FileException : public BaseException {
  45. public:
  46. FileException(const char* base, ...);
  47. };
  48. // Compile assertion- throws CompileException if needed
  49. // Currently tied to NDEBUG but can be tied to anything else
  50. #ifndef NDEBUG
  51. #define COMPILEASSERT
  52. #endif
  53. #ifdef COMPILEASSERT
  54. // Compile exceptions should cause an abort of compilation and discarding of bytecode
  55. class CompileException : public BaseException {
  56. public:
  57. CompileException(const char* base, ...);
  58. };
  59. #define compileAssert(e) if (!(e)) throw CompileException("Internal compiler error: %s at %s, %d", #e, __FILE__, __LINE__)
  60. #define bytecodeAssert(e) if (!(e)) throw CompileException("Internal bytecode error: %s at %s, %d", #e, __FILE__, __LINE__)
  61. #define tokenizerAssert(e) if (!(e)) throw CompileException("Internal tokenizer error: %s at %s, %d", #e, __FILE__, __LINE__)
  62. #else
  63. #define compileAssert(x) ((void)0)
  64. #define bytecodeAssert(x) ((void)0)
  65. #define tokenizerAssert(x) ((void)0)
  66. #endif
  67. // Interpreter assertion- throws InterpretException if needed
  68. // Currently tied to NDEBUG but can be tied to anything else
  69. #ifndef NDEBUG
  70. #define INTERPRETASSERT
  71. #endif
  72. #ifdef INTERPRETASSERT
  73. // Interpreter exceptions should cause an abort of running script
  74. class InterpretException : public BaseException {
  75. public:
  76. InterpretException(const char* base, ...);
  77. };
  78. #define interpretAssert(e) if (!(e)) throw InterpretException("Runtime interpreter error: %s at %s, %d", #e, __FILE__, __LINE__)
  79. #else
  80. #define interpretAssert(x) ((void)0)
  81. #endif
  82. // Undo exceptions signify cancelling whatever action was being done
  83. class UndoException : public BaseException {
  84. public:
  85. UndoException();
  86. };
  87. // Instead of using exception specifications, we use these macros.
  88. // This allows us to define which functions throw what exceptions,
  89. // or ones where we're unsure, or nothing is thrown, but we don't
  90. // have to use actual exception specifications which are bad(tm)
  91. #define throw_Video
  92. #define throw_File
  93. #define throw_Compile
  94. #define throw_Interpret
  95. #define throw_Undo
  96. #define throw_int
  97. #endif