All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
document.h
Go to the documentation of this file.
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 "memorystream.h"
24 #include "encodedstream.h"
25 #include <new> // placement new
26 #include <limits>
27 
28 RAPIDJSON_DIAG_PUSH
29 #ifdef _MSC_VER
30 RAPIDJSON_DIAG_OFF(4127) // conditional expression is constant
31 RAPIDJSON_DIAG_OFF(4244) // conversion from kXxxFlags to 'uint16_t', possible loss of data
32 #ifdef _MINWINDEF_ // see: http://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max
33 #ifndef NOMINMAX
34 #pragma push_macro("min")
35 #pragma push_macro("max")
36 #undef min
37 #undef max
38 #endif
39 #endif
40 #endif
41 
42 #ifdef __clang__
43 RAPIDJSON_DIAG_OFF(padded)
44 RAPIDJSON_DIAG_OFF(switch-enum)
45 RAPIDJSON_DIAG_OFF(c++98-compat)
46 #endif
47 
48 #ifdef __GNUC__
49 RAPIDJSON_DIAG_OFF(effc++)
50 #if __GNUC__ >= 6
51 RAPIDJSON_DIAG_OFF(terminate) // ignore throwing RAPIDJSON_ASSERT in RAPIDJSON_NOEXCEPT functions
52 #endif
53 #endif // __GNUC__
54 
55 #ifndef RAPIDJSON_NOMEMBERITERATORCLASS
56 #include <iterator> // std::iterator, std::random_access_iterator_tag
57 #endif
58 
59 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
60 #include <utility> // std::move
61 #endif
62 
63 RAPIDJSON_NAMESPACE_BEGIN
64 
65 // Forward declaration.
66 template <typename Encoding, typename Allocator>
68 
69 template <typename Encoding, typename Allocator, typename StackAllocator>
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  Iterator& operator=(const NonConstIterator & it) { ptr_ = it.ptr_; return *this; }
159 
160  //! @name stepping
161  //@{
162  Iterator& operator++(){ ++ptr_; return *this; }
163  Iterator& operator--(){ --ptr_; return *this; }
164  Iterator operator++(int){ Iterator old(*this); ++ptr_; return old; }
165  Iterator operator--(int){ Iterator old(*this); --ptr_; return old; }
166  //@}
167 
168  //! @name increment/decrement
169  //@{
170  Iterator operator+(DifferenceType n) const { return Iterator(ptr_+n); }
171  Iterator operator-(DifferenceType n) const { return Iterator(ptr_-n); }
172 
173  Iterator& operator+=(DifferenceType n) { ptr_+=n; return *this; }
174  Iterator& operator-=(DifferenceType n) { ptr_-=n; return *this; }
175  //@}
176 
177  //! @name relations
178  //@{
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  bool operator> (ConstIterator that) const { return ptr_ > that.ptr_; }
185  //@}
186 
187  //! @name dereference
188  //@{
189  Reference operator*() const { return *ptr_; }
190  Pointer operator->() const { return ptr_; }
191  Reference operator[](DifferenceType n) const { return ptr_[n]; }
192  //@}
193 
194  //! Distance
195  DifferenceType operator-(ConstIterator that) const { return ptr_-that.ptr_; }
196 
197 private:
198  //! Internal constructor from plain pointer
199  explicit GenericMemberIterator(Pointer p) : ptr_(p) {}
200 
201  Pointer ptr_; //!< raw pointer
202 };
203 
204 #else // RAPIDJSON_NOMEMBERITERATORCLASS
205 
206 // class-based member iterator implementation disabled, use plain pointers
207 
208 template <bool Const, typename Encoding, typename Allocator>
209 struct GenericMemberIterator;
210 
211 //! non-const GenericMemberIterator
212 template <typename Encoding, typename Allocator>
213 struct GenericMemberIterator<false,Encoding,Allocator> {
214  //! use plain pointer as iterator type
215  typedef GenericMember<Encoding,Allocator>* Iterator;
216 };
217 //! const GenericMemberIterator
218 template <typename Encoding, typename Allocator>
219 struct GenericMemberIterator<true,Encoding,Allocator> {
220  //! use plain const pointer as iterator type
221  typedef const GenericMember<Encoding,Allocator>* Iterator;
222 };
223 
224 #endif // RAPIDJSON_NOMEMBERITERATORCLASS
225 
226 ///////////////////////////////////////////////////////////////////////////////
227 // GenericStringRef
228 
229 //! Reference to a constant string (not taking a copy)
230 /*!
231  \tparam CharType character type of the string
232 
233  This helper class is used to automatically infer constant string
234  references for string literals, especially from \c const \b (!)
235  character arrays.
236 
237  The main use is for creating JSON string values without copying the
238  source string via an \ref Allocator. This requires that the referenced
239  string pointers have a sufficient lifetime, which exceeds the lifetime
240  of the associated GenericValue.
241 
242  \b Example
243  \code
244  Value v("foo"); // ok, no need to copy & calculate length
245  const char foo[] = "foo";
246  v.SetString(foo); // ok
247 
248  const char* bar = foo;
249  // Value x(bar); // not ok, can't rely on bar's lifetime
250  Value x(StringRef(bar)); // lifetime explicitly guaranteed by user
251  Value y(StringRef(bar, 3)); // ok, explicitly pass length
252  \endcode
253 
254  \see StringRef, GenericValue::SetString
255 */
256 template<typename CharType>
258  typedef CharType Ch; //!< character type of the string
259 
260  //! Create string reference from \c const character array
261 #ifndef __clang__ // -Wdocumentation
262  /*!
263  This constructor implicitly creates a constant string reference from
264  a \c const character array. It has better performance than
265  \ref StringRef(const CharType*) by inferring the string \ref length
266  from the array length, and also supports strings containing null
267  characters.
268 
269  \tparam N length of the string, automatically inferred
270 
271  \param str Constant character array, lifetime assumed to be longer
272  than the use of the string in e.g. a GenericValue
273 
274  \post \ref s == str
275 
276  \note Constant complexity.
277  \note There is a hidden, private overload to disallow references to
278  non-const character arrays to be created via this constructor.
279  By this, e.g. function-scope arrays used to be filled via
280  \c snprintf are excluded from consideration.
281  In such cases, the referenced string should be \b copied to the
282  GenericValue instead.
283  */
284 #endif
285  template<SizeType N>
286  GenericStringRef(const CharType (&str)[N]) RAPIDJSON_NOEXCEPT
287  : s(str), length(N-1) {}
288 
289  //! Explicitly create string reference from \c const character pointer
290 #ifndef __clang__ // -Wdocumentation
291  /*!
292  This constructor can be used to \b explicitly create a reference to
293  a constant string pointer.
294 
295  \see StringRef(const CharType*)
296 
297  \param str Constant character pointer, lifetime assumed to be longer
298  than the use of the string in e.g. a GenericValue
299 
300  \post \ref s == str
301 
302  \note There is a hidden, private overload to disallow references to
303  non-const character arrays to be created via this constructor.
304  By this, e.g. function-scope arrays used to be filled via
305  \c snprintf are excluded from consideration.
306  In such cases, the referenced string should be \b copied to the
307  GenericValue instead.
308  */
309 #endif
310  explicit GenericStringRef(const CharType* str)
311  : s(str), length(internal::StrLen(str)){ RAPIDJSON_ASSERT(s != 0); }
312 
313  //! Create constant string reference from pointer and length
314 #ifndef __clang__ // -Wdocumentation
315  /*! \param str constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
316  \param len length of the string, excluding the trailing NULL terminator
317 
318  \post \ref s == str && \ref length == len
319  \note Constant complexity.
320  */
321 #endif
322  GenericStringRef(const CharType* str, SizeType len)
323  : s(str), length(len) { RAPIDJSON_ASSERT(s != 0); }
324 
325  GenericStringRef(const GenericStringRef& rhs) : s(rhs.s), length(rhs.length) {}
326 
327  //! implicit conversion to plain CharType pointer
328  operator const Ch *() const { return s; }
329 
330  const Ch* const s; //!< plain CharType pointer
331  const SizeType length; //!< length of the string (excluding the trailing NULL terminator)
332 
333 private:
334  //! Disallow construction from non-const array
335  template<SizeType N>
336  GenericStringRef(CharType (&str)[N]) /* = delete */;
337  //! Copy assignment operator not permitted - immutable type
338  GenericStringRef& operator=(const GenericStringRef& rhs) /* = delete */;
339 };
340 
341 //! Mark a character pointer as constant string
342 /*! Mark a plain character pointer as a "string literal". This function
343  can be used to avoid copying a character string to be referenced as a
344  value in a JSON GenericValue object, if the string's lifetime is known
345  to be valid long enough.
346  \tparam CharType Character type of the string
347  \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
348  \return GenericStringRef string reference object
349  \relatesalso GenericStringRef
350 
351  \see GenericValue::GenericValue(StringRefType), GenericValue::operator=(StringRefType), GenericValue::SetString(StringRefType), GenericValue::PushBack(StringRefType, Allocator&), GenericValue::AddMember
352 */
353 template<typename CharType>
354 inline GenericStringRef<CharType> StringRef(const CharType* str) {
355  return GenericStringRef<CharType>(str, internal::StrLen(str));
356 }
357 
358 //! Mark a character pointer as constant string
359 /*! Mark a plain character pointer as a "string literal". This function
360  can be used to avoid copying a character string to be referenced as a
361  value in a JSON GenericValue object, if the string's lifetime is known
362  to be valid long enough.
363 
364  This version has better performance with supplied length, and also
365  supports string containing null characters.
366 
367  \tparam CharType character type of the string
368  \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
369  \param length The length of source string.
370  \return GenericStringRef string reference object
371  \relatesalso GenericStringRef
372 */
373 template<typename CharType>
374 inline GenericStringRef<CharType> StringRef(const CharType* str, size_t length) {
375  return GenericStringRef<CharType>(str, SizeType(length));
376 }
377 
378 #if RAPIDJSON_HAS_STDSTRING
379 //! Mark a string object as constant string
380 /*! Mark a string object (e.g. \c std::string) as a "string literal".
381  This function can be used to avoid copying a string to be referenced as a
382  value in a JSON GenericValue object, if the string's lifetime is known
383  to be valid long enough.
384 
385  \tparam CharType character type of the string
386  \param str Constant string, lifetime assumed to be longer than the use of the string in e.g. a GenericValue
387  \return GenericStringRef string reference object
388  \relatesalso GenericStringRef
389  \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
390 */
391 template<typename CharType>
392 inline GenericStringRef<CharType> StringRef(const std::basic_string<CharType>& str) {
393  return GenericStringRef<CharType>(str.data(), SizeType(str.size()));
394 }
395 #endif
396 
397 ///////////////////////////////////////////////////////////////////////////////
398 // GenericValue type traits
399 namespace internal {
400 
401 template <typename T, typename Encoding = void, typename Allocator = void>
402 struct IsGenericValueImpl : FalseType {};
403 
404 // select candidates according to nested encoding and allocator types
405 template <typename T> struct IsGenericValueImpl<T, typename Void<typename T::EncodingType>::Type, typename Void<typename T::AllocatorType>::Type>
406  : IsBaseOf<GenericValue<typename T::EncodingType, typename T::AllocatorType>, T>::Type {};
407 
408 // helper to match arbitrary GenericValue instantiations, including derived classes
409 template <typename T> struct IsGenericValue : IsGenericValueImpl<T>::Type {};
410 
411 } // namespace internal
412 
413 ///////////////////////////////////////////////////////////////////////////////
414 // TypeHelper
415 
416 namespace internal {
417 
418 template <typename ValueType, typename T>
419 struct TypeHelper {};
420 
421 template<typename ValueType>
422 struct TypeHelper<ValueType, bool> {
423  static bool Is(const ValueType& v) { return v.IsBool(); }
424  static bool Get(const ValueType& v) { return v.GetBool(); }
425  static ValueType& Set(ValueType& v, bool data) { return v.SetBool(data); }
426  static ValueType& Set(ValueType& v, bool data, typename ValueType::AllocatorType&) { return v.SetBool(data); }
427 };
428 
429 template<typename ValueType>
430 struct TypeHelper<ValueType, int> {
431  static bool Is(const ValueType& v) { return v.IsInt(); }
432  static int Get(const ValueType& v) { return v.GetInt(); }
433  static ValueType& Set(ValueType& v, int data) { return v.SetInt(data); }
434  static ValueType& Set(ValueType& v, int data, typename ValueType::AllocatorType&) { return v.SetInt(data); }
435 };
436 
437 template<typename ValueType>
438 struct TypeHelper<ValueType, unsigned> {
439  static bool Is(const ValueType& v) { return v.IsUint(); }
440  static unsigned Get(const ValueType& v) { return v.GetUint(); }
441  static ValueType& Set(ValueType& v, unsigned data) { return v.SetUint(data); }
442  static ValueType& Set(ValueType& v, unsigned data, typename ValueType::AllocatorType&) { return v.SetUint(data); }
443 };
444 
445 template<typename ValueType>
446 struct TypeHelper<ValueType, int64_t> {
447  static bool Is(const ValueType& v) { return v.IsInt64(); }
448  static int64_t Get(const ValueType& v) { return v.GetInt64(); }
449  static ValueType& Set(ValueType& v, int64_t data) { return v.SetInt64(data); }
450  static ValueType& Set(ValueType& v, int64_t data, typename ValueType::AllocatorType&) { return v.SetInt64(data); }
451 };
452 
453 template<typename ValueType>
454 struct TypeHelper<ValueType, uint64_t> {
455  static bool Is(const ValueType& v) { return v.IsUint64(); }
456  static uint64_t Get(const ValueType& v) { return v.GetUint64(); }
457  static ValueType& Set(ValueType& v, uint64_t data) { return v.SetUint64(data); }
458  static ValueType& Set(ValueType& v, uint64_t data, typename ValueType::AllocatorType&) { return v.SetUint64(data); }
459 };
460 
461 template<typename ValueType>
462 struct TypeHelper<ValueType, double> {
463  static bool Is(const ValueType& v) { return v.IsDouble(); }
464  static double Get(const ValueType& v) { return v.GetDouble(); }
465  static ValueType& Set(ValueType& v, double data) { return v.SetDouble(data); }
466  static ValueType& Set(ValueType& v, double data, typename ValueType::AllocatorType&) { return v.SetDouble(data); }
467 };
468 
469 template<typename ValueType>
470 struct TypeHelper<ValueType, float> {
471  static bool Is(const ValueType& v) { return v.IsFloat(); }
472  static float Get(const ValueType& v) { return v.GetFloat(); }
473  static ValueType& Set(ValueType& v, float data) { return v.SetFloat(data); }
474  static ValueType& Set(ValueType& v, float data, typename ValueType::AllocatorType&) { return v.SetFloat(data); }
475 };
476 
477 template<typename ValueType>
478 struct TypeHelper<ValueType, const typename ValueType::Ch*> {
479  typedef const typename ValueType::Ch* StringType;
480  static bool Is(const ValueType& v) { return v.IsString(); }
481  static StringType Get(const ValueType& v) { return v.GetString(); }
482  static ValueType& Set(ValueType& v, const StringType data) { return v.SetString(typename ValueType::StringRefType(data)); }
483  static ValueType& Set(ValueType& v, const StringType data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); }
484 };
485 
486 #if RAPIDJSON_HAS_STDSTRING
487 template<typename ValueType>
488 struct TypeHelper<ValueType, std::basic_string<typename ValueType::Ch> > {
489  typedef std::basic_string<typename ValueType::Ch> StringType;
490  static bool Is(const ValueType& v) { return v.IsString(); }
491  static StringType Get(const ValueType& v) { return StringType(v.GetString(), v.GetStringLength()); }
492  static ValueType& Set(ValueType& v, const StringType& data, typename ValueType::AllocatorType& a) { return v.SetString(data, a); }
493 };
494 #endif
495 
496 template<typename ValueType>
497 struct TypeHelper<ValueType, typename ValueType::Array> {
498  typedef typename ValueType::Array ArrayType;
499  static bool Is(const ValueType& v) { return v.IsArray(); }
500  static ArrayType Get(ValueType& v) { return v.GetArray(); }
501  static ValueType& Set(ValueType& v, ArrayType data) { return v = data; }
502  static ValueType& Set(ValueType& v, ArrayType data, typename ValueType::AllocatorType&) { return v = data; }
503 };
504 
505 template<typename ValueType>
506 struct TypeHelper<ValueType, typename ValueType::ConstArray> {
507  typedef typename ValueType::ConstArray ArrayType;
508  static bool Is(const ValueType& v) { return v.IsArray(); }
509  static ArrayType Get(const ValueType& v) { return v.GetArray(); }
510 };
511 
512 template<typename ValueType>
513 struct TypeHelper<ValueType, typename ValueType::Object> {
514  typedef typename ValueType::Object ObjectType;
515  static bool Is(const ValueType& v) { return v.IsObject(); }
516  static ObjectType Get(ValueType& v) { return v.GetObject(); }
517  static ValueType& Set(ValueType& v, ObjectType data) { return v = data; }
518  static ValueType& Set(ValueType& v, ObjectType data, typename ValueType::AllocatorType&) { return v = data; }
519 };
520 
521 template<typename ValueType>
522 struct TypeHelper<ValueType, typename ValueType::ConstObject> {
523  typedef typename ValueType::ConstObject ObjectType;
524  static bool Is(const ValueType& v) { return v.IsObject(); }
525  static ObjectType Get(const ValueType& v) { return v.GetObject(); }
526 };
527 
528 } // namespace internal
529 
530 // Forward declarations
531 template <bool, typename> class GenericArray;
532 template <bool, typename> class GenericObject;
533 
534 ///////////////////////////////////////////////////////////////////////////////
535 // GenericValue
536 
537 //! Represents a JSON value. Use Value for UTF8 encoding and default allocator.
538 /*!
539  A JSON value can be one of 7 types. This class is a variant type supporting
540  these types.
541 
542  Use the Value if UTF8 and default allocator
543 
544  \tparam Encoding Encoding of the value. (Even non-string values need to have the same encoding in a document)
545  \tparam Allocator Allocator type for allocating memory of object, array and string.
546 */
547 template <typename Encoding, typename Allocator = MemoryPoolAllocator<> >
548 class GenericValue {
549 public:
550  //! Name-value pair in an object.
552  typedef Encoding EncodingType; //!< Encoding type from template parameter.
553  typedef Allocator AllocatorType; //!< Allocator type from template parameter.
554  typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding.
555  typedef GenericStringRef<Ch> StringRefType; //!< Reference to a constant string
556  typedef typename GenericMemberIterator<false,Encoding,Allocator>::Iterator MemberIterator; //!< Member iterator for iterating in object.
557  typedef typename GenericMemberIterator<true,Encoding,Allocator>::Iterator ConstMemberIterator; //!< Constant member iterator for iterating in object.
558  typedef GenericValue* ValueIterator; //!< Value iterator for iterating in array.
559  typedef const GenericValue* ConstValueIterator; //!< Constant value iterator for iterating in array.
560  typedef GenericValue<Encoding, Allocator> ValueType; //!< Value type of itself.
565 
566  //!@name Constructors and destructor.
567  //@{
568 
569  //! Default constructor creates a null value.
570  GenericValue() RAPIDJSON_NOEXCEPT : data_() { data_.f.flags = kNullFlag; }
571 
572 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
573  //! Move constructor in C++11
574  GenericValue(GenericValue&& rhs) RAPIDJSON_NOEXCEPT : data_(rhs.data_) {
575  rhs.data_.f.flags = kNullFlag; // give up contents
576  }
577 #endif
578 
579 private:
580  //! Copy constructor is not permitted.
581  GenericValue(const GenericValue& rhs);
582 
583 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
584  //! Moving from a GenericDocument is not permitted.
585  template <typename StackAllocator>
586  GenericValue(GenericDocument<Encoding,Allocator,StackAllocator>&& rhs);
587 
588  //! Move assignment from a GenericDocument is not permitted.
589  template <typename StackAllocator>
590  GenericValue& operator=(GenericDocument<Encoding,Allocator,StackAllocator>&& rhs);
591 #endif
592 
593 public:
594 
595  //! Constructor with JSON value type.
596  /*! This creates a Value of specified type with default content.
597  \param type Type of the value.
598  \note Default content for number is zero.
599  */
600  explicit GenericValue(Type type) RAPIDJSON_NOEXCEPT : data_() {
601  static const uint16_t defaultFlags[7] = {
602  kNullFlag, kFalseFlag, kTrueFlag, kObjectFlag, kArrayFlag, kShortStringFlag,
603  kNumberAnyFlag
604  };
605  RAPIDJSON_ASSERT(type <= kNumberType);
606  data_.f.flags = defaultFlags[type];
607 
608  // Use ShortString to store empty string.
609  if (type == kStringType)
610  data_.ss.SetLength(0);
611  }
612 
613  //! Explicit copy constructor (with allocator)
614  /*! Creates a copy of a Value by using the given Allocator
615  \tparam SourceAllocator allocator of \c rhs
616  \param rhs Value to copy from (read-only)
617  \param allocator Allocator for allocating copied elements and buffers. Commonly use GenericDocument::GetAllocator().
618  \param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer)
619  \see CopyFrom()
620  */
621  template <typename SourceAllocator>
622  GenericValue(const GenericValue<Encoding,SourceAllocator>& rhs, Allocator& allocator, bool copyConstStrings = false) {
623  switch (rhs.GetType()) {
624  case kObjectType: {
625  SizeType count = rhs.data_.o.size;
626  Member* lm = reinterpret_cast<Member*>(allocator.Malloc(count * sizeof(Member)));
627  const typename GenericValue<Encoding,SourceAllocator>::Member* rm = rhs.GetMembersPointer();
628  for (SizeType i = 0; i < count; i++) {
629  new (&lm[i].name) GenericValue(rm[i].name, allocator, copyConstStrings);
630  new (&lm[i].value) GenericValue(rm[i].value, allocator, copyConstStrings);
631  }
632  data_.f.flags = kObjectFlag;
633  data_.o.size = data_.o.capacity = count;
634  SetMembersPointer(lm);
635  }
636  break;
637  case kArrayType: {
638  SizeType count = rhs.data_.a.size;
639  GenericValue* le = reinterpret_cast<GenericValue*>(allocator.Malloc(count * sizeof(GenericValue)));
640  const GenericValue<Encoding,SourceAllocator>* re = rhs.GetElementsPointer();
641  for (SizeType i = 0; i < count; i++)
642  new (&le[i]) GenericValue(re[i], allocator, copyConstStrings);
643  data_.f.flags = kArrayFlag;
644  data_.a.size = data_.a.capacity = count;
645  SetElementsPointer(le);
646  }
647  break;
648  case kStringType:
649  if (rhs.data_.f.flags == kConstStringFlag && !copyConstStrings) {
650  data_.f.flags = rhs.data_.f.flags;
651  data_ = *reinterpret_cast<const Data*>(&rhs.data_);
652  }
653  else
654  SetStringRaw(StringRef(rhs.GetString(), rhs.GetStringLength()), allocator);
655  break;
656  default:
657  data_.f.flags = rhs.data_.f.flags;
658  data_ = *reinterpret_cast<const Data*>(&rhs.data_);
659  break;
660  }
661  }
662 
663  //! Constructor for boolean value.
664  /*! \param b Boolean value
665  \note This constructor is limited to \em real boolean values and rejects
666  implicitly converted types like arbitrary pointers. Use an explicit cast
667  to \c bool, if you want to construct a boolean JSON value in such cases.
668  */
669 #ifndef RAPIDJSON_DOXYGEN_RUNNING // hide SFINAE from Doxygen
670  template <typename T>
671  explicit GenericValue(T b, RAPIDJSON_ENABLEIF((internal::IsSame<bool, T>))) RAPIDJSON_NOEXCEPT // See #472
672 #else
673  explicit GenericValue(bool b) RAPIDJSON_NOEXCEPT
674 #endif
675  : data_() {
676  // safe-guard against failing SFINAE
678  data_.f.flags = b ? kTrueFlag : kFalseFlag;
679  }
680 
681  //! Constructor for int value.
682  explicit GenericValue(int i) RAPIDJSON_NOEXCEPT : data_() {
683  data_.n.i64 = i;
684  data_.f.flags = (i >= 0) ? (kNumberIntFlag | kUintFlag | kUint64Flag) : kNumberIntFlag;
685  }
686 
687  //! Constructor for unsigned value.
688  explicit GenericValue(unsigned u) RAPIDJSON_NOEXCEPT : data_() {
689  data_.n.u64 = u;
690  data_.f.flags = (u & 0x80000000) ? kNumberUintFlag : (kNumberUintFlag | kIntFlag | kInt64Flag);
691  }
692 
693  //! Constructor for int64_t value.
694  explicit GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT : data_() {
695  data_.n.i64 = i64;
696  data_.f.flags = kNumberInt64Flag;
697  if (i64 >= 0) {
698  data_.f.flags |= kNumberUint64Flag;
699  if (!(static_cast<uint64_t>(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
700  data_.f.flags |= kUintFlag;
701  if (!(static_cast<uint64_t>(i64) & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
702  data_.f.flags |= kIntFlag;
703  }
704  else if (i64 >= static_cast<int64_t>(RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
705  data_.f.flags |= kIntFlag;
706  }
707 
708  //! Constructor for uint64_t value.
709  explicit GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT : data_() {
710  data_.n.u64 = u64;
711  data_.f.flags = kNumberUint64Flag;
712  if (!(u64 & RAPIDJSON_UINT64_C2(0x80000000, 0x00000000)))
713  data_.f.flags |= kInt64Flag;
714  if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x00000000)))
715  data_.f.flags |= kUintFlag;
716  if (!(u64 & RAPIDJSON_UINT64_C2(0xFFFFFFFF, 0x80000000)))
717  data_.f.flags |= kIntFlag;
718  }
719 
720  //! Constructor for double value.
721  explicit GenericValue(double d) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = d; data_.f.flags = kNumberDoubleFlag; }
722 
723  //! Constructor for float value.
724  explicit GenericValue(float f) RAPIDJSON_NOEXCEPT : data_() { data_.n.d = static_cast<double>(f); data_.f.flags = kNumberDoubleFlag; }
725 
726  //! Constructor for constant string (i.e. do not make a copy of string)
727  GenericValue(const Ch* s, SizeType length) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(StringRef(s, length)); }
728 
729  //! Constructor for constant string (i.e. do not make a copy of string)
730  explicit GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT : data_() { SetStringRaw(s); }
731 
732  //! Constructor for copy-string (i.e. do make a copy of string)
733  GenericValue(const Ch* s, SizeType length, Allocator& allocator) : data_() { SetStringRaw(StringRef(s, length), allocator); }
734 
735  //! Constructor for copy-string (i.e. do make a copy of string)
736  GenericValue(const Ch*s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); }
737 
738 #if RAPIDJSON_HAS_STDSTRING
739  //! Constructor for copy-string from a string object (i.e. do make a copy of string)
740  /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
741  */
742  GenericValue(const std::basic_string<Ch>& s, Allocator& allocator) : data_() { SetStringRaw(StringRef(s), allocator); }
743 #endif
744 
745  //! Constructor for Array.
746  /*!
747  \param a An array obtained by \c GetArray().
748  \note \c Array is always pass-by-value.
749  \note the source array is moved into this value and the sourec array becomes empty.
750  */
751  GenericValue(Array a) RAPIDJSON_NOEXCEPT : data_(a.value_.data_) {
752  a.value_.data_ = Data();
753  a.value_.data_.f.flags = kArrayFlag;
754  }
755 
756  //! Constructor for Object.
757  /*!
758  \param o An object obtained by \c GetObject().
759  \note \c Object is always pass-by-value.
760  \note the source object is moved into this value and the sourec object becomes empty.
761  */
762  GenericValue(Object o) RAPIDJSON_NOEXCEPT : data_(o.value_.data_) {
763  o.value_.data_ = Data();
764  o.value_.data_.f.flags = kObjectFlag;
765  }
766 
767  //! Destructor.
768  /*! Need to destruct elements of array, members of object, or copy-string.
769  */
771  if (Allocator::kNeedFree) { // Shortcut by Allocator's trait
772  switch(data_.f.flags) {
773  case kArrayFlag:
774  {
775  GenericValue* e = GetElementsPointer();
776  for (GenericValue* v = e; v != e + data_.a.size; ++v)
777  v->~GenericValue();
778  Allocator::Free(e);
779  }
780  break;
781 
782  case kObjectFlag:
783  for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m)
784  m->~Member();
785  Allocator::Free(GetMembersPointer());
786  break;
787 
788  case kCopyStringFlag:
789  Allocator::Free(const_cast<Ch*>(GetStringPointer()));
790  break;
791 
792  default:
793  break; // Do nothing for other types.
794  }
795  }
796  }
797 
798  //@}
799 
800  //!@name Assignment operators
801  //@{
802 
803  //! Assignment with move semantics.
804  /*! \param rhs Source of the assignment. It will become a null value after assignment.
805  */
806  GenericValue& operator=(GenericValue& rhs) RAPIDJSON_NOEXCEPT {
807  RAPIDJSON_ASSERT(this != &rhs);
808  this->~GenericValue();
809  RawAssign(rhs);
810  return *this;
811  }
812 
813 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
814  //! Move assignment in C++11
815  GenericValue& operator=(GenericValue&& rhs) RAPIDJSON_NOEXCEPT {
816  return *this = rhs.Move();
817  }
818 #endif
819 
820  //! Assignment of constant string reference (no copy)
821  /*! \param str Constant string reference to be assigned
822  \note This overload is needed to avoid clashes with the generic primitive type assignment overload below.
823  \see GenericStringRef, operator=(T)
824  */
825  GenericValue& operator=(StringRefType str) RAPIDJSON_NOEXCEPT {
826  GenericValue s(str);
827  return *this = s;
828  }
829 
830  //! Assignment with primitive types.
831  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
832  \param value The value to be assigned.
833 
834  \note The source type \c T explicitly disallows all pointer types,
835  especially (\c const) \ref Ch*. This helps avoiding implicitly
836  referencing character strings with insufficient lifetime, use
837  \ref SetString(const Ch*, Allocator&) (for copying) or
838  \ref StringRef() (to explicitly mark the pointer as constant) instead.
839  All other pointer types would implicitly convert to \c bool,
840  use \ref SetBool() instead.
841  */
842  template <typename T>
843  RAPIDJSON_DISABLEIF_RETURN((internal::IsPointer<T>), (GenericValue&))
844  operator=(T value) {
845  GenericValue v(value);
846  return *this = v;
847  }
848 
849  //! Deep-copy assignment from Value
850  /*! Assigns a \b copy of the Value to the current Value object
851  \tparam SourceAllocator Allocator type of \c rhs
852  \param rhs Value to copy from (read-only)
853  \param allocator Allocator to use for copying
854  \param copyConstStrings Force copying of constant strings (e.g. referencing an in-situ buffer)
855  */
856  template <typename SourceAllocator>
857  GenericValue& CopyFrom(const GenericValue<Encoding, SourceAllocator>& rhs, Allocator& allocator, bool copyConstStrings = false) {
858  RAPIDJSON_ASSERT(static_cast<void*>(this) != static_cast<void const*>(&rhs));
859  this->~GenericValue();
860  new (this) GenericValue(rhs, allocator, copyConstStrings);
861  return *this;
862  }
863 
864  //! Exchange the contents of this value with those of other.
865  /*!
866  \param other Another value.
867  \note Constant complexity.
868  */
869  GenericValue& Swap(GenericValue& other) RAPIDJSON_NOEXCEPT {
870  GenericValue temp;
871  temp.RawAssign(*this);
872  RawAssign(other);
873  other.RawAssign(temp);
874  return *this;
875  }
876 
877  //! free-standing swap function helper
878  /*!
879  Helper function to enable support for common swap implementation pattern based on \c std::swap:
880  \code
881  void swap(MyClass& a, MyClass& b) {
882  using std::swap;
883  swap(a.value, b.value);
884  // ...
885  }
886  \endcode
887  \see Swap()
888  */
889  friend inline void swap(GenericValue& a, GenericValue& b) RAPIDJSON_NOEXCEPT { a.Swap(b); }
890 
891  //! Prepare Value for move semantics
892  /*! \return *this */
893  GenericValue& Move() RAPIDJSON_NOEXCEPT { return *this; }
894  //@}
895 
896  //!@name Equal-to and not-equal-to operators
897  //@{
898  //! Equal-to operator
899  /*!
900  \note If an object contains duplicated named member, comparing equality with any object is always \c false.
901  \note Linear time complexity (number of all values in the subtree and total lengths of all strings).
902  */
903  template <typename SourceAllocator>
906  if (GetType() != rhs.GetType())
907  return false;
908 
909  switch (GetType()) {
910  case kObjectType: // Warning: O(n^2) inner-loop
911  if (data_.o.size != rhs.data_.o.size)
912  return false;
913  for (ConstMemberIterator lhsMemberItr = MemberBegin(); lhsMemberItr != MemberEnd(); ++lhsMemberItr) {
914  typename RhsType::ConstMemberIterator rhsMemberItr = rhs.FindMember(lhsMemberItr->name);
915  if (rhsMemberItr == rhs.MemberEnd() || lhsMemberItr->value != rhsMemberItr->value)
916  return false;
917  }
918  return true;
919 
920  case kArrayType:
921  if (data_.a.size != rhs.data_.a.size)
922  return false;
923  for (SizeType i = 0; i < data_.a.size; i++)
924  if ((*this)[i] != rhs[i])
925  return false;
926  return true;
927 
928  case kStringType:
929  return StringEqual(rhs);
930 
931  case kNumberType:
932  if (IsDouble() || rhs.IsDouble()) {
933  double a = GetDouble(); // May convert from integer to double.
934  double b = rhs.GetDouble(); // Ditto
935  return a >= b && a <= b; // Prevent -Wfloat-equal
936  }
937  else
938  return data_.n.u64 == rhs.data_.n.u64;
939 
940  default:
941  return true;
942  }
943  }
944 
945  //! Equal-to operator with const C-string pointer
946  bool operator==(const Ch* rhs) const { return *this == GenericValue(StringRef(rhs)); }
947 
948 #if RAPIDJSON_HAS_STDSTRING
949  //! Equal-to operator with string object
950  /*! \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
951  */
952  bool operator==(const std::basic_string<Ch>& rhs) const { return *this == GenericValue(StringRef(rhs)); }
953 #endif
954 
955  //! Equal-to operator with primitive types
956  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c true, \c false
957  */
958  template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>,internal::IsGenericValue<T> >), (bool)) operator==(const T& rhs) const { return *this == GenericValue(rhs); }
959 
960  //! Not-equal-to operator
961  /*! \return !(*this == rhs)
962  */
963  template <typename SourceAllocator>
964  bool operator!=(const GenericValue<Encoding, SourceAllocator>& rhs) const { return !(*this == rhs); }
965 
966  //! Not-equal-to operator with const C-string pointer
967  bool operator!=(const Ch* rhs) const { return !(*this == rhs); }
968 
969  //! Not-equal-to operator with arbitrary types
970  /*! \return !(*this == rhs)
971  */
972  template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (bool)) operator!=(const T& rhs) const { return !(*this == rhs); }
973 
974  //! Equal-to operator with arbitrary types (symmetric version)
975  /*! \return (rhs == lhs)
976  */
977  template <typename T> friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (bool)) operator==(const T& lhs, const GenericValue& rhs) { return rhs == lhs; }
978 
979  //! Not-Equal-to operator with arbitrary types (symmetric version)
980  /*! \return !(rhs == lhs)
981  */
982  template <typename T> friend RAPIDJSON_DISABLEIF_RETURN((internal::IsGenericValue<T>), (bool)) operator!=(const T& lhs, const GenericValue& rhs) { return !(rhs == lhs); }
983  //@}
984 
985  //!@name Type
986  //@{
987 
988  Type GetType() const { return static_cast<Type>(data_.f.flags & kTypeMask); }
989  bool IsNull() const { return data_.f.flags == kNullFlag; }
990  bool IsFalse() const { return data_.f.flags == kFalseFlag; }
991  bool IsTrue() const { return data_.f.flags == kTrueFlag; }
992  bool IsBool() const { return (data_.f.flags & kBoolFlag) != 0; }
993  bool IsObject() const { return data_.f.flags == kObjectFlag; }
994  bool IsArray() const { return data_.f.flags == kArrayFlag; }
995  bool IsNumber() const { return (data_.f.flags & kNumberFlag) != 0; }
996  bool IsInt() const { return (data_.f.flags & kIntFlag) != 0; }
997  bool IsUint() const { return (data_.f.flags & kUintFlag) != 0; }
998  bool IsInt64() const { return (data_.f.flags & kInt64Flag) != 0; }
999  bool IsUint64() const { return (data_.f.flags & kUint64Flag) != 0; }
1000  bool IsDouble() const { return (data_.f.flags & kDoubleFlag) != 0; }
1001  bool IsString() const { return (data_.f.flags & kStringFlag) != 0; }
1002 
1003  // Checks whether a number can be losslessly converted to a double.
1004  bool IsLosslessDouble() const {
1005  if (!IsNumber()) return false;
1006  if (IsUint64()) {
1007  uint64_t u = GetUint64();
1008  volatile double d = static_cast<double>(u);
1009  return (d >= 0.0)
1010  && (d < static_cast<double>(std::numeric_limits<uint64_t>::max()))
1011  && (u == static_cast<uint64_t>(d));
1012  }
1013  if (IsInt64()) {
1014  int64_t i = GetInt64();
1015  volatile double d = static_cast<double>(i);
1016  return (d >= static_cast<double>(std::numeric_limits<int64_t>::min()))
1017  && (d < static_cast<double>(std::numeric_limits<int64_t>::max()))
1018  && (i == static_cast<int64_t>(d));
1019  }
1020  return true; // double, int, uint are always lossless
1021  }
1022 
1023  // Checks whether a number is a float (possible lossy).
1024  bool IsFloat() const {
1025  if ((data_.f.flags & kDoubleFlag) == 0)
1026  return false;
1027  double d = GetDouble();
1028  return d >= -3.4028234e38 && d <= 3.4028234e38;
1029  }
1030  // Checks whether a number can be losslessly converted to a float.
1031  bool IsLosslessFloat() const {
1032  if (!IsNumber()) return false;
1033  double a = GetDouble();
1034  if (a < static_cast<double>(-std::numeric_limits<float>::max())
1035  || a > static_cast<double>(std::numeric_limits<float>::max()))
1036  return false;
1037  double b = static_cast<double>(static_cast<float>(a));
1038  return a >= b && a <= b; // Prevent -Wfloat-equal
1039  }
1040 
1041  //@}
1042 
1043  //!@name Null
1044  //@{
1045 
1046  GenericValue& SetNull() { this->~GenericValue(); new (this) GenericValue(); return *this; }
1047 
1048  //@}
1049 
1050  //!@name Bool
1051  //@{
1052 
1053  bool GetBool() const { RAPIDJSON_ASSERT(IsBool()); return data_.f.flags == kTrueFlag; }
1054  //!< Set boolean value
1055  /*! \post IsBool() == true */
1056  GenericValue& SetBool(bool b) { this->~GenericValue(); new (this) GenericValue(b); return *this; }
1057 
1058  //@}
1059 
1060  //!@name Object
1061  //@{
1062 
1063  //! Set this value as an empty object.
1064  /*! \post IsObject() == true */
1065  GenericValue& SetObject() { this->~GenericValue(); new (this) GenericValue(kObjectType); return *this; }
1066 
1067  //! Get the number of members in the object.
1068  SizeType MemberCount() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size; }
1069 
1070  //! Check whether the object is empty.
1071  bool ObjectEmpty() const { RAPIDJSON_ASSERT(IsObject()); return data_.o.size == 0; }
1072 
1073  //! Get a value from an object associated with the name.
1074  /*! \pre IsObject() == true
1075  \tparam T Either \c Ch or \c const \c Ch (template used for disambiguation with \ref operator[](SizeType))
1076  \note In version 0.1x, if the member is not found, this function returns a null value. This makes issue 7.
1077  Since 0.2, if the name is not correct, it will assert.
1078  If user is unsure whether a member exists, user should use HasMember() first.
1079  A better approach is to use FindMember().
1080  \note Linear time complexity.
1081  */
1082  template <typename T>
1083  RAPIDJSON_DISABLEIF_RETURN((internal::NotExpr<internal::IsSame<typename internal::RemoveConst<T>::Type, Ch> >),(GenericValue&)) operator[](T* name) {
1084  GenericValue n(StringRef(name));
1085  return (*this)[n];
1086  }
1087  template <typename T>
1088  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]; }
1089 
1090  //! Get a value from an object associated with the name.
1091  /*! \pre IsObject() == true
1092  \tparam SourceAllocator Allocator of the \c name value
1093 
1094  \note Compared to \ref operator[](T*), this version is faster because it does not need a StrLen().
1095  And it can also handle strings with embedded null characters.
1096 
1097  \note Linear time complexity.
1098  */
1099  template <typename SourceAllocator>
1101  MemberIterator member = FindMember(name);
1102  if (member != MemberEnd())
1103  return member->value;
1104  else {
1105  RAPIDJSON_ASSERT(false); // see above note
1106 
1107  // This will generate -Wexit-time-destructors in clang
1108  // static GenericValue NullValue;
1109  // return NullValue;
1110 
1111  // Use static buffer and placement-new to prevent destruction
1112  static char buffer[sizeof(GenericValue)];
1113  return *new (buffer) GenericValue();
1114  }
1115  }
1116  template <typename SourceAllocator>
1117  const GenericValue& operator[](const GenericValue<Encoding, SourceAllocator>& name) const { return const_cast<GenericValue&>(*this)[name]; }
1118 
1119 #if RAPIDJSON_HAS_STDSTRING
1120  //! Get a value from an object associated with name (string object).
1121  GenericValue& operator[](const std::basic_string<Ch>& name) { return (*this)[GenericValue(StringRef(name))]; }
1122  const GenericValue& operator[](const std::basic_string<Ch>& name) const { return (*this)[GenericValue(StringRef(name))]; }
1123 #endif
1124 
1125  //! Const member iterator
1126  /*! \pre IsObject() == true */
1127  ConstMemberIterator MemberBegin() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(GetMembersPointer()); }
1128  //! Const \em past-the-end member iterator
1129  /*! \pre IsObject() == true */
1130  ConstMemberIterator MemberEnd() const { RAPIDJSON_ASSERT(IsObject()); return ConstMemberIterator(GetMembersPointer() + data_.o.size); }
1131  //! Member iterator
1132  /*! \pre IsObject() == true */
1133  MemberIterator MemberBegin() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer()); }
1134  //! \em Past-the-end member iterator
1135  /*! \pre IsObject() == true */
1136  MemberIterator MemberEnd() { RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer() + data_.o.size); }
1137 
1138  //! Check whether a member exists in the object.
1139  /*!
1140  \param name Member name to be searched.
1141  \pre IsObject() == true
1142  \return Whether a member with that name exists.
1143  \note It is better to use FindMember() directly if you need the obtain the value as well.
1144  \note Linear time complexity.
1145  */
1146  bool HasMember(const Ch* name) const { return FindMember(name) != MemberEnd(); }
1147 
1148 #if RAPIDJSON_HAS_STDSTRING
1149  //! Check whether a member exists in the object with string object.
1150  /*!
1151  \param name Member name to be searched.
1152  \pre IsObject() == true
1153  \return Whether a member with that name exists.
1154  \note It is better to use FindMember() directly if you need the obtain the value as well.
1155  \note Linear time complexity.
1156  */
1157  bool HasMember(const std::basic_string<Ch>& name) const { return FindMember(name) != MemberEnd(); }
1158 #endif
1159 
1160  //! Check whether a member exists in the object with GenericValue name.
1161  /*!
1162  This version is faster because it does not need a StrLen(). It can also handle string with null character.
1163  \param name Member name to be searched.
1164  \pre IsObject() == true
1165  \return Whether a member with that name exists.
1166  \note It is better to use FindMember() directly if you need the obtain the value as well.
1167  \note Linear time complexity.
1168  */
1169  template <typename SourceAllocator>
1170  bool HasMember(const GenericValue<Encoding, SourceAllocator>& name) const { return FindMember(name) != MemberEnd(); }
1171 
1172  //! Find member by name.
1173  /*!
1174  \param name Member name to be searched.
1175  \pre IsObject() == true
1176  \return Iterator to member, if it exists.
1177  Otherwise returns \ref MemberEnd().
1178 
1179  \note Earlier versions of Rapidjson returned a \c NULL pointer, in case
1180  the requested member doesn't exist. For consistency with e.g.
1181  \c std::map, this has been changed to MemberEnd() now.
1182  \note Linear time complexity.
1183  */
1185  GenericValue n(StringRef(name));
1186  return FindMember(n);
1187  }
1188 
1189  ConstMemberIterator FindMember(const Ch* name) const { return const_cast<GenericValue&>(*this).FindMember(name); }
1190 
1191  //! Find member by name.
1192  /*!
1193  This version is faster because it does not need a StrLen(). It can also handle string with null character.
1194  \param name Member name to be searched.
1195  \pre IsObject() == true
1196  \return Iterator to member, if it exists.
1197  Otherwise returns \ref MemberEnd().
1198 
1199  \note Earlier versions of Rapidjson returned a \c NULL pointer, in case
1200  the requested member doesn't exist. For consistency with e.g.
1201  \c std::map, this has been changed to MemberEnd() now.
1202  \note Linear time complexity.
1203  */
1204  template <typename SourceAllocator>
1206  RAPIDJSON_ASSERT(IsObject());
1207  RAPIDJSON_ASSERT(name.IsString());
1208  MemberIterator member = MemberBegin();
1209  for ( ; member != MemberEnd(); ++member)
1210  if (name.StringEqual(member->name))
1211  break;
1212  return member;
1213  }
1214  template <typename SourceAllocator> ConstMemberIterator FindMember(const GenericValue<Encoding, SourceAllocator>& name) const { return const_cast<GenericValue&>(*this).FindMember(name); }
1215 
1216 #if RAPIDJSON_HAS_STDSTRING
1217  //! Find member by string object name.
1218  /*!
1219  \param name Member name to be searched.
1220  \pre IsObject() == true
1221  \return Iterator to member, if it exists.
1222  Otherwise returns \ref MemberEnd().
1223  */
1224  MemberIterator FindMember(const std::basic_string<Ch>& name) { return FindMember(GenericValue(StringRef(name))); }
1225  ConstMemberIterator FindMember(const std::basic_string<Ch>& name) const { return FindMember(GenericValue(StringRef(name))); }
1226 #endif
1227 
1228  //! Add a member (name-value pair) to the object.
1229  /*! \param name A string value as name of member.
1230  \param value Value of any type.
1231  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1232  \return The value itself for fluent API.
1233  \note The ownership of \c name and \c value will be transferred to this object on success.
1234  \pre IsObject() && name.IsString()
1235  \post name.IsNull() && value.IsNull()
1236  \note Amortized Constant time complexity.
1237  */
1239  RAPIDJSON_ASSERT(IsObject());
1240  RAPIDJSON_ASSERT(name.IsString());
1241 
1242  ObjectData& o = data_.o;
1243  if (o.size >= o.capacity) {
1244  if (o.capacity == 0) {
1245  o.capacity = kDefaultObjectCapacity;
1246  SetMembersPointer(reinterpret_cast<Member*>(allocator.Malloc(o.capacity * sizeof(Member))));
1247  }
1248  else {
1249  SizeType oldCapacity = o.capacity;
1250  o.capacity += (oldCapacity + 1) / 2; // grow by factor 1.5
1251  SetMembersPointer(reinterpret_cast<Member*>(allocator.Realloc(GetMembersPointer(), oldCapacity * sizeof(Member), o.capacity * sizeof(Member))));
1252  }
1253  }
1254  Member* members = GetMembersPointer();
1255  members[o.size].name.RawAssign(name);
1256  members[o.size].value.RawAssign(value);
1257  o.size++;
1258  return *this;
1259  }
1260 
1261  //! Add a constant string value as member (name-value pair) to the object.
1262  /*! \param name A string value as name of member.
1263  \param value constant string reference as value of member.
1264  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1265  \return The value itself for fluent API.
1266  \pre IsObject()
1267  \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below.
1268  \note Amortized Constant time complexity.
1269  */
1271  GenericValue v(value);
1272  return AddMember(name, v, allocator);
1273  }
1274 
1275 #if RAPIDJSON_HAS_STDSTRING
1276  //! Add a string object as member (name-value pair) to the object.
1277  /*! \param name A string value as name of member.
1278  \param value constant string reference as value of member.
1279  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1280  \return The value itself for fluent API.
1281  \pre IsObject()
1282  \note This overload is needed to avoid clashes with the generic primitive type AddMember(GenericValue&,T,Allocator&) overload below.
1283  \note Amortized Constant time complexity.
1284  */
1285  GenericValue& AddMember(GenericValue& name, std::basic_string<Ch>& value, Allocator& allocator) {
1286  GenericValue v(value, allocator);
1287  return AddMember(name, v, allocator);
1288  }
1289 #endif
1290 
1291  //! Add any primitive value as member (name-value pair) to the object.
1292  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
1293  \param name A string value as name of member.
1294  \param value Value of primitive type \c T as value of member
1295  \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator().
1296  \return The value itself for fluent API.
1297  \pre IsObject()
1298 
1299  \note The source type \c T explicitly disallows all pointer types,
1300  especially (\c const) \ref Ch*. This helps avoiding implicitly
1301  referencing character strings with insufficient lifetime, use
1302  \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref
1303  AddMember(StringRefType, StringRefType, Allocator&).
1304  All other pointer types would implicitly convert to \c bool,
1305  use an explicit cast instead, if needed.
1306  \note Amortized Constant time complexity.
1307  */
1308  template <typename T>
1309  RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericValue&))
1310  AddMember(GenericValue& name, T value, Allocator& allocator) {
1311  GenericValue v(value);
1312  return AddMember(name, v, allocator);
1313  }
1314 
1315 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
1316  GenericValue& AddMember(GenericValue&& name, GenericValue&& value, Allocator& allocator) {
1317  return AddMember(name, value, allocator);
1318  }
1319  GenericValue& AddMember(GenericValue&& name, GenericValue& value, Allocator& allocator) {
1320  return AddMember(name, value, allocator);
1321  }
1322  GenericValue& AddMember(GenericValue& name, GenericValue&& value, Allocator& allocator) {
1323  return AddMember(name, value, allocator);
1324  }
1325  GenericValue& AddMember(StringRefType name, GenericValue&& value, Allocator& allocator) {
1326  GenericValue n(name);
1327  return AddMember(n, value, allocator);
1328  }
1329 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
1330 
1331 
1332  //! Add a member (name-value pair) to the object.
1333  /*! \param name A constant string reference as name of member.
1334  \param value Value of any type.
1335  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1336  \return The value itself for fluent API.
1337  \note The ownership of \c value will be transferred to this object on success.
1338  \pre IsObject()
1339  \post value.IsNull()
1340  \note Amortized Constant time complexity.
1341  */
1343  GenericValue n(name);
1344  return AddMember(n, value, allocator);
1345  }
1346 
1347  //! Add a constant string value as member (name-value pair) to the object.
1348  /*! \param name A constant string reference as name of member.
1349  \param value constant string reference as value of member.
1350  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1351  \return The value itself for fluent API.
1352  \pre IsObject()
1353  \note This overload is needed to avoid clashes with the generic primitive type AddMember(StringRefType,T,Allocator&) overload below.
1354  \note Amortized Constant time complexity.
1355  */
1357  GenericValue v(value);
1358  return AddMember(name, v, allocator);
1359  }
1360 
1361  //! Add any primitive value as member (name-value pair) to the object.
1362  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
1363  \param name A constant string reference as name of member.
1364  \param value Value of primitive type \c T as value of member
1365  \param allocator Allocator for reallocating memory. Commonly use GenericDocument::GetAllocator().
1366  \return The value itself for fluent API.
1367  \pre IsObject()
1368 
1369  \note The source type \c T explicitly disallows all pointer types,
1370  especially (\c const) \ref Ch*. This helps avoiding implicitly
1371  referencing character strings with insufficient lifetime, use
1372  \ref AddMember(StringRefType, GenericValue&, Allocator&) or \ref
1373  AddMember(StringRefType, StringRefType, Allocator&).
1374  All other pointer types would implicitly convert to \c bool,
1375  use an explicit cast instead, if needed.
1376  \note Amortized Constant time complexity.
1377  */
1378  template <typename T>
1379  RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericValue&))
1380  AddMember(StringRefType name, T value, Allocator& allocator) {
1381  GenericValue n(name);
1382  return AddMember(n, value, allocator);
1383  }
1384 
1385  //! Remove all members in the object.
1386  /*! This function do not deallocate memory in the object, i.e. the capacity is unchanged.
1387  \note Linear time complexity.
1388  */
1390  RAPIDJSON_ASSERT(IsObject());
1391  for (MemberIterator m = MemberBegin(); m != MemberEnd(); ++m)
1392  m->~Member();
1393  data_.o.size = 0;
1394  }
1395 
1396  //! Remove a member in object by its name.
1397  /*! \param name Name of member to be removed.
1398  \return Whether the member existed.
1399  \note This function may reorder the object members. Use \ref
1400  EraseMember(ConstMemberIterator) if you need to preserve the
1401  relative order of the remaining members.
1402  \note Linear time complexity.
1403  */
1404  bool RemoveMember(const Ch* name) {
1405  GenericValue n(StringRef(name));
1406  return RemoveMember(n);
1407  }
1408 
1409 #if RAPIDJSON_HAS_STDSTRING
1410  bool RemoveMember(const std::basic_string<Ch>& name) { return RemoveMember(GenericValue(StringRef(name))); }
1411 #endif
1412 
1413  template <typename SourceAllocator>
1414  bool RemoveMember(const GenericValue<Encoding, SourceAllocator>& name) {
1415  MemberIterator m = FindMember(name);
1416  if (m != MemberEnd()) {
1417  RemoveMember(m);
1418  return true;
1419  }
1420  else
1421  return false;
1422  }
1423 
1424  //! Remove a member in object by iterator.
1425  /*! \param m member iterator (obtained by FindMember() or MemberBegin()).
1426  \return the new iterator after removal.
1427  \note This function may reorder the object members. Use \ref
1428  EraseMember(ConstMemberIterator) if you need to preserve the
1429  relative order of the remaining members.
1430  \note Constant time complexity.
1431  */
1433  RAPIDJSON_ASSERT(IsObject());
1434  RAPIDJSON_ASSERT(data_.o.size > 0);
1435  RAPIDJSON_ASSERT(GetMembersPointer() != 0);
1436  RAPIDJSON_ASSERT(m >= MemberBegin() && m < MemberEnd());
1437 
1438  MemberIterator last(GetMembersPointer() + (data_.o.size - 1));
1439  if (data_.o.size > 1 && m != last)
1440  *m = *last; // Move the last one to this place
1441  else
1442  m->~Member(); // Only one left, just destroy
1443  --data_.o.size;
1444  return m;
1445  }
1446 
1447  //! Remove a member from an object by iterator.
1448  /*! \param pos iterator to the member to remove
1449  \pre IsObject() == true && \ref MemberBegin() <= \c pos < \ref MemberEnd()
1450  \return Iterator following the removed element.
1451  If the iterator \c pos refers to the last element, the \ref MemberEnd() iterator is returned.
1452  \note This function preserves the relative order of the remaining object
1453  members. If you do not need this, use the more efficient \ref RemoveMember(MemberIterator).
1454  \note Linear time complexity.
1455  */
1457  return EraseMember(pos, pos +1);
1458  }
1459 
1460  //! Remove members in the range [first, last) from an object.
1461  /*! \param first iterator to the first member to remove
1462  \param last iterator following the last member to remove
1463  \pre IsObject() == true && \ref MemberBegin() <= \c first <= \c last <= \ref MemberEnd()
1464  \return Iterator following the last removed element.
1465  \note This function preserves the relative order of the remaining object
1466  members.
1467  \note Linear time complexity.
1468  */
1470  RAPIDJSON_ASSERT(IsObject());
1471  RAPIDJSON_ASSERT(data_.o.size > 0);
1472  RAPIDJSON_ASSERT(GetMembersPointer() != 0);
1473  RAPIDJSON_ASSERT(first >= MemberBegin());
1474  RAPIDJSON_ASSERT(first <= last);
1475  RAPIDJSON_ASSERT(last <= MemberEnd());
1476 
1477  MemberIterator pos = MemberBegin() + (first - MemberBegin());
1478  for (MemberIterator itr = pos; itr != last; ++itr)
1479  itr->~Member();
1480  std::memmove(&*pos, &*last, static_cast<size_t>(MemberEnd() - last) * sizeof(Member));
1481  data_.o.size -= static_cast<SizeType>(last - first);
1482  return pos;
1483  }
1484 
1485  //! Erase a member in object by its name.
1486  /*! \param name Name of member to be removed.
1487  \return Whether the member existed.
1488  \note Linear time complexity.
1489  */
1490  bool EraseMember(const Ch* name) {
1491  GenericValue n(StringRef(name));
1492  return EraseMember(n);
1493  }
1494 
1495 #if RAPIDJSON_HAS_STDSTRING
1496  bool EraseMember(const std::basic_string<Ch>& name) { return EraseMember(GenericValue(StringRef(name))); }
1497 #endif
1498 
1499  template <typename SourceAllocator>
1500  bool EraseMember(const GenericValue<Encoding, SourceAllocator>& name) {
1501  MemberIterator m = FindMember(name);
1502  if (m != MemberEnd()) {
1503  EraseMember(m);
1504  return true;
1505  }
1506  else
1507  return false;
1508  }
1509 
1510  Object GetObject() { RAPIDJSON_ASSERT(IsObject()); return Object(*this); }
1511  ConstObject GetObject() const { RAPIDJSON_ASSERT(IsObject()); return ConstObject(*this); }
1512 
1513  //@}
1514 
1515  //!@name Array
1516  //@{
1517 
1518  //! Set this value as an empty array.
1519  /*! \post IsArray == true */
1520  GenericValue& SetArray() { this->~GenericValue(); new (this) GenericValue(kArrayType); return *this; }
1521 
1522  //! Get the number of elements in array.
1523  SizeType Size() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size; }
1524 
1525  //! Get the capacity of array.
1526  SizeType Capacity() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.capacity; }
1527 
1528  //! Check whether the array is empty.
1529  bool Empty() const { RAPIDJSON_ASSERT(IsArray()); return data_.a.size == 0; }
1530 
1531  //! Remove all elements in the array.
1532  /*! This function do not deallocate memory in the array, i.e. the capacity is unchanged.
1533  \note Linear time complexity.
1534  */
1535  void Clear() {
1536  RAPIDJSON_ASSERT(IsArray());
1537  GenericValue* e = GetElementsPointer();
1538  for (GenericValue* v = e; v != e + data_.a.size; ++v)
1539  v->~GenericValue();
1540  data_.a.size = 0;
1541  }
1542 
1543  //! Get an element from array by index.
1544  /*! \pre IsArray() == true
1545  \param index Zero-based index of element.
1546  \see operator[](T*)
1547  */
1549  RAPIDJSON_ASSERT(IsArray());
1550  RAPIDJSON_ASSERT(index < data_.a.size);
1551  return GetElementsPointer()[index];
1552  }
1553  const GenericValue& operator[](SizeType index) const { return const_cast<GenericValue&>(*this)[index]; }
1554 
1555  //! Element iterator
1556  /*! \pre IsArray() == true */
1557  ValueIterator Begin() { RAPIDJSON_ASSERT(IsArray()); return GetElementsPointer(); }
1558  //! \em Past-the-end element iterator
1559  /*! \pre IsArray() == true */
1560  ValueIterator End() { RAPIDJSON_ASSERT(IsArray()); return GetElementsPointer() + data_.a.size; }
1561  //! Constant element iterator
1562  /*! \pre IsArray() == true */
1563  ConstValueIterator Begin() const { return const_cast<GenericValue&>(*this).Begin(); }
1564  //! Constant \em past-the-end element iterator
1565  /*! \pre IsArray() == true */
1566  ConstValueIterator End() const { return const_cast<GenericValue&>(*this).End(); }
1567 
1568  //! Request the array to have enough capacity to store elements.
1569  /*! \param newCapacity The capacity that the array at least need to have.
1570  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1571  \return The value itself for fluent API.
1572  \note Linear time complexity.
1573  */
1574  GenericValue& Reserve(SizeType newCapacity, Allocator &allocator) {
1575  RAPIDJSON_ASSERT(IsArray());
1576  if (newCapacity > data_.a.capacity) {
1577  SetElementsPointer(reinterpret_cast<GenericValue*>(allocator.Realloc(GetElementsPointer(), data_.a.capacity * sizeof(GenericValue), newCapacity * sizeof(GenericValue))));
1578  data_.a.capacity = newCapacity;
1579  }
1580  return *this;
1581  }
1582 
1583  //! Append a GenericValue at the end of the array.
1584  /*! \param value Value to be appended.
1585  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1586  \pre IsArray() == true
1587  \post value.IsNull() == true
1588  \return The value itself for fluent API.
1589  \note The ownership of \c value will be transferred to this array on success.
1590  \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient.
1591  \note Amortized constant time complexity.
1592  */
1594  RAPIDJSON_ASSERT(IsArray());
1595  if (data_.a.size >= data_.a.capacity)
1596  Reserve(data_.a.capacity == 0 ? kDefaultArrayCapacity : (data_.a.capacity + (data_.a.capacity + 1) / 2), allocator);
1597  GetElementsPointer()[data_.a.size++].RawAssign(value);
1598  return *this;
1599  }
1600 
1601 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
1602  GenericValue& PushBack(GenericValue&& value, Allocator& allocator) {
1603  return PushBack(value, allocator);
1604  }
1605 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
1606 
1607  //! Append a constant string reference at the end of the array.
1608  /*! \param value Constant string reference to be appended.
1609  \param allocator Allocator for reallocating memory. It must be the same one used previously. Commonly use GenericDocument::GetAllocator().
1610  \pre IsArray() == true
1611  \return The value itself for fluent API.
1612  \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient.
1613  \note Amortized constant time complexity.
1614  \see GenericStringRef
1615  */
1617  return (*this).template PushBack<StringRefType>(value, allocator);
1618  }
1619 
1620  //! Append a primitive value at the end of the array.
1621  /*! \tparam T Either \ref Type, \c int, \c unsigned, \c int64_t, \c uint64_t
1622  \param value Value of primitive type T to be appended.
1623  \param allocator Allocator for reallocating memory. It must be the same one as used before. Commonly use GenericDocument::GetAllocator().
1624  \pre IsArray() == true
1625  \return The value itself for fluent API.
1626  \note If the number of elements to be appended is known, calls Reserve() once first may be more efficient.
1627 
1628  \note The source type \c T explicitly disallows all pointer types,
1629  especially (\c const) \ref Ch*. This helps avoiding implicitly
1630  referencing character strings with insufficient lifetime, use
1631  \ref PushBack(GenericValue&, Allocator&) or \ref
1632  PushBack(StringRefType, Allocator&).
1633  All other pointer types would implicitly convert to \c bool,
1634  use an explicit cast instead, if needed.
1635  \note Amortized constant time complexity.
1636  */
1637  template <typename T>
1638  RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericValue&))
1639  PushBack(T value, Allocator& allocator) {
1640  GenericValue v(value);
1641  return PushBack(v, allocator);
1642  }
1643 
1644  //! Remove the last element in the array.
1645  /*!
1646  \note Constant time complexity.
1647  */
1649  RAPIDJSON_ASSERT(IsArray());
1650  RAPIDJSON_ASSERT(!Empty());
1651  GetElementsPointer()[--data_.a.size].~GenericValue();
1652  return *this;
1653  }
1654 
1655  //! Remove an element of array by iterator.
1656  /*!
1657  \param pos iterator to the element to remove
1658  \pre IsArray() == true && \ref Begin() <= \c pos < \ref End()
1659  \return Iterator following the removed element. If the iterator pos refers to the last element, the End() iterator is returned.
1660  \note Linear time complexity.
1661  */
1663  return Erase(pos, pos + 1);
1664  }
1665 
1666  //! Remove elements in the range [first, last) of the array.
1667  /*!
1668  \param first iterator to the first element to remove
1669  \param last iterator following the last element to remove
1670  \pre IsArray() == true && \ref Begin() <= \c first <= \c last <= \ref End()
1671  \return Iterator following the last removed element.
1672  \note Linear time complexity.
1673  */
1675  RAPIDJSON_ASSERT(IsArray());
1676  RAPIDJSON_ASSERT(data_.a.size > 0);
1677  RAPIDJSON_ASSERT(GetElementsPointer() != 0);
1678  RAPIDJSON_ASSERT(first >= Begin());
1679  RAPIDJSON_ASSERT(first <= last);
1680  RAPIDJSON_ASSERT(last <= End());
1681  ValueIterator pos = Begin() + (first - Begin());
1682  for (ValueIterator itr = pos; itr != last; ++itr)
1683  itr->~GenericValue();
1684  std::memmove(pos, last, static_cast<size_t>(End() - last) * sizeof(GenericValue));
1685  data_.a.size -= static_cast<SizeType>(last - first);
1686  return pos;
1687  }
1688 
1689  Array GetArray() { RAPIDJSON_ASSERT(IsArray()); return Array(*this); }
1690  ConstArray GetArray() const { RAPIDJSON_ASSERT(IsArray()); return ConstArray(*this); }
1691 
1692  //@}
1693 
1694  //!@name Number
1695  //@{
1696 
1697  int GetInt() const { RAPIDJSON_ASSERT(data_.f.flags & kIntFlag); return data_.n.i.i; }
1698  unsigned GetUint() const { RAPIDJSON_ASSERT(data_.f.flags & kUintFlag); return data_.n.u.u; }
1699  int64_t GetInt64() const { RAPIDJSON_ASSERT(data_.f.flags & kInt64Flag); return data_.n.i64; }
1700  uint64_t GetUint64() const { RAPIDJSON_ASSERT(data_.f.flags & kUint64Flag); return data_.n.u64; }
1701 
1702  //! Get the value as double type.
1703  /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessDouble() to check whether the converison is lossless.
1704  */
1705  double GetDouble() const {
1706  RAPIDJSON_ASSERT(IsNumber());
1707  if ((data_.f.flags & kDoubleFlag) != 0) return data_.n.d; // exact type, no conversion.
1708  if ((data_.f.flags & kIntFlag) != 0) return data_.n.i.i; // int -> double
1709  if ((data_.f.flags & kUintFlag) != 0) return data_.n.u.u; // unsigned -> double
1710  if ((data_.f.flags & kInt64Flag) != 0) return static_cast<double>(data_.n.i64); // int64_t -> double (may lose precision)
1711  RAPIDJSON_ASSERT((data_.f.flags & kUint64Flag) != 0); return static_cast<double>(data_.n.u64); // uint64_t -> double (may lose precision)
1712  }
1713 
1714  //! Get the value as float type.
1715  /*! \note If the value is 64-bit integer type, it may lose precision. Use \c IsLosslessFloat() to check whether the converison is lossless.
1716  */
1717  float GetFloat() const {
1718  return static_cast<float>(GetDouble());
1719  }
1720 
1721  GenericValue& SetInt(int i) { this->~GenericValue(); new (this) GenericValue(i); return *this; }
1722  GenericValue& SetUint(unsigned u) { this->~GenericValue(); new (this) GenericValue(u); return *this; }
1723  GenericValue& SetInt64(int64_t i64) { this->~GenericValue(); new (this) GenericValue(i64); return *this; }
1724  GenericValue& SetUint64(uint64_t u64) { this->~GenericValue(); new (this) GenericValue(u64); return *this; }
1725  GenericValue& SetDouble(double d) { this->~GenericValue(); new (this) GenericValue(d); return *this; }
1726  GenericValue& SetFloat(float f) { this->~GenericValue(); new (this) GenericValue(static_cast<double>(f)); return *this; }
1727 
1728  //@}
1729 
1730  //!@name String
1731  //@{
1732 
1733  const Ch* GetString() const { RAPIDJSON_ASSERT(IsString()); return (data_.f.flags & kInlineStrFlag) ? data_.ss.str : GetStringPointer(); }
1734 
1735  //! Get the length of string.
1736  /*! Since rapidjson permits "\\u0000" in the json string, strlen(v.GetString()) may not equal to v.GetStringLength().
1737  */
1738  SizeType GetStringLength() const { RAPIDJSON_ASSERT(IsString()); return ((data_.f.flags & kInlineStrFlag) ? (data_.ss.GetLength()) : data_.s.length); }
1739 
1740  //! Set this value as a string without copying source string.
1741  /*! This version has better performance with supplied length, and also support string containing null character.
1742  \param s source string pointer.
1743  \param length The length of source string, excluding the trailing null terminator.
1744  \return The value itself for fluent API.
1745  \post IsString() == true && GetString() == s && GetStringLength() == length
1746  \see SetString(StringRefType)
1747  */
1748  GenericValue& SetString(const Ch* s, SizeType length) { return SetString(StringRef(s, length)); }
1749 
1750  //! Set this value as a string without copying source string.
1751  /*! \param s source string reference
1752  \return The value itself for fluent API.
1753  \post IsString() == true && GetString() == s && GetStringLength() == s.length
1754  */
1755  GenericValue& SetString(StringRefType s) { this->~GenericValue(); SetStringRaw(s); return *this; }
1756 
1757  //! Set this value as a string by copying from source string.
1758  /*! This version has better performance with supplied length, and also support string containing null character.
1759  \param s source string.
1760  \param length The length of source string, excluding the trailing null terminator.
1761  \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
1762  \return The value itself for fluent API.
1763  \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length
1764  */
1765  GenericValue& SetString(const Ch* s, SizeType length, Allocator& allocator) { this->~GenericValue(); SetStringRaw(StringRef(s, length), allocator); return *this; }
1766 
1767  //! Set this value as a string by copying from source string.
1768  /*! \param s source string.
1769  \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
1770  \return The value itself for fluent API.
1771  \post IsString() == true && GetString() != s && strcmp(GetString(),s) == 0 && GetStringLength() == length
1772  */
1773  GenericValue& SetString(const Ch* s, Allocator& allocator) { return SetString(s, internal::StrLen(s), allocator); }
1774 
1775 #if RAPIDJSON_HAS_STDSTRING
1776  //! Set this value as a string by copying from source string.
1777  /*! \param s source string.
1778  \param allocator Allocator for allocating copied buffer. Commonly use GenericDocument::GetAllocator().
1779  \return The value itself for fluent API.
1780  \post IsString() == true && GetString() != s.data() && strcmp(GetString(),s.data() == 0 && GetStringLength() == s.size()
1781  \note Requires the definition of the preprocessor symbol \ref RAPIDJSON_HAS_STDSTRING.
1782  */
1783  GenericValue& SetString(const std::basic_string<Ch>& s, Allocator& allocator) { return SetString(s.data(), SizeType(s.size()), allocator); }
1784 #endif
1785 
1786  //@}
1787 
1788  //!@name Array
1789  //@{
1790 
1791  //! Templated version for checking whether this value is type T.
1792  /*!
1793  \tparam T Either \c bool, \c int, \c unsigned, \c int64_t, \c uint64_t, \c double, \c float, \c const \c char*, \c std::basic_string<Ch>
1794  */
1795  template <typename T>
1796  bool Is() const { return internal::TypeHelper<ValueType, T>::Is(*this); }
1797 
1798  template <typename T>
1799  T Get() const { return internal::TypeHelper<ValueType, T>::Get(*this); }
1800 
1801  template <typename T>
1802  T Get() { return internal::TypeHelper<ValueType, T>::Get(*this); }
1803 
1804  template<typename T>
1805  ValueType& Set(const T& data) { return internal::TypeHelper<ValueType, T>::Set(*this, data); }
1806 
1807  template<typename T>
1808  ValueType& Set(const T& data, AllocatorType& allocator) { return internal::TypeHelper<ValueType, T>::Set(*this, data, allocator); }
1809 
1810  //@}
1811 
1812  //! Generate events of this value to a Handler.
1813  /*! This function adopts the GoF visitor pattern.
1814  Typical usage is to output this JSON value as JSON text via Writer, which is a Handler.
1815  It can also be used to deep clone this value via GenericDocument, which is also a Handler.
1816  \tparam Handler type of handler.
1817  \param handler An object implementing concept Handler.
1818  */
1819  template <typename Handler>
1820  bool Accept(Handler& handler) const {
1821  switch(GetType()) {
1822  case kNullType: return handler.Null();
1823  case kFalseType: return handler.Bool(false);
1824  case kTrueType: return handler.Bool(true);
1825 
1826  case kObjectType:
1827  if (RAPIDJSON_UNLIKELY(!handler.StartObject()))
1828  return false;
1829  for (ConstMemberIterator m = MemberBegin(); m != MemberEnd(); ++m) {
1830  RAPIDJSON_ASSERT(m->name.IsString()); // User may change the type of name by MemberIterator.
1831  if (RAPIDJSON_UNLIKELY(!handler.Key(m->name.GetString(), m->name.GetStringLength(), (m->name.data_.f.flags & kCopyFlag) != 0)))
1832  return false;
1833  if (RAPIDJSON_UNLIKELY(!m->value.Accept(handler)))
1834  return false;
1835  }
1836  return handler.EndObject(data_.o.size);
1837 
1838  case kArrayType:
1839  if (RAPIDJSON_UNLIKELY(!handler.StartArray()))
1840  return false;
1841  for (const GenericValue* v = Begin(); v != End(); ++v)
1842  if (RAPIDJSON_UNLIKELY(!v->Accept(handler)))
1843  return false;
1844  return handler.EndArray(data_.a.size);
1845 
1846  case kStringType:
1847  return handler.String(GetString(), GetStringLength(), (data_.f.flags & kCopyFlag) != 0);
1848 
1849  default:
1850  RAPIDJSON_ASSERT(GetType() == kNumberType);
1851  if (IsDouble()) return handler.Double(data_.n.d);
1852  else if (IsInt()) return handler.Int(data_.n.i.i);
1853  else if (IsUint()) return handler.Uint(data_.n.u.u);
1854  else if (IsInt64()) return handler.Int64(data_.n.i64);
1855  else return handler.Uint64(data_.n.u64);
1856  }
1857  }
1858 
1859 private:
1860  template <typename, typename> friend class GenericValue;
1861  template <typename, typename, typename> friend class GenericDocument;
1862 
1863  enum {
1864  kBoolFlag = 0x0008,
1865  kNumberFlag = 0x0010,
1866  kIntFlag = 0x0020,
1867  kUintFlag = 0x0040,
1868  kInt64Flag = 0x0080,
1869  kUint64Flag = 0x0100,
1870  kDoubleFlag = 0x0200,
1871  kStringFlag = 0x0400,
1872  kCopyFlag = 0x0800,
1873  kInlineStrFlag = 0x1000,
1874 
1875  // Initial flags of different types.
1876  kNullFlag = kNullType,
1877  kTrueFlag = kTrueType | kBoolFlag,
1878  kFalseFlag = kFalseType | kBoolFlag,
1879  kNumberIntFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag,
1880  kNumberUintFlag = kNumberType | kNumberFlag | kUintFlag | kUint64Flag | kInt64Flag,
1881  kNumberInt64Flag = kNumberType | kNumberFlag | kInt64Flag,
1882  kNumberUint64Flag = kNumberType | kNumberFlag | kUint64Flag,
1883  kNumberDoubleFlag = kNumberType | kNumberFlag | kDoubleFlag,
1884  kNumberAnyFlag = kNumberType | kNumberFlag | kIntFlag | kInt64Flag | kUintFlag | kUint64Flag | kDoubleFlag,
1885  kConstStringFlag = kStringType | kStringFlag,
1886  kCopyStringFlag = kStringType | kStringFlag | kCopyFlag,
1887  kShortStringFlag = kStringType | kStringFlag | kCopyFlag | kInlineStrFlag,
1888  kObjectFlag = kObjectType,
1889  kArrayFlag = kArrayType,
1890 
1891  kTypeMask = 0x07
1892  };
1893 
1894  static const SizeType kDefaultArrayCapacity = 16;
1895  static const SizeType kDefaultObjectCapacity = 16;
1896 
1897  struct Flag {
1898 #if RAPIDJSON_48BITPOINTER_OPTIMIZATION
1899  char payload[sizeof(SizeType) * 2 + 6]; // 2 x SizeType + lower 48-bit pointer
1900 #elif RAPIDJSON_64BIT
1901  char payload[sizeof(SizeType) * 2 + sizeof(void*) + 6]; // 6 padding bytes
1902 #else
1903  char payload[sizeof(SizeType) * 2 + sizeof(void*) + 2]; // 2 padding bytes
1904 #endif
1905  uint16_t flags;
1906  };
1907 
1908  struct String {
1909  SizeType length;
1910  SizeType hashcode; //!< reserved
1911  const Ch* str;
1912  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1913 
1914  // implementation detail: ShortString can represent zero-terminated strings up to MaxSize chars
1915  // (excluding the terminating zero) and store a value to determine the length of the contained
1916  // string in the last character str[LenPos] by storing "MaxSize - length" there. If the string
1917  // to store has the maximal length of MaxSize then str[LenPos] will be 0 and therefore act as
1918  // the string terminator as well. For getting the string length back from that value just use
1919  // "MaxSize - str[LenPos]".
1920  // This allows to store 13-chars strings in 32-bit mode, 21-chars strings in 64-bit mode,
1921  // 13-chars strings for RAPIDJSON_48BITPOINTER_OPTIMIZATION=1 inline (for `UTF8`-encoded strings).
1922  struct ShortString {
1923  enum { MaxChars = sizeof(static_cast<Flag*>(0)->payload) / sizeof(Ch), MaxSize = MaxChars - 1, LenPos = MaxSize };
1924  Ch str[MaxChars];
1925 
1926  inline static bool Usable(SizeType len) { return (MaxSize >= len); }
1927  inline void SetLength(SizeType len) { str[LenPos] = static_cast<Ch>(MaxSize - len); }
1928  inline SizeType GetLength() const { return static_cast<SizeType>(MaxSize - str[LenPos]); }
1929  }; // at most as many bytes as "String" above => 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1930 
1931  // By using proper binary layout, retrieval of different integer types do not need conversions.
1932  union Number {
1933 #if RAPIDJSON_ENDIAN == RAPIDJSON_LITTLEENDIAN
1934  struct I {
1935  int i;
1936  char padding[4];
1937  }i;
1938  struct U {
1939  unsigned u;
1940  char padding2[4];
1941  }u;
1942 #else
1943  struct I {
1944  char padding[4];
1945  int i;
1946  }i;
1947  struct U {
1948  char padding2[4];
1949  unsigned u;
1950  }u;
1951 #endif
1952  int64_t i64;
1953  uint64_t u64;
1954  double d;
1955  }; // 8 bytes
1956 
1957  struct ObjectData {
1958  SizeType size;
1959  SizeType capacity;
1960  Member* members;
1961  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1962 
1963  struct ArrayData {
1964  SizeType size;
1965  SizeType capacity;
1966  GenericValue* elements;
1967  }; // 12 bytes in 32-bit mode, 16 bytes in 64-bit mode
1968 
1969  union Data {
1970  String s;
1971  ShortString ss;
1972  Number n;
1973  ObjectData o;
1974  ArrayData a;
1975  Flag f;
1976  }; // 16 bytes in 32-bit mode, 24 bytes in 64-bit mode, 16 bytes in 64-bit with RAPIDJSON_48BITPOINTER_OPTIMIZATION
1977 
1978  RAPIDJSON_FORCEINLINE const Ch* GetStringPointer() const { return RAPIDJSON_GETPOINTER(Ch, data_.s.str); }
1979  RAPIDJSON_FORCEINLINE const Ch* SetStringPointer(const Ch* str) { return RAPIDJSON_SETPOINTER(Ch, data_.s.str, str); }
1980  RAPIDJSON_FORCEINLINE GenericValue* GetElementsPointer() const { return RAPIDJSON_GETPOINTER(GenericValue, data_.a.elements); }
1981  RAPIDJSON_FORCEINLINE GenericValue* SetElementsPointer(GenericValue* elements) { return RAPIDJSON_SETPOINTER(GenericValue, data_.a.elements, elements); }
1982  RAPIDJSON_FORCEINLINE Member* GetMembersPointer() const { return RAPIDJSON_GETPOINTER(Member, data_.o.members); }
1983  RAPIDJSON_FORCEINLINE Member* SetMembersPointer(Member* members) { return RAPIDJSON_SETPOINTER(Member, data_.o.members, members); }
1984 
1985  // Initialize this value as array with initial data, without calling destructor.
1986  void SetArrayRaw(GenericValue* values, SizeType count, Allocator& allocator) {
1987  data_.f.flags = kArrayFlag;
1988  if (count) {
1989  GenericValue* e = static_cast<GenericValue*>(allocator.Malloc(count * sizeof(GenericValue)));
1990  SetElementsPointer(e);
1991  std::memcpy(e, values, count * sizeof(GenericValue));
1992  }
1993  else
1994  SetElementsPointer(0);
1995  data_.a.size = data_.a.capacity = count;
1996  }
1997 
1998  //! Initialize this value as object with initial data, without calling destructor.
1999  void SetObjectRaw(Member* members, SizeType count, Allocator& allocator) {
2000  data_.f.flags = kObjectFlag;
2001  if (count) {
2002  Member* m = static_cast<Member*>(allocator.Malloc(count * sizeof(Member)));
2003  SetMembersPointer(m);
2004  std::memcpy(m, members, count * sizeof(Member));
2005  }
2006  else
2007  SetMembersPointer(0);
2008  data_.o.size = data_.o.capacity = count;
2009  }
2010 
2011  //! Initialize this value as constant string, without calling destructor.
2012  void SetStringRaw(StringRefType s) RAPIDJSON_NOEXCEPT {
2013  data_.f.flags = kConstStringFlag;
2014  SetStringPointer(s);
2015  data_.s.length = s.length;
2016  }
2017 
2018  //! Initialize this value as copy string with initial data, without calling destructor.
2019  void SetStringRaw(StringRefType s, Allocator& allocator) {
2020  Ch* str = 0;
2021  if (ShortString::Usable(s.length)) {
2022  data_.f.flags = kShortStringFlag;
2023  data_.ss.SetLength(s.length);
2024  str = data_.ss.str;
2025  } else {
2026  data_.f.flags = kCopyStringFlag;
2027  data_.s.length = s.length;
2028  str = static_cast<Ch *>(allocator.Malloc((s.length + 1) * sizeof(Ch)));
2029  SetStringPointer(str);
2030  }
2031  std::memcpy(str, s, s.length * sizeof(Ch));
2032  str[s.length] = '\0';
2033  }
2034 
2035  //! Assignment without calling destructor
2036  void RawAssign(GenericValue& rhs) RAPIDJSON_NOEXCEPT {
2037  data_ = rhs.data_;
2038  // data_.f.flags = rhs.data_.f.flags;
2039  rhs.data_.f.flags = kNullFlag;
2040  }
2041 
2042  template <typename SourceAllocator>
2043  bool StringEqual(const GenericValue<Encoding, SourceAllocator>& rhs) const {
2044  RAPIDJSON_ASSERT(IsString());
2045  RAPIDJSON_ASSERT(rhs.IsString());
2046 
2047  const SizeType len1 = GetStringLength();
2048  const SizeType len2 = rhs.GetStringLength();
2049  if(len1 != len2) { return false; }
2050 
2051  const Ch* const str1 = GetString();
2052  const Ch* const str2 = rhs.GetString();
2053  if(str1 == str2) { return true; } // fast path for constant string
2054 
2055  return (std::memcmp(str1, str2, sizeof(Ch) * len1) == 0);
2056  }
2057 
2058  Data data_;
2059 };
2060 
2061 //! GenericValue with UTF8 encoding
2063 
2064 ///////////////////////////////////////////////////////////////////////////////
2065 // GenericDocument
2066 
2067 //! A document for parsing JSON text as DOM.
2068 /*!
2069  \note implements Handler concept
2070  \tparam Encoding Encoding for both parsing and string storage.
2071  \tparam Allocator Allocator for allocating memory for the DOM
2072  \tparam StackAllocator Allocator for allocating memory for stack during parsing.
2073  \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.
2074 */
2075 template <typename Encoding, typename Allocator = MemoryPoolAllocator<>, typename StackAllocator = CrtAllocator>
2076 class GenericDocument : public GenericValue<Encoding, Allocator> {
2077 public:
2078  typedef typename Encoding::Ch Ch; //!< Character type derived from Encoding.
2079  typedef GenericValue<Encoding, Allocator> ValueType; //!< Value type of the document.
2080  typedef Allocator AllocatorType; //!< Allocator type from template parameter.
2081 
2082  //! Constructor
2083  /*! Creates an empty document of specified type.
2084  \param type Mandatory type of object to create.
2085  \param allocator Optional allocator for allocating memory.
2086  \param stackCapacity Optional initial capacity of stack in bytes.
2087  \param stackAllocator Optional allocator for allocating memory for stack.
2088  */
2089  explicit GenericDocument(Type type, Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) :
2090  GenericValue<Encoding, Allocator>(type), allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_()
2091  {
2092  if (!allocator_)
2093  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
2094  }
2095 
2096  //! Constructor
2097  /*! Creates an empty document which type is Null.
2098  \param allocator Optional allocator for allocating memory.
2099  \param stackCapacity Optional initial capacity of stack in bytes.
2100  \param stackAllocator Optional allocator for allocating memory for stack.
2101  */
2102  GenericDocument(Allocator* allocator = 0, size_t stackCapacity = kDefaultStackCapacity, StackAllocator* stackAllocator = 0) :
2103  allocator_(allocator), ownAllocator_(0), stack_(stackAllocator, stackCapacity), parseResult_()
2104  {
2105  if (!allocator_)
2106  ownAllocator_ = allocator_ = RAPIDJSON_NEW(Allocator)();
2107  }
2108 
2109 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
2110  //! Move constructor in C++11
2111  GenericDocument(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT
2112  : ValueType(std::forward<ValueType>(rhs)), // explicit cast to avoid prohibited move from Document
2113  allocator_(rhs.allocator_),
2114  ownAllocator_(rhs.ownAllocator_),
2115  stack_(std::move(rhs.stack_)),
2116  parseResult_(rhs.parseResult_)
2117  {
2118  rhs.allocator_ = 0;
2119  rhs.ownAllocator_ = 0;
2120  rhs.parseResult_ = ParseResult();
2121  }
2122 #endif
2123 
2124  ~GenericDocument() {
2125  Destroy();
2126  }
2127 
2128 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
2129  //! Move assignment in C++11
2130  GenericDocument& operator=(GenericDocument&& rhs) RAPIDJSON_NOEXCEPT
2131  {
2132  // The cast to ValueType is necessary here, because otherwise it would
2133  // attempt to call GenericValue's templated assignment operator.
2134  ValueType::operator=(std::forward<ValueType>(rhs));
2135 
2136  // Calling the destructor here would prematurely call stack_'s destructor
2137  Destroy();
2138 
2139  allocator_ = rhs.allocator_;
2140  ownAllocator_ = rhs.ownAllocator_;
2141  stack_ = std::move(rhs.stack_);
2142  parseResult_ = rhs.parseResult_;
2143 
2144  rhs.allocator_ = 0;
2145  rhs.ownAllocator_ = 0;
2146  rhs.parseResult_ = ParseResult();
2147 
2148  return *this;
2149  }
2150 #endif
2151 
2152  //! Exchange the contents of this document with those of another.
2153  /*!
2154  \param rhs Another document.
2155  \note Constant complexity.
2156  \see GenericValue::Swap
2157  */
2158  GenericDocument& Swap(GenericDocument& rhs) RAPIDJSON_NOEXCEPT {
2159  ValueType::Swap(rhs);
2160  stack_.Swap(rhs.stack_);
2161  internal::Swap(allocator_, rhs.allocator_);
2162  internal::Swap(ownAllocator_, rhs.ownAllocator_);
2163  internal::Swap(parseResult_, rhs.parseResult_);
2164  return *this;
2165  }
2166 
2167  //! free-standing swap function helper
2168  /*!
2169  Helper function to enable support for common swap implementation pattern based on \c std::swap:
2170  \code
2171  void swap(MyClass& a, MyClass& b) {
2172  using std::swap;
2173  swap(a.doc, b.doc);
2174  // ...
2175  }
2176  \endcode
2177  \see Swap()
2178  */
2179  friend inline void swap(GenericDocument& a, GenericDocument& b) RAPIDJSON_NOEXCEPT { a.Swap(b); }
2180 
2181  //! Populate this document by a generator which produces SAX events.
2182  /*! \tparam Generator A functor with <tt>bool f(Handler)</tt> prototype.
2183  \param g Generator functor which sends SAX events to the parameter.
2184  \return The document itself for fluent API.
2185  */
2186  template <typename Generator>
2187  GenericDocument& Populate(Generator& g) {
2188  ClearStackOnExit scope(*this);
2189  if (g(*this)) {
2190  RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object
2191  ValueType::operator=(*stack_.template Pop<ValueType>(1));// Move value from stack to document
2192  }
2193  return *this;
2194  }
2195 
2196  //!@name Parse from stream
2197  //!@{
2198 
2199  //! Parse JSON text from an input stream (with Encoding conversion)
2200  /*! \tparam parseFlags Combination of \ref ParseFlag.
2201  \tparam SourceEncoding Encoding of input stream
2202  \tparam InputStream Type of input stream, implementing Stream concept
2203  \param is Input stream to be parsed.
2204  \return The document itself for fluent API.
2205  */
2206  template <unsigned parseFlags, typename SourceEncoding, typename InputStream>
2207  GenericDocument& ParseStream(InputStream& is) {
2209  stack_.HasAllocator() ? &stack_.GetAllocator() : 0);
2210  ClearStackOnExit scope(*this);
2211  parseResult_ = reader.template Parse<parseFlags>(is, *this);
2212  if (parseResult_) {
2213  RAPIDJSON_ASSERT(stack_.GetSize() == sizeof(ValueType)); // Got one and only one root object
2214  ValueType::operator=(*stack_.template Pop<ValueType>(1));// Move value from stack to document
2215  }
2216  return *this;
2217  }
2218 
2219  //! Parse JSON text from an input stream
2220  /*! \tparam parseFlags Combination of \ref ParseFlag.
2221  \tparam InputStream Type of input stream, implementing Stream concept
2222  \param is Input stream to be parsed.
2223  \return The document itself for fluent API.
2224  */
2225  template <unsigned parseFlags, typename InputStream>
2226  GenericDocument& ParseStream(InputStream& is) {
2227  return ParseStream<parseFlags, Encoding, InputStream>(is);
2228  }
2229 
2230  //! Parse JSON text from an input stream (with \ref kParseDefaultFlags)
2231  /*! \tparam InputStream Type of input stream, implementing Stream concept
2232  \param is Input stream to be parsed.
2233  \return The document itself for fluent API.
2234  */
2235  template <typename InputStream>
2236  GenericDocument& ParseStream(InputStream& is) {
2237  return ParseStream<kParseDefaultFlags, Encoding, InputStream>(is);
2238  }
2239  //!@}
2240 
2241  //!@name Parse in-place from mutable string
2242  //!@{
2243 
2244  //! Parse JSON text from a mutable string
2245  /*! \tparam parseFlags Combination of \ref ParseFlag.
2246  \param str Mutable zero-terminated string to be parsed.
2247  \return The document itself for fluent API.
2248  */
2249  template <unsigned parseFlags>
2252  return ParseStream<parseFlags | kParseInsituFlag>(s);
2253  }
2254 
2255  //! Parse JSON text from a mutable string (with \ref kParseDefaultFlags)
2256  /*! \param str Mutable zero-terminated string to be parsed.
2257  \return The document itself for fluent API.
2258  */
2260  return ParseInsitu<kParseDefaultFlags>(str);
2261  }
2262  //!@}
2263 
2264  //!@name Parse from read-only string
2265  //!@{
2266 
2267  //! Parse JSON text from a read-only string (with Encoding conversion)
2268  /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag).
2269  \tparam SourceEncoding Transcoding from input Encoding
2270  \param str Read-only zero-terminated string to be parsed.
2271  */
2272  template <unsigned parseFlags, typename SourceEncoding>
2273  GenericDocument& Parse(const typename SourceEncoding::Ch* str) {
2274  RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag));
2276  return ParseStream<parseFlags, SourceEncoding>(s);
2277  }
2278 
2279  //! Parse JSON text from a read-only string
2280  /*! \tparam parseFlags Combination of \ref ParseFlag (must not contain \ref kParseInsituFlag).
2281  \param str Read-only zero-terminated string to be parsed.
2282  */
2283  template <unsigned parseFlags>
2284  GenericDocument& Parse(const Ch* str) {
2285  return Parse<parseFlags, Encoding>(str);
2286  }
2287 
2288  //! Parse JSON text from a read-only string (with \ref kParseDefaultFlags)
2289  /*! \param str Read-only zero-terminated string to be parsed.
2290  */
2291  GenericDocument& Parse(const Ch* str) {
2292  return Parse<kParseDefaultFlags>(str);
2293  }
2294 
2295  template <unsigned parseFlags, typename SourceEncoding>
2296  GenericDocument& Parse(const typename SourceEncoding::Ch* str, size_t length) {
2297  RAPIDJSON_ASSERT(!(parseFlags & kParseInsituFlag));
2298  MemoryStream ms(reinterpret_cast<const char*>(str), length * sizeof(typename SourceEncoding::Ch));
2300  ParseStream<parseFlags, SourceEncoding>(is);
2301  return *this;
2302  }
2303 
2304  template <unsigned parseFlags>
2305  GenericDocument& Parse(const Ch* str, size_t length) {
2306  return Parse<parseFlags, Encoding>(str, length);
2307  }
2308 
2309  GenericDocument& Parse(const Ch* str, size_t length) {
2310  return Parse<kParseDefaultFlags>(str, length);
2311  }
2312 
2313 #if RAPIDJSON_HAS_STDSTRING
2314  template <unsigned parseFlags, typename SourceEncoding>
2315  GenericDocument& Parse(const std::basic_string<typename SourceEncoding::Ch>& str) {
2316  // c_str() is constant complexity according to standard. Should be faster than Parse(const char*, size_t)
2317  return Parse<parseFlags, SourceEncoding>(str.c_str());
2318  }
2319 
2320  template <unsigned parseFlags>
2321  GenericDocument& Parse(const std::basic_string<Ch>& str) {
2322  return Parse<parseFlags, Encoding>(str.c_str());
2323  }
2324 
2325  GenericDocument& Parse(const std::basic_string<Ch>& str) {
2326  return Parse<kParseDefaultFlags>(str);
2327  }
2328 #endif // RAPIDJSON_HAS_STDSTRING
2329 
2330  //!@}
2331 
2332  //!@name Handling parse errors
2333  //!@{
2334 
2335  //! Whether a parse error has occured in the last parsing.
2336  bool HasParseError() const { return parseResult_.IsError(); }
2337 
2338  //! Get the \ref ParseErrorCode of last parsing.
2339  ParseErrorCode GetParseError() const { return parseResult_.Code(); }
2340 
2341  //! Get the position of last parsing error in input, 0 otherwise.
2342  size_t GetErrorOffset() const { return parseResult_.Offset(); }
2343 
2344  //! Implicit conversion to get the last parse result
2345 #ifndef __clang // -Wdocumentation
2346  /*! \return \ref ParseResult of the last parse operation
2347 
2348  \code
2349  Document doc;
2350  ParseResult ok = doc.Parse(json);
2351  if (!ok)
2352  printf( "JSON parse error: %s (%u)\n", GetParseError_En(ok.Code()), ok.Offset());
2353  \endcode
2354  */
2355 #endif
2356  operator ParseResult() const { return parseResult_; }
2357  //!@}
2358 
2359  //! Get the allocator of this document.
2361  RAPIDJSON_ASSERT(allocator_);
2362  return *allocator_;
2363  }
2364 
2365  //! Get the capacity of stack in bytes.
2366  size_t GetStackCapacity() const { return stack_.GetCapacity(); }
2367 
2368 private:
2369  // clear stack on any exit from ParseStream, e.g. due to exception
2370  struct ClearStackOnExit {
2371  explicit ClearStackOnExit(GenericDocument& d) : d_(d) {}
2372  ~ClearStackOnExit() { d_.ClearStack(); }
2373  private:
2374  ClearStackOnExit(const ClearStackOnExit&);
2375  ClearStackOnExit& operator=(const ClearStackOnExit&);
2376  GenericDocument& d_;
2377  };
2378 
2379  // callers of the following private Handler functions
2380  // template <typename,typename,typename> friend class GenericReader; // for parsing
2381  template <typename, typename> friend class GenericValue; // for deep copying
2382 
2383 public:
2384  // Implementation of Handler
2385  bool Null() { new (stack_.template Push<ValueType>()) ValueType(); return true; }
2386  bool Bool(bool b) { new (stack_.template Push<ValueType>()) ValueType(b); return true; }
2387  bool Int(int i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
2388  bool Uint(unsigned i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
2389  bool Int64(int64_t i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
2390  bool Uint64(uint64_t i) { new (stack_.template Push<ValueType>()) ValueType(i); return true; }
2391  bool Double(double d) { new (stack_.template Push<ValueType>()) ValueType(d); return true; }
2392 
2393  bool RawNumber(const Ch* str, SizeType length, bool copy) {
2394  if (copy)
2395  new (stack_.template Push<ValueType>()) ValueType(str, length, GetAllocator());
2396  else
2397  new (stack_.template Push<ValueType>()) ValueType(str, length);
2398  return true;
2399  }
2400 
2401  bool String(const Ch* str, SizeType length, bool copy) {
2402  if (copy)
2403  new (stack_.template Push<ValueType>()) ValueType(str, length, GetAllocator());
2404  else
2405  new (stack_.template Push<ValueType>()) ValueType(str, length);
2406  return true;
2407  }
2408 
2409  bool StartObject() { new (stack_.template Push<ValueType>()) ValueType(kObjectType); return true; }
2410 
2411  bool Key(const Ch* str, SizeType length, bool copy) { return String(str, length, copy); }
2412 
2413  bool EndObject(SizeType memberCount) {
2414  typename ValueType::Member* members = stack_.template Pop<typename ValueType::Member>(memberCount);
2415  stack_.template Top<ValueType>()->SetObjectRaw(members, memberCount, GetAllocator());
2416  return true;
2417  }
2418 
2419  bool StartArray() { new (stack_.template Push<ValueType>()) ValueType(kArrayType); return true; }
2420 
2421  bool EndArray(SizeType elementCount) {
2422  ValueType* elements = stack_.template Pop<ValueType>(elementCount);
2423  stack_.template Top<ValueType>()->SetArrayRaw(elements, elementCount, GetAllocator());
2424  return true;
2425  }
2426 
2427 private:
2428  //! Prohibit copying
2429  GenericDocument(const GenericDocument&);
2430  //! Prohibit assignment
2431  GenericDocument& operator=(const GenericDocument&);
2432 
2433  void ClearStack() {
2434  if (Allocator::kNeedFree)
2435  while (stack_.GetSize() > 0) // Here assumes all elements in stack array are GenericValue (Member is actually 2 GenericValue objects)
2436  (stack_.template Pop<ValueType>(1))->~ValueType();
2437  else
2438  stack_.Clear();
2439  stack_.ShrinkToFit();
2440  }
2441 
2442  void Destroy() {
2443  RAPIDJSON_DELETE(ownAllocator_);
2444  }
2445 
2446  static const size_t kDefaultStackCapacity = 1024;
2447  Allocator* allocator_;
2448  Allocator* ownAllocator_;
2449  internal::Stack<StackAllocator> stack_;
2450  ParseResult parseResult_;
2451 };
2452 
2453 //! GenericDocument with UTF8 encoding
2455 
2456 //! Helper class for accessing Value of array type.
2457 /*!
2458  Instance of this helper class is obtained by \c GenericValue::GetArray().
2459  In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1.
2460 */
2461 template <bool Const, typename ValueT>
2462 class GenericArray {
2463 public:
2464  typedef GenericArray<true, ValueT> ConstArray;
2465  typedef GenericArray<false, ValueT> Array;
2466  typedef ValueT PlainType;
2467  typedef typename internal::MaybeAddConst<Const,PlainType>::Type ValueType;
2468  typedef ValueType* ValueIterator; // This may be const or non-const iterator
2469  typedef const ValueT* ConstValueIterator;
2470  typedef typename ValueType::AllocatorType AllocatorType;
2471  typedef typename ValueType::StringRefType StringRefType;
2472 
2473  template <typename, typename>
2474  friend class GenericValue;
2475 
2476  GenericArray(const GenericArray& rhs) : value_(rhs.value_) {}
2477  GenericArray& operator=(const GenericArray& rhs) { value_ = rhs.value_; return *this; }
2478  ~GenericArray() {}
2479 
2480  SizeType Size() const { return value_.Size(); }
2481  SizeType Capacity() const { return value_.Capacity(); }
2482  bool Empty() const { return value_.Empty(); }
2483  void Clear() const { value_.Clear(); }
2484  ValueType& operator[](SizeType index) const { return value_[index]; }
2485  ValueIterator Begin() const { return value_.Begin(); }
2486  ValueIterator End() const { return value_.End(); }
2487  GenericArray Reserve(SizeType newCapacity, AllocatorType &allocator) const { value_.Reserve(newCapacity, allocator); return *this; }
2488  GenericArray PushBack(ValueType& value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; }
2489 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
2490  GenericArray PushBack(ValueType&& value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; }
2491 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
2492  GenericArray PushBack(StringRefType value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; }
2493  template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (const GenericArray&)) PushBack(T value, AllocatorType& allocator) const { value_.PushBack(value, allocator); return *this; }
2494  GenericArray PopBack() const { value_.PopBack(); return *this; }
2495  ValueIterator Erase(ConstValueIterator pos) const { return value_.Erase(pos); }
2496  ValueIterator Erase(ConstValueIterator first, ConstValueIterator last) const { return value_.Erase(first, last); }
2497 
2498 #if RAPIDJSON_HAS_CXX11_RANGE_FOR
2499  ValueIterator begin() const { return value_.Begin(); }
2500  ValueIterator end() const { return value_.End(); }
2501 #endif
2502 
2503 private:
2504  GenericArray();
2505  GenericArray(ValueType& value) : value_(value) {}
2506  ValueType& value_;
2507 };
2508 
2509 //! Helper class for accessing Value of object type.
2510 /*!
2511  Instance of this helper class is obtained by \c GenericValue::GetObject().
2512  In addition to all APIs for array type, it provides range-based for loop if \c RAPIDJSON_HAS_CXX11_RANGE_FOR=1.
2513 */
2514 template <bool Const, typename ValueT>
2515 class GenericObject {
2516 public:
2517  typedef GenericObject<true, ValueT> ConstObject;
2518  typedef GenericObject<false, ValueT> Object;
2519  typedef ValueT PlainType;
2520  typedef typename internal::MaybeAddConst<Const,PlainType>::Type ValueType;
2521  typedef GenericMemberIterator<Const, typename ValueT::EncodingType, typename ValueT::AllocatorType> MemberIterator; // This may be const or non-const iterator
2522  typedef GenericMemberIterator<true, typename ValueT::EncodingType, typename ValueT::AllocatorType> ConstMemberIterator;
2523  typedef typename ValueType::AllocatorType AllocatorType;
2524  typedef typename ValueType::StringRefType StringRefType;
2525  typedef typename ValueType::EncodingType EncodingType;
2526  typedef typename ValueType::Ch Ch;
2527 
2528  template <typename, typename>
2529  friend class GenericValue;
2530 
2531  GenericObject(const GenericObject& rhs) : value_(rhs.value_) {}
2532  GenericObject& operator=(const GenericObject& rhs) { value_ = rhs.value_; return *this; }
2533  ~GenericObject() {}
2534 
2535  SizeType MemberCount() const { return value_.MemberCount(); }
2536  bool ObjectEmpty() const { return value_.ObjectEmpty(); }
2537  template <typename T> ValueType& operator[](T* name) const { return value_[name]; }
2538  template <typename SourceAllocator> ValueType& operator[](const GenericValue<EncodingType, SourceAllocator>& name) const { return value_[name]; }
2539 #if RAPIDJSON_HAS_STDSTRING
2540  ValueType& operator[](const std::basic_string<Ch>& name) const { return value_[name]; }
2541 #endif
2542  MemberIterator MemberBegin() const { return value_.MemberBegin(); }
2543  MemberIterator MemberEnd() const { return value_.MemberEnd(); }
2544  bool HasMember(const Ch* name) const { return value_.HasMember(name); }
2545 #if RAPIDJSON_HAS_STDSTRING
2546  bool HasMember(const std::basic_string<Ch>& name) const { return value_.HasMember(name); }
2547 #endif
2548  template <typename SourceAllocator> bool HasMember(const GenericValue<EncodingType, SourceAllocator>& name) const { return value_.HasMember(name); }
2549  MemberIterator FindMember(const Ch* name) const { return value_.FindMember(name); }
2550  template <typename SourceAllocator> MemberIterator FindMember(const GenericValue<EncodingType, SourceAllocator>& name) const { return value_.FindMember(name); }
2551 #if RAPIDJSON_HAS_STDSTRING
2552  MemberIterator FindMember(const std::basic_string<Ch>& name) const { return value_.FindMember(name); }
2553 #endif
2554  GenericObject AddMember(ValueType& name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2555  GenericObject AddMember(ValueType& name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2556 #if RAPIDJSON_HAS_STDSTRING
2557  GenericObject AddMember(ValueType& name, std::basic_string<Ch>& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2558 #endif
2559  template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (ValueType&)) AddMember(ValueType& name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2560 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
2561  GenericObject AddMember(ValueType&& name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2562  GenericObject AddMember(ValueType&& name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2563  GenericObject AddMember(ValueType& name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2564  GenericObject AddMember(StringRefType name, ValueType&& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2565 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
2566  GenericObject AddMember(StringRefType name, ValueType& value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2567  GenericObject AddMember(StringRefType name, StringRefType value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2568  template <typename T> RAPIDJSON_DISABLEIF_RETURN((internal::OrExpr<internal::IsPointer<T>, internal::IsGenericValue<T> >), (GenericObject)) AddMember(StringRefType name, T value, AllocatorType& allocator) const { value_.AddMember(name, value, allocator); return *this; }
2569  void RemoveAllMembers() { value_.RemoveAllMembers(); }
2570  bool RemoveMember(const Ch* name) const { return value_.RemoveMember(name); }
2571 #if RAPIDJSON_HAS_STDSTRING
2572  bool RemoveMember(const std::basic_string<Ch>& name) const { return value_.RemoveMember(name); }
2573 #endif
2574  template <typename SourceAllocator> bool RemoveMember(const GenericValue<EncodingType, SourceAllocator>& name) const { return value_.RemoveMember(name); }
2575  MemberIterator RemoveMember(MemberIterator m) const { return value_.RemoveMember(m); }
2576  MemberIterator EraseMember(ConstMemberIterator pos) const { return value_.EraseMember(pos); }
2577  MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last) const { return value_.EraseMember(first, last); }
2578  bool EraseMember(const Ch* name) const { return value_.EraseMember(name); }
2579 #if RAPIDJSON_HAS_STDSTRING
2580  bool EraseMember(const std::basic_string<Ch>& name) const { return EraseMember(ValueType(StringRef(name))); }
2581 #endif
2582  template <typename SourceAllocator> bool EraseMember(const GenericValue<EncodingType, SourceAllocator>& name) const { return value_.EraseMember(name); }
2583 
2584 #if RAPIDJSON_HAS_CXX11_RANGE_FOR
2585  MemberIterator begin() const { return value_.MemberBegin(); }
2586  MemberIterator end() const { return value_.MemberEnd(); }
2587 #endif
2588 
2589 private:
2590  GenericObject();
2591  GenericObject(ValueType& value) : value_(value) {}
2592  ValueType& value_;
2593 };
2594 
2595 RAPIDJSON_NAMESPACE_END
2596 #ifdef _MINWINDEF_ // see: http://stackoverflow.com/questions/22744262/cant-call-stdmax-because-minwindef-h-defines-max
2597 #ifndef NOMINMAX
2598 #pragma pop_macro("min")
2599 #pragma pop_macro("max")
2600 #endif
2601 #endif
2602 RAPIDJSON_DIAG_POP
2603 
2604 #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:2079
GenericValue(unsigned u) RAPIDJSON_NOEXCEPT
Constructor for unsigned value.
Definition: document.h:688
true
Definition: rapidjson.h:619
ValueIterator Erase(ConstValueIterator first, ConstValueIterator last)
Remove elements in the range [first, last) of the array.
Definition: document.h:1674
Definition: document.h:1943
Read-only string stream.
Definition: fwd.h:47
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:893
ValueIterator Begin()
Element iterator.
Definition: document.h:1557
GenericMemberIterator< true, Encoding, Allocator > ConstIterator
Constant iterator type.
Definition: document.h:124
DifferenceType operator-(ConstIterator that) const
Distance.
Definition: document.h:195
GenericValue(const Ch *s, SizeType length, Allocator &allocator)
Constructor for copy-string (i.e. do make a copy of string)
Definition: document.h:733
GenericValue< UTF8<> > Value
GenericValue with UTF8 encoding.
Definition: document.h:2062
GenericValue(int64_t i64) RAPIDJSON_NOEXCEPT
Constructor for int64_t value.
Definition: document.h:694
~GenericValue()
Destructor.
Definition: document.h:770
#define RAPIDJSON_UINT64_C2(high32, low32)
Construct a 64-bit literal by a pair of 32-bit integer.
Definition: rapidjson.h:294
SAX-style JSON parser. Use Reader for UTF8 encoding and default allocator.
Definition: fwd.h:88
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with kParseDefaultFlags)
Definition: document.h:2236
GenericPointer< Value, CrtAllocator > Pointer
GenericPointer for Value (UTF-8, default allocator).
Definition: fwd.h:126
GenericValue(Object o) RAPIDJSON_NOEXCEPT
Constructor for Object.
Definition: document.h:762
GenericValue(StringRefType s) RAPIDJSON_NOEXCEPT
Constructor for constant string (i.e. do not make a copy of string)
Definition: document.h:730
GenericValue & SetString(const std::basic_string< Ch > &s, Allocator &allocator)
Set this value as a string by copying from source string.
Definition: document.h:1783
GenericValue & SetObject()
Set this value as an empty object.
Definition: document.h:1065
GenericStringRef< Ch > StringRefType
Reference to a constant string.
Definition: document.h:555
GenericMemberIterator Iterator
Iterator type itself.
Definition: document.h:122
ConstMemberIterator MemberBegin() const
Const member iterator.
Definition: document.h:1127
GenericValue(const GenericValue< Encoding, SourceAllocator > &rhs, Allocator &allocator, bool copyConstStrings=false)
Explicit copy constructor (with allocator)
Definition: document.h:622
GenericValue & operator=(GenericValue &rhs) RAPIDJSON_NOEXCEPT
Assignment with move semantics.
Definition: document.h:806
ConstMemberIterator MemberEnd() const
Const past-the-end member iterator.
Definition: document.h:1130
unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:389
GenericMemberIterator< false, Encoding, Allocator > NonConstIterator
Non-constant iterator type.
Definition: document.h:126
Helper class for accessing Value of array type.
Definition: document.h:531
Allocator AllocatorType
Allocator type from template parameter.
Definition: document.h:2080
MemberIterator FindMember(const GenericValue< Encoding, SourceAllocator > &name)
Find member by name.
Definition: document.h:1205
bool ObjectEmpty() const
Check whether the object is empty.
Definition: document.h:1071
false
Definition: rapidjson.h:618
ParseErrorCode
Error code of parsing.
Definition: error.h:64
bool operator!=(const GenericValue< Encoding, SourceAllocator > &rhs) const
Not-equal-to operator.
Definition: document.h:964
#define RAPIDJSON_STATIC_ASSERT(x)
(Internal) macro to check for conditions at compile-time
Definition: rapidjson.h:450
GenericValue(const Ch *s, Allocator &allocator)
Constructor for copy-string (i.e. do make a copy of string)
Definition: document.h:736
bool RemoveMember(const Ch *name)
Remove a member in object by its name.
Definition: document.h:1404
bool Accept(Handler &handler) const
Generate events of this value to a Handler.
Definition: document.h:1820
Represents an in-memory input byte stream.
Definition: memorystream.h:40
GenericStringRef< CharType > StringRef(const std::basic_string< CharType > &str)
Mark a string object as constant string.
Definition: document.h:392
GenericValue & PopBack()
Remove the last element in the array.
Definition: document.h:1648
GenericValue & Reserve(SizeType newCapacity, Allocator &allocator)
Request the array to have enough capacity to store elements.
Definition: document.h:1574
ValueIterator End()
Past-the-end element iterator
Definition: document.h:1560
bool EraseMember(const Ch *name)
Erase a member in object by its name.
Definition: document.h:1490
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string (with kParseDefaultFlags)
Definition: document.h:2291
bool GetBool() const
Set boolean value.
Definition: document.h:1053
MemberIterator RemoveMember(MemberIterator m)
Remove a member in object by iterator.
Definition: document.h:1432
MemberIterator MemberBegin()
Member iterator.
Definition: document.h:1133
friend void swap(GenericValue &a, GenericValue &b) RAPIDJSON_NOEXCEPT
free-standing swap function helper
Definition: document.h:889
Allocator AllocatorType
Allocator type from template parameter.
Definition: document.h:553
GenericValue & AddMember(StringRefType name, StringRefType value, Allocator &allocator)
Add a constant string value as member (name-value pair) to the object.
Definition: document.h:1356
float GetFloat() const
Get the value as float type.
Definition: document.h:1717
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:1285
GenericValue & operator[](const std::basic_string< Ch > &name)
Get a value from an object associated with name (string object).
Definition: document.h:1121
double GetDouble() const
Get the value as double type.
Definition: document.h:1705
GenericValue(const Ch *s, SizeType length) RAPIDJSON_NOEXCEPT
Constructor for constant string (i.e. do not make a copy of string)
Definition: document.h:727
GenericValue & operator[](const GenericValue< Encoding, SourceAllocator > &name)
Get a value from an object associated with the name.
Definition: document.h:1100
GenericValue & SetString(const Ch *s, SizeType length, Allocator &allocator)
Set this value as a string by copying from source string.
Definition: document.h:1765
MemberIterator EraseMember(ConstMemberIterator pos)
Remove a member from an object by iterator.
Definition: document.h:1456
GenericStringRef(const CharType *str, SizeType len)
Create constant string reference from pointer and length.
Definition: document.h:322
bool operator==(const std::basic_string< Ch > &rhs) const
Equal-to operator with string object.
Definition: document.h:952
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:1748
GenericStringRef< CharType > StringRef(const CharType *str)
Mark a character pointer as constant string.
Definition: document.h:354
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string (with kParseDefaultFlags)
Definition: document.h:2259
#define RAPIDJSON_NEW(TypeName)
! customization point for global new
Definition: rapidjson.h:599
SizeType Size() const
Get the number of elements in array.
Definition: document.h:1523
const SizeType length
length of the string (excluding the trailing NULL terminator)
Definition: document.h:331
BaseType::reference Reference
Reference to (const) GenericMember.
Definition: document.h:131
const GenericValue * ConstValueIterator
Constant value iterator for iterating in array.
Definition: document.h:559
size_t GetErrorOffset() const
Get the position of last parsing error in input, 0 otherwise.
Definition: document.h:2342
Concept for encoding of Unicode characters.
MemberIterator FindMember(const std::basic_string< Ch > &name)
Find member by string object name.
Definition: document.h:1224
GenericValue & AddMember(GenericValue &name, GenericValue &value, Allocator &allocator)
Add a member (name-value pair) to the object.
Definition: document.h:1238
Result of parsing (wraps ParseErrorCode)
Definition: error.h:106
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream (with Encoding conversion)
Definition: document.h:2207
MemberIterator FindMember(const Ch *name)
Find member by name.
Definition: document.h:1184
GenericValue(Array a) RAPIDJSON_NOEXCEPT
Constructor for Array.
Definition: document.h:751
ConstValueIterator Begin() const
Constant element iterator.
Definition: document.h:1563
void Clear()
Remove all elements in the array.
Definition: document.h:1535
GenericDocument & Parse(const Ch *str)
Parse JSON text from a read-only string.
Definition: document.h:2284
Represents a JSON Pointer. Use Pointer for UTF8 encoding and default allocator.
Definition: fwd.h:126
Type
Type of JSON value.
Definition: rapidjson.h:616
Encoding::Ch Ch
Character type derived from Encoding.
Definition: document.h:2078
GenericDocument(Type type, Allocator *allocator=0, size_t stackCapacity=kDefaultStackCapacity, StackAllocator *stackAllocator=0)
Constructor.
Definition: document.h:2089
GenericValue & operator[](SizeType index)
Get an element from array by index.
Definition: document.h:1548
BaseType::pointer Pointer
Pointer to (const) GenericMember.
Definition: document.h:129
ParseErrorCode GetParseError() const
Get the ParseErrorCode of last parsing.
Definition: document.h:2339
object
Definition: rapidjson.h:620
GenericValue & PushBack(GenericValue &value, Allocator &allocator)
Append a GenericValue at the end of the array.
Definition: document.h:1593
bool Is() const
Templated version for checking whether this value is type T.
Definition: document.h:1796
GenericValue & Swap(GenericValue &other) RAPIDJSON_NOEXCEPT
Exchange the contents of this value with those of other.
Definition: document.h:869
MemberIterator MemberEnd()
Past-the-end member iterator
Definition: document.h:1136
GenericStringRef(const CharType(&str)[N]) RAPIDJSON_NOEXCEPT
Create string reference from const character array.
Definition: document.h:286
bool operator!=(const Ch *rhs) const
Not-equal-to operator with const C-string pointer.
Definition: document.h:967
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:742
#define RAPIDJSON_UNLIKELY(x)
Compiler branching hint for expression with low probability to be true.
Definition: rapidjson.h:481
GenericValue(Type type) RAPIDJSON_NOEXCEPT
Constructor with JSON value type.
Definition: document.h:600
A document for parsing JSON text as DOM.
Definition: document.h:70
array
Definition: rapidjson.h:621
GenericValue * ValueIterator
Value iterator for iterating in array.
Definition: document.h:558
bool operator==(const GenericValue< Encoding, SourceAllocator > &rhs) const
Equal-to operator.
Definition: document.h:904
#define RAPIDJSON_DELETE(x)
! customization point for global delete
Definition: rapidjson.h:603
Encoding::Ch Ch
Character type derived from Encoding.
Definition: document.h:554
Definition: document.h:1947
GenericMemberIterator< false, Encoding, Allocator >::Iterator MemberIterator
Member iterator for iterating in object.
Definition: document.h:556
size_t GetStackCapacity() const
Get the capacity of stack in bytes.
Definition: document.h:2366
GenericValue(bool b) RAPIDJSON_NOEXCEPT
Constructor for boolean value.
Definition: document.h:673
GenericValue(int i) RAPIDJSON_NOEXCEPT
Constructor for int value.
Definition: document.h:682
GenericValue< Encoding, Allocator > value
value of member.
Definition: document.h:81
GenericValue() RAPIDJSON_NOEXCEPT
Default constructor creates a null value.
Definition: document.h:570
GenericStringRef(const CharType *str)
Explicitly create string reference from const character pointer.
Definition: document.h:310
friend void swap(GenericDocument &a, GenericDocument &b) RAPIDJSON_NOEXCEPT
free-standing swap function helper
Definition: document.h:2179
null
Definition: rapidjson.h:617
GenericDocument(Allocator *allocator=0, size_t stackCapacity=kDefaultStackCapacity, StackAllocator *stackAllocator=0)
Constructor.
Definition: document.h:2102
GenericValue< Encoding, Allocator > name
name of member (must be a string)
Definition: document.h:80
string
Definition: rapidjson.h:622
Input byte stream wrapper with a statically bound encoding.
Definition: encodedstream.h:39
Allocator & GetAllocator()
Get the allocator of this document.
Definition: document.h:2360
GenericMember< Encoding, Allocator > Member
Name-value pair in an object.
Definition: document.h:551
GenericDocument & ParseStream(InputStream &is)
Parse JSON text from an input stream.
Definition: document.h:2226
SizeType MemberCount() const
Get the number of members in the object.
Definition: document.h:1068
CharType Ch
character type of the string
Definition: document.h:258
Encoding EncodingType
Encoding type from template parameter.
Definition: document.h:552
bool HasMember(const GenericValue< Encoding, SourceAllocator > &name) const
Check whether a member exists in the object with GenericValue name.
Definition: document.h:1170
GenericDocument & Parse(const typename SourceEncoding::Ch *str)
Parse JSON text from a read-only string (with Encoding conversion)
Definition: document.h:2273
GenericValue & SetString(StringRefType s)
Set this value as a string without copying source string.
Definition: document.h:1755
void RemoveAllMembers()
Remove all members in the object.
Definition: document.h:1389
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:825
GenericDocument< UTF8<> > Document
GenericDocument with UTF8 encoding.
Definition: document.h:2454
GenericDocument & Swap(GenericDocument &rhs) RAPIDJSON_NOEXCEPT
Exchange the contents of this document with those of another.
Definition: document.h:2158
bool operator==(const Ch *rhs) const
Equal-to operator with const C-string pointer.
Definition: document.h:946
In-situ(destructive) parsing.
Definition: reader.h:149
bool HasMember(const Ch *name) const
Check whether a member exists in the object.
Definition: document.h:1146
bool HasMember(const std::basic_string< Ch > &name) const
Check whether a member exists in the object with string object.
Definition: document.h:1157
bool Empty() const
Check whether the array is empty.
Definition: document.h:1529
GenericDocument & Populate(Generator &g)
Populate this document by a generator which produces SAX events.
Definition: document.h:2187
Reference to a constant string (not taking a copy)
Definition: document.h:257
GenericValue(double d) RAPIDJSON_NOEXCEPT
Constructor for double value.
Definition: document.h:721
GenericValue & SetBool(bool b)
Definition: document.h:1056
Concept for allocating, resizing and freeing memory block.
Represents a JSON value. Use Value for UTF8 encoding and default allocator.
Definition: document.h:67
GenericValue & CopyFrom(const GenericValue< Encoding, SourceAllocator > &rhs, Allocator &allocator, bool copyConstStrings=false)
Deep-copy assignment from Value.
Definition: document.h:857
GenericValue & SetArray()
Set this value as an empty array.
Definition: document.h:1520
(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:1773
GenericValue(float f) RAPIDJSON_NOEXCEPT
Constructor for float value.
Definition: document.h:724
MemberIterator EraseMember(ConstMemberIterator first, ConstMemberIterator last)
Remove members in the range [first, last) from an object.
Definition: document.h:1469
const Ch *const s
plain CharType pointer
Definition: document.h:330
Helper class for accessing Value of object type.
Definition: document.h:532
GenericMemberIterator< true, Encoding, Allocator >::Iterator ConstMemberIterator
Constant member iterator for iterating in object.
Definition: document.h:557
ConstValueIterator End() const
Constant past-the-end element iterator.
Definition: document.h:1566
GenericMemberIterator()
Default constructor (singular value)
Definition: document.h:139
SizeType Capacity() const
Get the capacity of array.
Definition: document.h:1526
GenericValue & AddMember(GenericValue &name, StringRefType value, Allocator &allocator)
Add a constant string value as member (name-value pair) to the object.
Definition: document.h:1270
GenericValue< Encoding, Allocator > ValueType
Value type of itself.
Definition: document.h:560
GenericValue & PushBack(StringRefType value, Allocator &allocator)
Append a constant string reference at the end of the array.
Definition: document.h:1616
bool HasParseError() const
Whether a parse error has occured in the last parsing.
Definition: document.h:2336
GenericValue & AddMember(StringRefType name, GenericValue &value, Allocator &allocator)
Add a member (name-value pair) to the object.
Definition: document.h:1342
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:411
GenericValue(uint64_t u64) RAPIDJSON_NOEXCEPT
Constructor for uint64_t value.
Definition: document.h:709
number
Definition: rapidjson.h:623
GenericDocument & ParseInsitu(Ch *str)
Parse JSON text from a mutable string.
Definition: document.h:2250
ValueIterator Erase(ConstValueIterator pos)
Remove an element of array by iterator.
Definition: document.h:1662
SizeType GetStringLength() const
Get the length of string.
Definition: document.h:1738
A read-write string stream.
Definition: fwd.h:52