assertions.h 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
  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 JSON_ASSERTIONS_H_INCLUDED
  6. #define JSON_ASSERTIONS_H_INCLUDED
  7. #include <cstdlib>
  8. #include <sstream>
  9. #if !defined(JSON_IS_AMALGAMATION)
  10. #include "config.h"
  11. #endif // if !defined(JSON_IS_AMALGAMATION)
  12. /** It should not be possible for a maliciously designed file to
  13. * cause an abort() or seg-fault, so these macros are used only
  14. * for pre-condition violations and internal logic errors.
  15. */
  16. #if JSON_USE_EXCEPTION
  17. // @todo <= add detail about condition in exception
  18. #define JSON_ASSERT(condition) \
  19. do { \
  20. if (!(condition)) { \
  21. Json::throwLogicError("assert json failed"); \
  22. } \
  23. } while (0)
  24. #define JSON_FAIL_MESSAGE(message) \
  25. do { \
  26. OStringStream oss; \
  27. oss << message; \
  28. Json::throwLogicError(oss.str()); \
  29. abort(); \
  30. } while (0)
  31. #else // JSON_USE_EXCEPTION
  32. #define JSON_ASSERT(condition) assert(condition)
  33. // The call to assert() will show the failure message in debug builds. In
  34. // release builds we abort, for a core-dump or debugger.
  35. #define JSON_FAIL_MESSAGE(message) \
  36. { \
  37. OStringStream oss; \
  38. oss << message; \
  39. assert(false && oss.str().c_str()); \
  40. abort(); \
  41. }
  42. #endif
  43. #define JSON_ASSERT_MESSAGE(condition, message) \
  44. do { \
  45. if (!(condition)) { \
  46. JSON_FAIL_MESSAGE(message); \
  47. } \
  48. } while (0)
  49. #endif // JSON_ASSERTIONS_H_INCLUDED