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_RAPIDJSON_H_
16 #define RAPIDJSON_RAPIDJSON_H_
17 
18 /*!\file rapidjson.h
19     \brief common definitions and configuration
20 
21     \see RAPIDJSON_CONFIG
22  */
23 
24 /*! \defgroup RAPIDJSON_CONFIG RapidJSON configuration
25     \brief Configuration macros for library features
26 
27     Some RapidJSON features are configurable to adapt the library to a wide
28     variety of platforms, environments and usage scenarios.  Most of the
29     features can be configured in terms of overriden or predefined
30     preprocessor macros at compile-time.
31 
32     Some additional customization is available in the \ref RAPIDJSON_ERRORS APIs.
33 
34     \note These macros should be given on the compiler command-line
35           (where applicable)  to avoid inconsistent values when compiling
36           different translation units of a single application.
37  */
38 
39 #include <cstdlib>  // malloc(), realloc(), free(), size_t
40 #include <cstring>  // memset(), memcpy(), memmove(), memcmp()
41 
42 ///////////////////////////////////////////////////////////////////////////////
43 // RAPIDJSON_VERSION_STRING
44 //
45 // ALWAYS synchronize the following 3 macros with corresponding variables in /CMakeLists.txt.
46 //
47 
48 //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
49 // token stringification
50 #define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x)
51 #define RAPIDJSON_DO_STRINGIFY(x) #x
52 //!@endcond
53 
54 /*! \def RAPIDJSON_MAJOR_VERSION
55     \ingroup RAPIDJSON_CONFIG
56     \brief Major version of RapidJSON in integer.
57 */
58 /*! \def RAPIDJSON_MINOR_VERSION
59     \ingroup RAPIDJSON_CONFIG
60     \brief Minor version of RapidJSON in integer.
61 */
62 /*! \def RAPIDJSON_PATCH_VERSION
63     \ingroup RAPIDJSON_CONFIG
64     \brief Patch version of RapidJSON in integer.
65 */
66 /*! \def RAPIDJSON_VERSION_STRING
67     \ingroup RAPIDJSON_CONFIG
68     \brief Version of RapidJSON in "<major>.<minor>.<patch>" string format.
69 */
70 #define RAPIDJSON_MAJOR_VERSION 1
71 #define RAPIDJSON_MINOR_VERSION 0
72 #define RAPIDJSON_PATCH_VERSION 2
73 #define RAPIDJSON_VERSION_STRING \
74     RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION)
75 
76 ///////////////////////////////////////////////////////////////////////////////
77 // RAPIDJSON_NAMESPACE_(BEGIN|END)
78 /*! \def RAPIDJSON_NAMESPACE
79     \ingroup RAPIDJSON_CONFIG
80     \brief   provide custom rapidjson namespace
81 
82     In order to avoid symbol clashes and/or "One Definition Rule" errors
83     between multiple inclusions of (different versions of) RapidJSON in
84     a single binary, users can customize the name of the main RapidJSON
85     namespace.
86 
87     In case of a single nesting level, defining \c RAPIDJSON_NAMESPACE
88     to a custom name (e.g. \c MyRapidJSON) is sufficient.  If multiple
89     levels are needed, both \ref RAPIDJSON_NAMESPACE_BEGIN and \ref
90     RAPIDJSON_NAMESPACE_END need to be defined as well:
91 
92     \code
93     // in some .cpp file
94     #define RAPIDJSON_NAMESPACE my::rapidjson
95     #define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapidjson {
96     #define RAPIDJSON_NAMESPACE_END   } }
97     #include "rapidjson/..."
98     \endcode
99 
100     \see rapidjson
101  */
102 /*! \def RAPIDJSON_NAMESPACE_BEGIN
103     \ingroup RAPIDJSON_CONFIG
104     \brief   provide custom rapidjson namespace (opening expression)
105     \see RAPIDJSON_NAMESPACE
106 */
107 /*! \def RAPIDJSON_NAMESPACE_END
108     \ingroup RAPIDJSON_CONFIG
109     \brief   provide custom rapidjson namespace (closing expression)
110     \see RAPIDJSON_NAMESPACE
111 */
112 #ifndef RAPIDJSON_NAMESPACE
113 #define RAPIDJSON_NAMESPACE rapidjson
114 #endif
115 #ifndef RAPIDJSON_NAMESPACE_BEGIN
116 #define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE {
117 #endif
118 #ifndef RAPIDJSON_NAMESPACE_END
119 #define RAPIDJSON_NAMESPACE_END }
120 #endif
121 
122 ///////////////////////////////////////////////////////////////////////////////
123 // RAPIDJSON_NO_INT64DEFINE
124 
125 /*! \def RAPIDJSON_NO_INT64DEFINE
126     \ingroup RAPIDJSON_CONFIG
127     \brief Use external 64-bit integer types.
128 
129     RapidJSON requires the 64-bit integer types \c int64_t and  \c uint64_t types
130     to be available at global scope.
131 
132     If users have their own definition, define RAPIDJSON_NO_INT64DEFINE to
133     prevent RapidJSON from defining its own types.
134 */
135 #ifndef RAPIDJSON_NO_INT64DEFINE
136 //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
137 #ifdef _MSC_VER
138 #include "msinttypes/stdint.h"
139 #include "msinttypes/inttypes.h"
140 #else
141 // Other compilers should have this.
142 #include <stdint.h>
143 #include <inttypes.h>
144 #endif
145 //!@endcond
146 #ifdef RAPIDJSON_DOXYGEN_RUNNING
147 #define RAPIDJSON_NO_INT64DEFINE
148 #endif
149 #endif // RAPIDJSON_NO_INT64TYPEDEF
150 
151 ///////////////////////////////////////////////////////////////////////////////
152 // RAPIDJSON_FORCEINLINE
153 
154 #ifndef RAPIDJSON_FORCEINLINE
155 //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
156 #if defined(_MSC_VER) && !defined(NDEBUG)
157 #define RAPIDJSON_FORCEINLINE __forceinline
158 #elif defined(__GNUC__) && __GNUC__ >= 4 && !defined(NDEBUG)
159 #define RAPIDJSON_FORCEINLINE __attribute__((always_inline))
160 #else
161 #define RAPIDJSON_FORCEINLINE
162 #endif
163 //!@endcond
164 #endif // RAPIDJSON_FORCEINLINE
165 
166 ///////////////////////////////////////////////////////////////////////////////
167 // RAPIDJSON_ENDIAN
168 #define RAPIDJSON_LITTLEENDIAN  0   //!< Little endian machine
169 #define RAPIDJSON_BIGENDIAN     1   //!< Big endian machine
170 
171 //! Endianness of the machine.
172 /*!
173     \def RAPIDJSON_ENDIAN
174     \ingroup RAPIDJSON_CONFIG
175 
176     GCC 4.6 provided macro for detecting endianness of the target machine. But other
177     compilers may not have this. User can define RAPIDJSON_ENDIAN to either
178     \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN.
179 
180     Default detection implemented with reference to
181     \li https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html
182     \li http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp
183 */
184 #ifndef RAPIDJSON_ENDIAN
185 // Detect with GCC 4.6's macro
186 #  ifdef __BYTE_ORDER__
187 #    if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
188 #      define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
189 #    elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
190 #      define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
191 #    else
192 #      error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
193 #    endif // __BYTE_ORDER__
194 // Detect with GLIBC's endian.h
195 #  elif defined(__GLIBC__)
196 #    include <endian.h>
197 #    if (__BYTE_ORDER == __LITTLE_ENDIAN)
198 #      define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
199 #    elif (__BYTE_ORDER == __BIG_ENDIAN)
200 #      define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
201 #    else
202 #      error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
203 #   endif // __GLIBC__
204 // Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro
205 #  elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
206 #    define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
207 #  elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
208 #    define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
209 // Detect with architecture macros
210 #  elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
211 #    define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
212 #  elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || defined(_M_X64) || defined(__bfin__)
213 #    define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
214 #  elif defined(RAPIDJSON_DOXYGEN_RUNNING)
215 #    define RAPIDJSON_ENDIAN
216 #  else
217 #    error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
218 #  endif
219 #endif // RAPIDJSON_ENDIAN
220 
221 ///////////////////////////////////////////////////////////////////////////////
222 // RAPIDJSON_64BIT
223 
224 //! Whether using 64-bit architecture
225 #ifndef RAPIDJSON_64BIT
226 #if defined(__LP64__) || defined(_WIN64) || defined(__EMSCRIPTEN__)
227 #define RAPIDJSON_64BIT 1
228 #else
229 #define RAPIDJSON_64BIT 0
230 #endif
231 #endif // RAPIDJSON_64BIT
232 
233 ///////////////////////////////////////////////////////////////////////////////
234 // RAPIDJSON_ALIGN
235 
236 //! Data alignment of the machine.
237 /*! \ingroup RAPIDJSON_CONFIG
238     \param x pointer to align
239 
240     Some machines require strict data alignment. Currently the default uses 4 bytes
241     alignment. User can customize by defining the RAPIDJSON_ALIGN function macro.
242 */
243 #ifndef RAPIDJSON_ALIGN
244 #if RAPIDJSON_64BIT == 1
245 #define RAPIDJSON_ALIGN(x) (((x) + static_cast<uint64_t>(7u)) & ~static_cast<uint64_t>(7u))
246 #else
247 #define RAPIDJSON_ALIGN(x) (((x) + 3u) & ~3u)
248 #endif
249 #endif
250 
251 ///////////////////////////////////////////////////////////////////////////////
252 // RAPIDJSON_UINT64_C2
253 
254 //! Construct a 64-bit literal by a pair of 32-bit integer.
255 /*!
256     64-bit literal with or without ULL suffix is prone to compiler warnings.
257     UINT64_C() is C macro which cause compilation problems.
258     Use this macro to define 64-bit constants by a pair of 32-bit integer.
259 */
260 #ifndef RAPIDJSON_UINT64_C2
261 #define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast<uint64_t>(high32) << 32) | static_cast<uint64_t>(low32))
262 #endif
263 
264 ///////////////////////////////////////////////////////////////////////////////
265 // RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD
266 
267 /*! \def RAPIDJSON_SIMD
268     \ingroup RAPIDJSON_CONFIG
269     \brief Enable SSE2/SSE4.2 optimization.
270 
271     RapidJSON supports optimized implementations for some parsing operations
272     based on the SSE2 or SSE4.2 SIMD extensions on modern Intel-compatible
273     processors.
274 
275     To enable these optimizations, two different symbols can be defined;
276     \code
277     // Enable SSE2 optimization.
278     #define RAPIDJSON_SSE2
279 
280     // Enable SSE4.2 optimization.
281     #define RAPIDJSON_SSE42
282     \endcode
283 
284     \c RAPIDJSON_SSE42 takes precedence, if both are defined.
285 
286     If any of these symbols is defined, RapidJSON defines the macro
287     \c RAPIDJSON_SIMD to indicate the availability of the optimized code.
288 */
289 #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \
290     || defined(RAPIDJSON_DOXYGEN_RUNNING)
291 #define RAPIDJSON_SIMD
292 #endif
293 
294 ///////////////////////////////////////////////////////////////////////////////
295 // RAPIDJSON_NO_SIZETYPEDEFINE
296 
297 #ifndef RAPIDJSON_NO_SIZETYPEDEFINE
298 /*! \def RAPIDJSON_NO_SIZETYPEDEFINE
299     \ingroup RAPIDJSON_CONFIG
300     \brief User-provided \c SizeType definition.
301 
302     In order to avoid using 32-bit size types for indexing strings and arrays,
303     define this preprocessor symbol and provide the type rapidjson::SizeType
304     before including RapidJSON:
305     \code
306     #define RAPIDJSON_NO_SIZETYPEDEFINE
307     namespace rapidjson { typedef ::std::size_t SizeType; }
308     #include "rapidjson/..."
309     \endcode
310 
311     \see rapidjson::SizeType
312 */
313 #ifdef RAPIDJSON_DOXYGEN_RUNNING
314 #define RAPIDJSON_NO_SIZETYPEDEFINE
315 #endif
316 RAPIDJSON_NAMESPACE_BEGIN
317 //! Size type (for string lengths, array sizes, etc.)
318 /*! RapidJSON uses 32-bit array/string indices even on 64-bit platforms,
319     instead of using \c size_t. Users may override the SizeType by defining
320     \ref RAPIDJSON_NO_SIZETYPEDEFINE.
321 */
322 typedef unsigned SizeType;
323 RAPIDJSON_NAMESPACE_END
324 #endif
325 
326 // always import std::size_t to rapidjson namespace
327 RAPIDJSON_NAMESPACE_BEGIN
328 using std::size_t;
329 RAPIDJSON_NAMESPACE_END
330 
331 ///////////////////////////////////////////////////////////////////////////////
332 // RAPIDJSON_ASSERT
333 
334 //! Assertion.
335 /*! \ingroup RAPIDJSON_CONFIG
336     By default, rapidjson uses C \c assert() for internal assertions.
337     User can override it by defining RAPIDJSON_ASSERT(x) macro.
338 
339     \note Parsing errors are handled and can be customized by the
340           \ref RAPIDJSON_ERRORS APIs.
341 */
342 #ifndef RAPIDJSON_ASSERT
343 #include <cassert>
344 #define RAPIDJSON_ASSERT(x) assert(x)
345 #endif // RAPIDJSON_ASSERT
346 
347 ///////////////////////////////////////////////////////////////////////////////
348 // RAPIDJSON_STATIC_ASSERT
349 
350 // Adopt from boost
351 #ifndef RAPIDJSON_STATIC_ASSERT
352 //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
353 RAPIDJSON_NAMESPACE_BEGIN
354 template <bool x> struct STATIC_ASSERTION_FAILURE;
355 template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
356 template<int x> struct StaticAssertTest {};
357 RAPIDJSON_NAMESPACE_END
358 
359 #define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y)
360 #define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y)
361 #define RAPIDJSON_DO_JOIN2(X, Y) X##Y
362 
363 #if defined(__GNUC__)
364 #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
365 #else
366 #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
367 #endif
368 //!@endcond
369 
370 /*! \def RAPIDJSON_STATIC_ASSERT
371     \brief (Internal) macro to check for conditions at compile-time
372     \param x compile-time condition
373     \hideinitializer
374  */
375 #define RAPIDJSON_STATIC_ASSERT(x) \
376     typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest< \
377       sizeof(::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE<bool(x) >)> \
378     RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
379 #endif
380 
381 ///////////////////////////////////////////////////////////////////////////////
382 // Helpers
383 
384 //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
385 
386 #define RAPIDJSON_MULTILINEMACRO_BEGIN do {
387 #define RAPIDJSON_MULTILINEMACRO_END \
388 } while((void)0, 0)
389 
390 // adopted from Boost
391 #define RAPIDJSON_VERSION_CODE(x,y,z) \
392   (((x)*100000) + ((y)*100) + (z))
393 
394 ///////////////////////////////////////////////////////////////////////////////
395 // RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF
396 
397 #if defined(__GNUC__)
398 #define RAPIDJSON_GNUC \
399     RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__)
400 #endif
401 
402 #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,2,0))
403 
404 #define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x))
405 #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x)
406 #define RAPIDJSON_DIAG_OFF(x) \
407     RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x)))
408 
409 // push/pop support in Clang and GCC>=4.6
410 #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0))
411 #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
412 #define RAPIDJSON_DIAG_POP  RAPIDJSON_DIAG_PRAGMA(pop)
413 #else // GCC >= 4.2, < 4.6
414 #define RAPIDJSON_DIAG_PUSH /* ignored */
415 #define RAPIDJSON_DIAG_POP /* ignored */
416 #endif
417 
418 #elif defined(_MSC_VER)
419 
420 // pragma (MSVC specific)
421 #define RAPIDJSON_PRAGMA(x) __pragma(x)
422 #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x))
423 
424 #define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x)
425 #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
426 #define RAPIDJSON_DIAG_POP  RAPIDJSON_DIAG_PRAGMA(pop)
427 
428 #else
429 
430 #define RAPIDJSON_DIAG_OFF(x) /* ignored */
431 #define RAPIDJSON_DIAG_PUSH   /* ignored */
432 #define RAPIDJSON_DIAG_POP    /* ignored */
433 
434 #endif // RAPIDJSON_DIAG_*
435 
436 ///////////////////////////////////////////////////////////////////////////////
437 // C++11 features
438 
439 #ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS
440 #if defined(__clang__)
441 #define RAPIDJSON_HAS_CXX11_RVALUE_REFS __has_feature(cxx_rvalue_references) && \
442     (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306)
443 #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
444       (defined(_MSC_VER) && _MSC_VER >= 1600)
445 
446 #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
447 #else
448 #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0
449 #endif
450 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
451 
452 #ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT
453 #if defined(__clang__)
454 #define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept)
455 #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__))
456 //    (defined(_MSC_VER) && _MSC_VER >= ????) // not yet supported
457 #define RAPIDJSON_HAS_CXX11_NOEXCEPT 1
458 #else
459 #define RAPIDJSON_HAS_CXX11_NOEXCEPT 0
460 #endif
461 #endif
462 #if RAPIDJSON_HAS_CXX11_NOEXCEPT
463 #define RAPIDJSON_NOEXCEPT noexcept
464 #else
465 #define RAPIDJSON_NOEXCEPT /* noexcept */
466 #endif // RAPIDJSON_HAS_CXX11_NOEXCEPT
467 
468 // no automatic detection, yet
469 #ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS
470 #define RAPIDJSON_HAS_CXX11_TYPETRAITS 0
471 #endif
472 
473 //!@endcond
474 
475 ///////////////////////////////////////////////////////////////////////////////
476 // new/delete
477 
478 #ifndef RAPIDJSON_NEW
479 ///! customization point for global \c new
480 #define RAPIDJSON_NEW(x) new x
481 #endif
482 #ifndef RAPIDJSON_DELETE
483 ///! customization point for global \c delete
484 #define RAPIDJSON_DELETE(x) delete x
485 #endif
486 
487 ///////////////////////////////////////////////////////////////////////////////
488 // Allocators and Encodings
489 
490 #include "allocators.h"
491 #include "encodings.h"
492 
493 /*! \namespace rapidjson
494     \brief main RapidJSON namespace
495     \see RAPIDJSON_NAMESPACE
496 */
497 RAPIDJSON_NAMESPACE_BEGIN
498 
499 ///////////////////////////////////////////////////////////////////////////////
500 //  Stream
501 
502 /*! \class rapidjson::Stream
503     \brief Concept for reading and writing characters.
504 
505     For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd().
506 
507     For write-only stream, only need to implement Put() and Flush().
508 
509 \code
510 concept Stream {
511     typename Ch;    //!< Character type of the stream.
512 
513     //! Read the current character from stream without moving the read cursor.
514     Ch Peek() const;
515 
516     //! Read the current character from stream and moving the read cursor to next character.
517     Ch Take();
518 
519     //! Get the current read cursor.
520     //! \return Number of characters read from start.
521     size_t Tell();
522 
523     //! Begin writing operation at the current read pointer.
524     //! \return The begin writer pointer.
525     Ch* PutBegin();
526 
527     //! Write a character.
528     void Put(Ch c);
529 
530     //! Flush the buffer.
531     void Flush();
532 
533     //! End the writing operation.
534     //! \param begin The begin write pointer returned by PutBegin().
535     //! \return Number of characters written.
536     size_t PutEnd(Ch* begin);
537 }
538 \endcode
539 */
540 
541 //! Provides additional information for stream.
542 /*!
543     By using traits pattern, this type provides a default configuration for stream.
544     For custom stream, this type can be specialized for other configuration.
545     See TEST(Reader, CustomStringStream) in readertest.cpp for example.
546 */
547 template<typename Stream>
548 struct StreamTraits {
549     //! Whether to make local copy of stream for optimization during parsing.
550     /*!
551         By default, for safety, streams do not use local copy optimization.
552         Stream that can be copied fast should specialize this, like StreamTraits<StringStream>.
553     */
554     enum { copyOptimization = 0 };
555 };
556 
557 //! Put N copies of a character to a stream.
558 template<typename Stream, typename Ch>
559 inline void PutN(Stream& stream, Ch c, size_t n) {
560     for (size_t i = 0; i < n; i++)
561         stream.Put(c);
562 }
563 
564 ///////////////////////////////////////////////////////////////////////////////
565 // StringStream
566 
567 //! Read-only string stream.
568 /*! \note implements Stream concept
569 */
570 template <typename Encoding>
571 struct GenericStringStream {
572     typedef typename Encoding::Ch Ch;
573 
574     GenericStringStream(const Ch *src) : src_(src), head_(src) {}
575 
576     Ch Peek() const { return *src_; }
577     Ch Take() { return *src_++; }
578     size_t Tell() const { return static_cast<size_t>(src_ - head_); }
579 
580     Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
581     void Put(Ch) { RAPIDJSON_ASSERT(false); }
582     void Flush() { RAPIDJSON_ASSERT(false); }
583     size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
584 
585     const Ch* src_;     //!< Current read position.
586     const Ch* head_;    //!< Original head of the string.
587 };
588 
589 template <typename Encoding>
590 struct StreamTraits<GenericStringStream<Encoding> > {
591     enum { copyOptimization = 1 };
592 };
593 
594 //! String stream with UTF8 encoding.
595 typedef GenericStringStream<UTF8<> > StringStream;
596 
597 ///////////////////////////////////////////////////////////////////////////////
598 // InsituStringStream
599 
600 //! A read-write string stream.
601 /*! This string stream is particularly designed for in-situ parsing.
602     \note implements Stream concept
603 */
604 template <typename Encoding>
605 struct GenericInsituStringStream {
606     typedef typename Encoding::Ch Ch;
607 
608     GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {}
609 
610     // Read
611     Ch Peek() { return *src_; }
612     Ch Take() { return *src_++; }
613     size_t Tell() { return static_cast<size_t>(src_ - head_); }
614 
615     // Write
616     void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; }
617 
618     Ch* PutBegin() { return dst_ = src_; }
619     size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); }
620     void Flush() {}
621 
622     Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; }
623     void Pop(size_t count) { dst_ -= count; }
624 
625     Ch* src_;
626     Ch* dst_;
627     Ch* head_;
628 };
629 
630 template <typename Encoding>
631 struct StreamTraits<GenericInsituStringStream<Encoding> > {
632     enum { copyOptimization = 1 };
633 };
634 
635 //! Insitu string stream with UTF8 encoding.
636 typedef GenericInsituStringStream<UTF8<> > InsituStringStream;
637 
638 ///////////////////////////////////////////////////////////////////////////////
639 // Type
640 
641 //! Type of JSON value
642 enum Type {
643     kNullType = 0,      //!< null
644     kFalseType = 1,     //!< false
645     kTrueType = 2,      //!< true
646     kObjectType = 3,    //!< object
647     kArrayType = 4,     //!< array
648     kStringType = 5,    //!< string
649     kNumberType = 6     //!< number
650 };
651 
652 RAPIDJSON_NAMESPACE_END
653 
654 #endif // RAPIDJSON_RAPIDJSON_H_
655