stream.h 6.6 KB

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