stream.h
Engine/source/persistence/rapidjson/stream.h
Classes:
class
A read-write string stream.
class
A Stream Wrapper.
class
Read-only string stream.
class
Provides additional information for stream.
Public Typedefs
InsituStringStream
Insitu string stream with UTF8 encoding.
GenericStringStream< UTF8<> >
Public Functions
Detailed Description
Public Typedefs
typedef GenericInsituStringStream< UTF8<> > InsituStringStream
Insitu string stream with UTF8 encoding.
typedef GenericStringStream< UTF8<> > StringStream
String stream with UTF8 encoding.
Public Functions
PutN(Stream & stream, Ch c, size_t n)
Put N copies of a character to a stream.
PutReserve(Stream & stream, size_t count)
Reserve n characters for writing to a stream.
PutUnsafe(Stream & stream, typename Stream::Ch c)
Write character to a stream, presuming buffer is reserved.
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#include "rapidjson.h" 17 18#ifndef RAPIDJSON_STREAM_H_ 19#define RAPIDJSON_STREAM_H_ 20 21#include "encodings.h" 22 23RAPIDJSON_NAMESPACE_BEGIN 24 25/////////////////////////////////////////////////////////////////////////////// 26// Stream 27 28/*! \class rapidjson::Stream 29 \brief Concept for reading and writing characters. 30 31 For read-only stream, no need to implement PutBegin(), Put(), Flush() and PutEnd(). 32 33 For write-only stream, only need to implement Put() and Flush(). 34 35\code 36concept Stream { 37 typename Ch; //!< Character type of the stream. 38 39 //! Read the current character from stream without moving the read cursor. 40 Ch Peek() const; 41 42 //! Read the current character from stream and moving the read cursor to next character. 43 Ch Take(); 44 45 //! Get the current read cursor. 46 //! \return Number of characters read from start. 47 size_t Tell(); 48 49 //! Begin writing operation at the current read pointer. 50 //! \return The begin writer pointer. 51 Ch* PutBegin(); 52 53 //! Write a character. 54 void Put(Ch c); 55 56 //! Flush the buffer. 57 void Flush(); 58 59 //! End the writing operation. 60 //! \param begin The begin write pointer returned by PutBegin(). 61 //! \return Number of characters written. 62 size_t PutEnd(Ch* begin); 63} 64\endcode 65*/ 66 67//! Provides additional information for stream. 68/*! 69 By using traits pattern, this type provides a default configuration for stream. 70 For custom stream, this type can be specialized for other configuration. 71 See TEST(Reader, CustomStringStream) in readertest.cpp for example. 72*/ 73template<typename Stream> 74struct StreamTraits { 75 //! Whether to make local copy of stream for optimization during parsing. 76 /*! 77 By default, for safety, streams do not use local copy optimization. 78 Stream that can be copied fast should specialize this, like StreamTraits<StringStream>. 79 */ 80 enum { copyOptimization = 0 }; 81}; 82 83//! Reserve n characters for writing to a stream. 84template<typename Stream> 85inline void PutReserve(Stream& stream, size_t count) { 86 (void)stream; 87 (void)count; 88} 89 90//! Write character to a stream, presuming buffer is reserved. 91template<typename Stream> 92inline void PutUnsafe(Stream& stream, typename Stream::Ch c) { 93 stream.Put(c); 94} 95 96//! Put N copies of a character to a stream. 97template<typename Stream, typename Ch> 98inline void PutN(Stream& stream, Ch c, size_t n) { 99 PutReserve(stream, n); 100 for (size_t i = 0; i < n; i++) 101 PutUnsafe(stream, c); 102} 103 104/////////////////////////////////////////////////////////////////////////////// 105// GenericStreamWrapper 106 107//! A Stream Wrapper 108/*! \tThis string stream is a wrapper for any stream by just forwarding any 109 \treceived message to the origin stream. 110 \note implements Stream concept 111*/ 112 113#if defined(_MSC_VER) && _MSC_VER <= 1800 114RAPIDJSON_DIAG_PUSH 115RAPIDJSON_DIAG_OFF(4702) // unreachable code 116RAPIDJSON_DIAG_OFF(4512) // assignment operator could not be generated 117#endif 118 119template <typename InputStream, typename Encoding = UTF8<> > 120class GenericStreamWrapper { 121public: 122 typedef typename Encoding::Ch Ch; 123 GenericStreamWrapper(InputStream& is): is_(is) {} 124 125 Ch Peek() const { return is_.Peek(); } 126 Ch Take() { return is_.Take(); } 127 size_t Tell() { return is_.Tell(); } 128 Ch* PutBegin() { return is_.PutBegin(); } 129 void Put(Ch ch) { is_.Put(ch); } 130 void Flush() { is_.Flush(); } 131 size_t PutEnd(Ch* ch) { return is_.PutEnd(ch); } 132 133 // wrapper for MemoryStream 134 const Ch* Peek4() const { return is_.Peek4(); } 135 136 // wrapper for AutoUTFInputStream 137 UTFType GetType() const { return is_.GetType(); } 138 bool HasBOM() const { return is_.HasBOM(); } 139 140protected: 141 InputStream& is_; 142}; 143 144#if defined(_MSC_VER) && _MSC_VER <= 1800 145RAPIDJSON_DIAG_POP 146#endif 147 148/////////////////////////////////////////////////////////////////////////////// 149// StringStream 150 151//! Read-only string stream. 152/*! \note implements Stream concept 153*/ 154template <typename Encoding> 155struct GenericStringStream { 156 typedef typename Encoding::Ch Ch; 157 158 GenericStringStream(const Ch *src) : src_(src), head_(src) {} 159 160 Ch Peek() const { return *src_; } 161 Ch Take() { return *src_++; } 162 size_t Tell() const { return static_cast<size_t>(src_ - head_); } 163 164 Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; } 165 void Put(Ch) { RAPIDJSON_ASSERT(false); } 166 void Flush() { RAPIDJSON_ASSERT(false); } 167 size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; } 168 169 const Ch* src_; //!< Current read position. 170 const Ch* head_; //!< Original head of the string. 171}; 172 173template <typename Encoding> 174struct StreamTraits<GenericStringStream<Encoding> > { 175 enum { copyOptimization = 1 }; 176}; 177 178//! String stream with UTF8 encoding. 179typedef GenericStringStream<UTF8<> > StringStream; 180 181/////////////////////////////////////////////////////////////////////////////// 182// InsituStringStream 183 184//! A read-write string stream. 185/*! This string stream is particularly designed for in-situ parsing. 186 \note implements Stream concept 187*/ 188template <typename Encoding> 189struct GenericInsituStringStream { 190 typedef typename Encoding::Ch Ch; 191 192 GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {} 193 194 // Read 195 Ch Peek() { return *src_; } 196 Ch Take() { return *src_++; } 197 size_t Tell() { return static_cast<size_t>(src_ - head_); } 198 199 // Write 200 void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; } 201 202 Ch* PutBegin() { return dst_ = src_; } 203 size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); } 204 void Flush() {} 205 206 Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; } 207 void Pop(size_t count) { dst_ -= count; } 208 209 Ch* src_; 210 Ch* dst_; 211 Ch* head_; 212}; 213 214template <typename Encoding> 215struct StreamTraits<GenericInsituStringStream<Encoding> > { 216 enum { copyOptimization = 1 }; 217}; 218 219//! Insitu string stream with UTF8 encoding. 220typedef GenericInsituStringStream<UTF8<> > InsituStringStream; 221 222RAPIDJSON_NAMESPACE_END 223 224#endif // RAPIDJSON_STREAM_H_ 225