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.

116 lines
3.7 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_ISTREAMWRAPPER_H_
  15. #define CEREAL_RAPIDJSON_ISTREAMWRAPPER_H_
  16. #include "stream.h"
  17. #include <iosfwd>
  18. #ifdef __clang__
  19. CEREAL_RAPIDJSON_DIAG_PUSH
  20. CEREAL_RAPIDJSON_DIAG_OFF(padded)
  21. #endif
  22. #ifdef _MSC_VER
  23. CEREAL_RAPIDJSON_DIAG_PUSH
  24. CEREAL_RAPIDJSON_DIAG_OFF(4351) // new behavior: elements of array 'array' will be default initialized
  25. CEREAL_RAPIDJSON_DIAG_OFF(4127) // ignore assert(false) for triggering exception
  26. #endif
  27. CEREAL_RAPIDJSON_NAMESPACE_BEGIN
  28. //! Wrapper of \c std::basic_istream into RapidJSON's Stream concept.
  29. /*!
  30. The classes can be wrapped including but not limited to:
  31. - \c std::istringstream
  32. - \c std::stringstream
  33. - \c std::wistringstream
  34. - \c std::wstringstream
  35. - \c std::ifstream
  36. - \c std::fstream
  37. - \c std::wifstream
  38. - \c std::wfstream
  39. \tparam StreamType Class derived from \c std::basic_istream.
  40. */
  41. template <typename StreamType>
  42. class BasicIStreamWrapper {
  43. public:
  44. typedef typename StreamType::char_type Ch;
  45. BasicIStreamWrapper(StreamType& stream) : stream_(stream), count_(), peekBuffer_() {}
  46. Ch Peek() const {
  47. typename StreamType::int_type c = stream_.peek();
  48. return CEREAL_RAPIDJSON_LIKELY(c != StreamType::traits_type::eof()) ? static_cast<Ch>(c) : '\0';
  49. }
  50. Ch Take() {
  51. typename StreamType::int_type c = stream_.get();
  52. if (CEREAL_RAPIDJSON_LIKELY(c != StreamType::traits_type::eof())) {
  53. count_++;
  54. return static_cast<Ch>(c);
  55. }
  56. else
  57. return '\0';
  58. }
  59. // tellg() may return -1 when failed. So we count by ourself.
  60. size_t Tell() const { return count_; }
  61. Ch* PutBegin() { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
  62. void Put(Ch) { CEREAL_RAPIDJSON_ASSERT(false); }
  63. void Flush() { CEREAL_RAPIDJSON_ASSERT(false); }
  64. size_t PutEnd(Ch*) { CEREAL_RAPIDJSON_ASSERT(false); return 0; }
  65. // For encoding detection only.
  66. const Ch* Peek4() const {
  67. CEREAL_RAPIDJSON_ASSERT(sizeof(Ch) == 1); // Only usable for byte stream.
  68. int i;
  69. bool hasError = false;
  70. for (i = 0; i < 4; ++i) {
  71. typename StreamType::int_type c = stream_.get();
  72. if (c == StreamType::traits_type::eof()) {
  73. hasError = true;
  74. stream_.clear();
  75. break;
  76. }
  77. peekBuffer_[i] = static_cast<Ch>(c);
  78. }
  79. for (--i; i >= 0; --i)
  80. stream_.putback(peekBuffer_[i]);
  81. return !hasError ? peekBuffer_ : 0;
  82. }
  83. private:
  84. BasicIStreamWrapper(const BasicIStreamWrapper&);
  85. BasicIStreamWrapper& operator=(const BasicIStreamWrapper&);
  86. StreamType& stream_;
  87. size_t count_; //!< Number of characters read. Note:
  88. mutable Ch peekBuffer_[4];
  89. };
  90. typedef BasicIStreamWrapper<std::istream> IStreamWrapper;
  91. typedef BasicIStreamWrapper<std::wistream> WIStreamWrapper;
  92. #if defined(__clang__) || defined(_MSC_VER)
  93. CEREAL_RAPIDJSON_DIAG_POP
  94. #endif
  95. CEREAL_RAPIDJSON_NAMESPACE_END
  96. #endif // CEREAL_RAPIDJSON_ISTREAMWRAPPER_H_