rapidjson.h 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  1. // Tencent is pleased to support the open source community by making RapidJSON
  2. // available.
  3. //
  4. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
  5. // rights reserved.
  6. //
  7. // Licensed under the MIT License (the "License"); you may not use this file
  8. // except in compliance with the License. You may obtain a copy of the License
  9. // at
  10. //
  11. // http://opensource.org/licenses/MIT
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. // License for the specific language governing permissions and limitations under
  17. // the License.
  18. #ifndef RAPIDJSON_RAPIDJSON_H_
  19. #define RAPIDJSON_RAPIDJSON_H_
  20. /*!\file rapidjson.h
  21. \brief common definitions and configuration
  22. \see RAPIDJSON_CONFIG
  23. */
  24. /*! \defgroup RAPIDJSON_CONFIG RapidJSON configuration
  25. \brief Configuration macros for library features
  26. Some RapidJSON features are configurable to adapt the library to a wide
  27. variety of platforms, environments and usage scenarios. Most of the
  28. features can be configured in terms of overridden or predefined
  29. preprocessor macros at compile-time.
  30. Some additional customization is available in the \ref RAPIDJSON_ERRORS
  31. APIs.
  32. \note These macros should be given on the compiler command-line
  33. (where applicable) to avoid inconsistent values when compiling
  34. different translation units of a single application.
  35. */
  36. #include <cstdlib> // malloc(), realloc(), free(), size_t
  37. #include <cstring> // memset(), memcpy(), memmove(), memcmp()
  38. ///////////////////////////////////////////////////////////////////////////////
  39. // RAPIDJSON_VERSION_STRING
  40. //
  41. // ALWAYS synchronize the following 3 macros with corresponding variables in
  42. // /CMakeLists.txt.
  43. //
  44. //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
  45. // token stringification
  46. #define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x)
  47. #define RAPIDJSON_DO_STRINGIFY(x) #x
  48. // token concatenation
  49. #define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y)
  50. #define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y)
  51. #define RAPIDJSON_DO_JOIN2(X, Y) X##Y
  52. //!@endcond
  53. /*! \def RAPIDJSON_MAJOR_VERSION
  54. \ingroup RAPIDJSON_CONFIG
  55. \brief Major version of RapidJSON in integer.
  56. */
  57. /*! \def RAPIDJSON_MINOR_VERSION
  58. \ingroup RAPIDJSON_CONFIG
  59. \brief Minor version of RapidJSON in integer.
  60. */
  61. /*! \def RAPIDJSON_PATCH_VERSION
  62. \ingroup RAPIDJSON_CONFIG
  63. \brief Patch version of RapidJSON in integer.
  64. */
  65. /*! \def RAPIDJSON_VERSION_STRING
  66. \ingroup RAPIDJSON_CONFIG
  67. \brief Version of RapidJSON in "<major>.<minor>.<patch>" string format.
  68. */
  69. #define RAPIDJSON_MAJOR_VERSION 1
  70. #define RAPIDJSON_MINOR_VERSION 1
  71. #define RAPIDJSON_PATCH_VERSION 0
  72. #define RAPIDJSON_VERSION_STRING \
  73. RAPIDJSON_STRINGIFY( \
  74. RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION)
  75. ///////////////////////////////////////////////////////////////////////////////
  76. // RAPIDJSON_NAMESPACE_(BEGIN|END)
  77. /*! \def RAPIDJSON_NAMESPACE
  78. \ingroup RAPIDJSON_CONFIG
  79. \brief provide custom rapidjson namespace
  80. In order to avoid symbol clashes and/or "One Definition Rule" errors
  81. between multiple inclusions of (different versions of) RapidJSON in
  82. a single binary, users can customize the name of the main RapidJSON
  83. namespace.
  84. In case of a single nesting level, defining \c RAPIDJSON_NAMESPACE
  85. to a custom name (e.g. \c MyRapidJSON) is sufficient. If multiple
  86. levels are needed, both \ref RAPIDJSON_NAMESPACE_BEGIN and \ref
  87. RAPIDJSON_NAMESPACE_END need to be defined as well:
  88. \code
  89. // in some .cpp file
  90. #define RAPIDJSON_NAMESPACE my::rapidjson
  91. #define RAPIDJSON_NAMESPACE_BEGIN namespace my { namespace rapidjson {
  92. #define RAPIDJSON_NAMESPACE_END } }
  93. #include "rapidjson/..."
  94. \endcode
  95. \see rapidjson
  96. */
  97. /*! \def RAPIDJSON_NAMESPACE_BEGIN
  98. \ingroup RAPIDJSON_CONFIG
  99. \brief provide custom rapidjson namespace (opening expression)
  100. \see RAPIDJSON_NAMESPACE
  101. */
  102. /*! \def RAPIDJSON_NAMESPACE_END
  103. \ingroup RAPIDJSON_CONFIG
  104. \brief provide custom rapidjson namespace (closing expression)
  105. \see RAPIDJSON_NAMESPACE
  106. */
  107. #ifndef RAPIDJSON_NAMESPACE
  108. #define RAPIDJSON_NAMESPACE rapidjson
  109. #endif
  110. #ifndef RAPIDJSON_NAMESPACE_BEGIN
  111. #define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE {
  112. #endif
  113. #ifndef RAPIDJSON_NAMESPACE_END
  114. #define RAPIDJSON_NAMESPACE_END }
  115. #endif
  116. ///////////////////////////////////////////////////////////////////////////////
  117. // RAPIDJSON_HAS_STDSTRING
  118. #ifndef RAPIDJSON_HAS_STDSTRING
  119. #ifdef RAPIDJSON_DOXYGEN_RUNNING
  120. #define RAPIDJSON_HAS_STDSTRING 1 // force generation of documentation
  121. #else
  122. #define RAPIDJSON_HAS_STDSTRING 0 // no std::string support by default
  123. #endif
  124. /*! \def RAPIDJSON_HAS_STDSTRING
  125. \ingroup RAPIDJSON_CONFIG
  126. \brief Enable RapidJSON support for \c std::string
  127. By defining this preprocessor symbol to \c 1, several convenience functions
  128. for using \ref rapidjson::GenericValue with \c std::string are enabled,
  129. especially for construction and comparison.
  130. \hideinitializer
  131. */
  132. #endif // !defined(RAPIDJSON_HAS_STDSTRING)
  133. #if RAPIDJSON_HAS_STDSTRING
  134. #include <string>
  135. #endif // RAPIDJSON_HAS_STDSTRING
  136. ///////////////////////////////////////////////////////////////////////////////
  137. // RAPIDJSON_NO_INT64DEFINE
  138. /*! \def RAPIDJSON_NO_INT64DEFINE
  139. \ingroup RAPIDJSON_CONFIG
  140. \brief Use external 64-bit integer types.
  141. RapidJSON requires the 64-bit integer types \c int64_t and \c uint64_t
  142. types to be available at global scope.
  143. If users have their own definition, define RAPIDJSON_NO_INT64DEFINE to
  144. prevent RapidJSON from defining its own types.
  145. */
  146. #ifndef RAPIDJSON_NO_INT64DEFINE
  147. //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
  148. #if defined(_MSC_VER) && (_MSC_VER < 1800) // Visual Studio 2013
  149. #include "msinttypes/inttypes.h"
  150. #include "msinttypes/stdint.h"
  151. #else
  152. // Other compilers should have this.
  153. #include <inttypes.h>
  154. #include <stdint.h>
  155. #endif
  156. //!@endcond
  157. #ifdef RAPIDJSON_DOXYGEN_RUNNING
  158. #define RAPIDJSON_NO_INT64DEFINE
  159. #endif
  160. #endif // RAPIDJSON_NO_INT64TYPEDEF
  161. ///////////////////////////////////////////////////////////////////////////////
  162. // RAPIDJSON_FORCEINLINE
  163. #ifndef RAPIDJSON_FORCEINLINE
  164. //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
  165. #if defined(_MSC_VER) && defined(NDEBUG)
  166. #define RAPIDJSON_FORCEINLINE __forceinline
  167. #elif defined(__GNUC__) && __GNUC__ >= 4 && defined(NDEBUG)
  168. #define RAPIDJSON_FORCEINLINE __attribute__((always_inline))
  169. #else
  170. #define RAPIDJSON_FORCEINLINE
  171. #endif
  172. //!@endcond
  173. #endif // RAPIDJSON_FORCEINLINE
  174. ///////////////////////////////////////////////////////////////////////////////
  175. // RAPIDJSON_ENDIAN
  176. #define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine
  177. #define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine
  178. //! Endianness of the machine.
  179. /*!
  180. \def RAPIDJSON_ENDIAN
  181. \ingroup RAPIDJSON_CONFIG
  182. GCC 4.6 provided macro for detecting endianness of the target machine. But
  183. other compilers may not have this. User can define RAPIDJSON_ENDIAN to either
  184. \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN.
  185. Default detection implemented with reference to
  186. \li
  187. https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html
  188. \li http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp
  189. */
  190. #ifndef RAPIDJSON_ENDIAN
  191. // Detect with GCC 4.6's macro
  192. #ifdef __BYTE_ORDER__
  193. #if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
  194. #define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
  195. #elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
  196. #define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
  197. #else
  198. # error Unknown machine endianness detected. User needs to define RAPIDJSON_ENDIAN.
  199. #endif // __BYTE_ORDER__
  200. // Detect with GLIBC's endian.h
  201. #elif defined(__GLIBC__)
  202. #include <endian.h>
  203. #if (__BYTE_ORDER == __LITTLE_ENDIAN)
  204. #define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
  205. #elif (__BYTE_ORDER == __BIG_ENDIAN)
  206. #define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
  207. #else
  208. # error Unknown machine endianness detected. User needs to define RAPIDJSON_ENDIAN.
  209. #endif // __GLIBC__
  210. // Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro
  211. #elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
  212. #define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
  213. #elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
  214. #define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
  215. // Detect with architecture macros
  216. #elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || \
  217. defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || \
  218. defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || \
  219. defined(__s390__)
  220. #define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
  221. #elif defined(__i386__) || defined(__alpha__) || defined(__ia64) || \
  222. defined(__ia64__) || defined(_M_IX86) || defined(_M_IA64) || \
  223. defined(_M_ALPHA) || defined(__amd64) || defined(__amd64__) || \
  224. defined(_M_AMD64) || defined(__x86_64) || defined(__x86_64__) || \
  225. defined(_M_X64) || defined(__bfin__)
  226. #define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
  227. #elif defined(_MSC_VER) && (defined(_M_ARM) || defined(_M_ARM64))
  228. #define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
  229. #elif defined(RAPIDJSON_DOXYGEN_RUNNING)
  230. #define RAPIDJSON_ENDIAN
  231. #else
  232. # error Unknown machine endianness detected. User needs to define RAPIDJSON_ENDIAN.
  233. #endif
  234. #endif // RAPIDJSON_ENDIAN
  235. ///////////////////////////////////////////////////////////////////////////////
  236. // RAPIDJSON_64BIT
  237. //! Whether using 64-bit architecture
  238. #ifndef RAPIDJSON_64BIT
  239. #if defined(__LP64__) || (defined(__x86_64__) && defined(__ILP32__)) || \
  240. defined(_WIN64) || defined(__EMSCRIPTEN__)
  241. #define RAPIDJSON_64BIT 1
  242. #else
  243. #define RAPIDJSON_64BIT 0
  244. #endif
  245. #endif // RAPIDJSON_64BIT
  246. ///////////////////////////////////////////////////////////////////////////////
  247. // RAPIDJSON_ALIGN
  248. //! Data alignment of the machine.
  249. /*! \ingroup RAPIDJSON_CONFIG
  250. \param x pointer to align
  251. Some machines require strict data alignment. The default is 8 bytes.
  252. User can customize by defining the RAPIDJSON_ALIGN function macro.
  253. */
  254. #ifndef RAPIDJSON_ALIGN
  255. #define RAPIDJSON_ALIGN(x) \
  256. (((x) + static_cast<size_t>(7u)) & ~static_cast<size_t>(7u))
  257. #endif
  258. ///////////////////////////////////////////////////////////////////////////////
  259. // RAPIDJSON_UINT64_C2
  260. //! Construct a 64-bit literal by a pair of 32-bit integer.
  261. /*!
  262. 64-bit literal with or without ULL suffix is prone to compiler warnings.
  263. UINT64_C() is C macro which cause compilation problems.
  264. Use this macro to define 64-bit constants by a pair of 32-bit integer.
  265. */
  266. #ifndef RAPIDJSON_UINT64_C2
  267. #define RAPIDJSON_UINT64_C2(high32, low32) \
  268. ((static_cast<uint64_t>(high32) << 32) | static_cast<uint64_t>(low32))
  269. #endif
  270. ///////////////////////////////////////////////////////////////////////////////
  271. // RAPIDJSON_48BITPOINTER_OPTIMIZATION
  272. //! Use only lower 48-bit address for some pointers.
  273. /*!
  274. \ingroup RAPIDJSON_CONFIG
  275. This optimization uses the fact that current X86-64 architecture only
  276. implement lower 48-bit virtual address. The higher 16-bit can be used for
  277. storing other data. \c GenericValue uses this optimization to reduce its size
  278. form 24 bytes to 16 bytes in 64-bit architecture.
  279. */
  280. #ifndef RAPIDJSON_48BITPOINTER_OPTIMIZATION
  281. #if defined(__amd64__) || defined(__amd64) || defined(__x86_64__) || \
  282. defined(__x86_64) || defined(_M_X64) || defined(_M_AMD64)
  283. #define RAPIDJSON_48BITPOINTER_OPTIMIZATION 1
  284. #else
  285. #define RAPIDJSON_48BITPOINTER_OPTIMIZATION 0
  286. #endif
  287. #endif // RAPIDJSON_48BITPOINTER_OPTIMIZATION
  288. #if RAPIDJSON_48BITPOINTER_OPTIMIZATION == 1
  289. #if RAPIDJSON_64BIT != 1
  290. #error RAPIDJSON_48BITPOINTER_OPTIMIZATION can only be set to 1 when RAPIDJSON_64BIT=1
  291. #endif
  292. #define RAPIDJSON_SETPOINTER(type, p, x) \
  293. (p = reinterpret_cast<type *>( \
  294. (reinterpret_cast<uintptr_t>(p) & \
  295. static_cast<uintptr_t>(RAPIDJSON_UINT64_C2(0xFFFF0000, 0x00000000))) | \
  296. reinterpret_cast<uintptr_t>(reinterpret_cast<const void *>(x))))
  297. #define RAPIDJSON_GETPOINTER(type, p) \
  298. (reinterpret_cast<type *>( \
  299. reinterpret_cast<uintptr_t>(p) & \
  300. static_cast<uintptr_t>(RAPIDJSON_UINT64_C2(0x0000FFFF, 0xFFFFFFFF))))
  301. #else
  302. #define RAPIDJSON_SETPOINTER(type, p, x) (p = (x))
  303. #define RAPIDJSON_GETPOINTER(type, p) (p)
  304. #endif
  305. ///////////////////////////////////////////////////////////////////////////////
  306. // RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_NEON/RAPIDJSON_SIMD
  307. /*! \def RAPIDJSON_SIMD
  308. \ingroup RAPIDJSON_CONFIG
  309. \brief Enable SSE2/SSE4.2/Neon optimization.
  310. RapidJSON supports optimized implementations for some parsing operations
  311. based on the SSE2, SSE4.2 or NEon SIMD extensions on modern Intel
  312. or ARM compatible processors.
  313. To enable these optimizations, three different symbols can be defined;
  314. \code
  315. // Enable SSE2 optimization.
  316. #define RAPIDJSON_SSE2
  317. // Enable SSE4.2 optimization.
  318. #define RAPIDJSON_SSE42
  319. \endcode
  320. // Enable ARM Neon optimization.
  321. #define RAPIDJSON_NEON
  322. \endcode
  323. \c RAPIDJSON_SSE42 takes precedence over SSE2, if both are defined.
  324. If any of these symbols is defined, RapidJSON defines the macro
  325. \c RAPIDJSON_SIMD to indicate the availability of the optimized code.
  326. */
  327. #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) || \
  328. defined(RAPIDJSON_NEON) || defined(RAPIDJSON_DOXYGEN_RUNNING)
  329. #define RAPIDJSON_SIMD
  330. #endif
  331. ///////////////////////////////////////////////////////////////////////////////
  332. // RAPIDJSON_NO_SIZETYPEDEFINE
  333. #ifndef RAPIDJSON_NO_SIZETYPEDEFINE
  334. /*! \def RAPIDJSON_NO_SIZETYPEDEFINE
  335. \ingroup RAPIDJSON_CONFIG
  336. \brief User-provided \c SizeType definition.
  337. In order to avoid using 32-bit size types for indexing strings and arrays,
  338. define this preprocessor symbol and provide the type rapidjson::SizeType
  339. before including RapidJSON:
  340. \code
  341. #define RAPIDJSON_NO_SIZETYPEDEFINE
  342. namespace rapidjson { typedef ::std::size_t SizeType; }
  343. #include "rapidjson/..."
  344. \endcode
  345. \see rapidjson::SizeType
  346. */
  347. #ifdef RAPIDJSON_DOXYGEN_RUNNING
  348. #define RAPIDJSON_NO_SIZETYPEDEFINE
  349. #endif
  350. RAPIDJSON_NAMESPACE_BEGIN
  351. //! Size type (for string lengths, array sizes, etc.)
  352. /*! RapidJSON uses 32-bit array/string indices even on 64-bit platforms,
  353. instead of using \c size_t. Users may override the SizeType by defining
  354. \ref RAPIDJSON_NO_SIZETYPEDEFINE.
  355. */
  356. typedef unsigned SizeType;
  357. RAPIDJSON_NAMESPACE_END
  358. #endif
  359. // always import std::size_t to rapidjson namespace
  360. RAPIDJSON_NAMESPACE_BEGIN
  361. using std::size_t;
  362. RAPIDJSON_NAMESPACE_END
  363. ///////////////////////////////////////////////////////////////////////////////
  364. // RAPIDJSON_ASSERT
  365. //! Assertion.
  366. /*! \ingroup RAPIDJSON_CONFIG
  367. By default, rapidjson uses C \c assert() for internal assertions.
  368. User can override it by defining RAPIDJSON_ASSERT(x) macro.
  369. \note Parsing errors are handled and can be customized by the
  370. \ref RAPIDJSON_ERRORS APIs.
  371. */
  372. #ifndef RAPIDJSON_ASSERT
  373. #include <cassert>
  374. #define RAPIDJSON_ASSERT(x) assert(x)
  375. #endif // RAPIDJSON_ASSERT
  376. ///////////////////////////////////////////////////////////////////////////////
  377. // RAPIDJSON_STATIC_ASSERT
  378. // Prefer C++11 static_assert, if available
  379. #ifndef RAPIDJSON_STATIC_ASSERT
  380. #if __cplusplus >= 201103L || (defined(_MSC_VER) && _MSC_VER >= 1800)
  381. #define RAPIDJSON_STATIC_ASSERT(x) static_assert(x, RAPIDJSON_STRINGIFY(x))
  382. #endif // C++11
  383. #endif // RAPIDJSON_STATIC_ASSERT
  384. // Adopt C++03 implementation from boost
  385. #ifndef RAPIDJSON_STATIC_ASSERT
  386. #ifndef __clang__
  387. //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
  388. #endif
  389. RAPIDJSON_NAMESPACE_BEGIN
  390. template <bool x>
  391. struct STATIC_ASSERTION_FAILURE;
  392. template <>
  393. struct STATIC_ASSERTION_FAILURE<true> {
  394. enum { value = 1 };
  395. };
  396. template <size_t x>
  397. struct StaticAssertTest {};
  398. RAPIDJSON_NAMESPACE_END
  399. #if defined(__GNUC__) || defined(__clang__)
  400. #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
  401. #else
  402. #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
  403. #endif
  404. #ifndef __clang__
  405. //!@endcond
  406. #endif
  407. /*! \def RAPIDJSON_STATIC_ASSERT
  408. \brief (Internal) macro to check for conditions at compile-time
  409. \param x compile-time condition
  410. \hideinitializer
  411. */
  412. #define RAPIDJSON_STATIC_ASSERT(x) \
  413. typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest<sizeof( \
  414. ::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE<bool(x)>)> \
  415. RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) \
  416. RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
  417. #endif // RAPIDJSON_STATIC_ASSERT
  418. ///////////////////////////////////////////////////////////////////////////////
  419. // RAPIDJSON_LIKELY, RAPIDJSON_UNLIKELY
  420. //! Compiler branching hint for expression with high probability to be true.
  421. /*!
  422. \ingroup RAPIDJSON_CONFIG
  423. \param x Boolean expression likely to be true.
  424. */
  425. #ifndef RAPIDJSON_LIKELY
  426. #if defined(__GNUC__) || defined(__clang__)
  427. #define RAPIDJSON_LIKELY(x) __builtin_expect(!!(x), 1)
  428. #else
  429. #define RAPIDJSON_LIKELY(x) (x)
  430. #endif
  431. #endif
  432. //! Compiler branching hint for expression with low probability to be true.
  433. /*!
  434. \ingroup RAPIDJSON_CONFIG
  435. \param x Boolean expression unlikely to be true.
  436. */
  437. #ifndef RAPIDJSON_UNLIKELY
  438. #if defined(__GNUC__) || defined(__clang__)
  439. #define RAPIDJSON_UNLIKELY(x) __builtin_expect(!!(x), 0)
  440. #else
  441. #define RAPIDJSON_UNLIKELY(x) (x)
  442. #endif
  443. #endif
  444. ///////////////////////////////////////////////////////////////////////////////
  445. // Helpers
  446. //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
  447. #define RAPIDJSON_MULTILINEMACRO_BEGIN do {
  448. #define RAPIDJSON_MULTILINEMACRO_END \
  449. } \
  450. while ((void)0, 0)
  451. // adopted from Boost
  452. #define RAPIDJSON_VERSION_CODE(x, y, z) (((x)*100000) + ((y)*100) + (z))
  453. #if defined(__has_builtin)
  454. #define RAPIDJSON_HAS_BUILTIN(x) __has_builtin(x)
  455. #else
  456. #define RAPIDJSON_HAS_BUILTIN(x) 0
  457. #endif
  458. ///////////////////////////////////////////////////////////////////////////////
  459. // RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF
  460. #if defined(__GNUC__)
  461. #define RAPIDJSON_GNUC \
  462. RAPIDJSON_VERSION_CODE(__GNUC__, __GNUC_MINOR__, __GNUC_PATCHLEVEL__)
  463. #endif
  464. #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && \
  465. RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4, 2, 0))
  466. #define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x))
  467. #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x)
  468. #define RAPIDJSON_DIAG_OFF(x) \
  469. RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W, x)))
  470. // push/pop support in Clang and GCC>=4.6
  471. #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && \
  472. RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4, 6, 0))
  473. #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
  474. #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
  475. #else // GCC >= 4.2, < 4.6
  476. #define RAPIDJSON_DIAG_PUSH /* ignored */
  477. #define RAPIDJSON_DIAG_POP /* ignored */
  478. #endif
  479. #elif defined(_MSC_VER)
  480. // pragma (MSVC specific)
  481. #define RAPIDJSON_PRAGMA(x) __pragma(x)
  482. #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x))
  483. #define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable : x)
  484. #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
  485. #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
  486. #else
  487. #define RAPIDJSON_DIAG_OFF(x) /* ignored */
  488. #define RAPIDJSON_DIAG_PUSH /* ignored */
  489. #define RAPIDJSON_DIAG_POP /* ignored */
  490. #endif // RAPIDJSON_DIAG_*
  491. ///////////////////////////////////////////////////////////////////////////////
  492. // C++11 features
  493. #ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS
  494. #if defined(__clang__)
  495. #if __has_feature(cxx_rvalue_references) && \
  496. (defined(_MSC_VER) || defined(_LIBCPP_VERSION) || \
  497. defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306)
  498. #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
  499. #else
  500. #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0
  501. #endif
  502. #elif (defined(RAPIDJSON_GNUC) && \
  503. (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4, 3, 0)) && \
  504. defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
  505. (defined(_MSC_VER) && _MSC_VER >= 1600) || \
  506. (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x5140 && \
  507. defined(__GXX_EXPERIMENTAL_CXX0X__))
  508. #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
  509. #else
  510. #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0
  511. #endif
  512. #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
  513. #ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT
  514. #if defined(__clang__)
  515. #define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept)
  516. #elif (defined(RAPIDJSON_GNUC) && \
  517. (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4, 6, 0)) && \
  518. defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
  519. (defined(_MSC_VER) && _MSC_VER >= 1900) || \
  520. (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x5140 && \
  521. defined(__GXX_EXPERIMENTAL_CXX0X__))
  522. #define RAPIDJSON_HAS_CXX11_NOEXCEPT 1
  523. #else
  524. #define RAPIDJSON_HAS_CXX11_NOEXCEPT 0
  525. #endif
  526. #endif
  527. #if RAPIDJSON_HAS_CXX11_NOEXCEPT
  528. #define RAPIDJSON_NOEXCEPT noexcept
  529. #else
  530. #define RAPIDJSON_NOEXCEPT /* noexcept */
  531. #endif // RAPIDJSON_HAS_CXX11_NOEXCEPT
  532. // no automatic detection, yet
  533. #ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS
  534. #if (defined(_MSC_VER) && _MSC_VER >= 1700)
  535. #define RAPIDJSON_HAS_CXX11_TYPETRAITS 1
  536. #else
  537. #define RAPIDJSON_HAS_CXX11_TYPETRAITS 0
  538. #endif
  539. #endif
  540. #ifndef RAPIDJSON_HAS_CXX11_RANGE_FOR
  541. #if defined(__clang__)
  542. #define RAPIDJSON_HAS_CXX11_RANGE_FOR __has_feature(cxx_range_for)
  543. #elif (defined(RAPIDJSON_GNUC) && \
  544. (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4, 6, 0)) && \
  545. defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
  546. (defined(_MSC_VER) && _MSC_VER >= 1700) || \
  547. (defined(__SUNPRO_CC) && __SUNPRO_CC >= 0x5140 && \
  548. defined(__GXX_EXPERIMENTAL_CXX0X__))
  549. #define RAPIDJSON_HAS_CXX11_RANGE_FOR 1
  550. #else
  551. #define RAPIDJSON_HAS_CXX11_RANGE_FOR 0
  552. #endif
  553. #endif // RAPIDJSON_HAS_CXX11_RANGE_FOR
  554. ///////////////////////////////////////////////////////////////////////////////
  555. // C++17 features
  556. #if defined(__has_cpp_attribute)
  557. #if __has_cpp_attribute(fallthrough)
  558. #define RAPIDJSON_DELIBERATE_FALLTHROUGH [[fallthrough]]
  559. #else
  560. #define RAPIDJSON_DELIBERATE_FALLTHROUGH
  561. #endif
  562. #else
  563. #define RAPIDJSON_DELIBERATE_FALLTHROUGH
  564. #endif
  565. //!@endcond
  566. //! Assertion (in non-throwing contexts).
  567. /*! \ingroup RAPIDJSON_CONFIG
  568. Some functions provide a \c noexcept guarantee, if the compiler supports it.
  569. In these cases, the \ref RAPIDJSON_ASSERT macro cannot be overridden to
  570. throw an exception. This macro adds a separate customization point for
  571. such cases.
  572. Defaults to C \c assert() (as \ref RAPIDJSON_ASSERT), if \c noexcept is
  573. supported, and to \ref RAPIDJSON_ASSERT otherwise.
  574. */
  575. ///////////////////////////////////////////////////////////////////////////////
  576. // RAPIDJSON_NOEXCEPT_ASSERT
  577. #ifndef RAPIDJSON_NOEXCEPT_ASSERT
  578. #ifdef RAPIDJSON_ASSERT_THROWS
  579. #if RAPIDJSON_HAS_CXX11_NOEXCEPT
  580. #define RAPIDJSON_NOEXCEPT_ASSERT(x)
  581. #else
  582. #define RAPIDJSON_NOEXCEPT_ASSERT(x) RAPIDJSON_ASSERT(x)
  583. #endif // RAPIDJSON_HAS_CXX11_NOEXCEPT
  584. #else
  585. #define RAPIDJSON_NOEXCEPT_ASSERT(x) RAPIDJSON_ASSERT(x)
  586. #endif // RAPIDJSON_ASSERT_THROWS
  587. #endif // RAPIDJSON_NOEXCEPT_ASSERT
  588. ///////////////////////////////////////////////////////////////////////////////
  589. // new/delete
  590. #ifndef RAPIDJSON_NEW
  591. ///! customization point for global \c new
  592. #define RAPIDJSON_NEW(TypeName) new TypeName
  593. #endif
  594. #ifndef RAPIDJSON_DELETE
  595. ///! customization point for global \c delete
  596. #define RAPIDJSON_DELETE(x) delete x
  597. #endif
  598. ///////////////////////////////////////////////////////////////////////////////
  599. // Type
  600. /*! \namespace rapidjson
  601. \brief main RapidJSON namespace
  602. \see RAPIDJSON_NAMESPACE
  603. */
  604. RAPIDJSON_NAMESPACE_BEGIN
  605. //! Type of JSON value
  606. enum Type {
  607. kNullType = 0, //!< null
  608. kFalseType = 1, //!< false
  609. kTrueType = 2, //!< true
  610. kObjectType = 3, //!< object
  611. kArrayType = 4, //!< array
  612. kStringType = 5, //!< string
  613. kNumberType = 6 //!< number
  614. };
  615. RAPIDJSON_NAMESPACE_END
  616. #endif // RAPIDJSON_RAPIDJSON_H_