Skip to content

Commit e3d0eca

Browse files
committed
Centralized assertion macros and made them obey JSON_USE_EXCEPTION.
1 parent a77a803 commit e3d0eca

4 files changed

Lines changed: 28 additions & 9 deletions

File tree

include/json/assertions.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
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
7+
# define CPPTL_JSON_ASSERTIONS_H_INCLUDED
8+
9+
#if !defined(JSON_IS_AMALGAMATION)
10+
# include <json/config.h>
11+
#endif // if !defined(JSON_IS_AMALGAMATION)
12+
13+
#if defined(JSON_USE_EXCEPTION)
14+
#define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw
15+
#define JSON_FAIL_MESSAGE( message ) throw std::runtime_error( message );
16+
#else // defined(JSON_USE_EXCEPTION)
17+
#define JSON_ASSERT( condition ) assert( condition );
18+
#define JSON_FAIL_MESSAGE( message ) { std::cerr << message; exit(123); }
19+
#endif
20+
21+
#define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) { JSON_FAIL_MESSAGE( message ) }
22+
23+
#endif // CPPTL_JSON_ASSERTIONS_H_INCLUDED

src/lib_json/json_internalarray.inl

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ public: // overridden from ValueArrayAllocator
5353
if ( minNewIndexCount > newIndexCount )
5454
newIndexCount = minNewIndexCount;
5555
void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount );
56-
if ( !newIndexes )
57-
throw std::bad_alloc();
56+
JSON_ASSERT_MESSAGE(newIndexes, "Couldn't realloc.");
5857
indexCount = newIndexCount;
5958
indexes = static_cast<Value **>( newIndexes );
6059
}
@@ -117,8 +116,7 @@ public: // overridden from ValueArrayAllocator
117116
if ( minNewIndexCount > newIndexCount )
118117
newIndexCount = minNewIndexCount;
119118
void *newIndexes = realloc( indexes, sizeof(Value*) * newIndexCount );
120-
if ( !newIndexes )
121-
throw std::bad_alloc();
119+
JSON_ASSERT_MESSAGE(newIndexes, "Couldn't realloc.");
122120
indexCount = newIndexCount;
123121
indexes = static_cast<Value **>( newIndexes );
124122
}

src/lib_json/json_reader.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
55

66
#if !defined(JSON_IS_AMALGAMATION)
7+
# include <json/assertions.h>
78
# include <json/reader.h>
89
# include <json/value.h>
910
# include "json_tool.h"
@@ -884,8 +885,7 @@ std::istream& operator>>( std::istream &sin, Value &root )
884885
{
885886
Json::Reader reader;
886887
bool ok = reader.parse(sin, root, true);
887-
//JSON_ASSERT( ok );
888-
if (!ok) throw std::runtime_error(reader.getFormattedErrorMessages());
888+
if (!ok) JSON_FAIL_MESSAGE(reader.getFormattedErrorMessages());
889889
return sin;
890890
}
891891

src/lib_json/json_value.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// See file LICENSE for detail or copy at http://jsoncpp.sourceforge.net/LICENSE
55

66
#if !defined(JSON_IS_AMALGAMATION)
7+
# include <json/assertions.h>
78
# include <json/value.h>
89
# include <json/writer.h>
910
# ifndef JSON_USE_SIMPLE_INTERNAL_ALLOCATOR
@@ -21,9 +22,6 @@
2122
#include <cstddef> // size_t
2223

2324
#define JSON_ASSERT_UNREACHABLE assert( false )
24-
#define JSON_ASSERT( condition ) assert( condition ); // @todo <= change this into an exception throw
25-
#define JSON_FAIL_MESSAGE( message ) throw std::runtime_error( message );
26-
#define JSON_ASSERT_MESSAGE( condition, message ) if (!( condition )) JSON_FAIL_MESSAGE( message )
2725

2826
namespace Json {
2927

0 commit comments

Comments
 (0)