stream.h Source File

stream.h Source File#

Composable Kernel: stream.h Source File
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.
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 Ch Peek() const;
40
42 Ch Take();
43
46 size_t Tell();
47
50 Ch* PutBegin();
51
53 void Put(Ch c);
54
56 void Flush();
57
61 size_t PutEnd(Ch* begin);
62}
63\endcode
64*/
65
67
72template <typename Stream>
74{
76
80 enum
81 {
83 };
84};
85
87template <typename Stream>
88inline void PutReserve(Stream& stream, size_t count)
89{
90 (void)stream;
91 (void)count;
92}
93
95template <typename Stream>
96inline void PutUnsafe(Stream& stream, typename Stream::Ch c)
97{
98 stream.Put(c);
99}
100
102template <typename Stream, typename Ch>
103inline 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
118
119#if defined(_MSC_VER) && _MSC_VER <= 1800
120RAPIDJSON_DIAG_PUSH
121RAPIDJSON_DIAG_OFF(4702) // unreachable code
122RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
123#endif
124
125template <typename InputStream, typename Encoding = UTF8<>>
127{
128 public:
129 typedef typename Encoding::Ch Ch;
130 GenericStreamWrapper(InputStream& is) : is_(is) {}
131
132 Ch Peek() const { return is_.Peek(); }
133 Ch Take() { return is_.Take(); }
134 size_t Tell() { return is_.Tell(); }
135 Ch* PutBegin() { return is_.PutBegin(); }
136 void Put(Ch ch) { is_.Put(ch); }
137 void Flush() { is_.Flush(); }
138 size_t PutEnd(Ch* ch) { return is_.PutEnd(ch); }
139
140 // wrapper for MemoryStream
141 const Ch* Peek4() const { return is_.Peek4(); }
142
143 // wrapper for AutoUTFInputStream
144 UTFType GetType() const { return is_.GetType(); }
145 bool HasBOM() const { return is_.HasBOM(); }
146
147 protected:
148 InputStream& is_;
149};
150
151#if defined(_MSC_VER) && _MSC_VER <= 1800
152RAPIDJSON_DIAG_POP
153#endif
154
156// StringStream
157
159
161template <typename Encoding>
163{
164 typedef typename Encoding::Ch Ch;
165
166 GenericStringStream(const Ch* src) : src_(src), head_(src) {}
167
168 Ch Peek() const { return *src_; }
169 Ch Take() { return *src_++; }
170 size_t Tell() const { return static_cast<size_t>(src_ - head_); }
171
173 {
174 RAPIDJSON_ASSERT(false);
175 return 0;
176 }
177 void Put(Ch) { RAPIDJSON_ASSERT(false); }
178 void Flush() { RAPIDJSON_ASSERT(false); }
179 size_t PutEnd(Ch*)
180 {
181 RAPIDJSON_ASSERT(false);
182 return 0;
183 }
184
185 const Ch* src_;
186 const Ch* head_;
187};
188
189template <typename Encoding>
191{
192 enum
193 {
195 };
196};
197
200
202// InsituStringStream
203
205
208template <typename Encoding>
210{
211 typedef typename Encoding::Ch Ch;
212
213 GenericInsituStringStream(Ch* src) : src_(src), dst_(0), head_(src) {}
214
215 // Read
216 Ch Peek() { return *src_; }
217 Ch Take() { return *src_++; }
218 size_t Tell() { return static_cast<size_t>(src_ - head_); }
219
220 // Write
221 void Put(Ch c)
222 {
224 *dst_++ = c;
225 }
226
227 Ch* PutBegin() { return dst_ = src_; }
228 size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); }
229 void Flush() {}
230
231 Ch* Push(size_t count)
232 {
233 Ch* begin = dst_;
234 dst_ += count;
235 return begin;
236 }
237 void Pop(size_t count) { dst_ -= count; }
238
242};
243
244template <typename Encoding>
246{
247 enum
248 {
250 };
251};
252
255
257
258#endif // RAPIDJSON_STREAM_H_
Ch * PutBegin()
Definition stream.h:135
Ch Take()
Definition stream.h:133
void Flush()
Definition stream.h:137
InputStream & is_
Definition stream.h:148
void Put(Ch ch)
Definition stream.h:136
size_t Tell()
Definition stream.h:134
bool HasBOM() const
Definition stream.h:145
UTFType GetType() const
Definition stream.h:144
GenericStreamWrapper(InputStream &is)
Definition stream.h:130
size_t PutEnd(Ch *ch)
Definition stream.h:138
Ch Peek() const
Definition stream.h:132
Encoding::Ch Ch
Definition stream.h:129
const Ch * Peek4() const
Definition stream.h:141
Concept for encoding of Unicode characters.
Concept for reading and writing characters.
UTFType
Runtime-specified UTF encoding type of a stream.
Definition encodings.h:757
GenericStringStream< UTF8< char > > StringStream
Definition fwd.h:58
GenericInsituStringStream< UTF8< char > > InsituStringStream
Definition fwd.h:63
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition rapidjson.h:451
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition rapidjson.h:121
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition rapidjson.h:124
common definitions and configuration
void PutReserve(Stream &stream, size_t count)
Reserve n characters for writing to a stream.
Definition stream.h:88
void PutN(Stream &stream, Ch c, size_t n)
Put N copies of a character to a stream.
Definition stream.h:103
void PutUnsafe(Stream &stream, typename Stream::Ch c)
Write character to a stream, presuming buffer is reserved.
Definition stream.h:96
A read-write string stream.
Definition stream.h:210
UTF8< char >::Ch Ch
Definition stream.h:211
void Flush()
Definition stream.h:229
void Put(Ch c)
Definition stream.h:221
Ch * PutBegin()
Definition stream.h:227
size_t PutEnd(Ch *begin)
Definition stream.h:228
size_t Tell()
Definition stream.h:218
Ch * dst_
Definition stream.h:240
void Pop(size_t count)
Definition stream.h:237
GenericInsituStringStream(Ch *src)
Definition stream.h:213
Ch Peek()
Definition stream.h:216
Ch * Push(size_t count)
Definition stream.h:231
Ch * src_
Definition stream.h:239
Ch * head_
Definition stream.h:241
Ch Take()
Definition stream.h:217
Read-only string stream.
Definition stream.h:163
size_t PutEnd(Ch *)
Definition stream.h:179
Ch Peek() const
Definition stream.h:168
Ch Take()
Definition stream.h:169
const Ch * head_
Definition stream.h:186
UTF8< char >::Ch Ch
Definition stream.h:164
Ch * PutBegin()
Definition stream.h:172
void Flush()
Definition stream.h:178
GenericStringStream(const Ch *src)
Definition stream.h:166
void Put(Ch)
Definition stream.h:177
size_t Tell() const
Definition stream.h:170
const Ch * src_
Definition stream.h:185
Provides additional information for stream.
Definition stream.h:74
@ copyOptimization
Definition stream.h:82