Torque3D Documentation / _generateds / istreamwrapper.h

istreamwrapper.h

Engine/source/persistence/rapidjson/istreamwrapper.h

More...

Classes:

class

Wrapper of

std::basic_istream
into RapidJSON's Stream concept.

Public Typedefs

BasicIStreamWrapper< std::istream >
IStreamWrapper 
BasicIStreamWrapper< std::wistream >
WIStreamWrapper 

Detailed Description

Public Typedefs

typedef BasicIStreamWrapper< std::istream > IStreamWrapper 
typedef BasicIStreamWrapper< std::wistream > WIStreamWrapper 
  1
  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
 31RAPIDJSON_NAMESPACE_BEGIN
 32
 33//! Wrapper of \c std::basic_istream into RapidJSON's Stream concept.
 34/*!
 35    The classes can be wrapped including but not limited to:
 36
 37    - \c std::istringstream
 38    - \c std::stringstream
 39    - \c std::wistringstream
 40    - \c std::wstringstream
 41    - \c std::ifstream
 42    - \c std::fstream
 43    - \c std::wifstream
 44    - \c std::wfstream
 45
 46    \tparam StreamType Class derived from \c std::basic_istream.
 47*/
 48   
 49template <typename StreamType>
 50class BasicIStreamWrapper {
 51public:
 52    typedef typename StreamType::char_type Ch;
 53
 54    //! Constructor.
 55    /*!
 56        \param stream stream opened for read.
 57    */
 58    BasicIStreamWrapper(StreamType &stream) : stream_(stream), buffer_(peekBuffer_), bufferSize_(4), bufferLast_(0), current_(buffer_), readCount_(0), count_(0), eof_(false) { 
 59        Read();
 60    }
 61
 62    //! Constructor.
 63    /*!
 64        \param stream stream opened for read.
 65        \param buffer user-supplied buffer.
 66        \param bufferSize size of buffer in bytes. Must >=4 bytes.
 67    */
 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:
 89    BasicIStreamWrapper();
 90    BasicIStreamWrapper(const BasicIStreamWrapper&);
 91    BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
 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_;  //!< Number of characters read
117    bool eof_;
118};
119
120typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
121typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
122
123#if defined(__clang__) || defined(_MSC_VER)
124RAPIDJSON_DIAG_POP
125#endif
126
127RAPIDJSON_NAMESPACE_END
128
129#endif // RAPIDJSON_ISTREAMWRAPPER_H_
130