assertions.h 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. // Copyright 2007-2010 Baptiste Lepilleur
  2. // Distributed under MIT license, or public domain if desired and
  3. // recognized in your jurisdiction.
  4. // See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
  5. #ifndef CPPTL_JSON_ASSERTIONS_H_INCLUDED
  6. #define CPPTL_JSON_ASSERTIONS_H_INCLUDED
  7. #if !defined(JSON_IS_AMALGAMATION)
  8. #include "config.h"
  9. #endif // if !defined(JSON_IS_AMALGAMATION)
  10. #include <stdlib.h>
  11. #if JSON_USE_EXCEPTION
  12. #include <stdexcept>
  13. #define JSON_ASSERT(condition) \
  14. assert(condition); // @todo <= change this into an exception throw
  15. #define JSON_FAIL_MESSAGE(message) throw std::runtime_error(message);
  16. #else // JSON_USE_EXCEPTION
  17. #define JSON_ASSERT(condition) assert(condition);
  18. // The call to assert() will show the failure message in debug builds. In
  19. // release bugs we write to invalid memory in order to crash hard, so that a
  20. // debugger or crash reporter gets the chance to take over. We still call exit()
  21. // afterward in order to tell the compiler that this macro doesn't return.
  22. #define JSON_FAIL_MESSAGE(message) \
  23. { \
  24. assert(false&& message); \
  25. strcpy(reinterpret_cast<char*>(666), message); \
  26. exit(123); \
  27. }
  28. #endif
  29. #define JSON_ASSERT_MESSAGE(condition, message) \
  30. if (!(condition)) { \
  31. JSON_FAIL_MESSAGE(message) \
  32. }
  33. #endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED