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