blob: 1cca28d959bcfd3c8a9574af41e92eccd874ac9a [file] [log] [blame]
Devin Jeanpierre59e4d352017-07-21 03:44:36 -07001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Aaron Jacobse3d0eca2011-05-24 01:03:22 +00002// 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
Christopher Dunn93763682015-03-08 12:39:27 -050021
22// @todo <= add detail about condition in exception
23# define JSON_ASSERT(condition) \
24 {if (!(condition)) {Json::throwLogicError( "assert json failed" );}}
25
26# define JSON_FAIL_MESSAGE(message) \
Christopher Dunnb3e6f3d2015-03-05 15:19:43 -060027 { \
Christopher Dunn38bb4912016-03-06 11:50:00 -060028 JSONCPP_OSTRINGSTREAM oss; oss << message; \
Christopher Dunn93763682015-03-08 12:39:27 -050029 Json::throwLogicError(oss.str()); \
30 abort(); \
Christopher Dunnb3e6f3d2015-03-05 15:19:43 -060031 }
Christopher Dunn93763682015-03-08 12:39:27 -050032
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100033#else // JSON_USE_EXCEPTION
Christopher Dunn93763682015-03-08 12:39:27 -050034
35# define JSON_ASSERT(condition) assert(condition)
Aaron Jacobs2b853c42011-12-22 03:18:24 +000036
37// The call to assert() will show the failure message in debug builds. In
Christopher Dunn717b0862015-03-08 12:05:28 -050038// release builds we abort, for a core-dump or debugger.
Christopher Dunn93763682015-03-08 12:39:27 -050039# define JSON_FAIL_MESSAGE(message) \
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100040 { \
Christopher Dunn38bb4912016-03-06 11:50:00 -060041 JSONCPP_OSTRINGSTREAM oss; oss << message; \
Christopher Dunn7956ccd2015-01-20 16:18:15 -060042 assert(false && oss.str().c_str()); \
43 abort(); \
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100044 }
Aaron Jacobs2b853c42011-12-22 03:18:24 +000045
Christopher Dunn7956ccd2015-01-20 16:18:15 -060046
Aaron Jacobse3d0eca2011-05-24 01:03:22 +000047#endif
48
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100049#define JSON_ASSERT_MESSAGE(condition, message) \
50 if (!(condition)) { \
Christopher Dunn7956ccd2015-01-20 16:18:15 -060051 JSON_FAIL_MESSAGE(message); \
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100052 }
Aaron Jacobse3d0eca2011-05-24 01:03:22 +000053
54#endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED