From 1320ba77a768cfb495d99e97eed6ed83aead9d74 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sat, 1 Feb 2014 17:50:46 +0100 Subject: [PATCH 1/8] drop trailing commas in enums In C++'98/03, trailing commas in enumerations are not allowed, but have been introduced in C++11. This patch drops the trailing commas in order to avoid compiler warnings (e.g. GCC with -pedantic). Fixes http://code.google.com/p/rapidjson/issues/detail?id=49. --- include/rapidjson/encodings.h | 2 +- include/rapidjson/rapidjson.h | 2 +- include/rapidjson/reader.h | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/include/rapidjson/encodings.h b/include/rapidjson/encodings.h index c7e8e5b..974798e 100644 --- a/include/rapidjson/encodings.h +++ b/include/rapidjson/encodings.h @@ -448,7 +448,7 @@ enum UTFType { kUTF16LE = 1, //!< UTF-16 little endian. kUTF16BE = 2, //!< UTF-16 big endian. kUTF32LE = 3, //!< UTF-32 little endian. - kUTF32BE = 4, //!< UTF-32 big endian. + kUTF32BE = 4 //!< UTF-32 big endian. }; //! Dynamically select encoding according to stream's runtime-specified UTF encoding type. diff --git a/include/rapidjson/rapidjson.h b/include/rapidjson/rapidjson.h index 2ba3c58..156f6d9 100644 --- a/include/rapidjson/rapidjson.h +++ b/include/rapidjson/rapidjson.h @@ -248,7 +248,7 @@ enum Type { kObjectType = 3, //!< object kArrayType = 4, //!< array kStringType = 5, //!< string - kNumberType = 6, //!< number + kNumberType = 6 //!< number }; } // namespace rapidjson diff --git a/include/rapidjson/reader.h b/include/rapidjson/reader.h index c932dea..7d3befa 100644 --- a/include/rapidjson/reader.h +++ b/include/rapidjson/reader.h @@ -39,7 +39,7 @@ namespace rapidjson { enum ParseFlag { kParseDefaultFlags = 0, //!< Default parse flags. Non-destructive parsing. Text strings are decoded into allocated buffer. kParseInsituFlag = 1, //!< In-situ(destructive) parsing. - kParseValidateEncodingFlag = 2, //!< Validate encoding of JSON strings. + kParseValidateEncodingFlag = 2 //!< Validate encoding of JSON strings. }; /////////////////////////////////////////////////////////////////////////////// From d4ff956b2d58e4aa3f7fae0419b9597c7cc7f72f Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sat, 1 Feb 2014 19:50:56 +0100 Subject: [PATCH 2/8] GenericValue: fixup construction from NumberType Constructing an empty GenericValue with a specific Type has failed for any kNumberType (Int, Int64, Double...) due to incomplete definition of the corresponding default flag value. This patch adds a new constant kNumberAnyFlag to the flags enumeration in GenericValue to cover this case. This fixes http://code.google.com/p/rapidjson/issues/detail?id=57 --- include/rapidjson/document.h | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index a41288e..3837889 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -63,7 +63,7 @@ public: GenericValue(Type type) { static const unsigned defaultFlags[7] = { kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kConstStringFlag, - kNumberFlag | kIntFlag | kUintFlag | kInt64Flag | kUint64Flag | kDoubleFlag + kNumberAnyFlag }; RAPIDJSON_ASSERT(type <= kNumberType); flags_ = defaultFlags[type]; @@ -579,6 +579,7 @@ private: kNumberInt64Flag = kNumberType | kNumberFlag | kInt64Flag, kNumberUint64Flag = kNumberType | kNumberFlag | kUint64Flag, kNumberDoubleFlag = kNumberType | kNumberFlag | kDoubleFlag, + kNumberAnyFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag, kConstStringFlag = kStringType | kStringFlag, kCopyStringFlag = kStringType | kStringFlag | kCopyFlag, kObjectFlag = kObjectType, From e1a97561bad7f1bbe9052737f3ec21b1ded4d53d Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sat, 1 Feb 2014 18:00:55 +0100 Subject: [PATCH 3/8] GenericValue: explicit constructors In case of overloaded functions taking either a GenericValue or another class that can also be constructed from the same primitive types (e.g. std::string, which can be constructed from const char*), the overloading becomes ambiguous: void foo( const std::string& ); void foo( const rapidjson::Value & ); Declaring the GenericValue constructors taking primitive types as 'explicit' avoids this problem. This should not have any negative side-effects, since a GenericValue can't be copied or implicitly converted to other types. Fixes http://code.google.com/p/rapidjson/issues/detail?id=70. --- include/rapidjson/document.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index a41288e..0a8904a 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -71,24 +71,24 @@ public: } //! Constructor for boolean value. - GenericValue(bool b) : flags_(b ? kTrueFlag : kFalseFlag) {} + explicit GenericValue(bool b) : flags_(b ? kTrueFlag : kFalseFlag) {} //! Constructor for int value. - GenericValue(int i) : flags_(kNumberIntFlag) { + explicit GenericValue(int i) : flags_(kNumberIntFlag) { data_.n.i64 = i; if (i >= 0) flags_ |= kUintFlag | kUint64Flag; } //! Constructor for unsigned value. - GenericValue(unsigned u) : flags_(kNumberUintFlag) { + explicit GenericValue(unsigned u) : flags_(kNumberUintFlag) { data_.n.u64 = u; if (!(u & 0x80000000)) flags_ |= kIntFlag | kInt64Flag; } //! Constructor for int64_t value. - GenericValue(int64_t i64) : flags_(kNumberInt64Flag) { + explicit GenericValue(int64_t i64) : flags_(kNumberInt64Flag) { data_.n.i64 = i64; if (i64 >= 0) { flags_ |= kNumberUint64Flag; @@ -102,7 +102,7 @@ public: } //! Constructor for uint64_t value. - GenericValue(uint64_t u64) : flags_(kNumberUint64Flag) { + explicit GenericValue(uint64_t u64) : flags_(kNumberUint64Flag) { data_.n.u64 = u64; if (!(u64 & 0x8000000000000000ULL)) flags_ |= kInt64Flag; @@ -113,7 +113,7 @@ public: } //! Constructor for double value. - GenericValue(double d) : flags_(kNumberDoubleFlag) { data_.n.d = d; } + explicit GenericValue(double d) : flags_(kNumberDoubleFlag) { data_.n.d = d; } //! Constructor for constant string (i.e. do not make a copy of string) GenericValue(const Ch* s, SizeType length) { @@ -124,7 +124,7 @@ public: } //! Constructor for constant string (i.e. do not make a copy of string) - GenericValue(const Ch* s) { SetStringRaw(s, internal::StrLen(s)); } + explicit GenericValue(const Ch* s) { SetStringRaw(s, internal::StrLen(s)); } //! Constructor for copy-string (i.e. do make a copy of string) GenericValue(const Ch* s, SizeType length, Allocator& allocator) { SetStringRaw(s, length, allocator); } From 37b3acf40c2bcad29f7564a8416a17d6de2b9134 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sat, 1 Feb 2014 18:09:35 +0100 Subject: [PATCH 4/8] GenericDocument: forward allocator to GenericReader Fixes http://code.google.com/p/rapidjson/issues/detail?id=72. --- include/rapidjson/document.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index a41288e..7872460 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -719,7 +719,7 @@ public: template GenericDocument& ParseStream(InputStream& is) { ValueType::SetNull(); // Remove existing root if exist - GenericReader reader; + GenericReader reader(&GetAllocator()); if (reader.template Parse(is, *this)) { RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object this->RawAssign(*stack_.template Pop(1)); // Add this-> to prevent issue 13. From 7fb82c252883e9800ccb5f7fce92aee831092fc7 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Fri, 21 Mar 2014 16:17:43 +0100 Subject: [PATCH 5/8] GenericDocument::ParseStream: make SourceEncoding optional The ParseStream() function current requires explicitly passing the SourceEncoding parameter as explicit template argument while the other Parse*() functions provide overloads to omit this parameter and use the document's own encoding by default. This patch adds the corresponding overload for ParseStream(), enabling the simple usage again: rapidjson::FileStream is(fp); rapidjson::Document d; d.ParseStream<0>(is); --- include/rapidjson/document.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index a41288e..a150f36 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -734,6 +734,11 @@ public: return *this; } + template + GenericDocument& ParseStream(InputStream& is) { + return ParseStream(is); + } + //! Parse JSON text from a mutable string. /*! \tparam parseFlags Combination of ParseFlag. \param str Mutable zero-terminated string to be parsed. From 72de00f672c7661a956cd6bf69fb1f851742b381 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sat, 1 Feb 2014 18:02:35 +0100 Subject: [PATCH 6/8] GenericValue/GenericDocument: fix alloctaor/Alloactor typos This patch fixes some misspellings of "allocator" in document.h. Fixes the Doxygen documentation of GenericDocument as well. --- include/rapidjson/document.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/rapidjson/document.h b/include/rapidjson/document.h index a41288e..b7d5af9 100644 --- a/include/rapidjson/document.h +++ b/include/rapidjson/document.h @@ -642,17 +642,17 @@ private: }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode // Initialize this value as array with initial data, without calling destructor. - void SetArrayRaw(GenericValue* values, SizeType count, Allocator& alloctaor) { + void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) { flags_ = kArrayFlag; - data_.a.elements = (GenericValue*)alloctaor.Malloc(count * sizeof(GenericValue)); + data_.a.elements = (GenericValue*)allocator.Malloc(count * sizeof(GenericValue)); memcpy(data_.a.elements, values, count * sizeof(GenericValue)); data_.a.size = data_.a.capacity = count; } //! Initialize this value as object with initial data, without calling destructor. - void SetObjectRaw(Member* members, SizeType count, Allocator& alloctaor) { + void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) { flags_ = kObjectFlag; - data_.o.members = (Member*)alloctaor.Malloc(count * sizeof(Member)); + data_.o.members = (Member*)allocator.Malloc(count * sizeof(Member)); memcpy(data_.o.members, members, count * sizeof(Member)); data_.o.size = data_.o.capacity = count; } @@ -696,7 +696,7 @@ typedef GenericValue > Value; /*! \implements Handler \tparam Encoding encoding for both parsing and string storage. - \tparam Alloactor allocator for allocating memory for the DOM, and the stack during parsing. + \tparam Allocator allocator for allocating memory for the DOM, and the stack during parsing. */ template > class GenericDocument : public GenericValue { From 4b32a3059368d09948d219deb41023d30c3f8c47 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sat, 1 Feb 2014 18:05:12 +0100 Subject: [PATCH 7/8] MemoryPoolAllocator: prohibit copying and assignment The MemoryPoolAllocator implementation cannot and should not be copied (Rule of Three). Declare copy constructor and copy-assignment operator as private. --- include/rapidjson/allocators.h | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/include/rapidjson/allocators.h b/include/rapidjson/allocators.h index 6dfa85a..ad5f3cb 100644 --- a/include/rapidjson/allocators.h +++ b/include/rapidjson/allocators.h @@ -187,6 +187,11 @@ public: static void Free(void *ptr) { (void)ptr; } // Do nothing private: + //! Copy constructor is not permitted. + MemoryPoolAllocator(const MemoryPoolAllocator& rhs) /* = delete */; + //! Copy assignment operator is not permitted. + MemoryPoolAllocator& operator=(const MemoryPoolAllocator& rhs) /* = delete */; + //! Creates a new chunk. /*! \param capacity Capacity of the chunk in bytes. */ From 16029e6b33a217d05d08976d7fb34ce488e88634 Mon Sep 17 00:00:00 2001 From: "Philipp A. Hartmann" Date: Sat, 1 Feb 2014 18:03:52 +0100 Subject: [PATCH 8/8] FileStream: avoid warning about missing initialisation FileStream::current_ is now explicitly initialized in the constructor. Avoids reading the garbage value in case of an empty file. --- include/rapidjson/filestream.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/rapidjson/filestream.h b/include/rapidjson/filestream.h index 57ab0ba..4cd4cc4 100644 --- a/include/rapidjson/filestream.h +++ b/include/rapidjson/filestream.h @@ -16,7 +16,7 @@ class FileStream { public: typedef char Ch; //!< Character type. Only support char. - FileStream(FILE* fp) : fp_(fp), count_(0) { Read(); } + FileStream(FILE* fp) : fp_(fp), current_('\0'), count_(0) { Read(); } char Peek() const { return current_; } char Take() { char c = current_; Read(); return c; } size_t Tell() const { return count_; }