blob: 20716b0674ba7877bffccd4098b9a55861280a7e [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
Hans Johnsone50bfef2018-12-12 13:34:37 -06009#include <cstdlib>
Billy Donahuedc4a7f92019-01-17 11:07:53 -050010#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
Billy Donahueb5e1fe82018-05-20 16:55:27 -040023#define JSON_ASSERT(condition) \
Christopher Dunnb3e6f3d2015-03-05 15:19:43 -060024 { \
Billy Donahueb5e1fe82018-05-20 16:55:27 -040025 if (!(condition)) { \
26 Json::throwLogicError("assert json failed"); \
27 } \
28 }
29
30#define JSON_FAIL_MESSAGE(message) \
31 { \
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050032 OStringStream oss; \
Billy Donahueb5e1fe82018-05-20 16:55:27 -040033 oss << message; \
Christopher Dunn93763682015-03-08 12:39:27 -050034 Json::throwLogicError(oss.str()); \
35 abort(); \
Christopher Dunnb3e6f3d2015-03-05 15:19:43 -060036 }
Christopher Dunn93763682015-03-08 12:39:27 -050037
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100038#else // JSON_USE_EXCEPTION
Christopher Dunn93763682015-03-08 12:39:27 -050039
Billy Donahueb5e1fe82018-05-20 16:55:27 -040040#define JSON_ASSERT(condition) assert(condition)
Aaron Jacobs2b853c42011-12-22 03:18:24 +000041
42// The call to assert() will show the failure message in debug builds. In
Christopher Dunn717b0862015-03-08 12:05:28 -050043// release builds we abort, for a core-dump or debugger.
Billy Donahueb5e1fe82018-05-20 16:55:27 -040044#define JSON_FAIL_MESSAGE(message) \
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100045 { \
Billy Donahue1c2ed7a2019-01-17 16:35:29 -050046 OStringStream oss; \
Billy Donahueb5e1fe82018-05-20 16:55:27 -040047 oss << message; \
Christopher Dunn7956ccd2015-01-20 16:18:15 -060048 assert(false && oss.str().c_str()); \
49 abort(); \
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100050 }
Aaron Jacobs2b853c42011-12-22 03:18:24 +000051
Aaron Jacobse3d0eca2011-05-24 01:03:22 +000052#endif
53
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100054#define JSON_ASSERT_MESSAGE(condition, message) \
55 if (!(condition)) { \
Christopher Dunn7956ccd2015-01-20 16:18:15 -060056 JSON_FAIL_MESSAGE(message); \
Aaron Jacobs9fa4e842014-07-01 08:48:54 +100057 }
Aaron Jacobse3d0eca2011-05-24 01:03:22 +000058
59#endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED