Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 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 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 6 | #ifndef JSON_ASSERTIONS_H_INCLUDED |
| 7 | #define JSON_ASSERTIONS_H_INCLUDED |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 8 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 9 | #include <cstdlib> |
| 10 | #include <sstream> |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 11 | |
| 12 | #if !defined(JSON_IS_AMALGAMATION) |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 13 | #include "config.h" |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 14 | #endif // if !defined(JSON_IS_AMALGAMATION) |
| 15 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 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 | */ |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 20 | #if JSON_USE_EXCEPTION |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 21 | |
| 22 | // @todo <= add detail about condition in exception |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 23 | #define JSON_ASSERT(condition) \ |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 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 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 38 | #else // JSON_USE_EXCEPTION |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 39 | |
| 40 | #define JSON_ASSERT(condition) assert(condition) |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 41 | |
| 42 | // The call to assert() will show the failure message in debug builds. In |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 43 | // release builds we abort, for a core-dump or debugger. |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 44 | #define JSON_FAIL_MESSAGE(message) \ |
| 45 | { \ |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 46 | OStringStream oss; \ |
| 47 | oss << message; \ |
| 48 | assert(false && oss.str().c_str()); \ |
| 49 | abort(); \ |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 50 | } |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 51 | |
| 52 | #endif |
| 53 | |
Derek Sollenberger | 2eb3b4d | 2016-01-11 14:41:40 -0500 | [diff] [blame] | 54 | #define JSON_ASSERT_MESSAGE(condition, message) \ |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 55 | do { \ |
| 56 | if (!(condition)) { \ |
| 57 | JSON_FAIL_MESSAGE(message); \ |
| 58 | } \ |
| 59 | } while (0) |
Leon Scroggins III | f59fb0e | 2014-05-28 15:19:42 -0400 | [diff] [blame] | 60 | |
Haibo Huang | b0bee82 | 2021-02-24 15:40:15 -0800 | [diff] [blame] | 61 | #endif // JSON_ASSERTIONS_H_INCLUDED |