Rewrite and revert some code to build in pre-C++11

Rewerite and revert some code that this library can be built in pre-C++11 and C++11 env.
Main Change List:
1. using -> typedef
2. not using auto & decltype
3. not using raw string literals
4. ..., other c++11 features will be chosen to compile, depending on env.
This commit is contained in:
chenguoping
2020-04-30 11:24:36 +08:00
parent a4fb5db543
commit d2c205206d
22 changed files with 688 additions and 511 deletions

View File

@@ -13,13 +13,16 @@
// Conditional NORETURN attribute on the throw functions would:
// a) suppress false positives from static code analysis
// b) possibly improve optimization opportunities.
// For compatibility, [[noreturn]] is not used
#if !defined(JSONCPP_NORETURN)
#if defined(_MSC_VER) && _MSC_VER == 1800
#if defined(_MSC_VER)
#define JSONCPP_NORETURN __declspec(noreturn)
#elif defined(__GNUC__) || defined(__clang__)
#define JSONCPP_NORETURN __attribute__((noreturn))
#else
#define JSONCPP_NORETURN [[noreturn]]
#endif
#define JSONCPP_NORETURN
#endif
#endif // if !defined(JSONCPP_NORETURN)
// Support for '= delete' with template declarations was a late addition
// to the c++11 standard and is rejected by clang 3.8 and Apple clang 8.2
@@ -39,10 +42,15 @@
#endif
#endif
#include <array>
#if JSONCPP_CXX_STD_11
#else
#undef JSONCPP_TEMPLATE_DELETE
#define JSONCPP_TEMPLATE_DELETE
#include <string.h>
#endif
#include <exception>
#include <map>
#include <memory>
#include <string>
#include <vector>
@@ -67,8 +75,8 @@ namespace Json {
class JSON_API Exception : public std::exception {
public:
Exception(String msg);
~Exception() noexcept override;
char const* what() const noexcept override;
~Exception() JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
char const* what() const JSONCPP_NOEXCEPT JSONCPP_OVERRIDE;
protected:
String msg_;
@@ -146,7 +154,7 @@ enum PrecisionType {
*/
class JSON_API StaticString {
public:
explicit StaticString(const char* czstring) : c_str_(czstring) {}
JSONCPP_OP_EXPLICIT StaticString(const char* czstring) : c_str_(czstring) {}
operator const char*() const { return c_str_; }
@@ -194,21 +202,21 @@ class JSON_API Value {
friend class ValueIteratorBase;
public:
using Members = std::vector<String>;
using iterator = ValueIterator;
using const_iterator = ValueConstIterator;
using UInt = Json::UInt;
using Int = Json::Int;
typedef std::vector<String> Members;
typedef ValueIterator iterator;
typedef ValueConstIterator const_iterator;
typedef Json::UInt UInt;
typedef Json::Int Int;
#if defined(JSON_HAS_INT64)
using UInt64 = Json::UInt64;
using Int64 = Json::Int64;
typedef Json::UInt64 UInt64;
typedef Json::Int64 Int64;
#endif // defined(JSON_HAS_INT64)
using LargestInt = Json::LargestInt;
using LargestUInt = Json::LargestUInt;
using ArrayIndex = Json::ArrayIndex;
typedef Json::LargestInt LargestInt;
typedef Json::LargestUInt LargestUInt;
typedef Json::ArrayIndex ArrayIndex;
// Required for boost integration, e. g. BOOST_TEST
using value_type = std::string;
typedef std::string value_type;
#if JSON_USE_NULLREF
// Binary compatibility kludges, do not use.
@@ -220,34 +228,35 @@ public:
static Value const& nullSingleton();
/// Minimum signed integer value that can be stored in a Json::Value.
static constexpr LargestInt minLargestInt =
static JSONCPP_CONST LargestInt minLargestInt =
LargestInt(~(LargestUInt(-1) / 2));
/// Maximum signed integer value that can be stored in a Json::Value.
static constexpr LargestInt maxLargestInt = LargestInt(LargestUInt(-1) / 2);
static JSONCPP_CONST LargestInt maxLargestInt =
LargestInt(LargestUInt(-1) / 2);
/// Maximum unsigned integer value that can be stored in a Json::Value.
static constexpr LargestUInt maxLargestUInt = LargestUInt(-1);
static JSONCPP_CONST LargestUInt maxLargestUInt = LargestUInt(-1);
/// Minimum signed int value that can be stored in a Json::Value.
static constexpr Int minInt = Int(~(UInt(-1) / 2));
static JSONCPP_CONST Int minInt = Int(~(UInt(-1) / 2));
/// Maximum signed int value that can be stored in a Json::Value.
static constexpr Int maxInt = Int(UInt(-1) / 2);
static JSONCPP_CONST Int maxInt = Int(UInt(-1) / 2);
/// Maximum unsigned int value that can be stored in a Json::Value.
static constexpr UInt maxUInt = UInt(-1);
static JSONCPP_CONST UInt maxUInt = UInt(-1);
#if defined(JSON_HAS_INT64)
/// Minimum signed 64 bits int value that can be stored in a Json::Value.
static constexpr Int64 minInt64 = Int64(~(UInt64(-1) / 2));
static JSONCPP_CONST Int64 minInt64 = Int64(~(UInt64(-1) / 2));
/// Maximum signed 64 bits int value that can be stored in a Json::Value.
static constexpr Int64 maxInt64 = Int64(UInt64(-1) / 2);
static JSONCPP_CONST Int64 maxInt64 = Int64(UInt64(-1) / 2);
/// Maximum unsigned 64 bits int value that can be stored in a Json::Value.
static constexpr UInt64 maxUInt64 = UInt64(-1);
static JSONCPP_CONST UInt64 maxUInt64 = UInt64(-1);
#endif // defined(JSON_HAS_INT64)
/// Default precision for real value for string representation.
static constexpr UInt defaultRealPrecision = 17;
static JSONCPP_CONST UInt defaultRealPrecision = 17;
// The constant is hard-coded because some compiler have trouble
// converting Value::maxUInt64 to a double correctly (AIX/xlC).
// Assumes that UInt64 is a 64 bits integer.
static constexpr double maxUInt64AsDouble = 18446744073709551615.0;
static JSONCPP_CONST double maxUInt64AsDouble = 18446744073709551615.0;
// Workaround for bug in the NVIDIAs CUDA 9.1 nvcc compiler
// when using gcc and clang backend compilers. CZString
// cannot be defined as private. See issue #486
@@ -263,11 +272,14 @@ private:
CZString(ArrayIndex index);
CZString(char const* str, unsigned length, DuplicationPolicy allocate);
CZString(CZString const& other);
#if JSONCPP_CXX_STD_11
CZString(CZString&& other);
#endif
~CZString();
CZString& operator=(const CZString& other);
#if JSONCPP_CXX_STD_11
CZString& operator=(CZString&& other);
#endif
bool operator<(CZString const& other) const;
bool operator==(CZString const& other) const;
ArrayIndex index() const;
@@ -343,13 +355,17 @@ public:
Value(const String& value);
Value(bool value);
Value(const Value& other);
#if JSONCPP_CXX_STD_11
Value(Value&& other);
#endif
~Value();
/// \note Overwrite existing comments. To preserve comments, use
/// #swapPayload().
Value& operator=(const Value& other);
#if JSONCPP_CXX_STD_11
Value& operator=(Value&& other);
#endif
/// Swap everything.
void swap(Value& other);
@@ -421,7 +437,7 @@ public:
bool empty() const;
/// Return !isNull()
explicit operator bool() const;
JSONCPP_OP_EXPLICIT operator bool() const;
/// Remove all object members and array elements.
/// \pre type() is arrayValue, objectValue, or nullValue
@@ -462,11 +478,15 @@ public:
///
/// Equivalent to jsonvalue[jsonvalue.size()] = value;
Value& append(const Value& value);
#if JSONCPP_CXX_STD_11
Value& append(Value&& value);
#endif
/// \brief Insert value in array at specific index
bool insert(ArrayIndex index, const Value& newValue);
#if JSONCPP_CXX_STD_11
bool insert(ArrayIndex index, Value&& newValue);
#endif
/// Access an object value by name, create a null member if it does not exist.
/// \note Because of our implementation, keys are limited to 2^30 -1 chars.
@@ -562,15 +582,11 @@ public:
/// \deprecated Always pass len.
JSONCPP_DEPRECATED("Use setComment(String const&) instead.")
void setComment(const char* comment, CommentPlacement placement) {
setComment(String(comment, strlen(comment)), placement);
}
void setComment(const char* comment, CommentPlacement placement);
/// Comments must be //... or /* ... */
void setComment(const char* comment, size_t len, CommentPlacement placement) {
setComment(String(comment, len), placement);
}
void setComment(const char* comment, size_t len, CommentPlacement placement);
/// Comments must be //... or /* ... */
void setComment(String comment, CommentPlacement placement);
void setComment(const String& comment, CommentPlacement placement);
bool hasComment(CommentPlacement placement) const;
/// Include delimiters and embedded newlines.
String getComment(CommentPlacement placement) const;
@@ -632,18 +648,15 @@ private:
class Comments {
public:
Comments() = default;
Comments() {}
Comments(const Comments& that);
Comments(Comments&& that);
Comments& operator=(const Comments& that);
Comments& operator=(Comments&& that);
bool has(CommentPlacement slot) const;
String get(CommentPlacement slot) const;
void set(CommentPlacement slot, String comment);
void set(CommentPlacement slot, String s);
private:
using Array = std::array<String, numberOfCommentPlacement>;
std::unique_ptr<Array> ptr_;
String ptr_[numberOfCommentPlacement];
};
Comments comments_;
@@ -698,8 +711,8 @@ public:
private:
enum Kind { kindNone = 0, kindIndex, kindKey };
String key_;
ArrayIndex index_{};
Kind kind_{kindNone};
ArrayIndex index_;
Kind kind_;
};
/** \brief Experimental and untested: represents a "path" to access a node.
@@ -728,8 +741,8 @@ public:
Value& make(Value& root) const;
private:
using InArgs = std::vector<const PathArgument*>;
using Args = std::vector<PathArgument>;
typedef std::vector<const PathArgument*> InArgs;
typedef std::vector<PathArgument> Args;
void makePath(const String& path, const InArgs& in);
void addPathInArg(const String& path, const InArgs& in,
@@ -744,10 +757,10 @@ private:
*/
class JSON_API ValueIteratorBase {
public:
using iterator_category = std::bidirectional_iterator_tag;
using size_t = unsigned int;
using difference_type = int;
using SelfType = ValueIteratorBase;
typedef std::bidirectional_iterator_tag iterator_category;
typedef unsigned int size_t;
typedef int difference_type;
typedef ValueIteratorBase SelfType;
bool operator==(const SelfType& other) const { return isEqual(other); }
@@ -804,13 +817,14 @@ protected:
private:
Value::ObjectValues::iterator current_;
// Indicates that iterator is for a null value.
bool isNull_{true};
bool isNull_;
public:
// For some reason, BORLAND needs these at the end, rather
// than earlier. No idea why.
ValueIteratorBase();
explicit ValueIteratorBase(const Value::ObjectValues::iterator& current);
JSONCPP_OP_EXPLICIT
ValueIteratorBase(const Value::ObjectValues::iterator& current);
};
/** \brief const iterator for object and array value.
@@ -820,12 +834,12 @@ class JSON_API ValueConstIterator : public ValueIteratorBase {
friend class Value;
public:
using value_type = const Value;
typedef const Value value_type;
// typedef unsigned int size_t;
// typedef int difference_type;
using reference = const Value&;
using pointer = const Value*;
using SelfType = ValueConstIterator;
typedef const Value& reference;
typedef const Value* pointer;
typedef ValueConstIterator SelfType;
ValueConstIterator();
ValueConstIterator(ValueIterator const& other);
@@ -833,7 +847,8 @@ public:
private:
/*! \internal Use by Value to create an iterator.
*/
explicit ValueConstIterator(const Value::ObjectValues::iterator& current);
JSONCPP_OP_EXPLICIT
ValueConstIterator(const Value::ObjectValues::iterator& current);
public:
SelfType& operator=(const ValueIteratorBase& other);
@@ -871,21 +886,22 @@ class JSON_API ValueIterator : public ValueIteratorBase {
friend class Value;
public:
using value_type = Value;
using size_t = unsigned int;
using difference_type = int;
using reference = Value&;
using pointer = Value*;
using SelfType = ValueIterator;
typedef Value value_type;
typedef unsigned int size_t;
typedef int difference_type;
typedef Value& reference;
typedef Value* pointer;
typedef ValueIterator SelfType;
ValueIterator();
explicit ValueIterator(const ValueConstIterator& other);
JSONCPP_OP_EXPLICIT ValueIterator(const ValueConstIterator& other);
ValueIterator(const ValueIterator& other);
private:
/*! \internal Use by Value to create an iterator.
*/
explicit ValueIterator(const Value::ObjectValues::iterator& current);
JSONCPP_OP_EXPLICIT
ValueIterator(const Value::ObjectValues::iterator& current);
public:
SelfType& operator=(const SelfType& other);