rapidjson.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 1
  61. #define RAPIDJSON_PATCH_VERSION 0
  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_HAS_STDSTRING
  107. #ifndef RAPIDJSON_HAS_STDSTRING
  108. #ifdef RAPIDJSON_DOXYGEN_RUNNING
  109. #define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation
  110. #else
  111. #define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default
  112. #endif
  113. /*! \def RAPIDJSON_HAS_STDSTRING
  114. \ingroup RAPIDJSON_CONFIG
  115. \brief Enable RapidJSON support for \c std::string
  116. By defining this preprocessor symbol to \c 1, several convenience functions for using
  117. \ref rapidjson::GenericValue with \c std::string are enabled, especially
  118. for construction and comparison.
  119. \hideinitializer
  120. */
  121. #endif // !defined(RAPIDJSON_HAS_STDSTRING)
  122. #if RAPIDJSON_HAS_STDSTRING
  123. #include <string>
  124. #endif // RAPIDJSON_HAS_STDSTRING
  125. ///////////////////////////////////////////////////////////////////////////////
  126. // RAPIDJSON_NO_INT64DEFINE
  127. /*! \def RAPIDJSON_NO_INT64DEFINE
  128. \ingroup RAPIDJSON_CONFIG
  129. \brief Use external 64-bit integer types.
  130. RapidJSON requires the 64-bit integer types \c int64_t and \c uint64_t types
  131. to be available at global scope.
  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. #if defined(_MSC_VER) && (_MSC_VER < 1800) // Visual Studio 2013
  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. // RAPIDJSON_FORCEINLINE
  152. #ifndef RAPIDJSON_FORCEINLINE
  153. //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
  154. #if defined(_MSC_VER) && defined(NDEBUG)
  155. #define RAPIDJSON_FORCEINLINE __forceinline
  156. #elif defined(__GNUC__) && __GNUC__ >= 4 && defined(NDEBUG)
  157. #define RAPIDJSON_FORCEINLINE __attribute__((always_inline))
  158. #else
  159. #define RAPIDJSON_FORCEINLINE
  160. #endif
  161. //!@endcond
  162. #endif // RAPIDJSON_FORCEINLINE
  163. ///////////////////////////////////////////////////////////////////////////////
  164. // RAPIDJSON_ENDIAN
  165. #define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine
  166. #define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine
  167. //! Endianness of the machine.
  168. /*!
  169. \def RAPIDJSON_ENDIAN
  170. \ingroup RAPIDJSON_CONFIG
  171. GCC 4.6 provided macro for detecting endianness of the target machine. But other
  172. compilers may not have this. User can define RAPIDJSON_ENDIAN to either
  173. \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN.
  174. Default detection implemented with reference to
  175. \li https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html
  176. \li http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp
  177. */
  178. #ifndef RAPIDJSON_ENDIAN
  179. // Detect with GCC 4.6's macro
  180. # ifdef __BYTE_ORDER__
  181. # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  182. # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
  183. # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  184. # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
  185. # else
  186. # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
  187. # endif // __BYTE_ORDER__
  188. // Detect with GLIBC's endian.h
  189. # elif defined(__GLIBC__)
  190. # include <endian.h>
  191. # if (__BYTE_ORDER == __LITTLE_ENDIAN)
  192. # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
  193. # elif (__BYTE_ORDER == __BIG_ENDIAN)
  194. # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
  195. # else
  196. # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
  197. # endif // __GLIBC__
  198. // Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro
  199. # elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
  200. # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
  201. # elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
  202. # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
  203. // Detect with architecture macros
  204. # elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
  205. # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
  206. # 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__)
  207. # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
  208. # elif defined(_MSC_VER) && defined(_M_ARM)
  209. # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
  210. # elif defined(RAPIDJSON_DOXYGEN_RUNNING)
  211. # define RAPIDJSON_ENDIAN
  212. # else
  213. # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
  214. # endif
  215. #endif // RAPIDJSON_ENDIAN
  216. ///////////////////////////////////////////////////////////////////////////////
  217. // RAPIDJSON_64BIT
  218. //! Whether using 64-bit architecture
  219. #ifndef RAPIDJSON_64BIT
  220. #if defined(__LP64__) || (defined(__x86_64__) && defined(__ILP32__)) || defined(_WIN64) || defined(__EMSCRIPTEN__)
  221. #define RAPIDJSON_64BIT 1
  222. #else
  223. #define RAPIDJSON_64BIT 0
  224. #endif
  225. #endif // RAPIDJSON_64BIT
  226. ///////////////////////////////////////////////////////////////////////////////
  227. // RAPIDJSON_ALIGN
  228. //! Data alignment of the machine.
  229. /*! \ingroup RAPIDJSON_CONFIG
  230. \param x pointer to align
  231. Some machines require strict data alignment. Currently the default uses 4 bytes
  232. alignment on 32-bit platforms and 8 bytes alignment for 64-bit platforms.
  233. User can customize by defining the RAPIDJSON_ALIGN function macro.
  234. */
  235. #ifndef RAPIDJSON_ALIGN
  236. #if RAPIDJSON_64BIT == 1
  237. #define RAPIDJSON_ALIGN(x) (((x) + static_cast<uint64_t>(7u)) & ~static_cast<uint64_t>(7u))
  238. #else
  239. #define RAPIDJSON_ALIGN(x) (((x) + 3u) & ~3u)
  240. #endif
  241. #endif
  242. ///////////////////////////////////////////////////////////////////////////////
  243. // RAPIDJSON_UINT64_C2
  244. //! Construct a 64-bit literal by a pair of 32-bit integer.
  245. /*!
  246. 64-bit literal with or without ULL suffix is prone to compiler warnings.
  247. UINT64_C() is C macro which cause compilation problems.
  248. Use this macro to define 64-bit constants by a pair of 32-bit integer.
  249. */
  250. #ifndef RAPIDJSON_UINT64_C2
  251. #define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast<uint64_t>(high32) << 32) | static_cast<uint64_t>(low32))
  252. #endif
  253. ///////////////////////////////////////////////////////////////////////////////
  254. // RAPIDJSON_48BITPOINTER_OPTIMIZATION
  255. //! Use only lower 48-bit address for some pointers.
  256. /*!
  257. \ingroup RAPIDJSON_CONFIG
  258. This optimization uses the fact that current X86-64 architecture only implement lower 48-bit virtual address.
  259. The higher 16-bit can be used for storing other data.
  260. \c GenericValue uses this optimization to reduce its size form 24 bytes to 16 bytes in 64-bit architecture.
  261. */
  262. #ifndef RAPIDJSON_48BITPOINTER_OPTIMIZATION
  263. #if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
  264. #define RAPIDJSON_48BITPOINTER_OPTIMIZATION 1
  265. #else
  266. #define RAPIDJSON_48BITPOINTER_OPTIMIZATION 0
  267. #endif
  268. #endif // RAPIDJSON_48BITPOINTER_OPTIMIZATION
  269. #if RAPIDJSON_48BITPOINTER_OPTIMIZATION == 1
  270. #if RAPIDJSON_64BIT != 1
  271. #error RAPIDJSON_48BITPOINTER_OPTIMIZATION can only be set to 1 when RAPIDJSON_64BIT=1
  272. #endif
  273. #define RAPIDJSON_SETPOINTER(type, p, x) (p = reinterpret_cast<type *>((reinterpret_cast<uintptr_t>(p) & static_cast<uintptr_t>(RAPIDJSON_UINT64_C2(0xFFFF0000, 0x00000000))) | reinterpret_cast<uintptr_t>(reinterpret_cast<const void*>(x))))
  274. #define RAPIDJSON_GETPOINTER(type, p) (reinterpret_cast<type *>(reinterpret_cast<uintptr_t>(p) & static_cast<uintptr_t>(RAPIDJSON_UINT64_C2(0x0000FFFF, 0xFFFFFFFF))))
  275. #else
  276. #define RAPIDJSON_SETPOINTER(type, p, x) (p = (x))
  277. #define RAPIDJSON_GETPOINTER(type, p) (p)
  278. #endif
  279. ///////////////////////////////////////////////////////////////////////////////
  280. // RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD
  281. /*! \def RAPIDJSON_SIMD
  282. \ingroup RAPIDJSON_CONFIG
  283. \brief Enable SSE2/SSE4.2 optimization.
  284. RapidJSON supports optimized implementations for some parsing operations
  285. based on the SSE2 or SSE4.2 SIMD extensions on modern Intel-compatible
  286. processors.
  287. To enable these optimizations, two different symbols can be defined;
  288. \code
  289. // Enable SSE2 optimization.
  290. #define RAPIDJSON_SSE2
  291. // Enable SSE4.2 optimization.
  292. #define RAPIDJSON_SSE42
  293. \endcode
  294. \c RAPIDJSON_SSE42 takes precedence, if both are defined.
  295. If any of these symbols is defined, RapidJSON defines the macro
  296. \c RAPIDJSON_SIMD to indicate the availability of the optimized code.
  297. */
  298. #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \
  299. || defined(RAPIDJSON_DOXYGEN_RUNNING)
  300. #define RAPIDJSON_SIMD
  301. #endif
  302. ///////////////////////////////////////////////////////////////////////////////
  303. // RAPIDJSON_NO_SIZETYPEDEFINE
  304. #ifndef RAPIDJSON_NO_SIZETYPEDEFINE
  305. /*! \def RAPIDJSON_NO_SIZETYPEDEFINE
  306. \ingroup RAPIDJSON_CONFIG
  307. \brief User-provided \c SizeType definition.
  308. In order to avoid using 32-bit size types for indexing strings and arrays,
  309. define this preprocessor symbol and provide the type rapidjson::SizeType
  310. before including RapidJSON:
  311. \code
  312. #define RAPIDJSON_NO_SIZETYPEDEFINE
  313. namespace rapidjson { typedef ::std::size_t SizeType; }
  314. #include "rapidjson/..."
  315. \endcode
  316. \see rapidjson::SizeType
  317. */
  318. #ifdef RAPIDJSON_DOXYGEN_RUNNING
  319. #define RAPIDJSON_NO_SIZETYPEDEFINE
  320. #endif
  321. RAPIDJSON_NAMESPACE_BEGIN
  322. //! Size type (for string lengths, array sizes, etc.)
  323. /*! RapidJSON uses 32-bit array/string indices even on 64-bit platforms,
  324. instead of using \c size_t. Users may override the SizeType by defining
  325. \ref RAPIDJSON_NO_SIZETYPEDEFINE.
  326. */
  327. typedef unsigned SizeType;
  328. RAPIDJSON_NAMESPACE_END
  329. #endif
  330. // always import std::size_t to rapidjson namespace
  331. RAPIDJSON_NAMESPACE_BEGIN
  332. using std::size_t;
  333. RAPIDJSON_NAMESPACE_END
  334. ///////////////////////////////////////////////////////////////////////////////
  335. // RAPIDJSON_ASSERT
  336. //! Assertion.
  337. /*! \ingroup RAPIDJSON_CONFIG
  338. By default, rapidjson uses C \c assert() for internal assertions.
  339. User can override it by defining RAPIDJSON_ASSERT(x) macro.
  340. \note Parsing errors are handled and can be customized by the
  341. \ref RAPIDJSON_ERRORS APIs.
  342. */
  343. #ifndef RAPIDJSON_ASSERT
  344. #include <cassert>
  345. #define RAPIDJSON_ASSERT(x) assert(x)
  346. #endif // RAPIDJSON_ASSERT
  347. ///////////////////////////////////////////////////////////////////////////////
  348. // RAPIDJSON_STATIC_ASSERT
  349. // Adopt from boost
  350. #ifndef RAPIDJSON_STATIC_ASSERT
  351. #ifndef __clang__
  352. //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
  353. #endif
  354. RAPIDJSON_NAMESPACE_BEGIN
  355. template <bool x> struct STATIC_ASSERTION_FAILURE;
  356. template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
  357. template<int x> struct StaticAssertTest {};
  358. RAPIDJSON_NAMESPACE_END
  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. #if defined(__GNUC__)
  363. #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
  364. #else
  365. #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
  366. #endif
  367. #ifndef __clang__
  368. //!@endcond
  369. #endif
  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. // RAPIDJSON_LIKELY, RAPIDJSON_UNLIKELY
  382. //! Compiler branching hint for expression with high probability to be true.
  383. /*!
  384. \ingroup RAPIDJSON_CONFIG
  385. \param x Boolean expression likely to be true.
  386. */
  387. #ifndef RAPIDJSON_LIKELY
  388. #if defined(__GNUC__) || defined(__clang__)
  389. #define RAPIDJSON_LIKELY(x) __builtin_expect(!!(x), 1)
  390. #else
  391. #define RAPIDJSON_LIKELY(x) (x)
  392. #endif
  393. #endif
  394. //! Compiler branching hint for expression with low probability to be true.
  395. /*!
  396. \ingroup RAPIDJSON_CONFIG
  397. \param x Boolean expression unlikely to be true.
  398. */
  399. #ifndef RAPIDJSON_UNLIKELY
  400. #if defined(__GNUC__) || defined(__clang__)
  401. #define RAPIDJSON_UNLIKELY(x) __builtin_expect(!!(x), 0)
  402. #else
  403. #define RAPIDJSON_UNLIKELY(x) (x)
  404. #endif
  405. #endif
  406. ///////////////////////////////////////////////////////////////////////////////
  407. // Helpers
  408. //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
  409. #define RAPIDJSON_MULTILINEMACRO_BEGIN do {
  410. #define RAPIDJSON_MULTILINEMACRO_END \
  411. } while((void)0, 0)
  412. // adopted from Boost
  413. #define RAPIDJSON_VERSION_CODE(x,y,z) \
  414. (((x)*100000) + ((y)*100) + (z))
  415. ///////////////////////////////////////////////////////////////////////////////
  416. // RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF
  417. #if defined(__GNUC__)
  418. #define RAPIDJSON_GNUC \
  419. RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__)
  420. #endif
  421. #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,2,0))
  422. #define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x))
  423. #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x)
  424. #define RAPIDJSON_DIAG_OFF(x) \
  425. RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x)))
  426. // push/pop support in Clang and GCC>=4.6
  427. #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0))
  428. #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
  429. #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
  430. #else // GCC >= 4.2, < 4.6
  431. #define RAPIDJSON_DIAG_PUSH /* ignored */
  432. #define RAPIDJSON_DIAG_POP /* ignored */
  433. #endif
  434. #elif defined(_MSC_VER)
  435. // pragma (MSVC specific)
  436. #define RAPIDJSON_PRAGMA(x) __pragma(x)
  437. #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x))
  438. #define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x)
  439. #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
  440. #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
  441. #else
  442. #define RAPIDJSON_DIAG_OFF(x) /* ignored */
  443. #define RAPIDJSON_DIAG_PUSH /* ignored */
  444. #define RAPIDJSON_DIAG_POP /* ignored */
  445. #endif // RAPIDJSON_DIAG_*
  446. ///////////////////////////////////////////////////////////////////////////////
  447. // C++11 features
  448. #ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS
  449. #if defined(__clang__)
  450. #if __has_feature(cxx_rvalue_references) && \
  451. (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306)
  452. #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
  453. #else
  454. #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0
  455. #endif
  456. #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
  457. (defined(_MSC_VER) && _MSC_VER >= 1600)
  458. #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
  459. #else
  460. #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0
  461. #endif
  462. #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
  463. #ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT
  464. #if defined(__clang__)
  465. #define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept)
  466. #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__))
  467. // (defined(_MSC_VER) && _MSC_VER >= ????) // not yet supported
  468. #define RAPIDJSON_HAS_CXX11_NOEXCEPT 1
  469. #else
  470. #define RAPIDJSON_HAS_CXX11_NOEXCEPT 0
  471. #endif
  472. #endif
  473. #if RAPIDJSON_HAS_CXX11_NOEXCEPT
  474. #define RAPIDJSON_NOEXCEPT noexcept
  475. #else
  476. #define RAPIDJSON_NOEXCEPT /* noexcept */
  477. #endif // RAPIDJSON_HAS_CXX11_NOEXCEPT
  478. // no automatic detection, yet
  479. #ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS
  480. #define RAPIDJSON_HAS_CXX11_TYPETRAITS 0
  481. #endif
  482. #ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR
  483. #if defined(__clang__)
  484. #define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for)
  485. #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
  486. (defined(_MSC_VER) && _MSC_VER >= 1700)
  487. #define RAPIDJSON_HAS_CXX11_RANGE_FOR 1
  488. #else
  489. #define RAPIDJSON_HAS_CXX11_RANGE_FOR 0
  490. #endif
  491. #endif // RAPIDJSON_HAS_CXX11_RANGE_FOR
  492. //!@endcond
  493. ///////////////////////////////////////////////////////////////////////////////
  494. // new/delete
  495. #ifndef RAPIDJSON_NEW
  496. ///! customization point for global \c new
  497. #define RAPIDJSON_NEW(x) new x
  498. #endif
  499. #ifndef RAPIDJSON_DELETE
  500. ///! customization point for global \c delete
  501. #define RAPIDJSON_DELETE(x) delete x
  502. #endif
  503. ///////////////////////////////////////////////////////////////////////////////
  504. // Type
  505. /*! \namespace rapidjson
  506. \brief main RapidJSON namespace
  507. \see RAPIDJSON_NAMESPACE
  508. */
  509. RAPIDJSON_NAMESPACE_BEGIN
  510. //! Type of JSON value
  511. enum Type {
  512. kNullType = 0, //!< null
  513. kFalseType = 1, //!< false
  514. kTrueType = 2, //!< true
  515. kObjectType = 3, //!< object
  516. kArrayType = 4, //!< array
  517. kStringType = 5, //!< string
  518. kNumberType = 6 //!< number
  519. };
  520. RAPIDJSON_NAMESPACE_END
  521. #endif // RAPIDJSON_RAPIDJSON_H_