document.h
浏览该文件的文档.
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14 
15 #ifndef RAPIDJSON_DOCUMENT_H_
16 #define RAPIDJSON_DOCUMENT_H_
17 
18 /*! \file document.h */
19 
20 #include "reader.h"
21 #include "internal/meta.h"
22 #include "internal/strfunc.h"
23 #include <new> // placement new
24 
25 #ifdef _MSC_VER
26 RAPIDJSON_DIAG_PUSH
27 RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
28 #elif defined(__GNUC__)
29 RAPIDJSON_DIAG_PUSH
30 RAPIDJSON_DIAG_OFF(effc++)
31 #endif
32 
33 ///////////////////////////////////////////////////////////////////////////////
34 // RAPIDJSON_HAS_STDSTRING
35 
36 #ifndef RAPIDJSON_HAS_STDSTRING
37 #ifdef RAPIDJSON_DOXYGEN_RUNNING
38 #define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation
39 #else
40 #define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default
41 #endif
42 /*! \def RAPIDJSON_HAS_STDSTRING
43  \ingroup RAPIDJSON_CONFIG
44  \brief Enable RapidJSON support for \c std::string
45 
46  By defining this preprocessor symbol to \c 1, several convenience functions for using
47  \ref rapidjson::GenericValue with \c std::string are enabled, especially
48  for construction and comparison.
49 
50  \hideinitializer
51 */
52 #endif // !defined(RAPIDJSON_HAS_STDSTRING)
53 
54 #if RAPIDJSON_HAS_STDSTRING
55 #include <string>
56 #endif // RAPIDJSON_HAS_STDSTRING
57 
58 #ifndef RAPIDJSON_NOMEMBERITERATORCLASS
59 #include <iterator> // std::iterator, std::random_access_iterator_tag
60 #endif
61 
62 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
63 #include <utility> // std::move
64 #endif
65 
66 RAPIDJSON_NAMESPACE_BEGIN
67 
68 // Forward declaration.
69 template <typename Encoding, typename Allocator>
71 
72 //! Name-value pair in a JSON object value.
73 /*!
74  This class was internal to GenericValue. It used to be a inner struct.
75  But a compiler (IBM XL C/C++ for AIX) have reported to have problem with that so it moved as a namespace scope struct.
76  https://code.google.com/p/rapidjson/issues/detail?id=64
77 */
78 template <typename Encoding, typename Allocator>
79 struct GenericMember {
80  GenericValue<Encoding, Allocator> name; //!< name of member (must be a string)
82 };
83 
84 ///////////////////////////////////////////////////////////////////////////////
85 // GenericMemberIterator
86 
87 #ifndef RAPIDJSON_NOMEMBERITERATORCLASS
88 
89 //! (Constant) member iterator for a JSON object value
90 /*!
91  \tparam Const Is this a constant iterator?
92  \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document)
93  \tparam Allocator Allocator type for allocating memory of object, array and string.
94 
95  This class implements a Random Access Iterator for GenericMember elements
96  of a GenericValue, see ISO/IEC 14882:2003(E) C++ standard, 24.1 [lib.iterator.requirements].
97 
98  \note This iterator implementation is mainly intended to avoid implicit
99  conversions from iterator values to \c NULL,
100  e.g. from GenericValue::FindMember.
101 
102  \note Define \c RAPIDJSON_NOMEMBERITERATORCLASS to fall back to a
103  pointer-based implementation, if your platform doesn't provide
104  the C++ <iterator> header.
105 
106  \see GenericMember, GenericValue::MemberIterator, GenericValue::ConstMemberIterator
107  */
108 template <bool Const, typename Encoding, typename Allocator>
110  : public std::iterator<std::random_access_iterator_tag
111  , typename internal::MaybeAddConst<Const,GenericMember<Encoding,Allocator> >::Type> {
112 
113  friend class GenericValue<Encoding,Allocator>;
114  template <bool, typename, typename> friend class GenericMemberIterator;
115 
117  typedef typename internal::MaybeAddConst<Const,PlainType>::Type ValueType;
118  typedef std::iterator<std::random_access_iterator_tag,ValueType> BaseType;
119 
120 public:
121  //! Iterator type itself
123  //! Constant iterator type
125  //! Non-constant iterator type
127 
128  //! Pointer to (const) GenericMember
129  typedef typename BaseType::pointer Pointer;
130  //! Reference to (const) GenericMember
131  typedef typename BaseType::reference Reference;
132  //! Signed integer type (e.g. \c ptrdiff_t)
133  typedef typename BaseType::difference_type DifferenceType;
134 
135  //! Default constructor (singular value)
136  /*! Creates an iterator pointing to no element.
137  \note All operations, except for comparisons, are undefined on such values.
138  */
139  GenericMemberIterator() : ptr_() {}
140 
141  //! Iterator conversions to more const
142  /*!
143  \param it (Non-const) iterator to copy from
144 
145  Allows the creation of an iterator from another GenericMemberIterator
146  that is "less const". Especially, creating a non-constant iterator
147  from a constant iterator are disabled:
148  \li const -> non-const (not ok)
149  \li const -> const (ok)
150  \li non-const -> const (ok)
151  \li non-const -> non-const (ok)
152 
153  \note If the \c Const template parameter is already \c false, this
154  constructor effectively defines a regular copy-constructor.
155  Otherwise, the copy constructor is implicitly defined.
156  */
157  GenericMemberIterator(const NonConstIterator & it) : ptr_(it.ptr_) {}
158 
159  //! @name stepping
160  //@{
161  Iterator& operator++(){ ++ptr_; return *this; }
162  Iterator& operator--(){ --ptr_; return *this; }
163  Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; }
164  Iterator operator--(int){ Iterator old(*this); --ptr_; return old; }
165  //@}
166 
167  //! @name increment/decrement
168  //@{
169  Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); }
170  Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); }
171 
172  Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; }
173  Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; }
174  //@}
175 
176  //! @name relations
177  //@{
178  bool operator==(ConstIterator that) const { return ptr_ == that.ptr_; }
179  bool operator!=(ConstIterator that) const { return ptr_ != that.ptr_; }
180  bool operator<=(ConstIterator that) const { return ptr_ <= that.ptr_; }
181  bool operator>=(ConstIterator that) const { return ptr_ >= that.ptr_; }
182  bool operator< (ConstIterator that) const { return ptr_ < that.ptr_; }
183  bool operator> (ConstIterator that) const { return ptr_ > that.ptr_; }
184  //@}
185 
186  //! @name dereference
187  //@{
188  Reference operator*() const { return *ptr_; }
189  Pointer operator->() const { return ptr_; }
190  Reference operator[](DifferenceType n) const { return ptr_[n]; }
191  //@}
192 
193  //! Distance
194  DifferenceType operator-(ConstIterator that) const { return ptr_-that.ptr_; }
195 
196 private:
197  //! Internal constructor from plain pointer
198  explicit GenericMemberIterator(Pointer p) : ptr_(p) {}
199 
200  Pointer ptr_; //!< raw pointer
201 };
202 
203 #else // RAPIDJSON_NOMEMBERITERATORCLASS
204 
205 // class-based member iterator implementation disabled, use plain pointers
206 
207 template <bool Const, typename Encoding, typename Allocator>
208 struct GenericMemberIterator;
209 
210 //! non-const GenericMemberIterator
211 template <typename Encoding, typename Allocator>
212 struct GenericMemberIterator<false,Encoding,Allocator> {
213  //! use plain pointer as iterator type
215 };
216 //! const GenericMemberIterator
217 template <typename Encoding, typename Allocator>
218 struct GenericMemberIterator<true,Encoding,Allocator> {
219  //! use plain const pointer as iterator type
221 };
222 
223 #endif // RAPIDJSON_NOMEMBERITERATORCLASS
224 
225 ///////////////////////////////////////////////////////////////////////////////
226 // GenericStringRef
227 
228 //! Reference to a constant string (not taking a copy)
229 /*!
230  \tparam CharType character type of the string
231 
232  This helper class is used to automatically infer constant string
233  references for string literals, especially from \c const \b (!)
234  character arrays.
235 
236  The main use is for creating JSON string values without copying the
237  source string via an \ref Allocator. This requires that the referenced
238  string pointers have a sufficient lifetime, which exceeds the lifetime
239  of the associated GenericValue.
240 
241  \b Example
242  \code
243  Value v("foo"); // ok, no need to copy & calculate length
244  const char foo[] = "foo";
245  v.SetString(foo); // ok
246 
247  const char* bar = foo;
248  // Value x(bar); // not ok, can't rely on bar's lifetime
249  Value x(StringRef(bar)); // lifetime explicitly guaranteed by user
250  Value y(StringRef(bar, 3)); // ok, explicitly pass length
251  \endcode
252 
253  \see StringRef, GenericValue::SetString
254 */
255 template<typename CharType>
257  typedef CharType Ch; //!< character type of the string
258 
259  //! Create string reference from \c const character array
260  /*!
261  This constructor implicitly creates a constant string reference from
262  a \c const character array. It has better performance than
263  \ref StringRef(const CharType*) by inferring the string \ref length
264  from the array length, and also supports strings containing null
265  characters.
266 
267  \tparam N length of the string, automatically inferred
268 
269  \param str Constant character array, lifetime assumed to be longer
270  than the use of the string in e.g. a GenericValue
271 
272  \post \ref s == str
273 
274  \note Constant complexity.
275  \note There is a hidden, private overload to disallow references to
276  non-const character arrays to be created via this constructor.
277  By this, e.g. function-scope arrays used to be filled via
278  \c snprintf are excluded from consideration.
279  In such cases, the referenced string should be \b copied to the
280  GenericValue instead.
281  */
282  template<SizeType N>
283  GenericStringRef(const CharType (&str)[N]) RAPIDJSON_NOEXCEPT
284  : s(str), length(N-1) {}
285 
286  //! Explicitly create string reference from \c const character pointer
287  /*!
288  This constructor can be used to \b explicitly create a reference to
289  a constant string pointer.
290 
291  \see StringRef(const CharType*)
292 
293  \param str Constant character pointer, lifetime assumed to be longer
294  than the use of the string in e.g. a GenericValue
295 
296  \post \ref s == str
297 
298  \note There is a hidden, private overload to disallow references to
299  non-const character arrays to be created via this constructor.
300  By this, e.g. function-scope arrays used to be filled via
301  \c snprintf are excluded from consideration.
302  In such cases, the referenced string should be \b copied to the
303  GenericValue instead.
304  */
305  explicit GenericStringRef(const CharType* str)
306  : s(str), length(internal::StrLen(str)){ RAPIDJSON_ASSERT(s != NULL); }
307 
308  //! Create constant string reference from pointer and length
309  /*! \param str constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
310  \param len length of the string, excluding the trailing NULL terminator
311 
312  \post \ref s == str && \ref length == len
313  \note Constant complexity.
314  */
315  GenericStringRef(const CharType* str, SizeType len)
316  : s(str), length(len) { RAPIDJSON_ASSERT(s != NULL); }
317 
318  //! implicit conversion to plain CharType pointer
319  operator const Ch *() const { return s; }
320 
321  const Ch* const s; //!< plain CharType pointer
322  const SizeType length; //!< length of the string (excluding the trailing NULL terminator)
323 
324 private:
325  //! Disallow copy-assignment
326  GenericStringRef operator=(const GenericStringRef&);
327  //! Disallow construction from non-const array
328  template<SizeType N>
329  GenericStringRef(CharType (&str)[N]) /* = delete */;
330 };
331 
332 //! Mark a character pointer as constant string
333 /*! Mark a plain character pointer as a "string literal". This function
334  can be used to avoid copying a character string to be referenced as a
335  value in a JSON GenericValue object, if the string's lifetime is known
336  to be valid long enough.
337  \tparam CharType Character type of the string
338  \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
339  \return GenericStringRef string reference object
340  \relatesalso GenericStringRef
341 
342  \see GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember
343 */
344 template<typename CharType>
345 inline GenericStringRef<CharType> StringRef(const CharType* str) {
346  return GenericStringRef<CharType>(str, internal::StrLen(str));
347 }
348 
349 //! Mark a character pointer as constant string
350 /*! Mark a plain character pointer as a "string literal". This function
351  can be used to avoid copying a character string to be referenced as a
352  value in a JSON GenericValue object, if the string's lifetime is known
353  to be valid long enough.
354 
355  This version has better performance with supplied length, and also
356  supports string containing null characters.
357 
358  \tparam CharType character type of the string
359  \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
360  \param length The length of source string.
361  \return GenericStringRef string reference object
362  \relatesalso GenericStringRef
363 */
364 template<typename CharType>
365 inline GenericStringRef<CharType> StringRef(const CharType* str, size_t length) {
366  return GenericStringRef<CharType>(str, SizeType(length));
367 }
368 
369 #if RAPIDJSON_HAS_STDSTRING
370 //! Mark a string object as constant string
371 /*! Mark a string object (e.g. \c std::string) as a "string literal".
372  This function can be used to avoid copying a string to be referenced as a
373  value in a JSON GenericValue object, if the string's lifetime is known
374  to be valid long enough.
375 
376  \tparam CharType character type of the string
377  \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
378  \return GenericStringRef string reference object
379  \relatesalso GenericStringRef
380  \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
381 */
382 template<typename CharType>
383 inline GenericStringRef<CharType> StringRef(const std::basic_string<CharType>& str) {
384  return GenericStringRef<CharType>(str.data(), SizeType(str.size()));
385 }
386 #endif
387 
388 ///////////////////////////////////////////////////////////////////////////////
389 // GenericValue type traits
390 namespace internal {
391 
392 template <typename T, typename Encoding = void, typename Allocator = void>
393 struct IsGenericValueImpl : FalseType {};
394 
395 // select candidates according to nested encoding and allocator types
396 template <typename T> struct IsGenericValueImpl<T, typename Void<typename T::EncodingType>::Type, typename Void<typename T::AllocatorType>::Type>
397  : IsBaseOf<GenericValue<typename T::EncodingType, typename T::AllocatorType>, T>::Type {};
398 
399 // helper to match arbitrary GenericValue instantiations, including derived classes
400 template <typename T> struct IsGenericValue : IsGenericValueImpl<T>::Type {};
401 
402 } // namespace internal
403 
404 ///////////////////////////////////////////////////////////////////////////////
405 // GenericValue
406 
407 //! Represents a JSON value. Use Value for UTF8 encoding and default allocator.
408 /*!
409  A JSON value can be one of 7 types. This class is a variant type supporting
410  these types.
411 
412  Use the Value if UTF8 and default allocator
413 
414  \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document)
415  \tparam Allocator Allocator type for allocating memory of object, array and string.
416 */
417 template <typename Encoding, typename Allocator = MemoryPoolAllocator<> >
418 class GenericValue {
419 public:
420  //! Name-value pair in an object.
422  typedef Encoding EncodingType; //!< Encoding type from template parameter.
423  typedef Allocator AllocatorType; //!< Allocator type from template parameter.
424  typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding.
425  typedef GenericStringRef<Ch> StringRefType; //!< Reference to a constant string
426  typedef typename GenericMemberIterator<false,Encoding,Allocator>::Iterator MemberIterator; //!< Member iterator for iterating in object.
427  typedef typename GenericMemberIterator<true,Encoding,Allocator>::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object.
428  typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array.
429  typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array.
430  typedef GenericValue<Encoding, Allocator> ValueType; //!< Value type of itself.
431 
432  //!@name Constructors and destructor.
433  //@{
434 
435  //! Default constructor creates a null value.
436  GenericValue() RAPIDJSON_NOEXCEPT : data_(), flags_(kNullFlag) {}
437 
438 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
439  //! Move constructor in C++11
440  GenericValue(GenericValue&& rhs) RAPIDJSON_NOEXCEPT : data_(rhs.data_), flags_(rhs.flags_) {
441  rhs.flags_ = kNullFlag; // give up contents
442  }
443 #endif
444 
445 private:
446  //! Copy constructor is not permitted.
447  GenericValue(const GenericValue& rhs);
448 
449 public:
450 
451  //! Constructor with JSON value type.
452  /*! This creates a Value of specified type with default content.
453  \param type Type of the value.
454  \note Default content for number is zero.
455  */
456  explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_(), flags_() {
457  static const unsigned defaultFlags[7] = {
458  kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag,
459  kNumberAnyFlag
460  };
461  RAPIDJSON_ASSERT(type <= kNumberType);
462  flags_ = defaultFlags[type];
463 
464  // Use ShortString to store empty string.
465  if (type == kStringType)
466  data_.ss.SetLength(0);
467  }
468 
469  //! Explicit copy constructor (with allocator)
470  /*! Creates a copy of a Value by using the given Allocator
471  \tparam SourceAllocator allocator of \c rhs
472  \param rhs Value to copy from (read-only)
473  \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator().
474  \see CopyFrom()
475  */
476  template< typename SourceAllocator >
477  GenericValue(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator & allocator);
478 
479  //! Constructor for boolean value.
480  /*! \param b Boolean value
481  \note This constructor is limited to \em real boolean values and rejects
482  implicitly converted types like arbitrary pointers. Use an explicit cast
483  to \c bool, if you want to construct a boolean JSON value in such cases.
484  */
485 #ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen
486  template <typename T>
487  explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame<T,bool>))) RAPIDJSON_NOEXCEPT
488 #else
489  explicit GenericValue(bool b) RAPIDJSON_NOEXCEPT
490 #endif
491  : data_(), flags_(b ? kTrueFlag : kFalseFlag) {
492  // safe-guard against failing SFINAE
494  }
495 
496  //! Constructor for int value.
497  explicit GenericValue(int i) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberIntFlag) {
498  data_.n.i64 = i;
499  if (i >= 0)
500  flags_ |= kUintFlag | kUint64Flag;
501  }
502 
503  //! Constructor for unsigned value.
504  explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberUintFlag) {
505  data_.n.u64 = u;
506  if (!(u & 0x80000000))
507  flags_ |= kIntFlag | kInt64Flag;
508  }
509 
510  //! Constructor for int64_t value.
511  explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberInt64Flag) {
512  data_.n.i64 = i64;
513  if (i64 >= 0) {
514  flags_ |= kNumberUint64Flag;
515  if (!(static_cast<uint64_t>(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
516  flags_ |= kUintFlag;
517  if (!(static_cast<uint64_t>(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
518  flags_ |= kIntFlag;
519  }
520  else if (i64 >= static_cast<int64_t>(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
521  flags_ |= kIntFlag;
522  }
523 
524  //! Constructor for uint64_t value.
525  explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberUint64Flag) {
526  data_.n.u64 = u64;
527  if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000)))
528  flags_ |= kInt64Flag;
529  if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
530  flags_ |= kUintFlag;
531  if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
532  flags_ |= kIntFlag;
533  }
534 
535  //! Constructor for double value.
536  explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_(), flags_(kNumberDoubleFlag) { data_.n.d = d; }
537 
538  //! Constructor for constant string (i.e. do not make a copy of string)
539  GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_(), flags_() { SetStringRaw(StringRef(s, length)); }
540 
541  //! Constructor for constant string (i.e. do not make a copy of string)
542  explicit GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT : data_(), flags_() { SetStringRaw(s); }
543 
544  //! Constructor for copy-string (i.e. do make a copy of string)
545  GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s, length), allocator); }
546 
547  //! Constructor for copy-string (i.e. do make a copy of string)
548  GenericValue(const Ch*s, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s), allocator); }
549 
550 #if RAPIDJSON_HAS_STDSTRING
551  //! Constructor for copy-string from a string object (i.e. do make a copy of string)
552  /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
553  */
554  GenericValue(const std::basic_string<Ch>& s, Allocator& allocator) : data_(), flags_() { SetStringRaw(StringRef(s), allocator); }
555 #endif
556 
557  //! Destructor.
558  /*! Need to destruct elements of array, members of object, or copy-string.
559  */
561  if (Allocator::kNeedFree) { // Shortcut by Allocator's trait
562  switch(flags_) {
563  case kArrayFlag:
564  for (GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v)
565  v->~GenericValue();
566  Allocator::Free(data_.a.elements);
567  break;
568 
569  case kObjectFlag:
570  for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m)
571  m->~Member();
572  Allocator::Free(data_.o.members);
573  break;
574 
575  case kCopyStringFlag:
576  Allocator::Free(const_cast<Ch*>(data_.s.str));
577  break;
578 
579  default:
580  break; // Do nothing for other types.
581  }
582  }
583  }
584 
585  //@}
586 
587  //!@name Assignment operators
588  //@{
589 
590  //! Assignment with move semantics.
591  /*! \param rhs Source of the assignment. It will become a null value after assignment.
592  */
593  GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT {
594  RAPIDJSON_ASSERT(this != &rhs);
595  this->~GenericValue();
596  RawAssign(rhs);
597  return *this;
598  }
599 
600 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
601  //! Move assignment in C++11
602  GenericValue& operator=(GenericValue&& rhs) RAPIDJSON_NOEXCEPT {
603  return *this = rhs.Move();
604  }
605 #endif
606 
607  //! Assignment of constant string reference (no copy)
608  /*! \param str Constant string reference to be assigned
609  \note This overload is needed to avoid clashes with the generic primitive type assignment overload below.
610  \see GenericStringRef, operator=(T)
611  */
612  GenericValue& operator=(StringRefType str) RAPIDJSON_NOEXCEPT {
613  GenericValue s(str);
614  return *this = s;
615  }
616 
617  //! Assignment with primitive types.
618  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
619  \param value The value to be assigned.
620 
621  \note The source type \c T explicitly disallows all pointer types,
622  especially (\c const) \ref Ch*. This helps avoiding implicitly
623  referencing character strings with insufficient lifetime, use
624  \ref SetString(const Ch*, Allocator&) (for copying) or
625  \ref StringRef() (to explicitly mark the pointer as constant) instead.
626  All other pointer types would implicitly convert to \c bool,
627  use \ref SetBool() instead.
628  */
629  template <typename T>
630  RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer<T>), (GenericValue&))
631  operator=(T value) {
632  GenericValue v(value);
633  return *this = v;
634  }
635 
636  //! Deep-copy assignment from Value
637  /*! Assigns a \b copy of the Value to the current Value object
638  \tparam SourceAllocator Allocator type of \c rhs
639  \param rhs Value to copy from (read-only)
640  \param allocator Allocator to use for copying
641  */
642  template <typename SourceAllocator>
643  GenericValue& CopyFrom(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator& allocator) {
644  RAPIDJSON_ASSERT((void*)this != (void const*)&rhs);
645  this->~GenericValue();
646  new (this) GenericValue(rhs, allocator);
647  return *this;
648  }
649 
650  //! Exchange the contents of this value with those of other.
651  /*!
652  \param other Another value.
653  \note Constant complexity.
654  */
655  GenericValue& Swap(GenericValue& other) RAPIDJSON_NOEXCEPT {
656  GenericValue temp;
657  temp.RawAssign(*this);
658  RawAssign(other);
659  other.RawAssign(temp);
660  return *this;
661  }
662 
663  //! Prepare Value for move semantics
664  /*! \return *this */
665  GenericValue& Move() RAPIDJSON_NOEXCEPT { return *this; }
666  //@}
667 
668  //!@name Equal-to and not-equal-to operators
669  //@{
670  //! Equal-to operator
671  /*!
672  \note If an object contains duplicated named member, comparing equality with any object is always \c false.
673  \note Linear time complexity (number of all values in the subtree and total lengths of all strings).
674  */
675  template <typename SourceAllocator>
678  if (GetType() != rhs.GetType())
679  return false;
680 
681  switch (GetType()) {
682  case kObjectType: // Warning: O(n^2) inner-loop
683  if (data_.o.size != rhs.data_.o.size)
684  return false;
685  for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) {
686  typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name);
687  if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value)
688  return false;
689  }
690  return true;
691 
692  case kArrayType:
693  if (data_.a.size != rhs.data_.a.size)
694  return false;
695  for (SizeType i = 0; i < data_.a.size; i++)
696  if ((*this)[i] != rhs[i])
697  return false;
698  return true;
699 
700  case kStringType:
701  return StringEqual(rhs);
702 
703  case kNumberType:
704  if (IsDouble() || rhs.IsDouble()) {
705  double a = GetDouble(); // May convert from integer to double.
706  double b = rhs.GetDouble(); // Ditto
707  return a >= b && a <= b; // Prevent -Wfloat-equal
708  }
709  else
710  return data_.n.u64 == rhs.data_.n.u64;
711 
712  default: // kTrueType, kFalseType, kNullType
713  return true;
714  }
715  }
716 
717  //! Equal-to operator with const C-string pointer
718  bool operator==(const Ch* rhs) const { return *this == GenericValue(StringRef(rhs)); }
719 
720 #if RAPIDJSON_HAS_STDSTRING
721  //! Equal-to operator with string object
722  /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
723  */
724  bool operator==(const std::basic_string<Ch>& rhs) const { return *this == GenericValue(StringRef(rhs)); }
725 #endif
726 
727  //! Equal-to operator with primitive types
728  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false
729  */
730  template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>,internal::IsGenericValue<T> >), (bool)) operator==(const T& rhs) const { return *this == GenericValue(rhs); }
731 
732  //! Not-equal-to operator
733  /*! \return !(*this == rhs)
734  */
735  template <typename SourceAllocator>
736  bool operator!=(const GenericValue<Encoding, SourceAllocator>& rhs) const { return !(*this == rhs); }
737 
738  //! Not-equal-to operator with const C-string pointer
739  bool operator!=(const Ch* rhs) const { return !(*this == rhs); }
740 
741  //! Not-equal-to operator with arbitrary types
742  /*! \return !(*this == rhs)
743  */
744  template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (bool)) operator!=(const T& rhs) const { return !(*this == rhs); }
745 
746  //! Equal-to operator with arbitrary types (symmetric version)
747  /*! \return (rhs == lhs)
748  */
749  template <typename T> friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (bool)) operator==(const T& lhs, const GenericValue& rhs) { return rhs == lhs; }
750 
751  //! Not-Equal-to operator with arbitrary types (symmetric version)
752  /*! \return !(rhs == lhs)
753  */
754  template <typename T> friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (bool)) operator!=(const T& lhs, const GenericValue& rhs) { return !(rhs == lhs); }
755  //@}
756 
757  //!@name Type
758  //@{
759 
760  Type GetType() const { return static_cast<Type>(flags_ & kTypeMask); }
761  bool IsNull() const { return flags_ == kNullFlag; }
762  bool IsFalse() const { return flags_ == kFalseFlag; }
763  bool IsTrue() const { return flags_ == kTrueFlag; }
764  bool IsBool() const { return (flags_ & kBoolFlag) != 0; }
765  bool IsObject() const { return flags_ == kObjectFlag; }
766  bool IsArray() const { return flags_ == kArrayFlag; }
767  bool IsNumber() const { return (flags_ & kNumberFlag) != 0; }
768  bool IsInt() const { return (flags_ & kIntFlag) != 0; }
769  bool IsUint() const { return (flags_ & kUintFlag) != 0; }
770  bool IsInt64() const { return (flags_ & kInt64Flag) != 0; }
771  bool IsUint64() const { return (flags_ & kUint64Flag) != 0; }
772  bool IsDouble() const { return (flags_ & kDoubleFlag) != 0; }
773  bool IsString() const { return (flags_ & kStringFlag) != 0; }
774 
775  //@}
776 
777  //!@name Null
778  //@{
779 
780  GenericValue& SetNull() { this->~GenericValue(); new (this) GenericValue(); return *this; }
781 
782  //@}
783 
784  //!@name Bool
785  //@{
786 
787  bool GetBool() const { RAPIDJSON_ASSERT(IsBool()); return flags_ == kTrueFlag; }
788  //!< Set boolean value
789  /*! \post IsBool() == true */
790  GenericValue& SetBool(bool b) { this->~GenericValue(); new (this) GenericValue(b); return *this; }
791 
792  //@}
793 
794  //!@name Object
795  //@{
796 
797  //! Set this value as an empty object.
798  /*! \post IsObject() == true */
799  GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; }
800 
801  //! Get the number of members in the object.
802  SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; }
803 
804  //! Check whether the object is empty.
805  bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; }
806 
807  //! Get a value from an object associated with the name.
808  /*! \pre IsObject() == true
809  \tparam T Either \c Ch or \c const \c Ch (template used for disambiguation with \ref operator[](SizeType))
810  \note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7.
811  Since 0.2, if the name is not correct, it will assert.
812  If user is unsure whether a member exists, user should use HasMember() first.
813  A better approach is to use FindMember().
814  \note Linear time complexity.
815  */
816  template <typename T>
817  RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr<internal::IsSame<typename internal::RemoveConst<T>::Type, Ch> >),(GenericValue&)) operator[](T* name) {
818  GenericValue n(StringRef(name));
819  return (*this)[n];
820  }
821  template <typename T>
822  RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr<internal::IsSame<typename internal::RemoveConst<T>::Type, Ch> >),(const GenericValue&)) operator[](T* name) const { return const_cast<GenericValue&>(*this)[name]; }
823 
824  //! Get a value from an object associated with the name.
825  /*! \pre IsObject() == true
826  \tparam SourceAllocator Allocator of the \c name value
827 
828  \note Compared to \ref operator[](T*), this version is faster because it does not need a StrLen().
829  And it can also handle strings with embedded null characters.
830 
831  \note Linear time complexity.
832  */
833  template <typename SourceAllocator>
835  MemberIterator member = FindMember(name);
836  if (member != MemberEnd())
837  return member->value;
838  else {
839  RAPIDJSON_ASSERT(false); // see above note
840  static GenericValue NullValue;
841  return NullValue;
842  }
843  }
844  template <typename SourceAllocator>
845  const GenericValue& operator[](const GenericValue<Encoding, SourceAllocator>& name) const { return const_cast<GenericValue&>(*this)[name]; }
846 
847 #if RAPIDJSON_HAS_STDSTRING
848  //! Get a value from an object associated with name (string object).
849  GenericValue& operator[](const std::basic_string<Ch>& name) { return (*this)[GenericValue(StringRef(name))]; }
850  const GenericValue& operator[](const std::basic_string<Ch>& name) const { return (*this)[GenericValue(StringRef(name))]; }
851 #endif
852 
853  //! Const member iterator
854  /*! \pre IsObject() == true */
855  ConstMemberIterator MemberBegin() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(data_.o.members); }
856  //! Const \em past-the-end member iterator
857  /*! \pre IsObject() == true */
858  ConstMemberIterator MemberEnd() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(data_.o.members + data_.o.size); }
859  //! Member iterator
860  /*! \pre IsObject() == true */
861  MemberIterator MemberBegin() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(data_.o.members); }
862  //! \em Past-the-end member iterator
863  /*! \pre IsObject() == true */
864  MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(data_.o.members + data_.o.size); }
865 
866  //! Check whether a member exists in the object.
867  /*!
868  \param name Member name to be searched.
869  \pre IsObject() == true
870  \return Whether a member with that name exists.
871  \note It is better to use FindMember() directly if you need the obtain the value as well.
872  \note Linear time complexity.
873  */
874  bool HasMember(const Ch* name) const { return FindMember(name) != MemberEnd(); }
875 
876 #if RAPIDJSON_HAS_STDSTRING
877  //! Check whether a member exists in the object with string object.
878  /*!
879  \param name Member name to be searched.
880  \pre IsObject() == true
881  \return Whether a member with that name exists.
882  \note It is better to use FindMember() directly if you need the obtain the value as well.
883  \note Linear time complexity.
884  */
885  bool HasMember(const std::basic_string<Ch>& name) const { return FindMember(name) != MemberEnd(); }
886 #endif
887 
888  //! Check whether a member exists in the object with GenericValue name.
889  /*!
890  This version is faster because it does not need a StrLen(). It can also handle string with null character.
891  \param name Member name to be searched.
892  \pre IsObject() == true
893  \return Whether a member with that name exists.
894  \note It is better to use FindMember() directly if you need the obtain the value as well.
895  \note Linear time complexity.
896  */
897  template <typename SourceAllocator>
898  bool HasMember(const GenericValue<Encoding, SourceAllocator>& name) const { return FindMember(name) != MemberEnd(); }
899 
900  //! Find member by name.
901  /*!
902  \param name Member name to be searched.
903  \pre IsObject() == true
904  \return Iterator to member, if it exists.
905  Otherwise returns \ref MemberEnd().
906 
907  \note Earlier versions of Rapidjson returned a \c NULL pointer, in case
908  the requested member doesn't exist. For consistency with e.g.
909  \c std::map, this has been changed to MemberEnd() now.
910  \note Linear time complexity.
911  */
912  MemberIterator FindMember(const Ch* name) {
913  GenericValue n(StringRef(name));
914  return FindMember(n);
915  }
916 
917  ConstMemberIterator FindMember(const Ch* name) const { return const_cast<GenericValue&>(*this).FindMember(name); }
918 
919  //! Find member by name.
920  /*!
921  This version is faster because it does not need a StrLen(). It can also handle string with null character.
922  \param name Member name to be searched.
923  \pre IsObject() == true
924  \return Iterator to member, if it exists.
925  Otherwise returns \ref MemberEnd().
926 
927  \note Earlier versions of Rapidjson returned a \c NULL pointer, in case
928  the requested member doesn't exist. For consistency with e.g.
929  \c std::map, this has been changed to MemberEnd() now.
930  \note Linear time complexity.
931  */
932  template <typename SourceAllocator>
934  RAPIDJSON_ASSERT(IsObject());
935  RAPIDJSON_ASSERT(name.IsString());
936  MemberIterator member = MemberBegin();
937  for ( ; member != MemberEnd(); ++member)
938  if (name.StringEqual(member->name))
939  break;
940  return member;
941  }
942  template <typename SourceAllocator> ConstMemberIterator FindMember(const GenericValue<Encoding, SourceAllocator>& name) const { return const_cast<GenericValue&>(*this).FindMember(name); }
943 
944 #if RAPIDJSON_HAS_STDSTRING
945  //! Find member by string object name.
946  /*!
947  \param name Member name to be searched.
948  \pre IsObject() == true
949  \return Iterator to member, if it exists.
950  Otherwise returns \ref MemberEnd().
951  */
952  MemberIterator FindMember(const std::basic_string<Ch>& name) { return FindMember(StringRef(name)); }
953  ConstMemberIterator FindMember(const std::basic_string<Ch>& name) const { return FindMember(StringRef(name)); }
954 #endif
955 
956  //! Add a member (name-value pair) to the object.
957  /*! \param name A string value as name of member.
958  \param value Value of any type.
959  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
960  \return The value itself for fluent API.
961  \note The ownership of \c name and \c value will be transferred to this object on success.
962  \pre IsObject() && name.IsString()
963  \post name.IsNull() && value.IsNull()
964  \note Amortized Constant time complexity.
965  */
966  GenericValue& AddMember(GenericValue& name, GenericValue& value, Allocator& allocator) {
967  RAPIDJSON_ASSERT(IsObject());
968  RAPIDJSON_ASSERT(name.IsString());
969 
970  Object& o = data_.o;
971  if (o.size >= o.capacity) {
972  if (o.capacity == 0) {
973  o.capacity = kDefaultObjectCapacity;
974  o.members = reinterpret_cast<Member*>(allocator.Malloc(o.capacity * sizeof(Member)));
975  }
976  else {
977  SizeType oldCapacity = o.capacity;
978  o.capacity += (oldCapacity + 1) / 2; // grow by factor 1.5
979  o.members = reinterpret_cast<Member*>(allocator.Realloc(o.members, oldCapacity * sizeof(Member), o.capacity * sizeof(Member)));
980  }
981  }
982  o.members[o.size].name.RawAssign(name);
983  o.members[o.size].value.RawAssign(value);
984  o.size++;
985  return *this;
986  }
987 
988  //! Add a constant string value as member (name-value pair) to the object.
989  /*! \param name A string value as name of member.
990  \param value constant string reference as value of member.
991  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
992  \return The value itself for fluent API.
993  \pre IsObject()
994  \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below.
995  \note Amortized Constant time complexity.
996  */
997  GenericValue& AddMember(GenericValue& name, StringRefType value, Allocator& allocator) {
998  GenericValue v(value);
999  return AddMember(name, v, allocator);
1000  }
1001 
1002 #if RAPIDJSON_HAS_STDSTRING
1003  //! Add a string object as member (name-value pair) to the object.
1004  /*! \param name A string value as name of member.
1005  \param value constant string reference as value of member.
1006  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1007  \return The value itself for fluent API.
1008  \pre IsObject()
1009  \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below.
1010  \note Amortized Constant time complexity.
1011  */
1012  GenericValue& AddMember(GenericValue& name, std::basic_string<Ch>& value, Allocator& allocator) {
1013  GenericValue v(value, allocator);
1014  return AddMember(name, v, allocator);
1015  }
1016 #endif
1017 
1018  //! Add any primitive value as member (name-value pair) to the object.
1019  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
1020  \param name A string value as name of member.
1021  \param value Value of primitive type \c T as value of member
1022  \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator().
1023  \return The value itself for fluent API.
1024  \pre IsObject()
1025 
1026  \note The source type \c T explicitly disallows all pointer types,
1027  especially (\c const) \ref Ch*. This helps avoiding implicitly
1028  referencing character strings with insufficient lifetime, use
1029  \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref
1030  AddMember(StringRefType, StringRefType, Allocator&).
1031  All other pointer types would implicitly convert to \c bool,
1032  use an explicit cast instead, if needed.
1033  \note Amortized Constant time complexity.
1034  */
1035  template <typename T>
1036  RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericValue&))
1037  AddMember(GenericValue& name, T value, Allocator& allocator) {
1038  GenericValue v(value);
1039  return AddMember(name, v, allocator);
1040  }
1041 
1042 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
1043  GenericValue& AddMember(GenericValue&& name, GenericValue&& value, Allocator& allocator) {
1044  return AddMember(name, value, allocator);
1045  }
1046  GenericValue& AddMember(GenericValue&& name, GenericValue& value, Allocator& allocator) {
1047  return AddMember(name, value, allocator);
1048  }
1049  GenericValue& AddMember(GenericValue& name, GenericValue&& value, Allocator& allocator) {
1050  return AddMember(name, value, allocator);
1051  }
1052  GenericValue& AddMember(StringRefType name, GenericValue&& value, Allocator& allocator) {
1053  GenericValue n(name);
1054  return AddMember(n, value, allocator);
1055  }
1056 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
1057 
1058 
1059  //! Add a member (name-value pair) to the object.
1060  /*! \param name A constant string reference as name of member.
1061  \param value Value of any type.
1062  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1063  \return The value itself for fluent API.
1064  \note The ownership of \c value will be transferred to this object on success.
1065  \pre IsObject()
1066  \post value.IsNull()
1067  \note Amortized Constant time complexity.
1068  */
1069  GenericValue& AddMember(StringRefType name, GenericValue& value, Allocator& allocator) {
1070  GenericValue n(name);
1071  return AddMember(n, value, allocator);
1072  }
1073 
1074  //! Add a constant string value as member (name-value pair) to the object.
1075  /*! \param name A constant string reference as name of member.
1076  \param value constant string reference as value of member.
1077  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1078  \return The value itself for fluent API.
1079  \pre IsObject()
1080  \note This overload is needed to avoid clashes with the generic primitive type AddMember(StringRefType,T,Allocator&) overload below.
1081  \note Amortized Constant time complexity.
1082  */
1083  GenericValue& AddMember(StringRefType name, StringRefType value, Allocator& allocator) {
1084  GenericValue v(value);
1085  return AddMember(name, v, allocator);
1086  }
1087 
1088  //! Add any primitive value as member (name-value pair) to the object.
1089  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
1090  \param name A constant string reference as name of member.
1091  \param value Value of primitive type \c T as value of member
1092  \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator().
1093  \return The value itself for fluent API.
1094  \pre IsObject()
1095 
1096  \note The source type \c T explicitly disallows all pointer types,
1097  especially (\c const) \ref Ch*. This helps avoiding implicitly
1098  referencing character strings with insufficient lifetime, use
1099  \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref
1100  AddMember(StringRefType, StringRefType, Allocator&).
1101  All other pointer types would implicitly convert to \c bool,
1102  use an explicit cast instead, if needed.
1103  \note Amortized Constant time complexity.
1104  */
1105  template <typename T>
1106  RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericValue&))
1107  AddMember(StringRefType name, T value, Allocator& allocator) {
1108  GenericValue n(name);
1109  return AddMember(n, value, allocator);
1110  }
1111 
1112  //! Remove all members in the object.
1113  /*! This function do not deallocate memory in the object, i.e. the capacity is unchanged.
1114  \note Linear time complexity.
1115  */
1117  RAPIDJSON_ASSERT(IsObject());
1118  for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m)
1119  m->~Member();
1120  data_.o.size = 0;
1121  }
1122 
1123  //! Remove a member in object by its name.
1124  /*! \param name Name of member to be removed.
1125  \return Whether the member existed.
1126  \note This function may reorder the object members. Use \ref
1127  EraseMember(ConstMemberIterator) if you need to preserve the
1128  relative order of the remaining members.
1129  \note Linear time complexity.
1130  */
1131  bool RemoveMember(const Ch* name) {
1132  GenericValue n(StringRef(name));
1133  return RemoveMember(n);
1134  }
1135 
1136 #if RAPIDJSON_HAS_STDSTRING
1137  bool RemoveMember(const std::basic_string<Ch>& name) { return RemoveMember(GenericValue(StringRef(name))); }
1138 #endif
1139 
1140  template <typename SourceAllocator>
1141  bool RemoveMember(const GenericValue<Encoding, SourceAllocator>& name) {
1142  MemberIterator m = FindMember(name);
1143  if (m != MemberEnd()) {
1144  RemoveMember(m);
1145  return true;
1146  }
1147  else
1148  return false;
1149  }
1150 
1151  //! Remove a member in object by iterator.
1152  /*! \param m member iterator (obtained by FindMember() or MemberBegin()).
1153  \return the new iterator after removal.
1154  \note This function may reorder the object members. Use \ref
1155  EraseMember(ConstMemberIterator) if you need to preserve the
1156  relative order of the remaining members.
1157  \note Constant time complexity.
1158  */
1160  RAPIDJSON_ASSERT(IsObject());
1161  RAPIDJSON_ASSERT(data_.o.size > 0);
1162  RAPIDJSON_ASSERT(data_.o.members != 0);
1163  RAPIDJSON_ASSERT(m >= MemberBegin() && m < MemberEnd());
1164 
1165  MemberIterator last(data_.o.members + (data_.o.size - 1));
1166  if (data_.o.size > 1 && m != last) {
1167  // Move the last one to this place
1168  *m = *last;
1169  }
1170  else {
1171  // Only one left, just destroy
1172  m->~Member();
1173  }
1174  --data_.o.size;
1175  return m;
1176  }
1177 
1178  //! Remove a member from an object by iterator.
1179  /*! \param pos iterator to the member to remove
1180  \pre IsObject() == true && \ref MemberBegin() <= \c pos < \ref MemberEnd()
1181  \return Iterator following the removed element.
1182  If the iterator \c pos refers to the last element, the \ref MemberEnd() iterator is returned.
1183  \note This function preserves the relative order of the remaining object
1184  members. If you do not need this, use the more efficient \ref RemoveMember(MemberIterator).
1185  \note Linear time complexity.
1186  */
1188  return EraseMember(pos, pos +1);
1189  }
1190 
1191  //! Remove members in the range [first, last) from an object.
1192  /*! \param first iterator to the first member to remove
1193  \param last iterator following the last member to remove
1194  \pre IsObject() == true && \ref MemberBegin() <= \c first <= \c last <= \ref MemberEnd()
1195  \return Iterator following the last removed element.
1196  \note This function preserves the relative order of the remaining object
1197  members.
1198  \note Linear time complexity.
1199  */
1201  RAPIDJSON_ASSERT(IsObject());
1202  RAPIDJSON_ASSERT(data_.o.size > 0);
1203  RAPIDJSON_ASSERT(data_.o.members != 0);
1204  RAPIDJSON_ASSERT(first >= MemberBegin());
1205  RAPIDJSON_ASSERT(first <= last);
1206  RAPIDJSON_ASSERT(last <= MemberEnd());
1207 
1208  MemberIterator pos = MemberBegin() + (first - MemberBegin());
1209  for (MemberIterator itr = pos; itr != last; ++itr)
1210  itr->~Member();
1211  std::memmove(&*pos, &*last, (MemberEnd() - last) * sizeof(Member));
1212  data_.o.size -= (last - first);
1213  return pos;
1214  }
1215 
1216  //@}
1217 
1218  //!@name Array
1219  //@{
1220 
1221  //! Set this value as an empty array.
1222  /*! \post IsArray == true */
1223  GenericValue& SetArray() { this->~GenericValue(); new (this) GenericValue(kArrayType); return *this; }
1224 
1225  //! Get the number of elements in array.
1226  SizeType Size() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size; }
1227 
1228  //! Get the capacity of array.
1229  SizeType Capacity() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.capacity; }
1230 
1231  //! Check whether the array is empty.
1232  bool Empty() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size == 0; }
1233 
1234  //! Remove all elements in the array.
1235  /*! This function do not deallocate memory in the array, i.e. the capacity is unchanged.
1236  \note Linear time complexity.
1237  */
1238  void Clear() {
1239  RAPIDJSON_ASSERT(IsArray());
1240  for (SizeType i = 0; i < data_.a.size; ++i)
1241  data_.a.elements[i].~GenericValue();
1242  data_.a.size = 0;
1243  }
1244 
1245  //! Get an element from array by index.
1246  /*! \pre IsArray() == true
1247  \param index Zero-based index of element.
1248  \see operator[](T*)
1249  */
1251  RAPIDJSON_ASSERT(IsArray());
1252  RAPIDJSON_ASSERT(index < data_.a.size);
1253  return data_.a.elements[index];
1254  }
1255  const GenericValue& operator[](SizeType index) const { return const_cast<GenericValue&>(*this)[index]; }
1256 
1257  //! Element iterator
1258  /*! \pre IsArray() == true */
1259  ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return data_.a.elements; }
1260  //! \em Past-the-end element iterator
1261  /*! \pre IsArray() == true */
1262  ValueIterator End() { RAPIDJSON_ASSERT(IsArray()); return data_.a.elements + data_.a.size; }
1263  //! Constant element iterator
1264  /*! \pre IsArray() == true */
1265  ConstValueIterator Begin() const { return const_cast<GenericValue&>(*this).Begin(); }
1266  //! Constant \em past-the-end element iterator
1267  /*! \pre IsArray() == true */
1268  ConstValueIterator End() const { return const_cast<GenericValue&>(*this).End(); }
1269 
1270  //! Request the array to have enough capacity to store elements.
1271  /*! \param newCapacity The capacity that the array at least need to have.
1272  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1273  \return The value itself for fluent API.
1274  \note Linear time complexity.
1275  */
1276  GenericValue& Reserve(SizeType newCapacity, Allocator &allocator) {
1277  RAPIDJSON_ASSERT(IsArray());
1278  if (newCapacity > data_.a.capacity) {
1279  data_.a.elements = (GenericValue*)allocator.Realloc(data_.a.elements, data_.a.capacity * sizeof(GenericValue), newCapacity * sizeof(GenericValue));
1280  data_.a.capacity = newCapacity;
1281  }
1282  return *this;
1283  }
1284 
1285  //! Append a GenericValue at the end of the array.
1286  /*! \param value Value to be appended.
1287  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1288  \pre IsArray() == true
1289  \post value.IsNull() == true
1290  \return The value itself for fluent API.
1291  \note The ownership of \c value will be transferred to this array on success.
1292  \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient.
1293  \note Amortized constant time complexity.
1294  */
1295  GenericValue& PushBack(GenericValue& value, Allocator& allocator) {
1296  RAPIDJSON_ASSERT(IsArray());
1297  if (data_.a.size >= data_.a.capacity)
1298  Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : (data_.a.capacity + (data_.a.capacity + 1) / 2), allocator);
1299  data_.a.elements[data_.a.size++].RawAssign(value);
1300  return *this;
1301  }
1302 
1303 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
1304  GenericValue& PushBack(GenericValue&& value, Allocator& allocator) {
1305  return PushBack(value, allocator);
1306  }
1307 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
1308 
1309  //! Append a constant string reference at the end of the array.
1310  /*! \param value Constant string reference to be appended.
1311  \param allocator Allocator for reallocating memory. It must be the same one used previously. Commonly use GenericDocument::GetAllocator().
1312  \pre IsArray() == true
1313  \return The value itself for fluent API.
1314  \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient.
1315  \note Amortized constant time complexity.
1316  \see GenericStringRef
1317  */
1318  GenericValue& PushBack(StringRefType value, Allocator& allocator) {
1319  return (*this).template PushBack<StringRefType>(value, allocator);
1320  }
1321 
1322  //! Append a primitive value at the end of the array.
1323  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
1324  \param value Value of primitive type T to be appended.
1325  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1326  \pre IsArray() == true
1327  \return The value itself for fluent API.
1328  \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient.
1329 
1330  \note The source type \c T explicitly disallows all pointer types,
1331  especially (\c const) \ref Ch*. This helps avoiding implicitly
1332  referencing character strings with insufficient lifetime, use
1333  \ref PushBack(GenericValue&, Allocator&) or \ref
1334  PushBack(StringRefType, Allocator&).
1335  All other pointer types would implicitly convert to \c bool,
1336  use an explicit cast instead, if needed.
1337  \note Amortized constant time complexity.
1338  */
1339  template <typename T>
1340  RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericValue&))
1341  PushBack(T value, Allocator& allocator) {
1342  GenericValue v(value);
1343  return PushBack(v, allocator);
1344  }
1345 
1346  //! Remove the last element in the array.
1347  /*!
1348  \note Constant time complexity.
1349  */
1351  RAPIDJSON_ASSERT(IsArray());
1352  RAPIDJSON_ASSERT(!Empty());
1353  data_.a.elements[--data_.a.size].~GenericValue();
1354  return *this;
1355  }
1356 
1357  //! Remove an element of array by iterator.
1358  /*!
1359  \param pos iterator to the element to remove
1360  \pre IsArray() == true && \ref Begin() <= \c pos < \ref End()
1361  \return Iterator following the removed element. If the iterator pos refers to the last element, the End() iterator is returned.
1362  \note Linear time complexity.
1363  */
1365  return Erase(pos, pos + 1);
1366  }
1367 
1368  //! Remove elements in the range [first, last) of the array.
1369  /*!
1370  \param first iterator to the first element to remove
1371  \param last iterator following the last element to remove
1372  \pre IsArray() == true && \ref Begin() <= \c first <= \c last <= \ref End()
1373  \return Iterator following the last removed element.
1374  \note Linear time complexity.
1375  */
1377  RAPIDJSON_ASSERT(IsArray());
1378  RAPIDJSON_ASSERT(data_.a.size > 0);
1379  RAPIDJSON_ASSERT(data_.a.elements != 0);
1380  RAPIDJSON_ASSERT(first >= Begin());
1381  RAPIDJSON_ASSERT(first <= last);
1382  RAPIDJSON_ASSERT(last <= End());
1383  ValueIterator pos = Begin() + (first - Begin());
1384  for (ValueIterator itr = pos; itr != last; ++itr)
1385  itr->~GenericValue();
1386  std::memmove(pos, last, (End() - last) * sizeof(GenericValue));
1387  data_.a.size -= (last - first);
1388  return pos;
1389  }
1390 
1391  //@}
1392 
1393  //!@name Number
1394  //@{
1395 
1396  int GetInt() const { RAPIDJSON_ASSERT(flags_ & kIntFlag); return data_.n.i.i; }
1397  unsigned GetUint() const { RAPIDJSON_ASSERT(flags_ & kUintFlag); return data_.n.u.u; }
1398  int64_t GetInt64() const { RAPIDJSON_ASSERT(flags_ & kInt64Flag); return data_.n.i64; }
1399  uint64_t GetUint64() const { RAPIDJSON_ASSERT(flags_ & kUint64Flag); return data_.n.u64; }
1400 
1401  double GetDouble() const {
1402  RAPIDJSON_ASSERT(IsNumber());
1403  if ((flags_ & kDoubleFlag) != 0) return data_.n.d; // exact type, no conversion.
1404  if ((flags_ & kIntFlag) != 0) return data_.n.i.i; // int -> double
1405  if ((flags_ & kUintFlag) != 0) return data_.n.u.u; // unsigned -> double
1406  if ((flags_ & kInt64Flag) != 0) return (double)data_.n.i64; // int64_t -> double (may lose precision)
1407  RAPIDJSON_ASSERT((flags_ & kUint64Flag) != 0); return (double)data_.n.u64; // uint64_t -> double (may lose precision)
1408  }
1409 
1410  GenericValue& SetInt(int i) { this->~GenericValue(); new (this) GenericValue(i); return *this; }
1411  GenericValue& SetUint(unsigned u) { this->~GenericValue(); new (this) GenericValue(u); return *this; }
1412  GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; }
1413  GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; }
1414  GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; }
1415 
1416  //@}
1417 
1418  //!@name String
1419  //@{
1420 
1421  const Ch* GetString() const { RAPIDJSON_ASSERT(IsString()); return ((flags_ & kInlineStrFlag) ? data_.ss.str : data_.s.str); }
1422 
1423  //! Get the length of string.
1424  /*! Since rapidjson permits "\\u0000" in the json string, strlen(v.GetString()) may not equal to v.GetStringLength().
1425  */
1426  SizeType GetStringLength() const { RAPIDJSON_ASSERT(IsString()); return ((flags_ & kInlineStrFlag) ? (data_.ss.GetLength()) : data_.s.length); }
1427 
1428  //! Set this value as a string without copying source string.
1429  /*! This version has better performance with supplied length, and also support string containing null character.
1430  \param s source string pointer.
1431  \param length The length of source string, excluding the trailing null terminator.
1432  \return The value itself for fluent API.
1433  \post IsString() == true && GetString() == s && GetStringLength() == length
1434  \see SetString(StringRefType)
1435  */
1436  GenericValue& SetString(const Ch* s, SizeType length) { return SetString(StringRef(s, length)); }
1437 
1438  //! Set this value as a string without copying source string.
1439  /*! \param s source string reference
1440  \return The value itself for fluent API.
1441  \post IsString() == true && GetString() == s && GetStringLength() == s.length
1442  */
1443  GenericValue& SetString(StringRefType s) { this->~GenericValue(); SetStringRaw(s); return *this; }
1444 
1445  //! Set this value as a string by copying from source string.
1446  /*! This version has better performance with supplied length, and also support string containing null character.
1447  \param s source string.
1448  \param length The length of source string, excluding the trailing null terminator.
1449  \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
1450  \return The value itself for fluent API.
1451  \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length
1452  */
1453  GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { this->~GenericValue(); SetStringRaw(StringRef(s, length), allocator); return *this; }
1454 
1455  //! Set this value as a string by copying from source string.
1456  /*! \param s source string.
1457  \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
1458  \return The value itself for fluent API.
1459  \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length
1460  */
1461  GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(s, internal::StrLen(s), allocator); }
1462 
1463 #if RAPIDJSON_HAS_STDSTRING
1464  //! Set this value as a string by copying from source string.
1465  /*! \param s source string.
1466  \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
1467  \return The value itself for fluent API.
1468  \post IsString() == true && GetString() != s.data() && strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size()
1469  \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
1470  */
1471  GenericValue& SetString(const std::basic_string<Ch>& s, Allocator& allocator) { return SetString(s.data(), SizeType(s.size()), allocator); }
1472 #endif
1473 
1474  //@}
1475 
1476  //! Generate events of this value to a Handler.
1477  /*! This function adopts the GoF visitor pattern.
1478  Typical usage is to output this JSON value as JSON text via Writer, which is a Handler.
1479  It can also be used to deep clone this value via GenericDocument, which is also a Handler.
1480  \tparam Handler type of handler.
1481  \param handler An object implementing concept Handler.
1482  */
1483  template <typename Handler>
1484  bool Accept(Handler& handler) const {
1485  switch(GetType()) {
1486  case kNullType: return handler.Null();
1487  case kFalseType: return handler.Bool(false);
1488  case kTrueType: return handler.Bool(true);
1489 
1490  case kObjectType:
1491  if (!handler.StartObject())
1492  return false;
1493  for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) {
1494  RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of name by MemberIterator.
1495  if (!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.flags_ & kCopyFlag) != 0))
1496  return false;
1497  if (!m->value.Accept(handler))
1498  return false;
1499  }
1500  return handler.EndObject(data_.o.size);
1501 
1502  case kArrayType:
1503  if (!handler.StartArray())
1504  return false;
1505  for (GenericValue* v = data_.a.elements; v != data_.a.elements + data_.a.size; ++v)
1506  if (!v->Accept(handler))
1507  return false;
1508  return handler.EndArray(data_.a.size);
1509 
1510  case kStringType:
1511  return handler.String(GetString(), GetStringLength(), (flags_ & kCopyFlag) != 0);
1512 
1513  default:
1514  RAPIDJSON_ASSERT(GetType() == kNumberType);
1515  if (IsInt()) return handler.Int(data_.n.i.i);
1516  else if (IsUint()) return handler.Uint(data_.n.u.u);
1517  else if (IsInt64()) return handler.Int64(data_.n.i64);
1518  else if (IsUint64()) return handler.Uint64(data_.n.u64);
1519  else return handler.Double(data_.n.d);
1520  }
1521  }
1522 
1523 private:
1524  template <typename, typename> friend class GenericValue;
1525  template <typename, typename, typename> friend class GenericDocument;
1526 
1527  enum {
1528  kBoolFlag = 0x100,
1529  kNumberFlag = 0x200,
1530  kIntFlag = 0x400,
1531  kUintFlag = 0x800,
1532  kInt64Flag = 0x1000,
1533  kUint64Flag = 0x2000,
1534  kDoubleFlag = 0x4000,
1535  kStringFlag = 0x100000,
1536  kCopyFlag = 0x200000,
1537  kInlineStrFlag = 0x400000,
1538 
1539  // Initial flags of different types.
1540  kNullFlag = kNullType,
1541  kTrueFlag = kTrueType | kBoolFlag,
1542  kFalseFlag = kFalseType | kBoolFlag,
1543  kNumberIntFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag,
1544  kNumberUintFlag = kNumberType | kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag,
1545  kNumberInt64Flag = kNumberType | kNumberFlag | kInt64Flag,
1546  kNumberUint64Flag = kNumberType | kNumberFlag | kUint64Flag,
1547  kNumberDoubleFlag = kNumberType | kNumberFlag | kDoubleFlag,
1548  kNumberAnyFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag,
1549  kConstStringFlag = kStringType | kStringFlag,
1550  kCopyStringFlag = kStringType | kStringFlag | kCopyFlag,
1551  kShortStringFlag = kStringType | kStringFlag | kCopyFlag | kInlineStrFlag,
1552  kObjectFlag = kObjectType,
1553  kArrayFlag = kArrayType,
1554 
1555  kTypeMask = 0xFF // bitwise-and with mask of 0xFF can be optimized by compiler
1556  };
1557 
1558  static const SizeType kDefaultArrayCapacity = 16;
1559  static const SizeType kDefaultObjectCapacity = 16;
1560 
1561  struct String {
1562  const Ch* str;
1563  SizeType length;
1564  unsigned hashcode; //!< reserved
1565  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1566 
1567  // implementation detail: ShortString can represent zero-terminated strings up to MaxSize chars
1568  // (excluding the terminating zero) and store a value to determine the length of the contained
1569  // string in the last character str[LenPos] by storing "MaxSize - length" there. If the string
1570  // to store has the maximal length of MaxSize then str[LenPos] will be 0 and therefore act as
1571  // the string terminator as well. For getting the string length back from that value just use
1572  // "MaxSize - str[LenPos]".
1573  // This allows to store 11-chars strings in 32-bit mode and 15-chars strings in 64-bit mode
1574  // inline (for `UTF8`-encoded strings).
1575  struct ShortString {
1576  enum { MaxChars = sizeof(String) / sizeof(Ch), MaxSize = MaxChars - 1, LenPos = MaxSize };
1577  Ch str[MaxChars];
1578 
1579  inline static bool Usable(SizeType len) { return (MaxSize >= len); }
1580  inline void SetLength(SizeType len) { str[LenPos] = (Ch)(MaxSize - len); }
1581  inline SizeType GetLength() const { return (SizeType)(MaxSize - str[LenPos]); }
1582  }; // at most as many bytes as "String" above => 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1583 
1584  // By using proper binary layout, retrieval of different integer types do not need conversions.
1585  union Number {
1586 #if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN
1587  struct I {
1588  int i;
1589  char padding[4];
1590  }i;
1591  struct U {
1592  unsigned u;
1593  char padding2[4];
1594  }u;
1595 #else
1596  struct I {
1597  char padding[4];
1598  int i;
1599  }i;
1600  struct U {
1601  char padding2[4];
1602  unsigned u;
1603  }u;
1604 #endif
1605  int64_t i64;
1606  uint64_t u64;
1607  double d;
1608  }; // 8 bytes
1609 
1610  struct Object {
1611  Member* members;
1612  SizeType size;
1613  SizeType capacity;
1614  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1615 
1616  struct Array {
1617  GenericValue* elements;
1618  SizeType size;
1619  SizeType capacity;
1620  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1621 
1622  union Data {
1623  String s;
1624  ShortString ss;
1625  Number n;
1626  Object o;
1627  Array a;
1628  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1629 
1630  // Initialize this value as array with initial data, without calling destructor.
1631  void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) {
1632  flags_ = kArrayFlag;
1633  if (count) {
1634  data_.a.elements = (GenericValue*)allocator.Malloc(count * sizeof(GenericValue));
1635  std::memcpy(data_.a.elements, values, count * sizeof(GenericValue));
1636  }
1637  else
1638  data_.a.elements = NULL;
1639  data_.a.size = data_.a.capacity = count;
1640  }
1641 
1642  //! Initialize this value as object with initial data, without calling destructor.
1643  void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) {
1644  flags_ = kObjectFlag;
1645  if (count) {
1646  data_.o.members = (Member*)allocator.Malloc(count * sizeof(Member));
1647  std::memcpy(data_.o.members, members, count * sizeof(Member));
1648  }
1649  else
1650  data_.o.members = NULL;
1651  data_.o.size = data_.o.capacity = count;
1652  }
1653 
1654  //! Initialize this value as constant string, without calling destructor.
1655  void SetStringRaw(StringRefType s) RAPIDJSON_NOEXCEPT {
1656  flags_ = kConstStringFlag;
1657  data_.s.str = s;
1658  data_.s.length = s.length;
1659  }
1660 
1661  //! Initialize this value as copy string with initial data, without calling destructor.
1662  void SetStringRaw(StringRefType s, Allocator& allocator) {
1663  Ch* str = NULL;
1664  if(ShortString::Usable(s.length)) {
1665  flags_ = kShortStringFlag;
1666  data_.ss.SetLength(s.length);
1667  str = data_.ss.str;
1668  } else {
1669  flags_ = kCopyStringFlag;
1670  data_.s.length = s.length;
1671  str = (Ch *)allocator.Malloc((s.length + 1) * sizeof(Ch));
1672  data_.s.str = str;
1673  }
1674  std::memcpy(str, s, s.length * sizeof(Ch));
1675  str[s.length] = '\0';
1676  }
1677 
1678  //! Assignment without calling destructor
1679  void RawAssign(GenericValue& rhs) RAPIDJSON_NOEXCEPT {
1680  data_ = rhs.data_;
1681  flags_ = rhs.flags_;
1682  rhs.flags_ = kNullFlag;
1683  }
1684 
1685  template <typename SourceAllocator>
1686  bool StringEqual(const GenericValue<Encoding, SourceAllocator>& rhs) const {
1687  RAPIDJSON_ASSERT(IsString());
1688  RAPIDJSON_ASSERT(rhs.IsString());
1689 
1690  const SizeType len1 = GetStringLength();
1691  const SizeType len2 = rhs.GetStringLength();
1692  if(len1 != len2) { return false; }
1693 
1694  const Ch* const str1 = GetString();
1695  const Ch* const str2 = rhs.GetString();
1696  if(str1 == str2) { return true; } // fast path for constant string
1697 
1698  return (std::memcmp(str1, str2, sizeof(Ch) * len1) == 0);
1699  }
1700 
1701  Data data_;
1702  unsigned flags_;
1703 };
1704 
1705 //! GenericValue with UTF8 encoding
1707 
1708 ///////////////////////////////////////////////////////////////////////////////
1709 // GenericDocument
1710 
1711 //! A document for parsing JSON text as DOM.
1712 /*!
1713  \note implements Handler concept
1714  \tparam Encoding Encoding for both parsing and string storage.
1715  \tparam Allocator Allocator for allocating memory for the DOM
1716  \tparam StackAllocator Allocator for allocating memory for stack during parsing.
1717  \warning Although GenericDocument inherits from GenericValue, the API does \b not provide any virtual functions, especially no virtual destructor. To avoid memory leaks, do not \c delete a GenericDocument object via a pointer to a GenericValue.
1718 */
1719 template <typename Encoding, typename Allocator = MemoryPoolAllocator<>, typename StackAllocator = CrtAllocator>
1720 class GenericDocument : public GenericValue<Encoding, Allocator> {
1721 public:
1722  typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding.
1723  typedef GenericValue<Encoding, Allocator> ValueType; //!< Value type of the document.
1724  typedef Allocator AllocatorType; //!< Allocator type from template parameter.
1725 
1726  //! Constructor
1727  /*! \param allocator Optional allocator for allocating memory.
1728  \param stackCapacity Optional initial capacity of stack in bytes.
1729  \param stackAllocator Optional allocator for allocating memory for stack.
1730  */
1731  GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) :
1732  allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_()
1733  {
1734  if (!allocator_)
1735  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator());
1736  }
1737 
1738 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
1739  //! Move constructor in C++11
1740  GenericDocument(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT
1741  : ValueType(std::move(rhs)),
1742  allocator_(rhs.allocator_),
1743  ownAllocator_(rhs.ownAllocator_),
1744  stack_(std::move(rhs.stack_)),
1745  parseResult_(rhs.parseResult_)
1746  {
1747  rhs.allocator_ = 0;
1748  rhs.ownAllocator_ = 0;
1749  rhs.parseResult_ = ParseResult();
1750  }
1751 #endif
1752 
1753  ~GenericDocument() {
1754  Destroy();
1755  }
1756 
1757 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
1758  //! Move assignment in C++11
1759  GenericDocument& operator=(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT
1760  {
1761  // The cast to ValueType is necessary here, because otherwise it would
1762  // attempt to call GenericValue's templated assignment operator.
1763  ValueType::operator=(std::forward<ValueType>(rhs));
1764 
1765  // Calling the destructor here would prematurely call stack_'s destructor
1766  Destroy();
1767 
1768  allocator_ = rhs.allocator_;
1769  ownAllocator_ = rhs.ownAllocator_;
1770  stack_ = std::move(rhs.stack_);
1771  parseResult_ = rhs.parseResult_;
1772 
1773  rhs.allocator_ = 0;
1774  rhs.ownAllocator_ = 0;
1775  rhs.parseResult_ = ParseResult();
1776 
1777  return *this;
1778  }
1779 #endif
1780 
1781  //!@name Parse from stream
1782  //!@{
1783 
1784  //! Parse JSON text from an input stream (with Encoding conversion)
1785  /*! \tparam parseFlags Combination of \ref ParseFlag.
1786  \tparam SourceEncoding Encoding of input stream
1787  \tparam InputStream Type of input stream, implementing Stream concept
1788  \param is Input stream to be parsed.
1789  \return The document itself for fluent API.
1790  */
1791  template <unsigned parseFlags, typename SourceEncoding, typename InputStream>
1792  GenericDocument& ParseStream(InputStream& is) {
1793  ValueType::SetNull(); // Remove existing root if exist
1794  GenericReader<SourceEncoding, Encoding, StackAllocator> reader(&stack_.GetAllocator());
1795  ClearStackOnExit scope(*this);
1796  parseResult_ = reader.template Parse<parseFlags>(is, *this);
1797  if (parseResult_) {
1798  RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object
1799  this->RawAssign(*stack_.template Pop<ValueType>(1)); // Add this-> to prevent issue 13.
1800  }
1801  return *this;
1802  }
1803 
1804  //! Parse JSON text from an input stream
1805  /*! \tparam parseFlags Combination of \ref ParseFlag.
1806  \tparam InputStream Type of input stream, implementing Stream concept
1807  \param is Input stream to be parsed.
1808  \return The document itself for fluent API.
1809  */
1810  template <unsigned parseFlags, typename InputStream>
1811  GenericDocument& ParseStream(InputStream& is) {
1812  return ParseStream<parseFlags, Encoding, InputStream>(is);
1813  }
1814 
1815  //! Parse JSON text from an input stream (with \ref kParseDefaultFlags)
1816  /*! \tparam InputStream Type of input stream, implementing Stream concept
1817  \param is Input stream to be parsed.
1818  \return The document itself for fluent API.
1819  */
1820  template <typename InputStream>
1821  GenericDocument& ParseStream(InputStream& is) {
1822  return ParseStream<kParseDefaultFlags, Encoding, InputStream>(is);
1823  }
1824  //!@}
1825 
1826  //!@name Parse in-place from mutable string
1827  //!@{
1828 
1829  //! Parse JSON text from a mutable string
1830  /*! \tparam parseFlags Combination of \ref ParseFlag.
1831  \param str Mutable zero-terminated string to be parsed.
1832  \return The document itself for fluent API.
1833  */
1834  template <unsigned parseFlags>
1837  return ParseStream<parseFlags | kParseInsituFlag>(s);
1838  }
1839 
1840  //! Parse JSON text from a mutable string (with \ref kParseDefaultFlags)
1841  /*! \param str Mutable zero-terminated string to be parsed.
1842  \return The document itself for fluent API.
1843  */
1845  return ParseInsitu<kParseDefaultFlags>(str);
1846  }
1847  //!@}
1848 
1849  //!@name Parse from read-only string
1850  //!@{
1851 
1852  //! Parse JSON text from a read-only string (with Encoding conversion)
1853  /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag).
1854  \tparam SourceEncoding Transcoding from input Encoding
1855  \param str Read-only zero-terminated string to be parsed.
1856  */
1857  template <unsigned parseFlags, typename SourceEncoding>
1858  GenericDocument& Parse(const Ch* str) {
1859  RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag));
1861  return ParseStream<parseFlags, SourceEncoding>(s);
1862  }
1863 
1864  //! Parse JSON text from a read-only string
1865  /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag).
1866  \param str Read-only zero-terminated string to be parsed.
1867  */
1868  template <unsigned parseFlags>
1869  GenericDocument& Parse(const Ch* str) {
1870  return Parse<parseFlags, Encoding>(str);
1871  }
1872 
1873  //! Parse JSON text from a read-only string (with \ref kParseDefaultFlags)
1874  /*! \param str Read-only zero-terminated string to be parsed.
1875  */
1876  GenericDocument& Parse(const Ch* str) {
1877  return Parse<kParseDefaultFlags>(str);
1878  }
1879  //!@}
1880 
1881  //!@name Handling parse errors
1882  //!@{
1883 
1884  //! Whether a parse error has occured in the last parsing.
1885  bool HasParseError() const { return parseResult_.IsError(); }
1886 
1887  //! Get the \ref ParseErrorCode of last parsing.
1888  ParseErrorCode GetParseError() const { return parseResult_.Code(); }
1889 
1890  //! Get the position of last parsing error in input, 0 otherwise.
1891  size_t GetErrorOffset() const { return parseResult_.Offset(); }
1892 
1893  //!@}
1894 
1895  //! Get the allocator of this document.
1896  Allocator& GetAllocator() { return *allocator_; }
1897 
1898  //! Get the capacity of stack in bytes.
1899  size_t GetStackCapacity() const { return stack_.GetCapacity(); }
1900 
1901 private:
1902  // clear stack on any exit from ParseStream, e.g. due to exception
1903  struct ClearStackOnExit {
1904  explicit ClearStackOnExit(GenericDocument& d) : d_(d) {}
1905  ~ClearStackOnExit() { d_.ClearStack(); }
1906  private:
1907  ClearStackOnExit(const ClearStackOnExit&);
1908  ClearStackOnExit& operator=(const ClearStackOnExit&);
1909  GenericDocument& d_;
1910  };
1911 
1912  // callers of the following private Handler functions
1913  template <typename,typename,typename> friend class GenericReader; // for parsing
1914  template <typename, typename> friend class GenericValue; // for deep copying
1915 
1916  // Implementation of Handler
1917  bool Null() { new (stack_.template Push<ValueType>()) ValueType(); return true; }
1918  bool Bool(bool b) { new (stack_.template Push<ValueType>()) ValueType(b); return true; }
1919  bool Int(int i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
1920  bool Uint(unsigned i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
1921  bool Int64(int64_t i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
1922  bool Uint64(uint64_t i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
1923  bool Double(double d) { new (stack_.template Push<ValueType>()) ValueType(d); return true; }
1924 
1925  bool String(const Ch* str, SizeType length, bool copy) {
1926  if (copy)
1927  new (stack_.template Push<ValueType>()) ValueType(str, length, GetAllocator());
1928  else
1929  new (stack_.template Push<ValueType>()) ValueType(str, length);
1930  return true;
1931  }
1932 
1933  bool StartObject() { new (stack_.template Push<ValueType>()) ValueType(kObjectType); return true; }
1934 
1935  bool Key(const Ch* str, SizeType length, bool copy) { return String(str, length, copy); }
1936 
1937  bool EndObject(SizeType memberCount) {
1938  typename ValueType::Member* members = stack_.template Pop<typename ValueType::Member>(memberCount);
1939  stack_.template Top<ValueType>()->SetObjectRaw(members, (SizeType)memberCount, GetAllocator());
1940  return true;
1941  }
1942 
1943  bool StartArray() { new (stack_.template Push<ValueType>()) ValueType(kArrayType); return true; }
1944 
1945  bool EndArray(SizeType elementCount) {
1946  ValueType* elements = stack_.template Pop<ValueType>(elementCount);
1947  stack_.template Top<ValueType>()->SetArrayRaw(elements, elementCount, GetAllocator());
1948  return true;
1949  }
1950 
1951 private:
1952  //! Prohibit copying
1954  //! Prohibit assignment
1955  GenericDocument& operator=(const GenericDocument&);
1956 
1957  void ClearStack() {
1958  if (Allocator::kNeedFree)
1959  while (stack_.GetSize() > 0) // Here assumes all elements in stack array are GenericValue (Member is actually 2 GenericValue objects)
1960  (stack_.template Pop<ValueType>(1))->~ValueType();
1961  else
1962  stack_.Clear();
1963  stack_.ShrinkToFit();
1964  }
1965 
1966  void Destroy() {
1967  RAPIDJSON_DELETE(ownAllocator_);
1968  }
1969 
1970  static const size_t kDefaultStackCapacity = 1024;
1971  Allocator* allocator_;
1972  Allocator* ownAllocator_;
1973  internal::Stack<StackAllocator> stack_;
1974  ParseResult parseResult_;
1975 };
1976 
1977 //! GenericDocument with UTF8 encoding
1979 
1980 // defined here due to the dependency on GenericDocument
1981 template <typename Encoding, typename Allocator>
1982 template <typename SourceAllocator>
1983 inline
1985 {
1986  switch (rhs.GetType()) {
1987  case kObjectType:
1988  case kArrayType: { // perform deep copy via SAX Handler
1990  rhs.Accept(d);
1991  RawAssign(*d.stack_.template Pop<GenericValue>(1));
1992  }
1993  break;
1994  case kStringType:
1995  if (rhs.flags_ == kConstStringFlag) {
1996  flags_ = rhs.flags_;
1997  data_ = *reinterpret_cast<const Data*>(&rhs.data_);
1998  } else {
1999  SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator);
2000  }
2001  break;
2002  default: // kNumberType, kTrueType, kFalseType, kNullType
2003  flags_ = rhs.flags_;
2004  data_ = *reinterpret_cast<const Data*>(&rhs.data_);
2005  }
2006 }
2007 
2008 RAPIDJSON_NAMESPACE_END
2009 
2010 #if defined(_MSC_VER) || defined(__GNUC__)
2011 RAPIDJSON_DIAG_POP
2012 #endif
2013 
2014 #endif // RAPIDJSON_DOCUMENT_H_
BaseType::difference_type DifferenceType
Signed integer type (e.g. ptrdiff_t)
Definition: document.h:133
GenericValue< Encoding, Allocator > ValueType
Value type of the document.
Definition: document.h:1723
GenericValue(unsigned u) RAPIDJSON_NOEXCEPT
Constructor for unsigned value.
Definition: document.h:504
true
Definition: rapidjson.h:645
ValueIterator Erase(ConstValueIterator first, ConstValueIterator last)
Remove elements in the range [first, last) of the array.
Definition: document.h:1376
Definition: document.h:1587
Read-only string stream.
Definition: rapidjson.h:571
Concept for receiving events from GenericReader upon parsing. The functions return true if no error o...
GenericValue & Move() RAPIDJSON_NOEXCEPT
Prepare Value for move semantics
Definition: document.h:665
bool operator==(const GenericValue< Encoding, SourceAllocator > &rhs) const
Equal-to operator
Definition: document.h:676
ValueIterator Begin()
Element iterator
Definition: document.h:1259
GenericMemberIterator< true, Encoding, Allocator > ConstIterator
Constant iterator type
Definition: document.h:124
GenericValue(const Ch *s, SizeType length, Allocator &allocator)
Constructor for copy-string (i.e. do make a copy of string)
Definition: document.h:545
GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT
Constructor for int64_t value.
Definition: document.h:511
bool operator==(const std::basic_string< Ch > &rhs) const
Equal-to operator with string object
Definition: document.h:724
~GenericValue()
Destructor.
Definition: document.h:560
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:261
SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.
Definition: reader.h:374
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with kParseDefaultFlags)
Definition: document.h:1821
GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT
Constructor for constant string (i.e. do not make a copy of string)
Definition: document.h:542
GenericValue & SetString(const std::basic_string< Ch > &s, Allocator &allocator)
Set this value as a string by copying from source string.
Definition: document.h:1471
bool HasMember(const std::basic_string< Ch > &name) const
Check whether a member exists in the object with string object.
Definition: document.h:885
GenericValue & SetObject()
Set this value as an empty object.
Definition: document.h:799
GenericStringRef< Ch > StringRefType
Reference to a constant string
Definition: document.h:425
GenericMemberIterator Iterator
Iterator type itself
Definition: document.h:122
GenericValue & operator=(GenericValue &rhs) RAPIDJSON_NOEXCEPT
Assignment with move semantics.
Definition: document.h:593
bool operator==(const Ch *rhs) const
Equal-to operator with const C-string pointer
Definition: document.h:718
unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:322
GenericMemberIterator< false, Encoding, Allocator > NonConstIterator
Non-constant iterator type
Definition: document.h:126
SizeType Size() const
Get the number of elements in array.
Definition: document.h:1226
Allocator AllocatorType
Allocator type from template parameter.
Definition: document.h:1724
MemberIterator FindMember(const GenericValue< Encoding, SourceAllocator > &name)
Find member by name.
Definition: document.h:933
false
Definition: rapidjson.h:644
ParseErrorCode
Error code of parsing.
Definition: error.h:59
ConstMemberIterator MemberEnd() const
Const past-the-end member iterator
Definition: document.h:858
#define RAPIDJSON_STATIC_ASSERT(x)
(Internal) macro to check for conditions at compile-time
Definition: rapidjson.h:375
GenericValue(const Ch *s, Allocator &allocator)
Constructor for copy-string (i.e. do make a copy of string)
Definition: document.h:548
friend bool operator!=(const T &lhs, const GenericValue &rhs)
Not-Equal-to operator with arbitrary types (symmetric version)
Definition: document.h:754
bool RemoveMember(const Ch *name)
Remove a member in object by its name.
Definition: document.h:1131
GenericStringRef< CharType > StringRef(const std::basic_string< CharType > &str)
Mark a string object as constant string
Definition: document.h:383
GenericValue & PopBack()
Remove the last element in the array.
Definition: document.h:1350
GenericValue & Reserve(SizeType newCapacity, Allocator &allocator)
Request the array to have enough capacity to store elements.
Definition: document.h:1276
ValueIterator End()
Past-the-end element iterator
Definition: document.h:1262
bool operator!=(const Ch *rhs) const
Not-equal-to operator with const C-string pointer
Definition: document.h:739
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string (with kParseDefaultFlags)
Definition: document.h:1876
MemberIterator RemoveMember(MemberIterator m)
Remove a member in object by iterator.
Definition: document.h:1159
MemberIterator MemberBegin()
Member iterator
Definition: document.h:861
Allocator AllocatorType
Allocator type from template parameter.
Definition: document.h:423
bool Empty() const
Check whether the array is empty.
Definition: document.h:1232
GenericValue & AddMember(StringRefType name, StringRefType value, Allocator &allocator)
Add a constant string value as member (name-value pair) to the object.
Definition: document.h:1083
ConstMemberIterator MemberBegin() const
Const member iterator
Definition: document.h:855
GenericValue & AddMember(GenericValue &name, std::basic_string< Ch > &value, Allocator &allocator)
Add a string object as member (name-value pair) to the object.
Definition: document.h:1012
GenericValue & operator[](const std::basic_string< Ch > &name)
Get a value from an object associated with name (string object).
Definition: document.h:849
GenericValue(const Ch *s, SizeType length) RAPIDJSON_NOEXCEPT
Constructor for constant string (i.e. do not make a copy of string)
Definition: document.h:539
GenericValue & operator[](const GenericValue< Encoding, SourceAllocator > &name)
Get a value from an object associated with the name.
Definition: document.h:834
GenericValue & SetString(const Ch *s, SizeType length, Allocator &allocator)
Set this value as a string by copying from source string.
Definition: document.h:1453
MemberIterator EraseMember(ConstMemberIterator pos)
Remove a member from an object by iterator.
Definition: document.h:1187
GenericStringRef(const CharType *str, SizeType len)
Create constant string reference from pointer and length
Definition: document.h:315
Name-value pair in a JSON object value.
Definition: document.h:79
GenericValue & SetString(const Ch *s, SizeType length)
Set this value as a string without copying source string.
Definition: document.h:1436
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string (with kParseDefaultFlags)
Definition: document.h:1844
const SizeType length
length of the string (excluding the trailing NULL terminator)
Definition: document.h:322
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: document.h:1891
BaseType::reference Reference
Reference to (const) GenericMember
Definition: document.h:131
const GenericValue * ConstValueIterator
Constant value iterator for iterating in array.
Definition: document.h:429
MemberIterator FindMember(const std::basic_string< Ch > &name)
Find member by string object name.
Definition: document.h:952
GenericValue & AddMember(GenericValue &name, GenericValue &value, Allocator &allocator)
Add a member (name-value pair) to the object.
Definition: document.h:966
Result of parsing (wraps ParseErrorCode)
Definition: error.h:101
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion)
Definition: document.h:1792
MemberIterator FindMember(const Ch *name)
Find member by name.
Definition: document.h:912
bool HasMember(const GenericValue< Encoding, SourceAllocator > &name) const
Check whether a member exists in the object with GenericValue name.
Definition: document.h:898
void Clear()
Remove all elements in the array.
Definition: document.h:1238
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string
Definition: document.h:1869
friend bool operator==(const T &lhs, const GenericValue &rhs)
Equal-to operator with arbitrary types (symmetric version)
Definition: document.h:749
Type
Type of JSON value
Definition: rapidjson.h:642
Encoding::Ch Ch
Character type derived from Encoding.
Definition: document.h:1722
GenericValue & operator[](SizeType index)
Get an element from array by index.
Definition: document.h:1250
BaseType::pointer Pointer
Pointer to (const) GenericMember
Definition: document.h:129
object
Definition: rapidjson.h:646
GenericValue & PushBack(GenericValue &value, Allocator &allocator)
Append a GenericValue at the end of the array.
Definition: document.h:1295
GenericValue & operator[](T *name)
Get a value from an object associated with the name.
Definition: document.h:817
GenericValue & Swap(GenericValue &other) RAPIDJSON_NOEXCEPT
Exchange the contents of this value with those of other.
Definition: document.h:655
MemberIterator MemberEnd()
Past-the-end member iterator
Definition: document.h:864
GenericStringRef(const CharType(&str)[N]) RAPIDJSON_NOEXCEPT
Create string reference from const character array
Definition: document.h:283
GenericValue(const std::basic_string< Ch > &s, Allocator &allocator)
Constructor for copy-string from a string object (i.e. do make a copy of string)
Definition: document.h:554
#define RAPIDJSON_NEW(x)
! customization point for global new
Definition: rapidjson.h:480
GenericValue(Type type) RAPIDJSON_NOEXCEPT
Constructor with JSON value type.
Definition: document.h:456
A document for parsing JSON text as DOM.
Definition: document.h:1720
array
Definition: rapidjson.h:647
GenericValue * ValueIterator
Value iterator for iterating in array.
Definition: document.h:428
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:484
Encoding::Ch Ch
Character type derived from Encoding.
Definition: document.h:424
Definition: document.h:1591
GenericMemberIterator< false, Encoding, Allocator >::Iterator MemberIterator
Member iterator for iterating in object.
Definition: document.h:426
GenericValue(bool b) RAPIDJSON_NOEXCEPT
Constructor for boolean value.
Definition: document.h:489
GenericValue(int i) RAPIDJSON_NOEXCEPT
Constructor for int value.
Definition: document.h:497
GenericValue< Encoding, Allocator > value
value of member.
Definition: document.h:81
GenericValue() RAPIDJSON_NOEXCEPT
Default constructor creates a null value.
Definition: document.h:436
GenericStringRef(const CharType *str)
Explicitly create string reference from const character pointer
Definition: document.h:305
SizeType MemberCount() const
Get the number of members in the object.
Definition: document.h:802
null
Definition: rapidjson.h:643
GenericDocument(Allocator *allocator=0, size_t stackCapacity=kDefaultStackCapacity, StackAllocator *stackAllocator=0)
Constructor
Definition: document.h:1731
ParseErrorCode GetParseError() const
Get the ParseErrorCode of last parsing.
Definition: document.h:1888
DifferenceType operator-(ConstIterator that) const
Distance
Definition: document.h:194
GenericValue< Encoding, Allocator > name
name of member (must be a string)
Definition: document.h:80
string
Definition: rapidjson.h:648
Allocator & GetAllocator()
Get the allocator of this document.
Definition: document.h:1896
GenericMember< Encoding, Allocator > Member
Name-value pair in an object.
Definition: document.h:421
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream
Definition: document.h:1811
CharType Ch
character type of the string
Definition: document.h:257
Encoding EncodingType
Encoding type from template parameter.
Definition: document.h:422
bool ObjectEmpty() const
Check whether the object is empty.
Definition: document.h:805
bool GetBool() const
Set boolean value
Definition: document.h:787
GenericValue & SetString(StringRefType s)
Set this value as a string without copying source string.
Definition: document.h:1443
void RemoveAllMembers()
Remove all members in the object.
Definition: document.h:1116
GenericMemberIterator(const NonConstIterator &it)
Iterator conversions to more const
Definition: document.h:157
GenericValue & operator=(StringRefType str) RAPIDJSON_NOEXCEPT
Assignment of constant string reference (no copy)
Definition: document.h:612
In-situ(destructive) parsing.
Definition: reader.h:138
bool HasParseError() const
Whether a parse error has occured in the last parsing.
Definition: document.h:1885
SizeType Capacity() const
Get the capacity of array.
Definition: document.h:1229
Reference to a constant string (not taking a copy)
Definition: document.h:256
GenericValue(double d) RAPIDJSON_NOEXCEPT
Constructor for double value.
Definition: document.h:536
GenericValue & SetBool(bool b)
Definition: document.h:790
ConstValueIterator End() const
Constant past-the-end element iterator
Definition: document.h:1268
bool operator==(const T &rhs) const
Equal-to operator with primitive types
Definition: document.h:730
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
Definition: document.h:70
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding
Definition: document.h:1706
GenericValue & CopyFrom(const GenericValue< Encoding, SourceAllocator > &rhs, Allocator &allocator)
Deep-copy assignment from Value
Definition: document.h:643
GenericValue & SetArray()
Set this value as an empty array.
Definition: document.h:1223
(Constant) member iterator for a JSON object value
Definition: document.h:109
GenericValue & SetString(const Ch *s, Allocator &allocator)
Set this value as a string by copying from source string.
Definition: document.h:1461
bool Accept(Handler &handler) const
Generate events of this value to a Handler.
Definition: document.h:1484
MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last)
Remove members in the range [first, last) from an object.
Definition: document.h:1200
const Ch *const s
plain CharType pointer
Definition: document.h:321
ConstValueIterator Begin() const
Constant element iterator
Definition: document.h:1265
GenericMemberIterator< true, Encoding, Allocator >::Iterator ConstMemberIterator
Constant member iterator for iterating in object.
Definition: document.h:427
GenericMemberIterator()
Default constructor (singular value)
Definition: document.h:139
GenericValue & AddMember(GenericValue &name, StringRefType value, Allocator &allocator)
Add a constant string value as member (name-value pair) to the object.
Definition: document.h:997
GenericValue< Encoding, Allocator > ValueType
Value type of itself.
Definition: document.h:430
GenericValue & PushBack(StringRefType value, Allocator &allocator)
Append a constant string reference at the end of the array.
Definition: document.h:1318
GenericValue & AddMember(StringRefType name, GenericValue &value, Allocator &allocator)
Add a member (name-value pair) to the object.
Definition: document.h:1069
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
bool HasMember(const Ch *name) const
Check whether a member exists in the object.
Definition: document.h:874
bool operator!=(const T &rhs) const
Not-equal-to operator with arbitrary types
Definition: document.h:744
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding
Definition: document.h:1978
GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT
Constructor for uint64_t value.
Definition: document.h:525
number
Definition: rapidjson.h:649
SizeType GetStringLength() const
Get the length of string.
Definition: document.h:1426
bool operator!=(const GenericValue< Encoding, SourceAllocator > &rhs) const
Not-equal-to operator
Definition: document.h:736
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string
Definition: document.h:1835
size_t GetStackCapacity() const
Get the capacity of stack in bytes.
Definition: document.h:1899
ValueIterator Erase(ConstValueIterator pos)
Remove an element of array by iterator.
Definition: document.h:1364
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string (with Encoding conversion)
Definition: document.h:1858
A read-write string stream.
Definition: rapidjson.h:605