blob: 07e7f2e953a1dbdcf7940f6bf7a24d8d279f4ce0 [file] [log] [blame]
Aaron Jacobse3d0eca2011-05-24 01:03:22 +00001// 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 Jacobs9fa4e842014-07-01 08:48:54 +10007#define CPPTL_JSON_ASSERTIONS_H_INCLUDED
Aaron Jacobse3d0eca2011-05-24 01:03:22 +00008
Aaron Jacobsa7615302011-05-24 06:27:36 +00009#include <stdlib.h>
Christopher Dunn7956ccd2015-01-20 16:18:15 -060010#include <sstream>
Aaron Jacobsa7615302011-05-24 06:27:36 +000011
Aaron Jacobse3d0eca2011-05-24 01:03:22 +000012#if !defined(JSON_IS_AMALGAMATION)
Christopher Dunn60f778b2014-07-09 21:37:23 -070013#include "config.h"
Aaron Jacobse3d0eca2011-05-24 01:03:22 +000014#endif // if !defined(JSON_IS_AMALGAMATION)
15
Christopher Dunn717b0862015-03-08 12:05:28 -050016/** 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 Jacobs7c507d72011-09-14 08:41:37 +000020#if JSON_USE_EXCEPTION
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100021#include <stdexcept>
22#define JSON_ASSERT(condition) \
Christopher Dunnb3e6f3d2015-03-05 15:19:43 -060023 {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 Dunn0c919272015-03-03 09:45:33 -060029//#define JSON_FAIL_MESSAGE(message) throw std::logic_error(message)
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100030#else // JSON_USE_EXCEPTION
Christopher Dunnb3e6f3d2015-03-05 15:19:43 -060031#define JSON_ASSERT(condition) assert(condition)
Aaron Jacobs2b853c42011-12-22 03:18:24 +000032
33// The call to assert() will show the failure message in debug builds. In
Christopher Dunn717b0862015-03-08 12:05:28 -050034// release builds we abort, for a core-dump or debugger.
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100035#define JSON_FAIL_MESSAGE(message) \
36 { \
Christopher Dunn7956ccd2015-01-20 16:18:15 -060037 std::ostringstream oss; oss << message; \
38 assert(false && oss.str().c_str()); \
39 abort(); \
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100040 }
Aaron Jacobs2b853c42011-12-22 03:18:24 +000041
Christopher Dunn7956ccd2015-01-20 16:18:15 -060042
Aaron Jacobse3d0eca2011-05-24 01:03:22 +000043#endif
44
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100045#define JSON_ASSERT_MESSAGE(condition, message) \
46 if (!(condition)) { \
Christopher Dunn7956ccd2015-01-20 16:18:15 -060047 JSON_FAIL_MESSAGE(message); \
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100048 }
Aaron Jacobse3d0eca2011-05-24 01:03:22 +000049
50#endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED