stream.h 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  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. // StringStream
  84. //! Read-only string stream.
  85. /*! \note implements Stream concept
  86. */
  87. template <typename Encoding>
  88. struct GenericStringStream {
  89. typedef typename Encoding::Ch Ch;
  90. GenericStringStream(const Ch *src) : src_(src), head_(src) {}
  91. Ch Peek() const { return *src_; }
  92. Ch Take() { return *src_++; }
  93. size_t Tell() const { return static_cast<size_t>(src_ - head_); }
  94. Ch* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  95. void Put(Ch) { RAPIDJSON_ASSERT(false); }
  96. void Flush() { RAPIDJSON_ASSERT(false); }
  97. size_t PutEnd(Ch*) { RAPIDJSON_ASSERT(false); return 0; }
  98. const Ch* src_; //!< Current read position.
  99. const Ch* head_; //!< Original head of the string.
  100. };
  101. template <typename Encoding>
  102. struct StreamTraits<GenericStringStream<Encoding> > {
  103. enum { copyOptimization = 1 };
  104. };
  105. //! String stream with UTF8 encoding.
  106. typedef GenericStringStream<UTF8<> > StringStream;
  107. ///////////////////////////////////////////////////////////////////////////////
  108. // InsituStringStream
  109. //! A read-write string stream.
  110. /*! This string stream is particularly designed for in-situ parsing.
  111. \note implements Stream concept
  112. */
  113. template <typename Encoding>
  114. struct GenericInsituStringStream {
  115. typedef typename Encoding::Ch Ch;
  116. GenericInsituStringStream(Ch *src) : src_(src), dst_(0), head_(src) {}
  117. // Read
  118. Ch Peek() { return *src_; }
  119. Ch Take() { return *src_++; }
  120. size_t Tell() { return static_cast<size_t>(src_ - head_); }
  121. // Write
  122. void Put(Ch c) { RAPIDJSON_ASSERT(dst_ != 0); *dst_++ = c; }
  123. Ch* PutBegin() { return dst_ = src_; }
  124. size_t PutEnd(Ch* begin) { return static_cast<size_t>(dst_ - begin); }
  125. void Flush() {}
  126. Ch* Push(size_t count) { Ch* begin = dst_; dst_ += count; return begin; }
  127. void Pop(size_t count) { dst_ -= count; }
  128. Ch* src_;
  129. Ch* dst_;
  130. Ch* head_;
  131. };
  132. template <typename Encoding>
  133. struct StreamTraits<GenericInsituStringStream<Encoding> > {
  134. enum { copyOptimization = 1 };
  135. };
  136. //! Insitu string stream with UTF8 encoding.
  137. typedef GenericInsituStringStream<UTF8<> > InsituStringStream;
  138. RAPIDJSON_NAMESPACE_END
  139. #endif // RAPIDJSON_STREAM_H_