All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
rapidjson.h
Go to the documentation of this file.
1 #ifndef RAPIDJSON_RAPIDJSON_H_
2 #define RAPIDJSON_RAPIDJSON_H_
3 
4 // Copyright (c) 2011 Milo Yip (miloyip@gmail.com)
5 // Version 0.1
6 
7 /*!\file rapidjson.h
8  \brief common definitions and configuration
9 
10  \todo Complete Doxygen documentation for configure macros.
11  */
12 
13 #include <cstdlib> // malloc(), realloc(), free()
14 #include <cstring> // memcpy()
15 
16 ///////////////////////////////////////////////////////////////////////////////
17 // RAPIDJSON_NO_INT64DEFINE
18 
19 // Here defines int64_t and uint64_t types in global namespace as well as the
20 // (U)INT64_C constant macros.
21 // If user have their own definition, can define RAPIDJSON_NO_INT64DEFINE to disable this.
22 #ifndef RAPIDJSON_NO_INT64DEFINE
23 //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
24 #ifndef __STDC_CONSTANT_MACROS
25 # define __STDC_CONSTANT_MACROS 1 // required by C++ standard
26 #endif
27 #ifdef _MSC_VER
28 #include "msinttypes/stdint.h"
29 #include "msinttypes/inttypes.h"
30 #else
31 // Other compilers should have this.
32 #include <stdint.h>
33 #include <inttypes.h>
34 #endif
35 //!@endcond
36 #endif // RAPIDJSON_NO_INT64TYPEDEF
37 
38 ///////////////////////////////////////////////////////////////////////////////
39 // RAPIDJSON_FORCEINLINE
40 
41 #ifndef RAPIDJSON_FORCEINLINE
42 #ifdef _MSC_VER
43 #define RAPIDJSON_FORCEINLINE __forceinline
44 #elif defined(__GNUC__) && __GNUC__ >= 4
45 #define RAPIDJSON_FORCEINLINE __attribute__((always_inline))
46 #else
47 #define RAPIDJSON_FORCEINLINE
48 #endif
49 #endif // RAPIDJSON_FORCEINLINE
50 
51 ///////////////////////////////////////////////////////////////////////////////
52 // RAPIDJSON_ENDIAN
53 #define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine
54 #define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine
55 
56 //! Endianness of the machine.
57 /*! GCC 4.6 provided macro for detecting endianness of the target machine. But other
58  compilers may not have this. User can define RAPIDJSON_ENDIAN to either
59  \ref RAPIDJSON_LITTLEENDIAN or \ref RAPIDJSON_BIGENDIAN.
60 
61  Implemented with reference to
62  https://gcc.gnu.org/onlinedocs/gcc-4.6.0/cpp/Common-Predefined-Macros.html
63  http://www.boost.org/doc/libs/1_42_0/boost/detail/endian.hpp
64 */
65 #ifndef RAPIDJSON_ENDIAN
66 // Detect with GCC 4.6's macro
67 # ifdef __BYTE_ORDER__
68 # if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__
69 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
70 # elif __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__
71 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
72 # else
73 # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
74 # endif // __BYTE_ORDER__
75 // Detect with GLIBC's endian.h
76 # elif defined(__GLIBC__)
77 # include <endian.h>
78 # if (__BYTE_ORDER == __LITTLE_ENDIAN)
79 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
80 # elif (__BYTE_ORDER == __BIG_ENDIAN)
81 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
82 # else
83 # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
84 # endif // __GLIBC__
85 // Detect with _LITTLE_ENDIAN and _BIG_ENDIAN macro
86 # elif defined(_LITTLE_ENDIAN) && !defined(_BIG_ENDIAN)
87 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
88 # elif defined(_BIG_ENDIAN) && !defined(_LITTLE_ENDIAN)
89 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
90 // Detect with architecture macros
91 # elif defined(__sparc) || defined(__sparc__) || defined(_POWER) || defined(__powerpc__) || defined(__ppc__) || defined(__hpux) || defined(__hppa) || defined(_MIPSEB) || defined(_POWER) || defined(__s390__)
92 # define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN
93 # 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__)
94 # define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN
95 # else
96 # error Unknown machine endianess detected. User needs to define RAPIDJSON_ENDIAN.
97 # endif
98 #endif // RAPIDJSON_ENDIAN
99 
100 ///////////////////////////////////////////////////////////////////////////////
101 // RAPIDJSON_ALIGNSIZE
102 
103 //! Data alignment of the machine.
104 /*!
105  Some machine requires strict data alignment.
106  Currently the default uses 4 bytes alignment. User can customize this.
107 */
108 #ifndef RAPIDJSON_ALIGN
109 #define RAPIDJSON_ALIGN(x) ((x + 3u) & ~3u)
110 #endif
111 
112 ///////////////////////////////////////////////////////////////////////////////
113 // RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD
114 
115 // Enable SSE2 optimization.
116 //#define RAPIDJSON_SSE2
117 
118 // Enable SSE4.2 optimization.
119 //#define RAPIDJSON_SSE42
120 
121 #if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42)
122 #define RAPIDJSON_SIMD
123 #endif
124 
125 ///////////////////////////////////////////////////////////////////////////////
126 // RAPIDJSON_NO_SIZETYPEDEFINE
127 
128 #ifndef RAPIDJSON_NO_SIZETYPEDEFINE
129 namespace rapidjson {
130 //! Use 32-bit array/string indices even for 64-bit platform, instead of using size_t.
131 /*! User may override the SizeType by defining RAPIDJSON_NO_SIZETYPEDEFINE.
132 */
133 typedef unsigned SizeType;
134 } // namespace rapidjson
135 #endif
136 
137 ///////////////////////////////////////////////////////////////////////////////
138 // RAPIDJSON_ASSERT
139 
140 //! Assertion.
141 /*! By default, rapidjson uses C assert() for assertion.
142  User can override it by defining RAPIDJSON_ASSERT(x) macro.
143 */
144 #ifndef RAPIDJSON_ASSERT
145 #include <cassert>
146 #define RAPIDJSON_ASSERT(x) assert(x)
147 #endif // RAPIDJSON_ASSERT
148 
149 ///////////////////////////////////////////////////////////////////////////////
150 // RAPIDJSON_STATIC_ASSERT
151 
152 // Adopt from boost
153 #ifndef RAPIDJSON_STATIC_ASSERT
154 //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
155 namespace rapidjson {
156 
157 template <bool x> struct STATIC_ASSERTION_FAILURE;
158 template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; };
159 template<int x> struct StaticAssertTest {};
160 } // namespace rapidjson
161 
162 #define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y)
163 #define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y)
164 #define RAPIDJSON_DO_JOIN2(X, Y) X##Y
165 
166 #if defined(__GNUC__)
167 #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE __attribute__((unused))
168 #else
169 #define RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
170 #endif
171 //!@endcond
172 
173 /*! \def RAPIDJSON_STATIC_ASSERT
174  \brief (internal) macro to check for conditions at compile-time
175  \param x compile-time condition
176  \hideinitializer
177  */
178 #define RAPIDJSON_STATIC_ASSERT(x) typedef ::rapidjson::StaticAssertTest<\
179  sizeof(::rapidjson::STATIC_ASSERTION_FAILURE<bool(x) >)>\
180  RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) RAPIDJSON_STATIC_ASSERT_UNUSED_ATTRIBUTE
181 #endif
182 
183 ///////////////////////////////////////////////////////////////////////////////
184 // Helpers
185 
186 //!@cond RAPIDJSON_HIDDEN_FROM_DOXYGEN
187 
188 #define RAPIDJSON_MULTILINEMACRO_BEGIN do {
189 #define RAPIDJSON_MULTILINEMACRO_END \
190 } while((void)0, 0)
191 
192 // adopted from Boost
193 #define RAPIDJSON_VERSION_CODE(x,y,z) \
194  (((x)*100000) + ((y)*100) + (z))
195 
196 // token stringification
197 #define RAPIDJSON_STRINGIFY(x) RAPIDJSON_DO_STRINGIFY(x)
198 #define RAPIDJSON_DO_STRINGIFY(x) #x
199 
200 ///////////////////////////////////////////////////////////////////////////////
201 // RAPIDJSON_DIAG_PUSH/POP, RAPIDJSON_DIAG_OFF
202 
203 #if defined(__clang__) || (defined(__GNUC__) && RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) >= RAPIDJSON_VERSION_CODE(4,2,0))
204 
205 #define RAPIDJSON_PRAGMA(x) _Pragma(RAPIDJSON_STRINGIFY(x))
206 #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(GCC diagnostic x)
207 #define RAPIDJSON_DIAG_OFF(x) \
208  RAPIDJSON_DIAG_PRAGMA(ignored RAPIDJSON_STRINGIFY(RAPIDJSON_JOIN(-W,x)))
209 
210 // push/pop support in Clang and GCC>=4.6
211 #if defined(__clang__) || (defined(__GNUC__) && RAPIDJSON_VERSION_CODE(__GNUC__,__GNUC_MINOR__,__GNUC_PATCHLEVEL__) >= RAPIDJSON_VERSION_CODE(4,6,0))
212 #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
213 #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
214 #else // GCC >= 4.2, < 4.6
215 #define RAPIDJSON_DIAG_PUSH /* ignored */
216 #define RAPIDJSON_DIAG_POP /* ignored */
217 #endif
218 
219 #elif defined(_MSC_VER)
220 
221 // pragma (MSVC specific)
222 #define RAPIDJSON_PRAGMA(x) __pragma(x)
223 #define RAPIDJSON_DIAG_PRAGMA(x) RAPIDJSON_PRAGMA(warning(x))
224 
225 #define RAPIDJSON_DIAG_OFF(x) RAPIDJSON_DIAG_PRAGMA(disable: x)
226 #define RAPIDJSON_DIAG_PUSH RAPIDJSON_DIAG_PRAGMA(push)
227 #define RAPIDJSON_DIAG_POP RAPIDJSON_DIAG_PRAGMA(pop)
228 
229 #else
230 
231 #define RAPIDJSON_DIAG_OFF(x) /* ignored */
232 #define RAPIDJSON_DIAG_PUSH /* ignored */
233 #define RAPIDJSON_DIAG_POP /* ignored */
234 
235 #endif // RAPIDJSON_DIAG_*
236 
237 //!@endcond
238 
239 ///////////////////////////////////////////////////////////////////////////////
240 // Allocators and Encodings
241 
242 #include "allocators.h"
243 #include "encodings.h"
244 
245 //! main RapidJSON namespace
246 namespace rapidjson {
247 
248 ///////////////////////////////////////////////////////////////////////////////
249 // Stream
250 
251 /*! \class rapidjson::Stream
252  \brief Concept for reading and writing characters.
253 
254  For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd().
255 
256  For write-only stream, only need to implement Put() and Flush().
257 
258 \code
259 concept Stream {
260  typename Ch; //!< Character type of the stream.
261 
262  //! Read the current character from stream without moving the read cursor.
263  Ch Peek() const;
264 
265  //! Read the current character from stream and moving the read cursor to next character.
266  Ch Take();
267 
268  //! Get the current read cursor.
269  //! \return Number of characters read from start.
270  size_t Tell();
271 
272  //! Begin writing operation at the current read pointer.
273  //! \return The begin writer pointer.
274  Ch* PutBegin();
275 
276  //! Write a character.
277  void Put(Ch c);
278 
279  //! Flush the buffer.
280  void Flush();
281 
282  //! End the writing operation.
283  //! \param begin The begin write pointer returned by PutBegin().
284  //! \return Number of characters written.
285  size_t PutEnd(Ch* begin);
286 }
287 \endcode
288 */
289 
290 //! Provides additional information for stream.
291 /*!
292  By using traits pattern, this type provides a default configuration for stream.
293  For custom stream, this type can be specialized for other configuration.
294  See TEST(Reader, CustomStringStream) in readertest.cpp for example.
295 */
296 template<typename Stream>
297 struct StreamTraits {
298  //! Whether to make local copy of stream for optimization during parsing.
299  /*!
300  By default, for safety, streams do not use local copy optimization.
301  Stream that can be copied fast should specialize this, like StreamTraits<StringStream>.
302  */
303  enum { copyOptimization = 0 };
304 };
305 
306 //! Put N copies of a character to a stream.
307 template<typename Stream, typename Ch>
308 inline void PutN(Stream& stream, Ch c, size_t n) {
309  for (size_t i = 0; i < n; i++)
310  stream.Put(c);
311 }
312 
313 ///////////////////////////////////////////////////////////////////////////////
314 // StringStream
315 
316 //! Read-only string stream.
317 /*! \note implements Stream concept
318 */
319 template <typename Encoding>
321  typedef typename Encoding::Ch Ch;
322 
323  GenericStringStream(const Ch *src) : src_(src), head_(src) {}
324 
325  Ch Peek() const { return *src_; }
326  Ch Take() { return *src_++; }
327  size_t Tell() const { return static_cast<size_t>(src_ - head_); }
328 
329  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
330  void Put(Ch) { RAPIDJSON_ASSERT(false); }
331  void Flush() { RAPIDJSON_ASSERT(false); }
332  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
333 
334  const Ch* src_; //!< Current read position.
335  const Ch* head_; //!< Original head of the string.
336 };
337 
338 template <typename Encoding>
340  enum { copyOptimization = 1 };
341 };
342 
343 //! String stream with UTF8 encoding.
345 
346 ///////////////////////////////////////////////////////////////////////////////
347 // InsituStringStream
348 
349 //! A read-write string stream.
350 /*! This string stream is particularly designed for in-situ parsing.
351  \note implements Stream concept
352 */
353 template <typename Encoding>
355  typedef typename Encoding::Ch Ch;
356 
357  GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {}
358 
359  // Read
360  Ch Peek() { return *src_; }
361  Ch Take() { return *src_++; }
362  size_t Tell() { return static_cast<size_t>(src_ - head_); }
363 
364  // Write
365  void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; }
366 
367  Ch* PutBegin() { return dst_ = src_; }
368  size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); }
369  void Flush() {}
370 
371  Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; }
372  void Pop(size_t count) { dst_ -= count; }
373 
374  Ch* src_;
375  Ch* dst_;
376  Ch* head_;
377 };
378 
379 template <typename Encoding>
381  enum { copyOptimization = 1 };
382 };
383 
384 //! Insitu string stream with UTF8 encoding.
386 
387 ///////////////////////////////////////////////////////////////////////////////
388 // Type
389 
390 //! Type of JSON value
391 enum Type {
392  kNullType = 0, //!< null
393  kFalseType = 1, //!< false
394  kTrueType = 2, //!< true
395  kObjectType = 3, //!< object
396  kArrayType = 4, //!< array
397  kStringType = 5, //!< string
398  kNumberType = 6 //!< number
399 };
400 
401 } // namespace rapidjson
402 
403 #endif // RAPIDJSON_RAPIDJSON_H_
true
Definition: rapidjson.h:394
Read-only string stream.
Definition: rapidjson.h:320
unsigned SizeType
Use 32-bit array/string indices even for 64-bit platform, instead of using size_t.
Definition: rapidjson.h:133
false
Definition: rapidjson.h:393
const Ch * head_
Original head of the string.
Definition: rapidjson.h:335
const Ch * src_
Current read position.
Definition: rapidjson.h:334
GenericInsituStringStream< UTF8<> > InsituStringStream
Insitu string stream with UTF8 encoding.
Definition: rapidjson.h:385
Concept for encoding of Unicode characters.
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:146
Type
Type of JSON value.
Definition: rapidjson.h:391
object
Definition: rapidjson.h:395
GenericStringStream< UTF8<> > StringStream
String stream with UTF8 encoding.
Definition: rapidjson.h:344
array
Definition: rapidjson.h:396
void PutN(FileWriteStream &stream, char c, size_t n)
Implement specialized version of PutN() with memset() for better performance.
Definition: filewritestream.h:71
null
Definition: rapidjson.h:392
Concept for reading and writing characters.
string
Definition: rapidjson.h:397
number
Definition: rapidjson.h:398
Provides additional information for stream.
Definition: rapidjson.h:297
A read-write string stream.
Definition: rapidjson.h:354