Guest User

Untitled

a guest
Feb 1st, 2013
61
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <sstream>
  2. #include <tchar.h>
  3.  
  4. //#define AssertBreakMethod DbgRaiseAssertionFailure
  5. //#define AssertBreakMethod DbgRaiseAssertionFailure
  6. #define AssertBreakMethod DebugBreak
  7.  
  8. #if !defined(NDEBUG)
  9. #define USP_INLINE_ASSERTS 1  // inline so stepping works nicely
  10. #else
  11. // Use lambda for out-of-line function generation for if body
  12. #define USP_INLINE_ASSERTS 0  // out of line body in release case
  13. #endif
  14. #if USP_INLINE_ASSERTS
  15. #define USP_ASSERT_PREFIX
  16. #define USP_ASSERT_SUFFIX
  17. #else
  18. #define USP_ASSERT_PREFIX ([&]()
  19. #define USP_ASSERT_SUFFIX )();
  20. #endif
  21.  
  22. #define shipassert(expr) \
  23.   do \
  24.   { \
  25.     if (!(expr)) \
  26.     USP_ASSERT_PREFIX \
  27.     { \
  28.       OutputDebugStringA(#expr "\n"); \
  29.       AssertBreakMethod(); \
  30.     } \
  31.     USP_ASSERT_SUFFIX \
  32.   } \
  33.   while (false)
  34.  
  35. #define WIDENW(m) L##m
  36. #define WIDENA(m) m
  37. #define MEXPANDA(m) m
  38.  
  39. #define WIDEN_MEXPAND(m) L ## m
  40. #define MEXPANDW(m) WIDEN_MEXPAND(m)
  41.  
  42. #define STRINGIFY2(m) #m
  43. #define STRINGIFYA(m) STRINGIFY2(m)
  44.  
  45. #define WIDEN_STRINGIFY(m) L## #m
  46. #define STRINGIFYW(m) WIDEN_STRINGIFY(m)
  47.  
  48. #ifdef UNICODE
  49. #define STRINGIFY STRINGIFYW
  50. #define MEXPAND MEXPANDW
  51. #define WIDEN WIDENW
  52. #else
  53. #define STRINGIFY STRINGIFYA
  54. #define MEXPAND MEXPANDA
  55. #define WIDEN WIDENA
  56. #endif
  57.  
  58. #define AssertBreakForce(expr) \
  59.   do \
  60.   { \
  61.     if (!(expr)) \
  62.     USP_ASSERT_PREFIX \
  63.     { \
  64.       OutputDebugStringW(MEXPANDW(__FILE__) \
  65.           WIDENW("(") STRINGIFYW(__LINE__) \
  66.           WIDENW("): Assertion failed: ") \
  67.           WIDENW(#expr) WIDENW("\n")); \
  68.       AssertBreakMethod(); \
  69.     } \
  70.     USP_ASSERT_SUFFIX \
  71.   } \
  72.   while (0)
  73.  
  74. // Use this instead of the goofy dialog box that pops up by default
  75. #if !defined(NDEBUG) || defined(FORCE_ASSERTS)
  76. #define AssertBreak AssertBreakForce
  77. #else
  78. #define AssertBreak __noop
  79. #endif
  80.  
  81. #if UNICODE
  82. #define _assertBreakForceSS std::wstringstream
  83. #else
  84. #define _assertBreakForceSS std::stringstream
  85. #endif
  86.  
  87. // Use this instead of the goofy dialog box that pops up by default
  88. #define AssertBreakForceOperator(a, b, op) \
  89.   do \
  90.   { \
  91.     if (!((a) op (b))) \
  92.     USP_ASSERT_PREFIX \
  93.     { \
  94.       _assertBreakForceSS ss; \
  95.       ss << WIDEN(__FILE__) WIDEN("(") STRINGIFY(__LINE__) WIDEN(")") << \
  96.       WIDEN(": Assertion failed: ") \
  97.       WIDEN(#a) << WIDEN(" (") << WIDEN(a) << WIDEN(") ") \
  98.       WIDEN(#op) WIDEN(" ") WIDEN(#b) << WIDEN(" (") << b << WIDEN(")\n"); \
  99.       OutputDebugString(ss.str().c_str()); \
  100.       AssertBreakMethod(); \
  101.     } \
  102.     USP_ASSERT_SUFFIX \
  103.   } \
  104.   while (0)
  105.  
  106. #if !defined(NDEBUG) || defined(FORCE_ASSERTS)
  107. #define AssertBreakOperator(a,b,o) AssertBreakForceOperator(a,b,o)
  108. #else
  109. #define AssertBreakOperator(a,b,o) __noop()
  110. #endif
  111.  
  112. #define AssertBreakEqual(a, b)    AssertBreakOperator((a), (b), ==)
  113. #define AssertBreakLess(a, b)     AssertBreakOperator((a), (b), <)
  114. #define AssertBreakGreater(a, b)  AssertBreakOperator((a), (b), >)
  115. #define AssertBreakLEqual(a, b)   AssertBreakOperator((a), (b), <=)
  116. #define AssertBreakGEqual(a, b)   AssertBreakOperator((a), (b), >=)
  117. #define AssertBreakNotEqual(a, b) AssertBreakOperator((a), (b), !=)
  118.  
  119. #define AssertBreakForceEqual(a, b)    AssertBreakForceOperator((a), (b), ==)
  120. #define AssertBreakForceLess(a, b)     AssertBreakForceOperator((a), (b), <)
  121. #define AssertBreakForceGreater(a, b)  AssertBreakForceOperator((a), (b), >)
  122. #define AssertBreakForceLEqual(a, b)   AssertBreakForceOperator((a), (b), <=)
  123. #define AssertBreakForceGEqual(a, b)   AssertBreakForceOperator((a), (b), >=)
  124. #define AssertBreakForceNotEqual(a, b) AssertBreakForceOperator((a), (b), !=)
RAW Paste Data