You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

76 lines
2.9 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  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. #ifndef CEREAL_RAPIDJSON_MEMORYSTREAM_H_
  15. #define CEREAL_RAPIDJSON_MEMORYSTREAM_H_
  16. #include "stream.h"
  17. #ifdef __clang__
  18. CEREAL_RAPIDJSON_DIAG_PUSH
  19. CEREAL_RAPIDJSON_DIAG_OFF(unreachable-code)
  20. CEREAL_RAPIDJSON_DIAG_OFF(missing-noreturn)
  21. #endif
  22. #ifdef _MSC_VER
  23. CEREAL_RAPIDJSON_DIAG_PUSH
  24. CEREAL_RAPIDJSON_DIAG_OFF( 4127 ) // ignore assert(false) for triggering exception
  25. #endif
  26. CEREAL_RAPIDJSON_NAMESPACE_BEGIN
  27. //! Represents an in-memory input byte stream.
  28. /*!
  29. This class is mainly for being wrapped by EncodedInputStream or AutoUTFInputStream.
  30. It is similar to FileReadBuffer but the source is an in-memory buffer instead of a file.
  31. Differences between MemoryStream and StringStream:
  32. 1. StringStream has encoding but MemoryStream is a byte stream.
  33. 2. MemoryStream needs size of the source buffer and the buffer don't need to be null terminated. StringStream assume null-terminated string as source.
  34. 3. MemoryStream supports Peek4() for encoding detection. StringStream is specified with an encoding so it should not have Peek4().
  35. \note implements Stream concept
  36. */
  37. struct MemoryStream {
  38. typedef char Ch; // byte
  39. MemoryStream(const Ch *src, size_t size) : src_(src), begin_(src), end_(src + size), size_(size) {}
  40. Ch Peek() const { return CEREAL_RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_; }
  41. Ch Take() { return CEREAL_RAPIDJSON_UNLIKELY(src_ == end_) ? '\0' : *src_++; }
  42. size_t Tell() const { return static_cast<size_t>(src_ - begin_); }
  43. Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
  44. void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); }
  45. void Flush() { CEREAL_RAPIDJSON_ASSERT(false); }
  46. size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
  47. // For encoding detection only.
  48. const Ch* Peek4() const {
  49. return Tell() + 4 <= size_ ? src_ : 0;
  50. }
  51. const Ch* src_; //!< Current read position.
  52. const Ch* begin_; //!< Original head of the string.
  53. const Ch* end_; //!< End of stream.
  54. size_t size_; //!< Size of the stream.
  55. };
  56. CEREAL_RAPIDJSON_NAMESPACE_END
  57. #if defined(__clang__) || defined(_MSC_VER)
  58. CEREAL_RAPIDJSON_DIAG_POP
  59. #endif
  60. #endif // CEREAL_RAPIDJSON_MEMORYBUFFER_H_