assertions.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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. { \
  20. if (!(condition)) { \
  21. Json::throwLogicError("assert json failed"); \
  22. } \
  23. }
  24. #define JSON_FAIL_MESSAGE(message) \
  25. { \
  26. OStringStream oss; \
  27. oss << message; \
  28. Json::throwLogicError(oss.str()); \
  29. abort(); \
  30. }
  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. if (!(condition)) { \
  45. JSON_FAIL_MESSAGE(message); \
  46. }
  47. #endif // JSON_ASSERTIONS_H_INCLUDED