2014-08-11 22:26:45 +08:00
|
|
|
// Copyright (C) 2011 Milo Yip
|
|
|
|
//
|
|
|
|
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
|
|
// of this software and associated documentation files (the "Software"), to deal
|
|
|
|
// in the Software without restriction, including without limitation the rights
|
|
|
|
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
|
|
// copies of the Software, and to permit persons to whom the Software is
|
|
|
|
// furnished to do so, subject to the following conditions:
|
|
|
|
//
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
// all copies or substantial portions of the Software.
|
|
|
|
//
|
|
|
|
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
|
|
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
|
|
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
|
|
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
|
|
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
|
|
// THE SOFTWARE.
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
#ifndef RAPIDJSON_RAPIDJSON_H_
|
|
|
|
#define RAPIDJSON_RAPIDJSON_H_
|
|
|
|
|
|
|
|
// Copyright (c) 2011 Milo Yip (miloyip@gmail.com)
|
|
|
|
// Version 0.1
|
|
|
|
|
2014-07-05 17:09:35 +02:00
|
|
|
/*!\file rapidjson.h
|
2014-08-11 22:26:45 +08:00
|
|
|
\brief common definitions and configuration
|
2014-07-05 17:09:35 +02:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
\todo Complete Doxygen documentation for configure macros.
|
2014-07-05 17:09:35 +02:00
|
|
|
*/
|
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
#include <cstdlib> // malloc(), realloc(), free()
|
|
|
|
#include <cstring> // memcpy()
|
2011-11-18 17:01:23 +00:00
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RAPIDJSON_NO_INT64DEFINE
|
|
|
|
|
2014-08-08 21:34:43 +08:00
|
|
|
// Here defines int64_t and uint64_t types in global namespace
|
2011-11-18 17:01:23 +00:00
|
|
|
// If user have their own definition, can define RAPIDJSON_NO_INT64DEFINE to disable this.
|
|
|
|
#ifndef RAPIDJSON_NO_INT64DEFINE
|
2014-07-05 17:09:35 +02:00
|
|
|
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
|
2011-11-18 17:01:23 +00:00
|
|
|
#ifdef _MSC_VER
|
2014-06-26 16:06:15 +02:00
|
|
|
#include "msinttypes/stdint.h"
|
2014-06-25 23:40:12 +08:00
|
|
|
#include "msinttypes/inttypes.h"
|
2011-11-18 17:01:23 +00:00
|
|
|
#else
|
2014-06-25 22:38:18 +08:00
|
|
|
// Other compilers should have this.
|
2014-06-26 16:06:15 +02:00
|
|
|
#include <stdint.h>
|
2011-11-18 17:01:23 +00:00
|
|
|
#include <inttypes.h>
|
|
|
|
#endif
|
2014-07-05 17:09:35 +02:00
|
|
|
//!@endcond
|
2011-11-18 17:01:23 +00:00
|
|
|
#endif // RAPIDJSON_NO_INT64TYPEDEF
|
|
|
|
|
2014-06-25 22:30:21 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RAPIDJSON_FORCEINLINE
|
|
|
|
|
|
|
|
#ifndef RAPIDJSON_FORCEINLINE
|
|
|
|
#ifdef _MSC_VER
|
|
|
|
#define RAPIDJSON_FORCEINLINE __forceinline
|
2014-07-26 23:23:52 +08:00
|
|
|
#elif defined(__GNUC__) && __GNUC__ >= 4
|
|
|
|
#define RAPIDJSON_FORCEINLINE __attribute__((always_inline))
|
2014-06-25 22:30:21 +08:00
|
|
|
#else
|
|
|
|
#define RAPIDJSON_FORCEINLINE
|
|
|
|
#endif
|
|
|
|
#endif // RAPIDJSON_FORCEINLINE
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RAPIDJSON_ENDIAN
|
2014-08-11 22:26:45 +08:00
|
|
|
#define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine
|
|
|
|
#define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine
|
2011-11-18 17:01:23 +00:00
|
|
|
|
|
|
|
//! Endianness of the machine.
|
2014-08-11 22:26:45 +08:00
|
|
|
/*! GCC 4.6 provided macro for detecting endianness of the target machine. But other
|
|
|
|
compilers may not have this. User can define RAPIDJSON_ENDIAN to either
|
|
|
|
\ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN.
|
2014-07-14 13:54:15 +08:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
Implemented with reference to
|
|
|
|
https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html
|
|
|
|
http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp
|
2011-11-18 17:01:23 +00:00
|
|
|
*/
|
|
|
|
#ifndef RAPIDJSON_ENDIAN
|
2014-07-14 14:16:34 +08:00
|
|
|
// Detect with GCC 4.6's macro
|
2014-07-14 13:54:15 +08:00
|
|
|
# ifdef __BYTE_ORDER__
|
|
|
|
# if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
|
|
|
|
# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
|
|
|
|
# elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
|
|
|
|
# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
|
|
|
|
# else
|
|
|
|
# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
|
2014-08-11 22:26:45 +08:00
|
|
|
# endif // __BYTE_ORDER__
|
2014-07-14 14:16:34 +08:00
|
|
|
// Detect with GLIBC's endian.h
|
|
|
|
# elif defined(__GLIBC__)
|
|
|
|
# include <endian.h>
|
|
|
|
# if (__BYTE_ORDER == __LITTLE_ENDIAN)
|
|
|
|
# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
|
2014-08-11 22:26:45 +08:00
|
|
|
# elif (__BYTE_ORDER == __BIG_ENDIAN)
|
2014-07-14 14:16:34 +08:00
|
|
|
# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
|
2014-08-11 22:26:45 +08:00
|
|
|
# else
|
2014-07-14 14:16:34 +08:00
|
|
|
# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
|
|
|
|
# endif // __GLIBC__
|
|
|
|
// Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro
|
2014-07-14 13:54:15 +08:00
|
|
|
# elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
|
2014-08-11 22:26:45 +08:00
|
|
|
# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
|
2014-07-14 13:54:15 +08:00
|
|
|
# elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
|
2014-08-11 22:26:45 +08:00
|
|
|
# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
|
2014-07-14 14:16:34 +08:00
|
|
|
// Detect with architecture macros
|
2014-07-14 13:54:15 +08:00
|
|
|
# elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
|
2014-08-11 22:26:45 +08:00
|
|
|
# define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
|
2014-07-14 13:54:15 +08:00
|
|
|
# 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__)
|
2014-08-11 22:26:45 +08:00
|
|
|
# define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
|
2014-07-14 13:54:15 +08:00
|
|
|
# else
|
2014-08-11 22:26:45 +08:00
|
|
|
# error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
|
2014-07-14 13:54:15 +08:00
|
|
|
# endif
|
2011-11-18 17:01:23 +00:00
|
|
|
#endif // RAPIDJSON_ENDIAN
|
|
|
|
|
2012-02-19 15:42:58 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RAPIDJSON_ALIGNSIZE
|
|
|
|
|
|
|
|
//! Data alignment of the machine.
|
|
|
|
/*!
|
2014-08-11 22:26:45 +08:00
|
|
|
Some machine requires strict data alignment.
|
|
|
|
Currently the default uses 4 bytes alignment. User can customize this.
|
2012-02-19 15:42:58 +00:00
|
|
|
*/
|
|
|
|
#ifndef RAPIDJSON_ALIGN
|
2014-06-25 19:21:17 +08:00
|
|
|
#define RAPIDJSON_ALIGN(x) ((x + 3u) & ~3u)
|
2012-02-19 15:42:58 +00:00
|
|
|
#endif
|
|
|
|
|
2014-08-08 21:34:43 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RAPIDJSON_UINT64_C2
|
|
|
|
|
|
|
|
//! Construct a 64-bit literal by a pair of 32-bit integer.
|
|
|
|
/*!
|
2014-08-11 22:26:45 +08:00
|
|
|
64-bit literal with or without ULL suffix is prone to compiler warnings.
|
|
|
|
UINT64_C() is C macro which cause compilation problems.
|
|
|
|
Use this macro to define 64-bit constants by a pair of 32-bit integer.
|
2014-08-08 21:34:43 +08:00
|
|
|
*/
|
|
|
|
#ifndef RAPIDJSON_UINT64_C2
|
|
|
|
#define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast<uint64_t>(high32) << 32) | static_cast<uint64_t>(low32))
|
|
|
|
#endif
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD
|
|
|
|
|
|
|
|
// Enable SSE2 optimization.
|
|
|
|
//#define RAPIDJSON_SSE2
|
|
|
|
|
|
|
|
// Enable SSE4.2 optimization.
|
|
|
|
//#define RAPIDJSON_SSE42
|
|
|
|
|
|
|
|
#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42)
|
|
|
|
#define RAPIDJSON_SIMD
|
|
|
|
#endif
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RAPIDJSON_NO_SIZETYPEDEFINE
|
|
|
|
|
|
|
|
#ifndef RAPIDJSON_NO_SIZETYPEDEFINE
|
|
|
|
namespace rapidjson {
|
|
|
|
//! Use 32-bit array/string indices even for 64-bit platform, instead of using size_t.
|
|
|
|
/*! User may override the SizeType by defining RAPIDJSON_NO_SIZETYPEDEFINE.
|
|
|
|
*/
|
|
|
|
typedef unsigned SizeType;
|
|
|
|
} // namespace rapidjson
|
|
|
|
#endif
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RAPIDJSON_ASSERT
|
|
|
|
|
|
|
|
//! Assertion.
|
|
|
|
/*! By default, rapidjson uses C assert() for assertion.
|
2014-08-11 22:26:45 +08:00
|
|
|
User can override it by defining RAPIDJSON_ASSERT(x) macro.
|
2011-11-18 17:01:23 +00:00
|
|
|
*/
|
|
|
|
#ifndef RAPIDJSON_ASSERT
|
|
|
|
#include <cassert>
|
|
|
|
#define RAPIDJSON_ASSERT(x) assert(x)
|
|
|
|
#endif // RAPIDJSON_ASSERT
|
|
|
|
|
2011-12-03 09:57:17 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RAPIDJSON_STATIC_ASSERT
|
|
|
|
|
|
|
|
// Adopt from boost
|
|
|
|
#ifndef RAPIDJSON_STATIC_ASSERT
|
2014-07-05 17:09:35 +02:00
|
|
|
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
|
2011-12-03 09:57:17 +00:00
|
|
|
namespace rapidjson {
|
2014-07-05 17:09:35 +02:00
|
|
|
|
2011-12-03 09:57:17 +00:00
|
|
|
template <bool x> struct STATIC_ASSERTION_FAILURE;
|
|
|
|
template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
|
|
|
|
template<int x> struct StaticAssertTest {};
|
|
|
|
} // namespace rapidjson
|
|
|
|
|
|
|
|
#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y)
|
|
|
|
#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y)
|
|
|
|
#define RAPIDJSON_DO_JOIN2(X, Y) X##Y
|
|
|
|
|
2014-06-25 00:04:24 +08:00
|
|
|
#if defined(__GNUC__)
|
|
|
|
#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
|
|
|
|
#else
|
|
|
|
#define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
|
|
|
|
#endif
|
2014-07-05 17:09:35 +02:00
|
|
|
//!@endcond
|
2014-06-25 00:04:24 +08:00
|
|
|
|
2014-07-05 17:09:35 +02:00
|
|
|
/*! \def RAPIDJSON_STATIC_ASSERT
|
2014-08-11 22:26:45 +08:00
|
|
|
\brief (internal) macro to check for conditions at compile-time
|
|
|
|
\param x compile-time condition
|
|
|
|
\hideinitializer
|
2014-07-05 17:09:35 +02:00
|
|
|
*/
|
2011-12-03 09:57:17 +00:00
|
|
|
#define RAPIDJSON_STATIC_ASSERT(x) typedef ::rapidjson::StaticAssertTest<\
|
2014-08-11 22:26:45 +08:00
|
|
|
sizeof(::rapidjson::STATIC_ASSERTION_FAILURE<bool(x) >)>\
|
|
|
|
RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
|
2011-12-03 09:57:17 +00:00
|
|
|
#endif
|
|
|
|
|
2012-11-13 08:02:22 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Helpers
|
|
|
|
|
2014-07-05 17:09:35 +02:00
|
|
|
//!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
|
2014-07-08 14:45:19 +02:00
|
|
|
|
2012-11-13 08:02:22 +00:00
|
|
|
#define RAPIDJSON_MULTILINEMACRO_BEGIN do {
|
|
|
|
#define RAPIDJSON_MULTILINEMACRO_END \
|
|
|
|
} while((void)0, 0)
|
2014-07-08 14:45:19 +02:00
|
|
|
|
|
|
|
// adopted from Boost
|
|
|
|
#define RAPIDJSON_VERSION_CODE(x,y,z) \
|
|
|
|
(((x)*100000) + ((y)*100) + (z))
|
|
|
|
|
|
|
|
// token stringification
|
|
|
|
#define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x)
|
|
|
|
#define RAPIDJSON_DO_STRINGIFY(x) #x
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF
|
|
|
|
|
|
|
|
#if defined(__clang__) || (defined(__GNUC__) && RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) >= RAPIDJSON_VERSION_CODE(4,2,0))
|
|
|
|
|
|
|
|
#define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x))
|
|
|
|
#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x)
|
|
|
|
#define RAPIDJSON_DIAG_OFF(x) \
|
2014-08-11 22:26:45 +08:00
|
|
|
RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x)))
|
2014-07-08 14:45:19 +02:00
|
|
|
|
|
|
|
// push/pop support in Clang and GCC>=4.6
|
|
|
|
#if defined(__clang__) || (defined(__GNUC__) && RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) >= RAPIDJSON_VERSION_CODE(4,6,0))
|
|
|
|
#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
|
|
|
|
#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
|
|
|
|
#else // GCC >= 4.2, < 4.6
|
|
|
|
#define RAPIDJSON_DIAG_PUSH /* ignored */
|
|
|
|
#define RAPIDJSON_DIAG_POP /* ignored */
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#elif defined(_MSC_VER)
|
|
|
|
|
|
|
|
// pragma (MSVC specific)
|
|
|
|
#define RAPIDJSON_PRAGMA(x) __pragma(x)
|
|
|
|
#define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x))
|
|
|
|
|
|
|
|
#define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x)
|
|
|
|
#define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
|
|
|
|
#define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
|
|
|
|
|
|
|
|
#else
|
|
|
|
|
|
|
|
#define RAPIDJSON_DIAG_OFF(x) /* ignored */
|
|
|
|
#define RAPIDJSON_DIAG_PUSH /* ignored */
|
|
|
|
#define RAPIDJSON_DIAG_POP /* ignored */
|
|
|
|
|
|
|
|
#endif // RAPIDJSON_DIAG_*
|
|
|
|
|
2014-07-05 17:09:35 +02:00
|
|
|
//!@endcond
|
2012-11-13 08:02:22 +00:00
|
|
|
|
2011-12-03 09:57:17 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Allocators and Encodings
|
|
|
|
|
2011-11-29 18:39:03 +00:00
|
|
|
#include "allocators.h"
|
|
|
|
#include "encodings.h"
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-07-05 17:09:35 +02:00
|
|
|
//! main RapidJSON namespace
|
2011-11-29 18:39:03 +00:00
|
|
|
namespace rapidjson {
|
2011-11-28 09:30:32 +00:00
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Stream
|
|
|
|
|
|
|
|
/*! \class rapidjson::Stream
|
2014-08-11 22:26:45 +08:00
|
|
|
\brief Concept for reading and writing characters.
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd().
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
For write-only stream, only need to implement Put() and Flush().
|
2011-11-18 17:01:23 +00:00
|
|
|
|
|
|
|
\code
|
|
|
|
concept Stream {
|
2014-08-11 22:26:45 +08:00
|
|
|
typename Ch; //!< Character type of the stream.
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
//! Read the current character from stream without moving the read cursor.
|
|
|
|
Ch Peek() const;
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
//! Read the current character from stream and moving the read cursor to next character.
|
|
|
|
Ch Take();
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
//! Get the current read cursor.
|
|
|
|
//! \return Number of characters read from start.
|
|
|
|
size_t Tell();
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
//! Begin writing operation at the current read pointer.
|
|
|
|
//! \return The begin writer pointer.
|
|
|
|
Ch* PutBegin();
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
//! Write a character.
|
|
|
|
void Put(Ch c);
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
//! Flush the buffer.
|
|
|
|
void Flush();
|
2011-11-22 05:10:46 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
//! End the writing operation.
|
|
|
|
//! \param begin The begin write pointer returned by PutBegin().
|
|
|
|
//! \return Number of characters written.
|
|
|
|
size_t PutEnd(Ch* begin);
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
\endcode
|
|
|
|
*/
|
|
|
|
|
2014-06-29 20:59:01 +08:00
|
|
|
//! Provides additional information for stream.
|
|
|
|
/*!
|
2014-08-11 22:26:45 +08:00
|
|
|
By using traits pattern, this type provides a default configuration for stream.
|
|
|
|
For custom stream, this type can be specialized for other configuration.
|
|
|
|
See TEST(Reader, CustomStringStream) in readertest.cpp for example.
|
2014-06-29 20:59:01 +08:00
|
|
|
*/
|
|
|
|
template<typename Stream>
|
|
|
|
struct StreamTraits {
|
2014-08-11 22:26:45 +08:00
|
|
|
//! Whether to make local copy of stream for optimization during parsing.
|
|
|
|
/*!
|
|
|
|
By default, for safety, streams do not use local copy optimization.
|
|
|
|
Stream that can be copied fast should specialize this, like StreamTraits<StringStream>.
|
|
|
|
*/
|
|
|
|
enum { copyOptimization = 0 };
|
2014-07-02 23:49:47 +08:00
|
|
|
};
|
|
|
|
|
2011-11-18 17:01:23 +00:00
|
|
|
//! Put N copies of a character to a stream.
|
|
|
|
template<typename Stream, typename Ch>
|
|
|
|
inline void PutN(Stream& stream, Ch c, size_t n) {
|
2014-08-11 22:26:45 +08:00
|
|
|
for (size_t i = 0; i < n; i++)
|
|
|
|
stream.Put(c);
|
2011-11-18 17:01:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// StringStream
|
|
|
|
|
|
|
|
//! Read-only string stream.
|
2014-06-25 19:21:17 +08:00
|
|
|
/*! \note implements Stream concept
|
2011-11-18 17:01:23 +00:00
|
|
|
*/
|
|
|
|
template <typename Encoding>
|
|
|
|
struct GenericStringStream {
|
2014-08-11 22:26:45 +08:00
|
|
|
typedef typename Encoding::Ch Ch;
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
GenericStringStream(const Ch *src) : src_(src), head_(src) {}
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
Ch Peek() const { return *src_; }
|
|
|
|
Ch Take() { return *src_++; }
|
|
|
|
size_t Tell() const { return static_cast<size_t>(src_ - head_); }
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
|
|
|
|
void Put(Ch) { RAPIDJSON_ASSERT(false); }
|
|
|
|
void Flush() { RAPIDJSON_ASSERT(false); }
|
|
|
|
size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
const Ch* src_; //!< Current read position.
|
|
|
|
const Ch* head_; //!< Original head of the string.
|
2011-11-18 17:01:23 +00:00
|
|
|
};
|
|
|
|
|
2014-06-29 20:59:01 +08:00
|
|
|
template <typename Encoding>
|
2014-06-29 21:18:31 +08:00
|
|
|
struct StreamTraits<GenericStringStream<Encoding> > {
|
2014-08-11 22:26:45 +08:00
|
|
|
enum { copyOptimization = 1 };
|
2014-06-29 20:59:01 +08:00
|
|
|
};
|
|
|
|
|
2014-07-05 17:05:48 +02:00
|
|
|
//! String stream with UTF8 encoding.
|
2011-11-18 17:01:23 +00:00
|
|
|
typedef GenericStringStream<UTF8<> > StringStream;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// InsituStringStream
|
|
|
|
|
|
|
|
//! A read-write string stream.
|
|
|
|
/*! This string stream is particularly designed for in-situ parsing.
|
2014-08-11 22:26:45 +08:00
|
|
|
\note implements Stream concept
|
2011-11-18 17:01:23 +00:00
|
|
|
*/
|
|
|
|
template <typename Encoding>
|
|
|
|
struct GenericInsituStringStream {
|
2014-08-11 22:26:45 +08:00
|
|
|
typedef typename Encoding::Ch Ch;
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {}
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
// Read
|
|
|
|
Ch Peek() { return *src_; }
|
|
|
|
Ch Take() { return *src_++; }
|
|
|
|
size_t Tell() { return static_cast<size_t>(src_ - head_); }
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
// Write
|
|
|
|
void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; }
|
2014-07-25 00:08:24 +08:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
Ch* PutBegin() { return dst_ = src_; }
|
|
|
|
size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); }
|
|
|
|
void Flush() {}
|
2014-07-25 00:08:24 +08:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; }
|
|
|
|
void Pop(size_t count) { dst_ -= count; }
|
2011-11-18 17:01:23 +00:00
|
|
|
|
2014-08-11 22:26:45 +08:00
|
|
|
Ch* src_;
|
|
|
|
Ch* dst_;
|
|
|
|
Ch* head_;
|
2011-11-18 17:01:23 +00:00
|
|
|
};
|
|
|
|
|
2014-06-29 20:59:01 +08:00
|
|
|
template <typename Encoding>
|
2014-06-29 21:18:31 +08:00
|
|
|
struct StreamTraits<GenericInsituStringStream<Encoding> > {
|
2014-08-11 22:26:45 +08:00
|
|
|
enum { copyOptimization = 1 };
|
2014-06-29 20:59:01 +08:00
|
|
|
};
|
|
|
|
|
2014-07-05 17:05:48 +02:00
|
|
|
//! Insitu string stream with UTF8 encoding.
|
2011-11-18 17:01:23 +00:00
|
|
|
typedef GenericInsituStringStream<UTF8<> > InsituStringStream;
|
|
|
|
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Type
|
|
|
|
|
|
|
|
//! Type of JSON value
|
|
|
|
enum Type {
|
2014-08-11 22:26:45 +08:00
|
|
|
kNullType = 0, //!< null
|
|
|
|
kFalseType = 1, //!< false
|
|
|
|
kTrueType = 2, //!< true
|
|
|
|
kObjectType = 3, //!< object
|
|
|
|
kArrayType = 4, //!< array
|
|
|
|
kStringType = 5, //!< string
|
|
|
|
kNumberType = 6 //!< number
|
2011-11-18 17:01:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace rapidjson
|
|
|
|
|
|
|
|
#endif // RAPIDJSON_RAPIDJSON_H_
|