SDL_assert.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #ifndef _SDL_assert_h
  2. #define _SDL_assert_h
  3. #include "SDL_config.h"
  4. #include "begin_code.h"
  5. #ifdef __cplusplus
  6. extern "C" {
  7. #endif
  8. #ifndef SDL_ASSERT_LEVEL
  9. #ifdef SDL_DEFAULT_ASSERT_LEVEL
  10. #define SDL_ASSERT_LEVEL SDL_DEFAULT_ASSERT_LEVEL
  11. #elif defined(_DEBUG) || defined(DEBUG) || \
  12. (defined(__GNUC__) && !defined(__OPTIMIZE__))
  13. #define SDL_ASSERT_LEVEL 2
  14. #else
  15. #define SDL_ASSERT_LEVEL 1
  16. #endif
  17. #endif
  18. #if defined(_MSC_VER)
  19. extern void __cdecl __debugbreak(void);
  20. #define SDL_TriggerBreakpoint() __debugbreak()
  21. #elif (defined(__GNUC__) && (defined(__i386__) || defined(__x86_64__)))
  22. #define SDL_TriggerBreakpoint() __asm__ __volatile__ ( "int $3\n\t" )
  23. #elif defined(HAVE_SIGNAL_H)
  24. #include <signal.h>
  25. #define SDL_TriggerBreakpoint() raise(SIGTRAP)
  26. #else
  27. #define SDL_TriggerBreakpoint()
  28. #endif
  29. #if defined(__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L)
  30. # define SDL_FUNCTION __func__
  31. #elif ((__GNUC__ >= 2) || defined(_MSC_VER))
  32. # define SDL_FUNCTION __FUNCTION__
  33. #else
  34. # define SDL_FUNCTION "???"
  35. #endif
  36. #define SDL_FILE __FILE__
  37. #define SDL_LINE __LINE__
  38. #define SDL_disabled_assert(condition) \
  39. do { (void) sizeof ((condition)); } while (0)
  40. typedef enum
  41. {
  42. SDL_ASSERTION_RETRY,
  43. SDL_ASSERTION_BREAK,
  44. SDL_ASSERTION_ABORT,
  45. SDL_ASSERTION_IGNORE,
  46. SDL_ASSERTION_ALWAYS_IGNORE
  47. } SDL_assert_state;
  48. typedef struct SDL_assert_data
  49. {
  50. int always_ignore;
  51. unsigned int trigger_count;
  52. const char *condition;
  53. const char *filename;
  54. int linenum;
  55. const char *function;
  56. const struct SDL_assert_data *next;
  57. } SDL_assert_data;
  58. #if (SDL_ASSERT_LEVEL > 0)
  59. typedef SDL_assert_state SDLCALL tSDL_ReportAssertion(SDL_assert_data *,
  60. const char *,
  61. const char *, int);
  62. #define SDL_enabled_assert(condition) \
  63. do { \
  64. while ( !(condition) ) { \
  65. static struct SDL_assert_data assert_data = { \
  66. 0, 0, #condition, 0, 0, 0, 0 \
  67. }; \
  68. const SDL_assert_state state = SDL_ReportAssertion(&assert_data, \
  69. SDL_FUNCTION, \
  70. SDL_FILE, \
  71. SDL_LINE); \
  72. if (state == SDL_ASSERTION_RETRY) { \
  73. continue; \
  74. } else if (state == SDL_ASSERTION_BREAK) { \
  75. SDL_TriggerBreakpoint(); \
  76. } \
  77. break; \
  78. } \
  79. } while (0)
  80. #endif
  81. #if SDL_ASSERT_LEVEL == 0
  82. # define SDL_assert(condition) SDL_disabled_assert(condition)
  83. # define SDL_assert_release(condition) SDL_disabled_assert(condition)
  84. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  85. #elif SDL_ASSERT_LEVEL == 1
  86. # define SDL_assert(condition) SDL_disabled_assert(condition)
  87. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  88. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  89. #elif SDL_ASSERT_LEVEL == 2
  90. # define SDL_assert(condition) SDL_enabled_assert(condition)
  91. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  92. # define SDL_assert_paranoid(condition) SDL_disabled_assert(condition)
  93. #elif SDL_ASSERT_LEVEL == 3
  94. # define SDL_assert(condition) SDL_enabled_assert(condition)
  95. # define SDL_assert_release(condition) SDL_enabled_assert(condition)
  96. # define SDL_assert_paranoid(condition) SDL_enabled_assert(condition)
  97. #else
  98. # error Unknown assertion level.
  99. #endif
  100. typedef SDL_assert_state (SDLCALL *SDL_AssertionHandler)(
  101. const SDL_assert_data* data, void* userdata);
  102. typedef void SDLCALL tSDL_SetAssertionHandler(
  103. SDL_AssertionHandler handler,
  104. void *userdata);
  105. typedef const SDL_assert_data * SDLCALL tSDL_GetAssertionReport(void);
  106. typedef void SDLCALL tSDL_ResetAssertionReport(void);
  107. extern tSDL_ReportAssertion *SDL_ReportAssertion;
  108. extern tSDL_SetAssertionHandler *SDL_SetAssertionHandler;
  109. extern tSDL_GetAssertionReport *SDL_GetAssertionReport;
  110. extern tSDL_ResetAssertionReport *SDL_ResetAssertionReport;
  111. #ifdef __cplusplus
  112. }
  113. #endif
  114. #include "close_code.h"
  115. #endif