assertions.h 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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 CPPTL_JSON_ASSERTIONS_H_INCLUDED
  6. #define CPPTL_JSON_ASSERTIONS_H_INCLUDED
  7. #include <stdlib.h>
  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. {if (!(condition)) {Json::throwLogicError( "assert json failed" );}}
  20. # define JSON_FAIL_MESSAGE(message) \
  21. { \
  22. JSONCPP_OSTRINGSTREAM oss; oss << message; \
  23. Json::throwLogicError(oss.str()); \
  24. abort(); \
  25. }
  26. #else // JSON_USE_EXCEPTION
  27. # define JSON_ASSERT(condition) assert(condition)
  28. // The call to assert() will show the failure message in debug builds. In
  29. // release builds we abort, for a core-dump or debugger.
  30. # define JSON_FAIL_MESSAGE(message) \
  31. { \
  32. JSONCPP_OSTRINGSTREAM oss; oss << message; \
  33. assert(false && oss.str().c_str()); \
  34. abort(); \
  35. }
  36. #endif
  37. #define JSON_ASSERT_MESSAGE(condition, message) \
  38. if (!(condition)) { \
  39. JSON_FAIL_MESSAGE(message); \
  40. }
  41. #endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED