istreamwrapper.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 #ifndef RAPIDJSON_ISTREAMWRAPPER_H_
16 #define RAPIDJSON_ISTREAMWRAPPER_H_
17 
18 #include "stream.h"
19 #include <iosfwd>
20 #include <ios>
21 
22 #if __clang__
23 RAPIDJSON_DIAG_PUSH
24 RAPIDJSON_DIAG_OFF(padded)
25 #elif defined(_MSC_VER)
26 RAPIDJSON_DIAG_PUSH
27 RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
28 #endif
29 
31 
33 
48 template <typename StreamType>
50 {
51 public:
52  typedef typename StreamType::char_type Ch;
53 
55 
59  : stream_(stream)
61  , bufferSize_(4)
62  , bufferLast_(0)
63  , current_(buffer_)
64  , readCount_(0)
65  , count_(0)
66  , eof_(false)
67  {
68  Read();
69  }
70 
72 
77  BasicIStreamWrapper(StreamType& stream, char* buffer, size_t bufferSize)
78  : stream_(stream)
79  , buffer_(buffer)
80  , bufferSize_(bufferSize)
81  , bufferLast_(0)
82  , current_(buffer_)
83  , readCount_(0)
84  , count_(0)
85  , eof_(false)
86  {
87  RAPIDJSON_ASSERT(bufferSize >= 4);
88  Read();
89  }
90 
91  Ch Peek() const
92  {
93  return *current_;
94  }
95  Ch Take()
96  {
97  Ch c = *current_;
98  Read();
99  return c;
100  }
101  size_t Tell() const
102  {
103  return count_ + static_cast<size_t>(current_ - buffer_);
104  }
105 
106  // Not implemented
107  void Put(Ch)
108  {
109  RAPIDJSON_ASSERT(false);
110  }
111  void Flush()
112  {
113  RAPIDJSON_ASSERT(false);
114  }
115  Ch* PutBegin()
116  {
117  RAPIDJSON_ASSERT(false);
118  return 0;
119  }
120  size_t PutEnd(Ch*)
121  {
122  RAPIDJSON_ASSERT(false);
123  return 0;
124  }
125 
126  // For encoding detection only.
127  const Ch* Peek4() const
128  {
129  return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
130  }
131 
132 private:
136 
137  void Read()
138  {
139  if (current_ < bufferLast_)
140  ++current_;
141  else if (!eof_)
142  {
143  count_ += readCount_;
146  current_ = buffer_;
147 
148  if (!stream_.read(buffer_, static_cast<std::streamsize>(bufferSize_)))
149  {
150  readCount_ = static_cast<size_t>(stream_.gcount());
151  *(bufferLast_ = buffer_ + readCount_) = '\0';
152  eof_ = true;
153  }
154  }
155  }
156 
159  size_t bufferSize_;
161  Ch* current_;
162  size_t readCount_;
163  size_t count_;
164  bool eof_;
165 };
166 
169 
170 #if defined(__clang__) || defined(_MSC_VER)
171 RAPIDJSON_DIAG_POP
172 #endif
173 
175 
176 #endif // RAPIDJSON_ISTREAMWRAPPER_H_
BasicIStreamWrapper< std::istream > IStreamWrapper
size_t count_
Number of characters read.
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:416
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:126
StreamType & stream_
BasicIStreamWrapper(StreamType &stream, char *buffer, size_t bufferSize)
Constructor.
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
BasicIStreamWrapper(StreamType &stream)
Constructor.
size_t Tell() const
BasicIStreamWrapper< std::wistream > WIStreamWrapper
StreamType
Wrapper of std::basic_istream into RapidJSON&#39;s Stream concept.
const Ch * Peek4() const
BasicIStreamWrapper & operator=(const BasicIStreamWrapper &)
StreamType::char_type Ch


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