rapidjson.h 21 KB

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