stream.h
Go to the documentation of this file.
1 // Tencent is pleased to support the open source community by making RapidJSON
2 // available.
3 //
4 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
5 // rights reserved.
6 //
7 // Licensed under the MIT License (the "License"); you may not use this file
8 // except in compliance with the License. You may obtain a copy of the License
9 // at
10 //
11 // http://opensource.org/licenses/MIT
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
16 // License for the specific language governing permissions and limitations under
17 // the License.
18 
19 #include "rapidjson.h"
20 
21 #ifndef RAPIDJSON_STREAM_H_
22 #define RAPIDJSON_STREAM_H_
23 
24 #include "encodings.h"
25 
27 
29 // Stream
30 
44 
47 
51 
55 
58 
61 
66 
71 
78 template <typename Stream>
79 struct StreamTraits {
81 
86  enum { copyOptimization = 0 };
87 };
88 
90 template <typename Stream>
91 inline void PutReserve(Stream &stream, size_t count) {
92  (void)stream;
93  (void)count;
94 }
95 
97 template <typename Stream>
98 inline void PutUnsafe(Stream &stream, typename Stream::Ch c) {
99  stream.Put(c);
100 }
101 
103 template <typename Stream, typename Ch>
104 inline void PutN(Stream &stream, Ch c, size_t n) {
105  PutReserve(stream, n);
106  for (size_t i = 0; i < n; i++) PutUnsafe(stream, c);
107 }
108 
110 // GenericStreamWrapper
111 
113 
118 #if defined(_MSC_VER) && _MSC_VER <= 1800
119 RAPIDJSON_DIAG_PUSH
120 RAPIDJSON_DIAG_OFF(4702) // unreachable code
121 RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated
122 #endif
123 
124 template <typename InputStream, typename Encoding = UTF8<>>
126  public:
127  typedef typename Encoding::Ch Ch;
128  GenericStreamWrapper(InputStream &is) : is_(is) {}
129 
130  Ch Peek() const { return is_.Peek(); }
131  Ch Take() { return is_.Take(); }
132  size_t Tell() { return is_.Tell(); }
133  Ch *PutBegin() { return is_.PutBegin(); }
134  void Put(Ch ch) { is_.Put(ch); }
135  void Flush() { is_.Flush(); }
136  size_t PutEnd(Ch *ch) { return is_.PutEnd(ch); }
137 
138  // wrapper for MemoryStream
139  const Ch *Peek4() const { return is_.Peek4(); }
140 
141  // wrapper for AutoUTFInputStream
142  UTFType GetType() const { return is_.GetType(); }
143  bool HasBOM() const { return is_.HasBOM(); }
144 
145  protected:
146  InputStream &is_;
147 };
148 
149 #if defined(_MSC_VER) && _MSC_VER <= 1800
150 RAPIDJSON_DIAG_POP
151 #endif
152 
154 // StringStream
155 
157 
159 template <typename Encoding>
160 struct GenericStringStream {
161  typedef typename Encoding::Ch Ch;
162 
163  GenericStringStream(const Ch *src) : src_(src), head_(src) {}
164 
165  Ch Peek() const { return *src_; }
166  Ch Take() { return *src_++; }
167  size_t Tell() const { return static_cast<size_t>(src_ - head_); }
168 
169  Ch *PutBegin() {
170  RAPIDJSON_ASSERT(false);
171  return 0;
172  }
173  void Put(Ch) { RAPIDJSON_ASSERT(false); }
174  void Flush() { RAPIDJSON_ASSERT(false); }
175  size_t PutEnd(Ch *) {
176  RAPIDJSON_ASSERT(false);
177  return 0;
178  }
179 
180  const Ch *src_;
181  const Ch *head_;
182 };
183 
184 template <typename Encoding>
186  enum { copyOptimization = 1 };
187 };
188 
191 
193 // InsituStringStream
194 
196 
199 template <typename Encoding>
201  typedef typename Encoding::Ch Ch;
202 
203  GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {}
204 
205  // Read
206  Ch Peek() { return *src_; }
207  Ch Take() { return *src_++; }
208  size_t Tell() { return static_cast<size_t>(src_ - head_); }
209 
210  // Write
211  void Put(Ch c) {
212  RAPIDJSON_ASSERT(dst_ != 0);
213  *dst_++ = c;
214  }
215 
216  Ch *PutBegin() { return dst_ = src_; }
217  size_t PutEnd(Ch *begin) { return static_cast<size_t>(dst_ - begin); }
218  void Flush() {}
219 
220  Ch *Push(size_t count) {
221  Ch *begin = dst_;
222  dst_ += count;
223  return begin;
224  }
225  void Pop(size_t count) { dst_ -= count; }
226 
227  Ch *src_;
228  Ch *dst_;
229  Ch *head_;
230 };
231 
232 template <typename Encoding>
234  enum { copyOptimization = 1 };
235 };
236 
239 
241 
242 #endif // RAPIDJSON_STREAM_H_
GenericStringStream< UTF8<> > StringStream
String stream with UTF8 encoding.
Definition: stream.h:190
GenericInsituStringStream< UTF8<> > InsituStringStream
Insitu string stream with UTF8 encoding.
Definition: stream.h:238
Ch Peek() const
Definition: stream.h:130
UTFType
Runtime-specified UTF encoding type of a stream.
Definition: encodings.h:688
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:433
Read-only string stream.
Definition: fwd.h:60
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:131
GenericInsituStringStream(Ch *src)
Definition: stream.h:203
const Ch * src_
Current read position.
Definition: stream.h:180
bool HasBOM() const
Definition: stream.h:143
Ch Peek() const
Definition: stream.h:165
void Pop(size_t count)
Definition: stream.h:225
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:128
A read-write string stream.
Definition: fwd.h:65
GenericStringStream(const Ch *src)
Definition: stream.h:163
A Stream Wrapper.
Definition: stream.h:125
GenericStreamWrapper(InputStream &is)
Definition: stream.h:128
void PutUnsafe(Stream &stream, typename Stream::Ch c)
Write character to a stream, presuming buffer is reserved.
Definition: stream.h:98
size_t PutEnd(Ch *ch)
Definition: stream.h:136
Encoding::Ch Ch
Definition: stream.h:127
void PutReserve(Stream &stream, size_t count)
Reserve n characters for writing to a stream.
Definition: stream.h:91
Provides additional information for stream.
Definition: stream.h:79
size_t PutEnd(Ch *)
Definition: stream.h:175
InputStream & is_
Definition: stream.h:146
void PutN(Stream &stream, Ch c, size_t n)
Put N copies of a character to a stream.
Definition: stream.h:104
UTFType GetType() const
Definition: stream.h:142
common definitions and configuration
const Ch * Peek4() const
Definition: stream.h:139
size_t PutEnd(Ch *begin)
Definition: stream.h:217
Encoding::Ch Ch
Definition: stream.h:161
void Put(Ch)
Definition: stream.h:173
void Put(Ch ch)
Definition: stream.h:134
size_t Tell() const
Definition: stream.h:167
Ch * Push(size_t count)
Definition: stream.h:220
const Ch * head_
Original head of the string.
Definition: stream.h:181


livox_ros_driver
Author(s): Livox Dev Team
autogenerated on Mon Mar 15 2021 02:40:46