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