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