| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
1 |
// Tencent is pleased to support the open source community by making RapidJSON available. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
2 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
3 |
// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
4 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
5 |
// Licensed under the MIT License (the "License"); you may not use this file except |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
6 |
// in compliance with the License. You may obtain a copy of the License at |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
7 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
8 |
// http://opensource.org/licenses/MIT |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
9 |
// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
10 |
// Unless required by applicable law or agreed to in writing, software distributed |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
11 |
// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
12 |
// CONDITIONS OF ANY KIND, either express or implied. See the License for the |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
13 |
// specific language governing permissions and limitations under the License. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
14 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
15 |
#include "rapidjson.h" |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
16 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
17 |
#ifndef CEREAL_RAPIDJSON_STREAM_H_ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
18 |
#define CEREAL_RAPIDJSON_STREAM_H_ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
19 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
20 |
#include "encodings.h" |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
21 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
22 |
CEREAL_RAPIDJSON_NAMESPACE_BEGIN |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
23 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
24 |
/////////////////////////////////////////////////////////////////////////////// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
25 |
// Stream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
26 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
27 |
/*! \class rapidjson::Stream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
28 |
\brief Concept for reading and writing characters. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
29 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
30 |
For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd(). |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
31 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
32 |
For write-only stream, only need to implement Put() and Flush(). |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
33 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
34 |
\code |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
35 |
concept Stream { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
36 |
typename Ch; //!< Character type of the stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
37 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
38 |
//! Read the current character from stream without moving the read cursor. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
39 |
Ch Peek() const; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
40 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
41 |
//! Read the current character from stream and moving the read cursor to next character. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
42 |
Ch Take(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
43 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
44 |
//! Get the current read cursor. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
45 |
//! \return Number of characters read from start. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
46 |
size_t Tell(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
47 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
48 |
//! Begin writing operation at the current read pointer. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
49 |
//! \return The begin writer pointer. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
50 |
Ch* PutBegin(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
51 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
52 |
//! Write a character. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
53 |
void Put(Ch c); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
54 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
55 |
//! Flush the buffer. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
56 |
void Flush(); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
57 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
58 |
//! End the writing operation. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
59 |
//! \param begin The begin write pointer returned by PutBegin(). |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
60 |
//! \return Number of characters written. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
61 |
size_t PutEnd(Ch* begin); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
62 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
63 |
\endcode |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
64 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
65 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
66 |
//! Provides additional information for stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
67 |
/*! |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
68 |
By using traits pattern, this type provides a default configuration for stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
69 |
For custom stream, this type can be specialized for other configuration. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
70 |
See TEST(Reader, CustomStringStream) in readertest.cpp for example. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
71 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
72 |
template<typename Stream> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
73 |
struct StreamTraits { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
74 |
//! Whether to make local copy of stream for optimization during parsing. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
75 |
/*! |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
76 |
By default, for safety, streams do not use local copy optimization. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
77 |
Stream that can be copied fast should specialize this, like StreamTraits<StringStream>. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
78 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
79 |
enum { copyOptimization = 0 }; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
80 |
}; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
81 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
82 |
//! Reserve n characters for writing to a stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
83 |
template<typename Stream> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
84 |
inline void PutReserve(Stream& stream, size_t count) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
85 |
(void)stream; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
86 |
(void)count; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
87 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
88 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
89 |
//! Write character to a stream, presuming buffer is reserved. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
90 |
template<typename Stream> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
91 |
inline void PutUnsafe(Stream& stream, typename Stream::Ch c) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
92 |
stream.Put(c); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
93 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
94 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
95 |
//! Put N copies of a character to a stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
96 |
template<typename Stream, typename Ch> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
97 |
inline void PutN(Stream& stream, Ch c, size_t n) { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
98 |
PutReserve(stream, n); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
99 |
for (size_t i = 0; i < n; i++) |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
100 |
PutUnsafe(stream, c); |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
101 |
} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
102 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
103 |
/////////////////////////////////////////////////////////////////////////////// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
104 |
// StringStream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
105 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
106 |
//! Read-only string stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
107 |
/*! \note implements Stream concept |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
108 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
109 |
template <typename Encoding> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
110 |
struct GenericStringStream { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
111 |
typedef typename Encoding::Ch Ch; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
112 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
113 |
GenericStringStream(const Ch *src) : src_(src), head_(src) {} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
114 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
115 |
Ch Peek() const { return *src_; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
116 |
Ch Take() { return *src_++; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
117 |
size_t Tell() const { return static_cast<size_t>(src_ - head_); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
118 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
119 |
Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
120 |
void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
121 |
void Flush() { CEREAL_RAPIDJSON_ASSERT(false); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
122 |
size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
123 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
124 |
const Ch* src_; //!< Current read position. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
125 |
const Ch* head_; //!< Original head of the string. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
126 |
}; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
127 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
128 |
template <typename Encoding> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
129 |
struct StreamTraits<GenericStringStream<Encoding> > { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
130 |
enum { copyOptimization = 1 }; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
131 |
}; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
132 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
133 |
//! String stream with UTF8 encoding. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
134 |
typedef GenericStringStream<UTF8<> > StringStream; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
135 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
136 |
/////////////////////////////////////////////////////////////////////////////// |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
137 |
// InsituStringStream |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
138 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
139 |
//! A read-write string stream. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
140 |
/*! This string stream is particularly designed for in-situ parsing. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
141 |
\note implements Stream concept |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
142 |
*/ |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
143 |
template <typename Encoding> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
144 |
struct GenericInsituStringStream { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
145 |
typedef typename Encoding::Ch Ch; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
146 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
147 |
GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
148 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
149 |
// Read |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
150 |
Ch Peek() { return *src_; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
151 |
Ch Take() { return *src_++; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
152 |
size_t Tell() { return static_cast<size_t>(src_ - head_); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
153 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
154 |
// Write |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
155 |
void Put(Ch c) { CEREAL_RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
156 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
157 |
Ch* PutBegin() { return dst_ = src_; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
158 |
size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
159 |
void Flush() {} |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
160 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
161 |
Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
162 |
void Pop(size_t count) { dst_ -= count; } |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
163 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
164 |
Ch* src_; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
165 |
Ch* dst_; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
166 |
Ch* head_; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
167 |
}; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
168 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
169 |
template <typename Encoding> |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
170 |
struct StreamTraits<GenericInsituStringStream<Encoding> > { |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
171 |
enum { copyOptimization = 1 }; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
172 |
}; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
173 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
174 |
//! Insitu string stream with UTF8 encoding. |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
175 |
typedef GenericInsituStringStream<UTF8<> > InsituStringStream; |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
176 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
177 |
CEREAL_RAPIDJSON_NAMESPACE_END |
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
178 |
|
| 6c5f2c01 |
sago007 |
2017-03-15 17:56 |
179 |
#endif // CEREAL_RAPIDJSON_STREAM_H_ |
|
|
1970-01-01 00:00 |
180 |
|