IN2OSM  1.0.1
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 
21 #ifdef __clang__
22 RAPIDJSON_DIAG_PUSH
23 RAPIDJSON_DIAG_OFF(padded)
24 #elif defined(_MSC_VER)
25 RAPIDJSON_DIAG_PUSH
26 RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
27 #endif
28 
30 
32 
47 template <typename StreamType>
49 public:
50  typedef typename StreamType::char_type Ch;
51 
53 
56  BasicIStreamWrapper(StreamType &stream) : stream_(stream), buffer_(peekBuffer_), bufferSize_(4), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
57  Read();
58  }
59 
61 
66  BasicIStreamWrapper(StreamType &stream, char* buffer, size_t bufferSize) : stream_(stream), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
67  RAPIDJSON_ASSERT(bufferSize >= 4);
68  Read();
69  }
70 
71  Ch Peek() const { return *current_; }
72  Ch Take() { Ch c = *current_; Read(); return c; }
73  size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
74 
75  // Not implemented
76  void Put(Ch) { RAPIDJSON_ASSERT(false); }
77  void Flush() { RAPIDJSON_ASSERT(false); }
78  Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
79  size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
80 
81  // For encoding detection only.
82  const Ch* Peek4() const {
83  return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
84  }
85 
86 private:
90 
91  void Read() {
92  if (current_ < bufferLast_)
93  ++current_;
94  else if (!eof_) {
95  count_ += readCount_;
98  current_ = buffer_;
99 
100  if (!stream_.read(buffer_, static_cast<std::streamsize>(bufferSize_))) {
101  readCount_ = static_cast<size_t>(stream_.gcount());
102  *(bufferLast_ = buffer_ + readCount_) = '\0';
103  eof_ = true;
104  }
105  }
106  }
107 
108  StreamType &stream_;
110  size_t bufferSize_;
112  Ch *current_;
113  size_t readCount_;
114  size_t count_;
115  bool eof_;
116 };
117 
120 
121 #if defined(__clang__) || defined(_MSC_VER)
122 RAPIDJSON_DIAG_POP
123 #endif
124 
126 
127 #endif // RAPIDJSON_ISTREAMWRAPPER_H_
BasicIStreamWrapper< std::istream > IStreamWrapper
StreamType & stream_
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition: rapidjson.h:121
const Ch * Peek4() const
size_t Tell() const
BasicIStreamWrapper(StreamType &stream, char *buffer, size_t bufferSize)
Constructor.
BasicIStreamWrapper< std::wistream > WIStreamWrapper
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition: rapidjson.h:124
size_t count_
Number of characters read.
BasicIStreamWrapper & operator=(const BasicIStreamWrapper &)
StreamType::char_type Ch
size_t PutEnd(Ch *)
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition: rapidjson.h:406
BasicIStreamWrapper(StreamType &stream)
Constructor.
Wrapper of std::basic_istream into RapidJSON&#39;s Stream concept.