webkit  2cdf99a9e3038c7e01b3c37e8ad903ecbe5eecf1
https://github.com/WebKit/webkit
rapidjson.h
Go to the documentation of this file.
1 // Copyright (C) 2011 Milo Yip
2 //
3 // Permission is hereby granted, free of charge, to any person obtaining a copy
4 // of this software and associated documentation files (the "Software"), to deal
5 // in the Software without restriction, including without limitation the rights
6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7 // copies of the Software, and to permit persons to whom the Software is
8 // furnished to do so, subject to the following conditions:
9 //
10 // The above copyright notice and this permission notice shall be included in
11 // all copies or substantial portions of the Software.
12 //
13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19 // THE SOFTWARE.
20 
21 #ifndef RAPIDJSON_RAPIDJSON_H_
22 #define RAPIDJSON_RAPIDJSON_H_
23 
24 // Copyright (c) 2011 Milo Yip (miloyip@gmail.com)
25 // Version 0.1
26 
48 #include <cstdlib> // malloc(), realloc(), free(), size_t
49 #include <cstring> // memset(), memcpy(), memmove(), memcmp()
50 
52 // RAPIDJSON_NAMESPACE_(BEGIN|END)
87 #ifndef RAPIDJSON_NAMESPACE
88 #define RAPIDJSON_NAMESPACE rapidjson
89 #endif
90 #ifndef RAPIDJSON_NAMESPACE_BEGIN
91 #define RAPIDJSON_NAMESPACE_BEGIN namespace RAPIDJSON_NAMESPACE {
92 #endif
93 #ifndef RAPIDJSON_NAMESPACE_END
94 #define RAPIDJSON_NAMESPACE_END }
95 #endif
96 
98 // RAPIDJSON_NO_INT64DEFINE
99 
110 #ifndef RAPIDJSON_NO_INT64DEFINE
111 #ifdef _MSC_VER
113 #include "msinttypes/stdint.h"
114 #include "msinttypes/inttypes.h"
115 #else
116 // Other compilers should have this.
117 #include <stdint.h>
118 #include <inttypes.h>
119 #endif
120 #ifdef RAPIDJSON_DOXYGEN_RUNNING
122 #define RAPIDJSON_NO_INT64DEFINE
123 #endif
124 #endif // RAPIDJSON_NO_INT64TYPEDEF
125 
127 // RAPIDJSON_FORCEINLINE
128 
129 #ifndef RAPIDJSON_FORCEINLINE
130 #ifdef _MSC_VER
132 #define RAPIDJSON_FORCEINLINE __forceinline
133 #elif defined(__GNUC__) && __GNUC__ >= 4
134 #define RAPIDJSON_FORCEINLINE __attribute__((always_inline))
135 #else
136 #define RAPIDJSON_FORCEINLINE
137 #endif
138 #endif // RAPIDJSON_FORCEINLINE
140 
142 // RAPIDJSON_ENDIAN
143 #define RAPIDJSON_LITTLEENDIAN 0
144 #define RAPIDJSON_BIGENDIAN 1
145 
146 
159 #ifndef RAPIDJSON_ENDIAN
160 // Detect with GCC 4.6's macro
161 # ifdef __BYTE_ORDER__
162 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
163 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
164 # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
165 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
166 # else
167 # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
168 # endif // __BYTE_ORDER__
169 // Detect with GLIBC's endian.h
170 # elif defined(__GLIBC__)
171 # include <endian.h>
172 # if (__BYTE_ORDER == __LITTLE_ENDIAN)
173 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
174 # elif (__BYTE_ORDER == __BIG_ENDIAN)
175 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
176 # else
177 # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
178 # endif // __GLIBC__
179 // Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro
180 # elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
181 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
182 # elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
183 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
184 // Detect with architecture macros
185 # elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
186 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
187 # 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__)
188 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
189 # elif defined(RAPIDJSON_DOXYGEN_RUNNING)
190 # define RAPIDJSON_ENDIAN
191 # else
192 # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
193 # endif
194 #endif // RAPIDJSON_ENDIAN
195 
197 // RAPIDJSON_64BIT
198 
200 #ifndef RAPIDJSON_64BIT
201 #if defined(__LP64__) || defined(_WIN64)
202 #define RAPIDJSON_64BIT 1
203 #else
204 #define RAPIDJSON_64BIT 0
205 #endif
206 #endif // RAPIDJSON_64BIT
207 
209 // RAPIDJSON_ALIGN
210 
212 
218 #ifndef RAPIDJSON_ALIGN
219 #define RAPIDJSON_ALIGN(x) ((x + 3u) & ~3u)
220 #endif
221 
223 // RAPIDJSON_UINT64_C2
224 
226 
231 #ifndef RAPIDJSON_UINT64_C2
232 #define RAPIDJSON_UINT64_C2(high32, low32) ((static_cast<uint64_t>(high32) << 32) | static_cast<uint64_t>(low32))
233 #endif
234 
236 // RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD
237 
260 #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) \
261  || defined(RAPIDJSON_DOXYGEN_RUNNING)
262 #define RAPIDJSON_SIMD
263 #endif
264 
266 // RAPIDJSON_NO_SIZETYPEDEFINE
267 
268 #ifndef RAPIDJSON_NO_SIZETYPEDEFINE
269 
284 #ifdef RAPIDJSON_DOXYGEN_RUNNING
285 #define RAPIDJSON_NO_SIZETYPEDEFINE
286 #endif
289 
293 typedef unsigned SizeType;
295 #endif
296 
297 // always import std::size_t to rapidjson namespace
299 using std::size_t;
301 
303 // RAPIDJSON_ASSERT
304 
306 
313 #ifndef RAPIDJSON_ASSERT
314 #include <cassert>
315 #define RAPIDJSON_ASSERT(x) assert(x)
316 #endif // RAPIDJSON_ASSERT
317 
319 // RAPIDJSON_STATIC_ASSERT
320 
321 // Adopt from boost
322 #ifndef RAPIDJSON_STATIC_ASSERT
325 template <bool x> struct STATIC_ASSERTION_FAILURE;
326 template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
327 template<int x> struct StaticAssertTest {};
329 
330 #define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y)
331 #define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y)
332 #define RAPIDJSON_DO_JOIN2(X, Y) X##Y
333 
334 #if defined(__GNUC__)
335 #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
336 #else
337 #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
338 #endif
339 
346 #define RAPIDJSON_STATIC_ASSERT(x) \
347  typedef ::RAPIDJSON_NAMESPACE::StaticAssertTest< \
348  sizeof(::RAPIDJSON_NAMESPACE::STATIC_ASSERTION_FAILURE<bool(x) >)> \
349  RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
350 #endif
351 
353 // Helpers
354 
356 
357 #define RAPIDJSON_MULTILINEMACRO_BEGIN do {
358 #define RAPIDJSON_MULTILINEMACRO_END \
359 } while((void)0, 0)
360 
361 // adopted from Boost
362 #define RAPIDJSON_VERSION_CODE(x,y,z) \
363  (((x)*100000) + ((y)*100) + (z))
364 
365 // token stringification
366 #define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x)
367 #define RAPIDJSON_DO_STRINGIFY(x) #x
368 
370 // RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF
371 
372 #if defined(__GNUC__)
373 #define RAPIDJSON_GNUC \
374  RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__)
375 #endif
376 
377 #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,2,0))
378 
379 #define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x))
380 #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x)
381 #define RAPIDJSON_DIAG_OFF(x) \
382  RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x)))
383 
384 // push/pop support in Clang and GCC>=4.6
385 #if defined(__clang__) || (defined(RAPIDJSON_GNUC) && RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0))
386 #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
387 #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
388 #else // GCC >= 4.2, < 4.6
389 #define RAPIDJSON_DIAG_PUSH /* ignored */
390 #define RAPIDJSON_DIAG_POP /* ignored */
391 #endif
392 
393 #elif defined(_MSC_VER)
394 
395 // pragma (MSVC specific)
396 #define RAPIDJSON_PRAGMA(x) __pragma(x)
397 #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x))
398 
399 #define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x)
400 #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
401 #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
402 
403 #else
404 
405 #define RAPIDJSON_DIAG_OFF(x) /* ignored */
406 #define RAPIDJSON_DIAG_PUSH /* ignored */
407 #define RAPIDJSON_DIAG_POP /* ignored */
408 
409 #endif // RAPIDJSON_DIAG_*
410 
412 // C++11 features
413 
414 #ifndef RAPIDJSON_HAS_CXX11_RVALUE_REFS
415 #if defined(__clang__)
416 #define RAPIDJSON_HAS_CXX11_RVALUE_REFS __has_feature(cxx_rvalue_references)
417 #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,3,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__)) || \
418  (defined(_MSC_VER) && _MSC_VER >= 1600)
419 
420 #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 1
421 #else
422 #define RAPIDJSON_HAS_CXX11_RVALUE_REFS 0
423 #endif
424 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
425 
426 #ifndef RAPIDJSON_HAS_CXX11_NOEXCEPT
427 #if defined(__clang__)
428 #define RAPIDJSON_HAS_CXX11_NOEXCEPT __has_feature(cxx_noexcept)
429 #elif (defined(RAPIDJSON_GNUC) && (RAPIDJSON_GNUC >= RAPIDJSON_VERSION_CODE(4,6,0)) && defined(__GXX_EXPERIMENTAL_CXX0X__))
430 // (defined(_MSC_VER) && _MSC_VER >= ????) // not yet supported
431 #define RAPIDJSON_HAS_CXX11_NOEXCEPT 1
432 #else
433 #define RAPIDJSON_HAS_CXX11_NOEXCEPT 0
434 #endif
435 #endif
436 #if RAPIDJSON_HAS_CXX11_NOEXCEPT
437 #define RAPIDJSON_NOEXCEPT noexcept
438 #else
439 #define RAPIDJSON_NOEXCEPT /* noexcept */
440 #endif // RAPIDJSON_HAS_CXX11_NOEXCEPT
441 
442 // no automatic detection, yet
443 #ifndef RAPIDJSON_HAS_CXX11_TYPETRAITS
444 #define RAPIDJSON_HAS_CXX11_TYPETRAITS 0
445 #endif
446 
448 
450 // new/delete
451 
452 #ifndef RAPIDJSON_NEW
453 #define RAPIDJSON_NEW(x) new x
455 #endif
456 #ifndef RAPIDJSON_DELETE
457 #define RAPIDJSON_DELETE(x) delete x
459 #endif
460 
462 // Allocators and Encodings
463 
464 #include "allocators.h"
465 #include "encodings.h"
466 
472 
474 // Stream
475 
488 
491 
495 
499 
502 
505 
510 
515 
521 template<typename Stream>
522 struct StreamTraits {
524 
528  enum { copyOptimization = 0 };
529 };
530 
532 template<typename Stream, typename Ch>
533 inline void PutN(Stream& stream, Ch c, size_t n) {
534  for (size_t i = 0; i < n; i++)
535  stream.Put(c);
536 }
537 
539 // StringStream
540 
542 
544 template <typename Encoding>
546  typedef typename Encoding::Ch Ch;
547 
548  GenericStringStream(const Ch *src) : src_(src), head_(src) {}
549 
550  Ch Peek() const { return *src_; }
551  Ch Take() { return *src_++; }
552  size_t Tell() const { return static_cast<size_t>(src_ - head_); }
553 
554  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
555  void Put(Ch) { RAPIDJSON_ASSERT(false); }
556  void Flush() { RAPIDJSON_ASSERT(false); }
557  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
558 
559  const Ch* src_;
560  const Ch* head_;
561 };
562 
563 template <typename Encoding>
564 struct StreamTraits<GenericStringStream<Encoding> > {
565  enum { copyOptimization = 1 };
566 };
567 
570 
572 // InsituStringStream
573 
575 
578 template <typename Encoding>
580  typedef typename Encoding::Ch Ch;
581 
582  GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {}
583 
584  // Read
585  Ch Peek() { return *src_; }
586  Ch Take() { return *src_++; }
587  size_t Tell() { return static_cast<size_t>(src_ - head_); }
588 
589  // Write
590  void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; }
591 
592  Ch* PutBegin() { return dst_ = src_; }
593  size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); }
594  void Flush() {}
595 
596  Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; }
597  void Pop(size_t count) { dst_ -= count; }
598 
599  Ch* src_;
600  Ch* dst_;
601  Ch* head_;
602 };
603 
604 template <typename Encoding>
606  enum { copyOptimization = 1 };
607 };
608 
611 
613 // Type
614 
616 enum Type {
617  kNullType = 0,
619  kTrueType = 2,
624 };
625 
627 
628 #endif // RAPIDJSON_RAPIDJSON_H_
Ch * dst_
Definition: rapidjson.h:600
GLint GLsizei count
Definition: gl2.h:421
Ch * src_
Definition: rapidjson.h:599
RAPIDJSON_NAMESPACE_BEGIN typedef unsigned SizeType
Size type (for string lengths, array sizes, etc.)
Definition: rapidjson.h:293
EGLStreamKHR stream
Definition: eglext.h:340
int c
Definition: cpp_unittests.cpp:275
object
Definition: rapidjson.h:620
Encoding::Ch Ch
Definition: rapidjson.h:546
void Flush()
Definition: rapidjson.h:556
Ch * Push(size_t count)
Definition: rapidjson.h:596
size_t Tell()
Definition: rapidjson.h:587
array
Definition: rapidjson.h:621
GenericInsituStringStream< UTF8<> > InsituStringStream
Insitu string stream with UTF8 encoding.
Definition: rapidjson.h:610
GenericStringStream< UTF8<> > StringStream
String stream with UTF8 encoding.
Definition: rapidjson.h:569
#define Ch(x, y, z)
Definition: sha256.c:217
Ch * head_
Definition: rapidjson.h:601
Ch Peek()
Definition: rapidjson.h:585
Ch Peek() const
Definition: rapidjson.h:550
false
Definition: rapidjson.h:618
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:91
std::integral_constant< std::size_t, V > size_t
Definition: Brigand.h:447
size_t PutEnd(Ch *begin)
Definition: rapidjson.h:593
Provides additional information for stream.
Definition: rapidjson.h:522
EGLStreamKHR EGLint n
Definition: eglext.h:984
string
Definition: rapidjson.h:622
const Ch * src_
Current read position.
Definition: rapidjson.h:559
Encoding::Ch Ch
Definition: rapidjson.h:580
EGLAttrib * value
Definition: eglext.h:120
Read-only string stream.
Definition: rapidjson.h:545
Ch * PutBegin()
Definition: rapidjson.h:554
number
Definition: rapidjson.h:623
size_t Tell() const
Definition: rapidjson.h:552
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:94
#define true
Definition: float-mm.c:6
void Put(Ch)
Definition: rapidjson.h:555
Ch Take()
Definition: rapidjson.h:586
size_t PutEnd(Ch *)
Definition: rapidjson.h:557
GLenum src
Definition: gl2ext.h:304
const Ch * head_
Original head of the string.
Definition: rapidjson.h:560
for i
Definition: complexityMeasures.m:24
GenericInsituStringStream(Ch *src)
Definition: rapidjson.h:582
Ch Take()
Definition: rapidjson.h:551
Ch * PutBegin()
Definition: rapidjson.h:592
true
Definition: rapidjson.h:619
GenericStringStream(const Ch *src)
Definition: rapidjson.h:548
A read-write string stream.
Definition: rapidjson.h:579
Type
Type of JSON value.
Definition: rapidjson.h:616
void Pop(size_t count)
Definition: rapidjson.h:597
void Flush()
Definition: rapidjson.h:594
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:315
void Put(Ch c)
Definition: rapidjson.h:590
null
Definition: rapidjson.h:617
void PutN(Stream &stream, Ch c, size_t n)
Put N copies of a character to a stream.
Definition: rapidjson.h:533