Skip to content

Commit 1c2ed7a

Browse files
committed
convert JSONCPP_STRING etc from macros to typedefs
1 parent 6e7cbf8 commit 1c2ed7a

File tree

14 files changed

+416
-444
lines changed

14 files changed

+416
-444
lines changed

Diff for: include/json/assertions.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929

3030
#define JSON_FAIL_MESSAGE(message) \
3131
{ \
32-
JSONCPP_OSTRINGSTREAM oss; \
32+
OStringStream oss; \
3333
oss << message; \
3434
Json::throwLogicError(oss.str()); \
3535
abort(); \
@@ -43,7 +43,7 @@
4343
// release builds we abort, for a core-dump or debugger.
4444
#define JSON_FAIL_MESSAGE(message) \
4545
{ \
46-
JSONCPP_OSTRINGSTREAM oss; \
46+
OStringStream oss; \
4747
oss << message; \
4848
assert(false && oss.str().c_str()); \
4949
abort(); \

Diff for: include/json/config.h

+31-25
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,13 @@
66
#ifndef JSON_CONFIG_H_INCLUDED
77
#define JSON_CONFIG_H_INCLUDED
88
#include <cstddef>
9-
#include <cstdint> //typedef int64_t, uint64_t
10-
#include <string> //typedef String
9+
#include <cstdint>
10+
#include <istream>
11+
#include <memory>
12+
#include <ostream>
13+
#include <sstream>
14+
#include <string>
15+
#include <type_traits>
1116

1217
/// If defined, indicates that json library is embedded in CppTL library.
1318
//# define JSON_IN_CPPTL 1
@@ -142,12 +147,9 @@ msvc_pre1900_c99_snprintf(char* outBuf, size_t size, const char* format, ...);
142147

143148
#if !defined(JSON_IS_AMALGAMATION)
144149

150+
#include "allocator.h"
145151
#include "version.h"
146152

147-
#if JSONCPP_USING_SECURE_MEMORY
148-
#include "allocator.h" //typedef Allocator
149-
#endif
150-
151153
#endif // if !defined(JSON_IS_AMALGAMATION)
152154

153155
namespace Json {
@@ -170,24 +172,28 @@ typedef Int64 LargestInt;
170172
typedef UInt64 LargestUInt;
171173
#define JSON_HAS_INT64
172174
#endif // if defined(JSON_NO_INT64)
173-
#if JSONCPP_USING_SECURE_MEMORY
174-
#define JSONCPP_STRING \
175-
std::basic_string<char, std::char_traits<char>, Json::SecureAllocator<char> >
176-
#define JSONCPP_OSTRINGSTREAM \
177-
std::basic_ostringstream<char, std::char_traits<char>, \
178-
Json::SecureAllocator<char> >
179-
#define JSONCPP_OSTREAM std::basic_ostream<char, std::char_traits<char> >
180-
#define JSONCPP_ISTRINGSTREAM \
181-
std::basic_istringstream<char, std::char_traits<char>, \
182-
Json::SecureAllocator<char> >
183-
#define JSONCPP_ISTREAM std::istream
184-
#else
185-
#define JSONCPP_STRING std::string
186-
#define JSONCPP_OSTRINGSTREAM std::ostringstream
187-
#define JSONCPP_OSTREAM std::ostream
188-
#define JSONCPP_ISTRINGSTREAM std::istringstream
189-
#define JSONCPP_ISTREAM std::istream
190-
#endif // if JSONCPP_USING_SECURE_MEMORY
191-
} // end namespace Json
175+
176+
template <typename T>
177+
using Allocator = typename std::conditional<JSONCPP_USING_SECURE_MEMORY,
178+
SecureAllocator<T>,
179+
std::allocator<T> >::type;
180+
using String =
181+
std::basic_string<char, std::char_traits<char>, Allocator<char> >;
182+
using IStringStream = std::basic_istringstream<String::value_type,
183+
String::traits_type,
184+
String::allocator_type>;
185+
using OStringStream = std::basic_ostringstream<String::value_type,
186+
String::traits_type,
187+
String::allocator_type>;
188+
using IStream = std::istream;
189+
using OStream = std::ostream;
190+
} // namespace Json
191+
192+
// Legacy names (formerly macros).
193+
using JSONCPP_STRING = Json::String;
194+
using JSONCPP_ISTRINGSTREAM = Json::IStringStream;
195+
using JSONCPP_OSTRINGSTREAM = Json::OStringStream;
196+
using JSONCPP_ISTREAM = Json::IStream;
197+
using JSONCPP_OSTREAM = Json::OStream;
192198

193199
#endif // JSON_CONFIG_H_INCLUDED

Diff for: include/json/reader.h

+19-23
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class JSON_API Reader {
4646
struct StructuredError {
4747
ptrdiff_t offset_start;
4848
ptrdiff_t offset_limit;
49-
JSONCPP_STRING message;
49+
String message;
5050
};
5151

5252
/** \brief Constructs a Reader allowing all features
@@ -103,7 +103,7 @@ class JSON_API Reader {
103103

104104
/// \brief Parse from input stream.
105105
/// \see Json::operator>>(std::istream&, Json::Value&).
106-
bool parse(JSONCPP_ISTREAM& is, Value& root, bool collectComments = true);
106+
bool parse(IStream& is, Value& root, bool collectComments = true);
107107

108108
/** \brief Returns a user friendly string that list errors in the parsed
109109
* document.
@@ -115,7 +115,7 @@ class JSON_API Reader {
115115
* \deprecated Use getFormattedErrorMessages() instead (typo fix).
116116
*/
117117
JSONCPP_DEPRECATED("Use getFormattedErrorMessages() instead.")
118-
JSONCPP_STRING getFormatedErrorMessages() const;
118+
String getFormatedErrorMessages() const;
119119

120120
/** \brief Returns a user friendly string that list errors in the parsed
121121
* document.
@@ -125,7 +125,7 @@ class JSON_API Reader {
125125
* occurred
126126
* during parsing.
127127
*/
128-
JSONCPP_STRING getFormattedErrorMessages() const;
128+
String getFormattedErrorMessages() const;
129129

130130
/** \brief Returns a vector of structured erros encounted while parsing.
131131
* \return A (possibly empty) vector of StructuredError objects. Currently
@@ -142,7 +142,7 @@ class JSON_API Reader {
142142
* \return \c true if the error was successfully added, \c false if the
143143
* Value offset exceeds the document size.
144144
*/
145-
bool pushError(const Value& value, const JSONCPP_STRING& message);
145+
bool pushError(const Value& value, const String& message);
146146

147147
/** \brief Add a semantic error message with extra context.
148148
* \param value JSON Value location associated with the error
@@ -151,9 +151,7 @@ class JSON_API Reader {
151151
* \return \c true if the error was successfully added, \c false if either
152152
* Value offset exceeds the document size.
153153
*/
154-
bool pushError(const Value& value,
155-
const JSONCPP_STRING& message,
156-
const Value& extra);
154+
bool pushError(const Value& value, const String& message, const Value& extra);
157155

158156
/** \brief Return whether there are any errors.
159157
* \return \c true if there are no errors to report \c false if
@@ -189,7 +187,7 @@ class JSON_API Reader {
189187
class ErrorInfo {
190188
public:
191189
Token token_;
192-
JSONCPP_STRING message_;
190+
String message_;
193191
Location extra_;
194192
};
195193

@@ -209,7 +207,7 @@ class JSON_API Reader {
209207
bool decodeNumber(Token& token);
210208
bool decodeNumber(Token& token, Value& decoded);
211209
bool decodeString(Token& token);
212-
bool decodeString(Token& token, JSONCPP_STRING& decoded);
210+
bool decodeString(Token& token, String& decoded);
213211
bool decodeDouble(Token& token);
214212
bool decodeDouble(Token& token, Value& decoded);
215213
bool decodeUnicodeCodePoint(Token& token,
@@ -220,35 +218,33 @@ class JSON_API Reader {
220218
Location& current,
221219
Location end,
222220
unsigned int& unicode);
223-
bool addError(const JSONCPP_STRING& message,
224-
Token& token,
225-
Location extra = nullptr);
221+
bool addError(const String& message, Token& token, Location extra = nullptr);
226222
bool recoverFromError(TokenType skipUntilToken);
227-
bool addErrorAndRecover(const JSONCPP_STRING& message,
223+
bool addErrorAndRecover(const String& message,
228224
Token& token,
229225
TokenType skipUntilToken);
230226
void skipUntilSpace();
231227
Value& currentValue();
232228
Char getNextChar();
233229
void
234230
getLocationLineAndColumn(Location location, int& line, int& column) const;
235-
JSONCPP_STRING getLocationLineAndColumn(Location location) const;
231+
String getLocationLineAndColumn(Location location) const;
236232
void addComment(Location begin, Location end, CommentPlacement placement);
237233
void skipCommentTokens(Token& token);
238234

239235
static bool containsNewLine(Location begin, Location end);
240-
static JSONCPP_STRING normalizeEOL(Location begin, Location end);
236+
static String normalizeEOL(Location begin, Location end);
241237

242238
typedef std::stack<Value*> Nodes;
243239
Nodes nodes_;
244240
Errors errors_;
245-
JSONCPP_STRING document_;
241+
String document_;
246242
Location begin_{};
247243
Location end_{};
248244
Location current_{};
249245
Location lastValueEnd_{};
250246
Value* lastValue_{};
251-
JSONCPP_STRING commentsBefore_;
247+
String commentsBefore_;
252248
Features features_;
253249
bool collectComments_{};
254250
}; // Reader
@@ -279,7 +275,7 @@ class JSON_API CharReader {
279275
virtual bool parse(char const* beginDoc,
280276
char const* endDoc,
281277
Value* root,
282-
JSONCPP_STRING* errs) = 0;
278+
String* errs) = 0;
283279

284280
class JSON_API Factory {
285281
public:
@@ -299,7 +295,7 @@ class JSON_API CharReader {
299295
CharReaderBuilder builder;
300296
builder["collectComments"] = false;
301297
Value value;
302-
JSONCPP_STRING errs;
298+
String errs;
303299
bool ok = parseFromStream(builder, std::cin, &value, &errs);
304300
\endcode
305301
*/
@@ -359,7 +355,7 @@ class JSON_API CharReaderBuilder : public CharReader::Factory {
359355

360356
/** A simple way to update a specific setting.
361357
*/
362-
Value& operator[](const JSONCPP_STRING& key);
358+
Value& operator[](const String& key);
363359

364360
/** Called by ctor, but you can use this to reset settings_.
365361
* \pre 'settings' != NULL (but Json::null is fine)
@@ -380,7 +376,7 @@ class JSON_API CharReaderBuilder : public CharReader::Factory {
380376
* is convenient.
381377
*/
382378
bool JSON_API parseFromStream(CharReader::Factory const&,
383-
JSONCPP_ISTREAM&,
379+
IStream&,
384380
Value* root,
385381
std::string* errs);
386382

@@ -408,7 +404,7 @@ bool JSON_API parseFromStream(CharReader::Factory const&,
408404
\throw std::exception on parse error.
409405
\see Json::operator<<()
410406
*/
411-
JSON_API JSONCPP_ISTREAM& operator>>(JSONCPP_ISTREAM&, Value&);
407+
JSON_API IStream& operator>>(IStream&, Value&);
412408

413409
} // namespace Json
414410

0 commit comments

Comments
 (0)