rapidjson.h 25 KB

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