TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
rapidjson.h
Go to the documentation of this file.
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 
39 #include <cstdlib> // malloc(), realloc(), free(), size_t
40 #include <cstring> // memset(), memcpy(), memmove(), memcmp()
41 
43 // RAPIDJSON_VERSION_STRING
44 //
45 // ALWAYS synchronize the following 3 macros with corresponding variables in /CMakeLists.txt.
46 //
47 
49 // token stringification
50 #define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x)
51 #define RAPIDJSON_DO_STRINGIFY(x) #x
52 
70 #define RAPIDJSON_MAJOR_VERSION 1
71 #define RAPIDJSON_MINOR_VERSION 0
72 #define RAPIDJSON_PATCH_VERSION 2
73 #define RAPIDJSON_VERSION_STRING \
74  RAPIDJSON_STRINGIFY(RAPIDJSON_MAJOR_VERSION.RAPIDJSON_MINOR_VERSION.RAPIDJSON_PATCH_VERSION)
75 
77 // RAPIDJSON_NAMESPACE_(BEGIN|END)
112 #ifndef RAPIDJSON_NAMESPACE
113 #define RAPIDJSON_NAMESPACE rapidjson
114 #endif
115 #ifndef RAPIDJSON_NAMESPACE_BEGIN
116 #define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE {
117 #endif
118 #ifndef RAPIDJSON_NAMESPACE_END
119 #define RAPIDJSON_NAMESPACE_END }
120 #endif
121 
123 // RAPIDJSON_NO_INT64DEFINE
124 
135 #ifndef RAPIDJSON_NO_INT64DEFINE
136 #ifdef _MSC_VER
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 #ifdef RAPIDJSON_DOXYGEN_RUNNING
147 #define RAPIDJSON_NO_INT64DEFINE
148 #endif
149 #endif // RAPIDJSON_NO_INT64TYPEDEF
150 
152 // RAPIDJSON_FORCEINLINE
153 
154 #ifndef RAPIDJSON_FORCEINLINE
155 #if defined(_MSC_VER) && !defined(NDEBUG)
157 #define RAPIDJSON_FORCEINLINE __forceinline
158 #elif defined(__GNUC__) && __GNUC__ >= 4 && !defined(NDEBUG)
159 #define RAPIDJSON_FORCEINLINE __attribute__((always_inline))
160 #else
161 #define RAPIDJSON_FORCEINLINE
162 #endif
163 #endif // RAPIDJSON_FORCEINLINE
165 
167 // RAPIDJSON_ENDIAN
168 #define RAPIDJSON_LITTLEENDIAN 0
169 #define RAPIDJSON_BIGENDIAN 1
170 
171 
184 #ifndef RAPIDJSON_ENDIAN
185 // Detect with GCC 4.6's macro
186 # ifdef __BYTE_ORDER__
187 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
188 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
189 # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
190 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
191 # else
192 # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
193 # endif // __BYTE_ORDER__
194 // Detect with GLIBC's endian.h
195 # elif defined(__GLIBC__)
196 # include <endian.h>
197 # if (__BYTE_ORDER == __LITTLE_ENDIAN)
198 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
199 # elif (__BYTE_ORDER == __BIG_ENDIAN)
200 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
201 # else
202 # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
203 # endif // __GLIBC__
204 // Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro
205 # elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
206 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
207 # elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
208 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
209 // Detect with architecture macros
210 # elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
211 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
212 # 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__)
213 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
214 # elif defined(RAPIDJSON_DOXYGEN_RUNNING)
215 # define RAPIDJSON_ENDIAN
216 # else
217 # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
218 # endif
219 #endif // RAPIDJSON_ENDIAN
220 
222 // RAPIDJSON_64BIT
223 
225 #ifndef RAPIDJSON_64BIT
226 #if defined(__LP64__) || defined(_WIN64)
227 #define RAPIDJSON_64BIT 1
228 #else
229 #define RAPIDJSON_64BIT 0
230 #endif
231 #endif // RAPIDJSON_64BIT
232 
234 // RAPIDJSON_ALIGN
235 
237 
243 #ifndef RAPIDJSON_ALIGN
244 #if RAPIDJSON_64BIT == 1
245 #define RAPIDJSON_ALIGN(x) ((x + 7u) & ~7u)
246 #else
247 #define RAPIDJSON_ALIGN(x) ((x + 3u) & ~3u)
248 #endif
249 #endif
250 
252 // RAPIDJSON_UINT64_C2
253 
255 
260 #ifndef RAPIDJSON_UINT64_C2
261 #define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast<uint64_t>(high32) << 32) | static_cast<uint64_t>(low32))
262 #endif
263 
265 // RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD
266 
289 #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \
290  || defined(RAPIDJSON_DOXYGEN_RUNNING)
291 #define RAPIDJSON_SIMD
292 #endif
293 
295 // RAPIDJSON_NO_SIZETYPEDEFINE
296 
297 #ifndef RAPIDJSON_NO_SIZETYPEDEFINE
298 
313 #ifdef RAPIDJSON_DOXYGEN_RUNNING
314 #define RAPIDJSON_NO_SIZETYPEDEFINE
315 #endif
318 
322 typedef unsigned SizeType;
324 #endif
325 
326 // always import std::size_t to rapidjson namespace
328 using std::size_t;
330 
332 // RAPIDJSON_ASSERT
333 
335 
342 #ifndef RAPIDJSON_ASSERT
343 #include <cassert>
344 #define RAPIDJSON_ASSERT(x) assert(x)
345 #endif // RAPIDJSON_ASSERT
346 
348 // RAPIDJSON_STATIC_ASSERT
349 
350 // Adopt from boost
351 #ifndef RAPIDJSON_STATIC_ASSERT
354 template <bool x> struct STATIC_ASSERTION_FAILURE;
355 template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
356 template<int x> struct StaticAssertTest {};
358 
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 
363 #if defined(__GNUC__)
364 #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
365 #else
366 #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
367 #endif
368 
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 
382 // Helpers
383 
385 
386 #define RAPIDJSON_MULTILINEMACRO_BEGIN do {
387 #define RAPIDJSON_MULTILINEMACRO_END \
388 } while((void)0, 0)
389 
390 // adopted from Boost
391 #define RAPIDJSON_VERSION_CODE(x,y,z) \
392  (((x)*100000) + ((y)*100) + (z))
393 
395 // RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF
396 
397 #if defined(__GNUC__)
398 #define RAPIDJSON_GNUC \
399  RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__)
400 #endif
401 
402 #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,2,0))
403 
404 #define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x))
405 #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x)
406 #define RAPIDJSON_DIAG_OFF(x) \
407  RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x)))
408 
409 // push/pop support in Clang and GCC>=4.6
410 #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0))
411 #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
412 #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
413 #else // GCC >= 4.2, < 4.6
414 #define RAPIDJSON_DIAG_PUSH /* ignored */
415 #define RAPIDJSON_DIAG_POP /* ignored */
416 #endif
417 
418 #elif defined(_MSC_VER)
419 
420 // pragma (MSVC specific)
421 #define RAPIDJSON_PRAGMA(x) __pragma(x)
422 #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x))
423 
424 #define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x)
425 #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
426 #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
427 
428 #else
429 
430 #define RAPIDJSON_DIAG_OFF(x) /* ignored */
431 #define RAPIDJSON_DIAG_PUSH /* ignored */
432 #define RAPIDJSON_DIAG_POP /* ignored */
433 
434 #endif // RAPIDJSON_DIAG_*
435 
437 // C++11 features
438 
439 #ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS
440 #if defined(__clang__)
441 #define RAPIDJSON_HAS_CXX11_RVALUE_REFS __has_feature(cxx_rvalue_references) && \
442  (defined(_LIBCPP_VERSION) || defined(__GLIBCXX__) && __GLIBCXX__ >= 20080306)
443 #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
444  (defined(_MSC_VER) && _MSC_VER >= 1600)
445 
446 #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
447 #else
448 #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0
449 #endif
450 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
451 
452 #ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT
453 #if defined(__clang__)
454 #define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept)
455 #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__))
456 // (defined(_MSC_VER) && _MSC_VER >= ????) // not yet supported
457 #define RAPIDJSON_HAS_CXX11_NOEXCEPT 1
458 #else
459 #define RAPIDJSON_HAS_CXX11_NOEXCEPT 0
460 #endif
461 #endif
462 #if RAPIDJSON_HAS_CXX11_NOEXCEPT
463 #define RAPIDJSON_NOEXCEPT noexcept
464 #else
465 #define RAPIDJSON_NOEXCEPT /* noexcept */
466 #endif // RAPIDJSON_HAS_CXX11_NOEXCEPT
467 
468 // no automatic detection, yet
469 #ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS
470 #define RAPIDJSON_HAS_CXX11_TYPETRAITS 0
471 #endif
472 
474 
476 // new/delete
477 
478 #ifndef RAPIDJSON_NEW
479 #define RAPIDJSON_NEW(x) new x
481 #endif
482 #ifndef RAPIDJSON_DELETE
483 #define RAPIDJSON_DELETE(x) delete x
485 #endif
486 
488 // Allocators and Encodings
489 
490 #include "allocators.h"
491 #include "encodings.h"
492 
498 
500 // Stream
501 
514 
517 
521 
525 
528 
531 
536 
541 
547 template<typename Stream>
548 struct StreamTraits {
550 
554  enum { copyOptimization = 0 };
555 };
556 
558 template<typename Stream, typename Ch>
559 inline void PutN(Stream& stream, Ch c, size_t n) {
560  for (size_t i = 0; i < n; i++)
561  stream.Put(c);
562 }
563 
565 // StringStream
566 
568 
570 template <typename Encoding>
572  typedef typename Encoding::Ch Ch;
573 
574  GenericStringStream(const Ch *src) : src_(src), head_(src) {}
575 
576  Ch Peek() const { return *src_; }
577  Ch Take() { return *src_++; }
578  size_t Tell() const { return static_cast<size_t>(src_ - head_); }
579 
580  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
581  void Put(Ch) { RAPIDJSON_ASSERT(false); }
582  void Flush() { RAPIDJSON_ASSERT(false); }
583  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
584 
585  const Ch* src_;
586  const Ch* head_;
587 };
588 
589 template <typename Encoding>
590 struct StreamTraits<GenericStringStream<Encoding> > {
591  enum { copyOptimization = 1 };
592 };
593 
596 
598 // InsituStringStream
599 
601 
604 template <typename Encoding>
606  typedef typename Encoding::Ch Ch;
607 
608  GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {}
609 
610  // Read
611  Ch Peek() { return *src_; }
612  Ch Take() { return *src_++; }
613  size_t Tell() { return static_cast<size_t>(src_ - head_); }
614 
615  // Write
616  void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; }
617 
618  Ch* PutBegin() { return dst_ = src_; }
619  size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); }
620  void Flush() {}
621 
622  Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; }
623  void Pop(size_t count) { dst_ -= count; }
624 
625  Ch* src_;
626  Ch* dst_;
627  Ch* head_;
628 };
629 
630 template <typename Encoding>
632  enum { copyOptimization = 1 };
633 };
634 
637 
639 // Type
640 
642 enum Type {
643  kNullType = 0,
645  kTrueType = 2,
650 };
651 
653 
654 #endif // RAPIDJSON_RAPIDJSON_H_
Ch * PutBegin()
Definition: rapidjson.h:580
Ch Take()
Definition: rapidjson.h:612
Ch Take()
Definition: rapidjson.h:577
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:322
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:344
Ch Peek()
Definition: rapidjson.h:611
object
Definition: rapidjson.h:646
Read-only string stream.
Definition: rapidjson.h:571
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:119
array
Definition: rapidjson.h:647
GenericInsituStringStream< UTF8<> > InsituStringStream
Insitu string stream with UTF8 encoding.
Definition: rapidjson.h:636
GenericStringStream< UTF8<> > StringStream
String stream with UTF8 encoding.
Definition: rapidjson.h:595
GenericInsituStringStream(Ch *src)
Definition: rapidjson.h:608
const Ch * src_
Current read position.
Definition: rapidjson.h:585
false
Definition: rapidjson.h:644
Ch Peek() const
Definition: rapidjson.h:576
void Pop(size_t count)
Definition: rapidjson.h:623
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:116
Ch * dst_
Definition: rapidjson.h:626
A read-write string stream.
Definition: rapidjson.h:605
#define true
Definition: CascPort.h:17
GenericStringStream(const Ch *src)
Definition: rapidjson.h:574
Ch * src_
Definition: rapidjson.h:625
void Flush()
Definition: rapidjson.h:582
string
Definition: rapidjson.h:648
size_t Tell()
Definition: rapidjson.h:613
number
Definition: rapidjson.h:649
Provides additional information for stream.
Definition: rapidjson.h:548
size_t PutEnd(Ch *)
Definition: rapidjson.h:583
Ch * head_
Definition: rapidjson.h:627
Definition: rapidjson.h:554
void Flush()
Definition: rapidjson.h:620
true
Definition: rapidjson.h:645
const FieldDescriptor value
Definition: descriptor.h:1522
size_t PutEnd(Ch *begin)
Definition: rapidjson.h:619
Encoding::Ch Ch
Definition: rapidjson.h:572
void Put(Ch)
Definition: rapidjson.h:581
void Put(Ch c)
Definition: rapidjson.h:616
Type
Type of JSON value.
Definition: rapidjson.h:642
size_t Tell() const
Definition: rapidjson.h:578
Ch * Push(size_t count)
Definition: rapidjson.h:622
Ch * PutBegin()
Definition: rapidjson.h:618
Encoding::Ch Ch
Definition: rapidjson.h:606
null
Definition: rapidjson.h:643
void PutN(Stream &stream, Ch c, size_t n)
Put N copies of a character to a stream.
Definition: rapidjson.h:559
const Ch * head_
Original head of the string.
Definition: rapidjson.h:586