Aaron Jacobs | e3d0eca | 2011-05-24 01:03:22 +0000 | [diff] [blame] | 1 | // Copyright 2007-2010 Baptiste Lepilleur |
| 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 CPPTL_JSON_ASSERTIONS_H_INCLUDED |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 7 | #define CPPTL_JSON_ASSERTIONS_H_INCLUDED |
Aaron Jacobs | e3d0eca | 2011-05-24 01:03:22 +0000 | [diff] [blame] | 8 | |
Aaron Jacobs | a761530 | 2011-05-24 06:27:36 +0000 | [diff] [blame] | 9 | #include <stdlib.h> |
Christopher Dunn | 7956ccd | 2015-01-20 16:18:15 -0600 | [diff] [blame] | 10 | #include <sstream> |
Aaron Jacobs | a761530 | 2011-05-24 06:27:36 +0000 | [diff] [blame] | 11 | |
Aaron Jacobs | e3d0eca | 2011-05-24 01:03:22 +0000 | [diff] [blame] | 12 | #if !defined(JSON_IS_AMALGAMATION) |
Christopher Dunn | 60f778b | 2014-07-09 21:37:23 -0700 | [diff] [blame] | 13 | #include "config.h" |
Aaron Jacobs | e3d0eca | 2011-05-24 01:03:22 +0000 | [diff] [blame] | 14 | #endif // if !defined(JSON_IS_AMALGAMATION) |
| 15 | |
Christopher Dunn | 717b086 | 2015-03-08 12:05:28 -0500 | [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 | */ |
Aaron Jacobs | 7c507d7 | 2011-09-14 08:41:37 +0000 | [diff] [blame] | 20 | #if JSON_USE_EXCEPTION |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 21 | #include <stdexcept> |
| 22 | #define JSON_ASSERT(condition) \ |
Christopher Dunn | b3e6f3d | 2015-03-05 15:19:43 -0600 | [diff] [blame] | 23 | {if (!(condition)) {throw std::logic_error( "assert json failed" );}} // @todo <= add detail about condition in exception |
| 24 | #define JSON_FAIL_MESSAGE(message) \ |
| 25 | { \ |
| 26 | std::ostringstream oss; oss << message; \ |
| 27 | throw std::logic_error(oss.str()); \ |
| 28 | } |
Christopher Dunn | 0c91927 | 2015-03-03 09:45:33 -0600 | [diff] [blame] | 29 | //#define JSON_FAIL_MESSAGE(message) throw std::logic_error(message) |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 30 | #else // JSON_USE_EXCEPTION |
Christopher Dunn | b3e6f3d | 2015-03-05 15:19:43 -0600 | [diff] [blame] | 31 | #define JSON_ASSERT(condition) assert(condition) |
Aaron Jacobs | 2b853c4 | 2011-12-22 03:18:24 +0000 | [diff] [blame] | 32 | |
| 33 | // The call to assert() will show the failure message in debug builds. In |
Christopher Dunn | 717b086 | 2015-03-08 12:05:28 -0500 | [diff] [blame^] | 34 | // release builds we abort, for a core-dump or debugger. |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 35 | #define JSON_FAIL_MESSAGE(message) \ |
| 36 | { \ |
Christopher Dunn | 7956ccd | 2015-01-20 16:18:15 -0600 | [diff] [blame] | 37 | std::ostringstream oss; oss << message; \ |
| 38 | assert(false && oss.str().c_str()); \ |
| 39 | abort(); \ |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 40 | } |
Aaron Jacobs | 2b853c4 | 2011-12-22 03:18:24 +0000 | [diff] [blame] | 41 | |
Christopher Dunn | 7956ccd | 2015-01-20 16:18:15 -0600 | [diff] [blame] | 42 | |
Aaron Jacobs | e3d0eca | 2011-05-24 01:03:22 +0000 | [diff] [blame] | 43 | #endif |
| 44 | |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 45 | #define JSON_ASSERT_MESSAGE(condition, message) \ |
| 46 | if (!(condition)) { \ |
Christopher Dunn | 7956ccd | 2015-01-20 16:18:15 -0600 | [diff] [blame] | 47 | JSON_FAIL_MESSAGE(message); \ |
Aaron Jacobs | 9fa4e84 | 2014-07-01 08:48:54 +1000 | [diff] [blame] | 48 | } |
Aaron Jacobs | e3d0eca | 2011-05-24 01:03:22 +0000 | [diff] [blame] | 49 | |
| 50 | #endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED |