memorystream.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Tencent is pleased to support the open source community by making RapidJSON
  2. // available.
  3. //
  4. // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip. All
  5. // rights reserved.
  6. //
  7. // Licensed under the MIT License (the "License"); you may not use this file
  8. // except in compliance with the License. You may obtain a copy of the License
  9. // at
  10. //
  11. // http://opensource.org/licenses/MIT
  12. //
  13. // Unless required by applicable law or agreed to in writing, software
  14. // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  16. // License for the specific language governing permissions and limitations under
  17. // the License.
  18. #ifndef RAPIDJSON_MEMORYSTREAM_H_
  19. #define RAPIDJSON_MEMORYSTREAM_H_
  20. #include "stream.h"
  21. #ifdef __clang__
  22. RAPIDJSON_DIAG_PUSH
  23. RAPIDJSON_DIAG_OFF(unreachable - code)
  24. RAPIDJSON_DIAG_OFF(missing - noreturn)
  25. #endif
  26. RAPIDJSON_NAMESPACE_BEGIN
  27. //! Represents an in-memory input byte stream.
  28. /*!
  29. This class is mainly for being wrapped by EncodedInputStream or
  30. AutoUTFInputStream.
  31. It is similar to FileReadBuffer but the source is an in-memory buffer
  32. instead of a file.
  33. Differences between MemoryStream and StringStream:
  34. 1. StringStream has encoding but MemoryStream is a byte stream.
  35. 2. MemoryStream needs size of the source buffer and the buffer don't need to
  36. be null terminated. StringStream assume null-terminated string as source.
  37. 3. MemoryStream supports Peek4() for encoding detection. StringStream is
  38. specified with an encoding so it should not have Peek4(). \note implements
  39. Stream concept
  40. */
  41. struct MemoryStream {
  42. typedef char Ch; // byte
  43. MemoryStream(const Ch *src, size_t size)
  44. : src_(src), begin_(src), end_(src + size), size_(size) {}
  45. Ch Peek() const { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_; }
  46. Ch Take() { return RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_++; }
  47. size_t Tell() const { return static_cast<size_t>(src_ - begin_); }
  48. Ch *PutBegin() {
  49. RAPIDJSON_ASSERT(false);
  50. return 0;
  51. }
  52. void Put(Ch) { RAPIDJSON_ASSERT(false); }
  53. void Flush() { RAPIDJSON_ASSERT(false); }
  54. size_t PutEnd(Ch *) {
  55. RAPIDJSON_ASSERT(false);
  56. return 0;
  57. }
  58. // For encoding detection only.
  59. const Ch *Peek4() const { return Tell() + 4 <= size_ ? src_ : 0; }
  60. const Ch *src_; //!< Current read position.
  61. const Ch *begin_; //!< Original head of the string.
  62. const Ch *end_; //!< End of stream.
  63. size_t size_; //!< Size of the stream.
  64. };
  65. RAPIDJSON_NAMESPACE_END
  66. #ifdef __clang__
  67. RAPIDJSON_DIAG_POP
  68. #endif
  69. #endif // RAPIDJSON_MEMORYBUFFER_H_