rapidjson.h
Engine/source/persistence/rapidjson/rapidjson.h
Classes:
class
A read-write string stream.
class
Read-only string stream.
Namespaces:
namespace
Public Defines
define
RAPIDJSON_ALIGN(x) ((x + 3) & ~3)
Data alignment of the machine.
define
RAPIDJSON_ASSERT(x) assert(x)
Assertion.
define
Big endian machine.
define
RAPIDJSON_DO_JOIN(X, Y) (X, Y)
define
RAPIDJSON_DO_JOIN2(X, Y) X##Y
define
Endianness of the machine.
define
define
RAPIDJSON_JOIN(X, Y) (X, Y)
define
Little endian machine.
define
define
RAPIDJSON_MULTILINEMACRO_END() } (()0, 0)
define
RAPIDJSON_STATIC_ASSERT(x) typedef ::<\ sizeof(::<bool(x) >)>\ (StaticAssertTypedef, __LINE__)
Detailed Description
Public Defines
RAPIDJSON_ALIGN(x) ((x + 3) & ~3)
Data alignment of the machine.
Some machine requires strict data alignment. Currently the default uses 4 bytes alignment. User can customize this.
RAPIDJSON_ASSERT(x) assert(x)
Assertion.
By default, rapidjson uses C assert() for assertion. User can override it by defining RAPIDJSON_ASSERT(x) macro.
RAPIDJSON_BIGENDIAN() 1
Big endian machine.
RAPIDJSON_DO_JOIN(X, Y) (X, Y)
RAPIDJSON_DO_JOIN2(X, Y) X##Y
RAPIDJSON_ENDIAN()
Endianness of the machine.
GCC provided macro for detecting endianness of the target machine. But other compilers may not have this. User can define RAPIDJSON_ENDIAN to either RAPIDJSON_LITTLEENDIAN or RAPIDJSON_BIGENDIAN.
RAPIDJSON_FORCEINLINE()
RAPIDJSON_JOIN(X, Y) (X, Y)
RAPIDJSON_LITTLEENDIAN() 0
Little endian machine.
RAPIDJSON_MULTILINEMACRO_BEGIN() do {
RAPIDJSON_MULTILINEMACRO_END() } (()0, 0)
RAPIDJSON_STATIC_ASSERT(x) typedef ::<\ sizeof(::<bool(x) >)>\ (StaticAssertTypedef, __LINE__)
1 2#ifndef RAPIDJSON_RAPIDJSON_H_ 3#define RAPIDJSON_RAPIDJSON_H_ 4 5// Copyright (c) 2011 Milo Yip (miloyip@gmail.com) 6// Version 0.1 7 8#include <cstdlib> // malloc(), realloc(), free() 9#include <cstring> // memcpy() 10 11/////////////////////////////////////////////////////////////////////////////// 12// RAPIDJSON_NO_INT64DEFINE 13 14// Here defines int64_t and uint64_t types in global namespace. 15// If user have their own definition, can define RAPIDJSON_NO_INT64DEFINE to disable this. 16#ifndef RAPIDJSON_NO_INT64DEFINE 17#ifdef _MSC_VER 18typedef __int64 int64_t; 19typedef unsigned __int64 uint64_t; 20#define RAPIDJSON_FORCEINLINE __forceinline 21#else 22#include <inttypes.h> 23#define RAPIDJSON_FORCEINLINE 24#endif 25#endif // RAPIDJSON_NO_INT64TYPEDEF 26 27/////////////////////////////////////////////////////////////////////////////// 28// RAPIDJSON_ENDIAN 29#define RAPIDJSON_LITTLEENDIAN 0 //!< Little endian machine 30#define RAPIDJSON_BIGENDIAN 1 //!< Big endian machine 31 32//! Endianness of the machine. 33/*! GCC provided macro for detecting endianness of the target machine. But other 34 compilers may not have this. User can define RAPIDJSON_ENDIAN to either 35 RAPIDJSON_LITTLEENDIAN or RAPIDJSON_BIGENDIAN. 36*/ 37#ifndef RAPIDJSON_ENDIAN 38#ifdef __BYTE_ORDER__ 39#if __BYTE_ORDER__ == __ORDER_LITTLE_ENDIAN__ 40#define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN 41#else 42#define RAPIDJSON_ENDIAN RAPIDJSON_BIGENDIAN 43#endif // __BYTE_ORDER__ 44#else 45#define RAPIDJSON_ENDIAN RAPIDJSON_LITTLEENDIAN // Assumes little endian otherwise. 46#endif 47#endif // RAPIDJSON_ENDIAN 48 49 50/////////////////////////////////////////////////////////////////////////////// 51// RAPIDJSON_ALIGNSIZE 52 53//! Data alignment of the machine. 54/*! 55 Some machine requires strict data alignment. 56 Currently the default uses 4 bytes alignment. User can customize this. 57*/ 58#ifndef RAPIDJSON_ALIGN 59#define RAPIDJSON_ALIGN(x) ((x + 3) & ~3) 60#endif 61 62/////////////////////////////////////////////////////////////////////////////// 63// RAPIDJSON_SSE2/RAPIDJSON_SSE42/RAPIDJSON_SIMD 64 65// Enable SSE2 optimization. 66//#define RAPIDJSON_SSE2 67 68// Enable SSE4.2 optimization. 69//#define RAPIDJSON_SSE42 70 71#if defined(RAPIDJSON_SSE2) || defined(RAPIDJSON_SSE42) 72#define RAPIDJSON_SIMD 73#endif 74 75/////////////////////////////////////////////////////////////////////////////// 76// RAPIDJSON_NO_SIZETYPEDEFINE 77 78#ifndef RAPIDJSON_NO_SIZETYPEDEFINE 79namespace rapidjson { 80//! Use 32-bit array/string indices even for 64-bit platform, instead of using size_t. 81/*! User may override the SizeType by defining RAPIDJSON_NO_SIZETYPEDEFINE. 82*/ 83typedef unsigned SizeType; 84} // namespace rapidjson 85#endif 86 87/////////////////////////////////////////////////////////////////////////////// 88// RAPIDJSON_ASSERT 89 90//! Assertion. 91/*! By default, rapidjson uses C assert() for assertion. 92 User can override it by defining RAPIDJSON_ASSERT(x) macro. 93*/ 94#ifndef RAPIDJSON_ASSERT 95#include <cassert> 96#define RAPIDJSON_ASSERT(x) assert(x) 97#endif // RAPIDJSON_ASSERT 98 99/////////////////////////////////////////////////////////////////////////////// 100// RAPIDJSON_STATIC_ASSERT 101 102// Adopt from boost 103#ifndef RAPIDJSON_STATIC_ASSERT 104namespace rapidjson { 105template <bool x> struct STATIC_ASSERTION_FAILURE; 106template <> struct STATIC_ASSERTION_FAILURE<true> { enum { value = 1 }; }; 107template<int x> struct StaticAssertTest {}; 108} // namespace rapidjson 109 110#define RAPIDJSON_JOIN(X, Y) RAPIDJSON_DO_JOIN(X, Y) 111#define RAPIDJSON_DO_JOIN(X, Y) RAPIDJSON_DO_JOIN2(X, Y) 112#define RAPIDJSON_DO_JOIN2(X, Y) X##Y 113 114#define RAPIDJSON_STATIC_ASSERT(x) typedef ::rapidjson::StaticAssertTest<\ 115 sizeof(::rapidjson::STATIC_ASSERTION_FAILURE<bool(x) >)>\ 116 RAPIDJSON_JOIN(StaticAssertTypedef, __LINE__) 117#endif 118 119/////////////////////////////////////////////////////////////////////////////// 120// Helpers 121 122#define RAPIDJSON_MULTILINEMACRO_BEGIN do { 123#define RAPIDJSON_MULTILINEMACRO_END \ 124} while((void)0, 0) 125 126/////////////////////////////////////////////////////////////////////////////// 127// Allocators and Encodings 128 129#include "allocators.h" 130#include "encodings.h" 131 132namespace rapidjson { 133 134/////////////////////////////////////////////////////////////////////////////// 135// Stream 136 137/*! \class rapidjson::Stream 138 \brief Concept for reading and writing characters. 139 140 For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd(). 141 142 For write-only stream, only need to implement Put() and Flush(). 143 144\code 145concept Stream { 146 typename Ch; //!< Character type of the stream. 147 148 //! Read the current character from stream without moving the read cursor. 149 Ch Peek() const; 150 151 //! Read the current character from stream and moving the read cursor to next character. 152 Ch Take(); 153 154 //! Get the current read cursor. 155 //! \return Number of characters read from start. 156 size_t Tell(); 157 158 //! Begin writing operation at the current read pointer. 159 //! \return The begin writer pointer. 160 Ch* PutBegin(); 161 162 //! Write a character. 163 void Put(Ch c); 164 165 //! Flush the buffer. 166 void Flush(); 167 168 //! End the writing operation. 169 //! \param begin The begin write pointer returned by PutBegin(). 170 //! \return Number of characters written. 171 size_t PutEnd(Ch* begin); 172} 173\endcode 174*/ 175 176//! Put N copies of a character to a stream. 177template<typename Stream, typename Ch> 178inline void PutN(Stream& stream, Ch c, size_t n) { 179 for (size_t i = 0; i < n; i++) 180 stream.Put(c); 181} 182 183/////////////////////////////////////////////////////////////////////////////// 184// StringStream 185 186//! Read-only string stream. 187/*! \implements Stream 188*/ 189template <typename Encoding> 190struct GenericStringStream { 191 typedef typename Encoding::Ch Ch; 192 193 GenericStringStream(const Ch *src) : src_(src), head_(src) {} 194 195 Ch Peek() const { return *src_; } 196 Ch Take() { return *src_++; } 197 size_t Tell() const { return src_ - head_; } 198 199 Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 200 void Put(Ch) { RAPIDJSON_ASSERT(false); } 201 void Flush() { RAPIDJSON_ASSERT(false); } 202 size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 203 204 const Ch* src_; //!< Current read position. 205 const Ch* head_; //!< Original head of the string. 206}; 207 208typedef GenericStringStream<UTF8<> > StringStream; 209 210/////////////////////////////////////////////////////////////////////////////// 211// InsituStringStream 212 213//! A read-write string stream. 214/*! This string stream is particularly designed for in-situ parsing. 215 \implements Stream 216*/ 217template <typename Encoding> 218struct GenericInsituStringStream { 219 typedef typename Encoding::Ch Ch; 220 221 GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {} 222 223 // Read 224 Ch Peek() { return *src_; } 225 Ch Take() { return *src_++; } 226 size_t Tell() { return src_ - head_; } 227 228 // Write 229 Ch* PutBegin() { return dst_ = src_; } 230 void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; } 231 void Flush() {} 232 size_t PutEnd(Ch* begin) { return dst_ - begin; } 233 234 Ch* src_; 235 Ch* dst_; 236 Ch* head_; 237}; 238 239typedef GenericInsituStringStream<UTF8<> > InsituStringStream; 240 241/////////////////////////////////////////////////////////////////////////////// 242// Type 243 244//! Type of JSON value 245enum Type { 246 kNullType = 0, //!< null 247 kFalseType = 1, //!< false 248 kTrueType = 2, //!< true 249 kObjectType = 3, //!< object 250 kArrayType = 4, //!< array 251 kStringType = 5, //!< string 252 kNumberType = 6, //!< number 253}; 254 255} // namespace rapidjson 256 257#endif // RAPIDJSON_RAPIDJSON_H_ 258
