platformAssert.h 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _PLATFORMASSERT_H_
  23. #define _PLATFORMASSERT_H_
  24. #ifndef _PLATFORM_H_
  25. #include "platform/platform.h"
  26. #endif
  27. class PlatformAssert
  28. {
  29. public:
  30. enum Type
  31. {
  32. Warning = 3,
  33. Fatal = 2,
  34. Fatal_ISV = 1
  35. };
  36. private:
  37. static PlatformAssert *platformAssert;
  38. bool processing;
  39. bool ignoreAll;
  40. virtual bool displayMessageBox(const char *title, const char *message, bool retry);
  41. virtual bool process(Type assertType,
  42. const char* filename,
  43. U32 lineNumber,
  44. const char* message);
  45. PlatformAssert();
  46. virtual ~PlatformAssert();
  47. public:
  48. static void create( PlatformAssert* newAssertClass = NULL );
  49. static void destroy();
  50. static bool processAssert(Type assertType,
  51. const char* filename,
  52. U32 lineNumber,
  53. const char* message);
  54. static char *message(const char *message, ...);
  55. static bool processingAssert();
  56. };
  57. #ifdef TORQUE_ENABLE_ASSERTS
  58. /*!
  59. Assert that the statement x is true, and continue processing.
  60. If the statment x is true, continue processing.
  61. If the statement x is false, log the file and line where the assert occured,
  62. the message y and continue processing.
  63. These asserts are only present in DEBUG builds.
  64. */
  65. #define AssertWarn(x, y) (void)(!!(x) || ::PlatformAssert::processAssert(::PlatformAssert::Warning, __FILE__, __LINE__, y))
  66. /*!
  67. Helper macro called when AssertFatal failed.
  68. Used for help static code analyzers.
  69. */
  70. #ifndef ON_FAIL_ASSERTFATAL
  71. #define ON_FAIL_ASSERTFATAL
  72. #endif
  73. /*!
  74. Assert that the statement x is true, otherwise halt.
  75. If the statement x is true, continue processing.
  76. If the statement x is false, log the file and line where the assert occured,
  77. the message y and displaying a dialog containing the message y. The user then
  78. has the option to halt or continue causing the debugger to break.
  79. These asserts are only present in DEBUG builds.
  80. This assert is very useful for verifying data as well as function entry and
  81. exit conditions.
  82. */
  83. #define AssertFatal(x, y) ((!(x) && ::PlatformAssert::processAssert(::PlatformAssert::Fatal, __FILE__, __LINE__, y)) ? ::Platform::debugBreak() : (void)0) \
  84. #else
  85. #define AssertFatal(x, y) TORQUE_UNUSED(x)
  86. #define AssertWarn(x, y) TORQUE_UNUSED(x)
  87. #endif
  88. /*!
  89. Assert (In Shipping Version) that the statement x is true, otherwise halt.
  90. If the statement x is true, continue processing.
  91. If the statement x is false, log the file and line where the assert occurred,
  92. the message y and exit the program displaying a dialog containing the message y.
  93. These asserts are present in both OPTIMIZED and DEBUG builds.
  94. This assert should only be used for rare conditions where the application cannot continue
  95. execution without seg-faulting and you want to display a nice exit message.
  96. */
  97. #define AssertISV(x, y) ((!(x) && ::PlatformAssert::processAssert(::PlatformAssert::Fatal_ISV, __FILE__, __LINE__, y)) ? ::Platform::debugBreak() : (void)0) \
  98. /*!
  99. Sprintf style string formating into a fixed temporary buffer.
  100. @param in_msg sprintf style format string
  101. @returns pointer to fixed buffer containing formatted string
  102. \b Example:
  103. \code
  104. U8 a = 5;
  105. S16 b = -10;
  106. char *output = avar("hello %s! a=%u, b=%d", "world");
  107. ouput = "hello world! a=5, b=-10"
  108. \endcode
  109. @warning avar uses a static fixed buffer. Treat the buffer as volatile data
  110. and use it immediately. Other functions my use avar too and clobber the buffer.
  111. */
  112. const char* avar(const char *in_msg, ...);
  113. #endif // _PLATFORM_ASSERT_H_