Arcane  v3.14.10.0
Documentation développeur
Chargement...
Recherche...
Aucune correspondance
istreamwrapper.h
1// -*- tab-width: 2; indent-tabs-mode: nil; coding: utf-8-with-signature -*-
2// Tencent is pleased to support the open source community by making RapidJSON available.
3//
4// Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All rights reserved.
5//
6// Licensed under the MIT License (the "License"); you may not use this file except
7// in compliance with the License. You may obtain a copy of the License at
8//
9// http://opensource.org/licenses/MIT
10//
11// Unless required by applicable law or agreed to in writing, software distributed
12// under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
13// CONDITIONS OF ANY KIND, either express or implied. See the License for the
14// specific language governing permissions and limitations under the License.
15
16#ifndef RAPIDJSON_ISTREAMWRAPPER_H_
17#define RAPIDJSON_ISTREAMWRAPPER_H_
18
19#include "stream.h"
20#include <iosfwd>
21#include <ios>
22
23#ifdef __clang__
24RAPIDJSON_DIAG_PUSH
25RAPIDJSON_DIAG_OFF(padded)
26#elif defined(_MSC_VER)
27RAPIDJSON_DIAG_PUSH
28RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
29#endif
30
32
34
49template <typename StreamType>
51public:
52 typedef typename StreamType::char_type Ch;
53
55
58 BasicIStreamWrapper(StreamType &stream) : stream_(stream), buffer_(peekBuffer_), bufferSize_(4), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
59 Read();
60 }
61
63
68 BasicIStreamWrapper(StreamType &stream, char* buffer, size_t bufferSize) : stream_(stream), buffer_(buffer), bufferSize_(bufferSize), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) {
69 RAPIDJSON_ASSERT(bufferSize >= 4);
70 Read();
71 }
72
73 Ch Peek() const { return *current_; }
74 Ch Take() { Ch c = *current_; Read(); return c; }
75 size_t Tell() const { return count_ + static_cast<size_t>(current_ - buffer_); }
76
77 // Not implemented
78 void Put(Ch) { RAPIDJSON_ASSERT(false); }
79 void Flush() { RAPIDJSON_ASSERT(false); }
80 Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
81 size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
82
83 // For encoding detection only.
84 const Ch* Peek4() const {
85 return (current_ + 4 - !eof_ <= bufferLast_) ? current_ : 0;
86 }
87
88private:
92
93 void Read() {
94 if (current_ < bufferLast_)
95 ++current_;
96 else if (!eof_) {
97 count_ += readCount_;
98 readCount_ = bufferSize_;
99 bufferLast_ = buffer_ + readCount_ - 1;
100 current_ = buffer_;
101
102 if (!stream_.read(buffer_, static_cast<std::streamsize>(bufferSize_))) {
103 readCount_ = static_cast<size_t>(stream_.gcount());
104 *(bufferLast_ = buffer_ + readCount_) = '\0';
105 eof_ = true;
106 }
107 }
108 }
109
110 StreamType &stream_;
111 Ch peekBuffer_[4], *buffer_;
112 size_t bufferSize_;
113 Ch *bufferLast_;
114 Ch *current_;
115 size_t readCount_;
116 size_t count_;
117 bool eof_;
118};
119
122
123#if defined(__clang__) || defined(_MSC_VER)
124RAPIDJSON_DIAG_POP
125#endif
126
128
129#endif // RAPIDJSON_ISTREAMWRAPPER_H_
Wrapper of std::basic_istream into RapidJSON's Stream concept.
BasicIStreamWrapper(StreamType &stream)
Constructor.
BasicIStreamWrapper(StreamType &stream, char *buffer, size_t bufferSize)
Constructor.
size_t count_
Number of characters read.
#define RAPIDJSON_ASSERT(x)
Assertion.
Definition rapidjson.h:407
#define RAPIDJSON_NAMESPACE_BEGIN
provide custom rapidjson namespace (opening expression)
Definition rapidjson.h:122
#define RAPIDJSON_NAMESPACE_END
provide custom rapidjson namespace (closing expression)
Definition rapidjson.h:125