stream.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 #include "rapidjson.h"
16 
17 #ifndef RAPIDJSON_STREAM_H_
18 #define RAPIDJSON_STREAM_H_
19 
20 #include "encodings.h"
21 
23 
25 // Stream
26 
39 
42 
46 
50 
53 
56 
61 
66 
72 template <typename Stream>
74 {
76 
80  enum
81  {
83  };
84 };
85 
87 template <typename Stream>
88 inline void PutReserve(Stream& stream, size_t count)
89 {
90  (void)stream;
91  (void)count;
92 }
93 
95 template <typename Stream>
96 inline void PutUnsafe(Stream& stream, typename Stream::Ch c)
97 {
98  stream.Put(c);
99 }
100 
102 template <typename Stream, typename Ch>
103 inline void PutN(Stream& stream, Ch c, size_t n)
104 {
105  PutReserve(stream, n);
106  for (size_t i = 0; i < n; i++)
107  PutUnsafe(stream, c);
108 }
109 
111 // GenericStreamWrapper
112 
114 
119 #if defined(_MSC_VER) && _MSC_VER <= 1800
120 RAPIDJSON_DIAG_PUSH
121 RAPIDJSON_DIAG_OFF(4702) // unreachable code
122 RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
123 #endif
124 
125 template <typename InputStream, typename Encoding = UTF8<> >
127 {
128 public:
129  typedef typename Encoding::Ch Ch;
130  GenericStreamWrapper(InputStream& is) : is_(is)
131  {
132  }
133 
134  Ch Peek() const
135  {
136  return is_.Peek();
137  }
138  Ch Take()
139  {
140  return is_.Take();
141  }
142  size_t Tell()
143  {
144  return is_.Tell();
145  }
146  Ch* PutBegin()
147  {
148  return is_.PutBegin();
149  }
150  void Put(Ch ch)
151  {
152  is_.Put(ch);
153  }
154  void Flush()
155  {
156  is_.Flush();
157  }
158  size_t PutEnd(Ch* ch)
159  {
160  return is_.PutEnd(ch);
161  }
162 
163  // wrapper for MemoryStream
164  const Ch* Peek4() const
165  {
166  return is_.Peek4();
167  }
168 
169  // wrapper for AutoUTFInputStream
170  UTFType GetType() const
171  {
172  return is_.GetType();
173  }
174  bool HasBOM() const
175  {
176  return is_.HasBOM();
177  }
178 
179 protected:
180  InputStream& is_;
181 };
182 
183 #if defined(_MSC_VER) && _MSC_VER <= 1800
184 RAPIDJSON_DIAG_POP
185 #endif
186 
188 // StringStream
189 
191 
193 template <typename Encoding>
194 struct GenericStringStream
195 {
196  typedef typename Encoding::Ch Ch;
197 
198  GenericStringStream(const Ch* src) : src_(src), head_(src)
199  {
200  }
201 
202  Ch Peek() const
203  {
204  return *src_;
205  }
206  Ch Take()
207  {
208  return *src_++;
209  }
210  size_t Tell() const
211  {
212  return static_cast<size_t>(src_ - head_);
213  }
214 
215  Ch* PutBegin()
216  {
217  RAPIDJSON_ASSERT(false);
218  return 0;
219  }
220  void Put(Ch)
221  {
222  RAPIDJSON_ASSERT(false);
223  }
224  void Flush()
225  {
226  RAPIDJSON_ASSERT(false);
227  }
228  size_t PutEnd(Ch*)
229  {
230  RAPIDJSON_ASSERT(false);
231  return 0;
232  }
233 
234  const Ch* src_;
235  const Ch* head_;
236 };
237 
238 template <typename Encoding>
240 {
241  enum
242  {
244  };
245 };
246 
249 
251 // InsituStringStream
252 
254 
257 template <typename Encoding>
259 {
260  typedef typename Encoding::Ch Ch;
261 
262  GenericInsituStringStream(Ch* src) : src_(src), dst_(0), head_(src)
263  {
264  }
265 
266  // Read
267  Ch Peek()
268  {
269  return *src_;
270  }
271  Ch Take()
272  {
273  return *src_++;
274  }
275  size_t Tell()
276  {
277  return static_cast<size_t>(src_ - head_);
278  }
279 
280  // Write
281  void Put(Ch c)
282  {
283  RAPIDJSON_ASSERT(dst_ != 0);
284  *dst_++ = c;
285  }
286 
287  Ch* PutBegin()
288  {
289  return dst_ = src_;
290  }
291  size_t PutEnd(Ch* begin)
292  {
293  return static_cast<size_t>(dst_ - begin);
294  }
295  void Flush()
296  {
297  }
298 
299  Ch* Push(size_t count)
300  {
301  Ch* begin = dst_;
302  dst_ += count;
303  return begin;
304  }
305  void Pop(size_t count)
306  {
307  dst_ -= count;
308  }
309 
310  Ch* src_;
311  Ch* dst_;
312  Ch* head_;
313 };
314 
315 template <typename Encoding>
317 {
318  enum
319  {
321  };
322 };
323 
326 
328 
329 #endif // RAPIDJSON_STREAM_H_
GenericStringStream< UTF8<> > StringStream
String stream with UTF8 encoding.
Definition: stream.h:248
GenericInsituStringStream< UTF8<> > InsituStringStream
Insitu string stream with UTF8 encoding.
Definition: stream.h:325
Ch Peek() const
Definition: stream.h:134
UTFType
Runtime-specified UTF encoding type of a stream.
Definition: encodings.h:753
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:416
Read-only string stream.
Definition: fwd.h:56
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:126
GenericInsituStringStream(Ch *src)
Definition: stream.h:262
const Ch * src_
Current read position.
Definition: stream.h:234
bool HasBOM() const
Definition: stream.h:174
Ch Peek() const
Definition: stream.h:202
void Pop(size_t count)
Definition: stream.h:305
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
A read-write string stream.
Definition: fwd.h:61
GenericStringStream(const Ch *src)
Definition: stream.h:198
A Stream Wrapper.
Definition: stream.h:126
GenericStreamWrapper(InputStream &is)
Definition: stream.h:130
void PutUnsafe(Stream &stream, typename Stream::Ch c)
Write character to a stream, presuming buffer is reserved.
Definition: stream.h:96
size_t PutEnd(Ch *ch)
Definition: stream.h:158
Encoding::Ch Ch
Definition: stream.h:129
void PutReserve(Stream &stream, size_t count)
Reserve n characters for writing to a stream.
Definition: stream.h:88
Provides additional information for stream.
Definition: stream.h:73
size_t PutEnd(Ch *)
Definition: stream.h:228
InputStream & is_
Definition: stream.h:180
void PutN(Stream &stream, Ch c, size_t n)
Put N copies of a character to a stream.
Definition: stream.h:103
UTFType GetType() const
Definition: stream.h:170
common definitions and configuration
const Ch * Peek4() const
Definition: stream.h:164
size_t PutEnd(Ch *begin)
Definition: stream.h:291
Encoding::Ch Ch
Definition: stream.h:196
void Put(Ch)
Definition: stream.h:220
void Put(Ch ch)
Definition: stream.h:150
size_t Tell() const
Definition: stream.h:210
Ch * Push(size_t count)
Definition: stream.h:299
const Ch * head_
Original head of the string.
Definition: stream.h:235


xbot_talker
Author(s): wangxiaoyun
autogenerated on Sat Oct 10 2020 03:27:54