blob: 666fa7f542ccef943baa133f3bbf9e8b19845104 [file] [log] [blame]
Haibo Huangb0bee822021-02-24 15:40:15 -08001// Copyright 2007-2010 Baptiste Lepilleur and The JsonCpp Authors
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -04002// 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 Huangb0bee822021-02-24 15:40:15 -08006#ifndef JSON_ASSERTIONS_H_INCLUDED
7#define JSON_ASSERTIONS_H_INCLUDED
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -04008
Haibo Huangb0bee822021-02-24 15:40:15 -08009#include <cstdlib>
10#include <sstream>
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040011
12#if !defined(JSON_IS_AMALGAMATION)
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050013#include "config.h"
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040014#endif // if !defined(JSON_IS_AMALGAMATION)
15
Haibo Huangb0bee822021-02-24 15:40:15 -080016/** 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 IIIf59fb0e2014-05-28 15:19:42 -040020#if JSON_USE_EXCEPTION
Haibo Huangb0bee822021-02-24 15:40:15 -080021
22// @todo <= add detail about condition in exception
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050023#define JSON_ASSERT(condition) \
Haibo Huangb0bee822021-02-24 15:40:15 -080024 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 Sollenberger2eb3b4d2016-01-11 14:41:40 -050038#else // JSON_USE_EXCEPTION
Haibo Huangb0bee822021-02-24 15:40:15 -080039
40#define JSON_ASSERT(condition) assert(condition)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040041
42// The call to assert() will show the failure message in debug builds. In
Haibo Huangb0bee822021-02-24 15:40:15 -080043// release builds we abort, for a core-dump or debugger.
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050044#define JSON_FAIL_MESSAGE(message) \
45 { \
Haibo Huangb0bee822021-02-24 15:40:15 -080046 OStringStream oss; \
47 oss << message; \
48 assert(false && oss.str().c_str()); \
49 abort(); \
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050050 }
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040051
52#endif
53
Derek Sollenberger2eb3b4d2016-01-11 14:41:40 -050054#define JSON_ASSERT_MESSAGE(condition, message) \
Haibo Huangb0bee822021-02-24 15:40:15 -080055 do { \
56 if (!(condition)) { \
57 JSON_FAIL_MESSAGE(message); \
58 } \
59 } while (0)
Leon Scroggins IIIf59fb0e2014-05-28 15:19:42 -040060
Haibo Huangb0bee822021-02-24 15:40:15 -080061#endif // JSON_ASSERTIONS_H_INCLUDED