istreamwrapper.h Source File

istreamwrapper.h Source File#

Composable Kernel: istreamwrapper.h Source File
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.
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#ifdef __clang__
23RAPIDJSON_DIAG_PUSH
24RAPIDJSON_DIAG_OFF(padded)
25#elif defined(_MSC_VER)
26RAPIDJSON_DIAG_PUSH
27RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
28#endif
29
31
33
47
48template <typename StreamType>
50{
51 public:
52 typedef typename StreamType::char_type Ch;
53
55
58 BasicIStreamWrapper(StreamType& stream)
59 : stream_(stream),
60 buffer_(peekBuffer_),
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 { return *current_; }
93 {
94 Ch c = *current_;
95 Read();
96 return c;
97 }
98 size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
99
100 // Not implemented
101 void Put(Ch) { RAPIDJSON_ASSERT(false); }
102 void Flush() { RAPIDJSON_ASSERT(false); }
104 {
105 RAPIDJSON_ASSERT(false);
106 return 0;
107 }
108 size_t PutEnd(Ch*)
109 {
110 RAPIDJSON_ASSERT(false);
111 return 0;
112 }
113
114 // For encoding detection only.
115 const Ch* Peek4() const { return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0; }
116
117 private:
120 BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
121
122 void Read()
123 {
124 if(current_ < bufferLast_)
125 ++current_;
126 else if(!eof_)
127 {
128 count_ += readCount_;
129 readCount_ = bufferSize_;
130 bufferLast_ = buffer_ + readCount_ - 1;
131 current_ = buffer_;
132
133 if(!stream_.read(buffer_, static_cast<std::streamsize>(bufferSize_)))
134 {
135 readCount_ = static_cast<size_t>(stream_.gcount());
136 *(bufferLast_ = buffer_ + readCount_) = '\0';
137 eof_ = true;
138 }
139 }
140 }
141
142 StreamType& stream_;
143 Ch peekBuffer_[4], *buffer_;
144 size_t bufferSize_;
145 Ch* bufferLast_;
146 Ch* current_;
147 size_t readCount_;
148 size_t count_;
149 bool eof_;
150};
151
154
155#if defined(__clang__) || defined(_MSC_VER)
156RAPIDJSON_DIAG_POP
157#endif
158
160
161#endif // RAPIDJSON_ISTREAMWRAPPER_H_
Wrapper of std::basic_istream into RapidJSON's Stream concept.
Definition istreamwrapper.h:50
Ch Peek() const
Definition istreamwrapper.h:91
Ch * PutBegin()
Definition istreamwrapper.h:103
void Flush()
Definition istreamwrapper.h:102
BasicIStreamWrapper(StreamType &stream)
Constructor.
Definition istreamwrapper.h:58
const Ch * Peek4() const
Definition istreamwrapper.h:115
BasicIStreamWrapper(StreamType &stream, char *buffer, size_t bufferSize)
Constructor.
Definition istreamwrapper.h:77
StreamType::char_type Ch
Definition istreamwrapper.h:52
size_t PutEnd(Ch *)
Definition istreamwrapper.h:108
size_t Tell() const
Definition istreamwrapper.h:98
void Put(Ch)
Definition istreamwrapper.h:101
Ch Take()
Definition istreamwrapper.h:92
#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
BasicIStreamWrapper< std::istream > IStreamWrapper
Definition istreamwrapper.h:152
BasicIStreamWrapper< std::wistream > WIStreamWrapper
Definition istreamwrapper.h:153