ostreamwrapper.h 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 RAPIDJSON_OSTREAMWRAPPER_H_
  15. #define RAPIDJSON_OSTREAMWRAPPER_H_
  16. #include "stream.h"
  17. #include <iosfwd>
  18. #ifdef __clang__
  19. RAPIDJSON_DIAG_PUSH
  20. RAPIDJSON_DIAG_OFF(padded)
  21. #endif
  22. RAPIDJSON_NAMESPACE_BEGIN
  23. //! Wrapper of \c std::basic_ostream into RapidJSON's Stream concept.
  24. /*!
  25. The classes can be wrapped including but not limited to:
  26. - \c std::ostringstream
  27. - \c std::stringstream
  28. - \c std::wpstringstream
  29. - \c std::wstringstream
  30. - \c std::ifstream
  31. - \c std::fstream
  32. - \c std::wofstream
  33. - \c std::wfstream
  34. \tparam StreamType Class derived from \c std::basic_ostream.
  35. */
  36. template <typename StreamType>
  37. class BasicOStreamWrapper {
  38. public:
  39. typedef typename StreamType::char_type Ch;
  40. BasicOStreamWrapper(StreamType& stream) : stream_(stream) {}
  41. void Put(Ch c) {
  42. stream_.put(c);
  43. }
  44. void Flush() {
  45. stream_.flush();
  46. }
  47. // Not implemented
  48. char Peek() const { RAPIDJSON_ASSERT(false); return 0; }
  49. char Take() { RAPIDJSON_ASSERT(false); return 0; }
  50. size_t Tell() const { RAPIDJSON_ASSERT(false); return 0; }
  51. char* PutBegin() { RAPIDJSON_ASSERT(false); return 0; }
  52. size_t PutEnd(char*) { RAPIDJSON_ASSERT(false); return 0; }
  53. private:
  54. BasicOStreamWrapper(const BasicOStreamWrapper&);
  55. BasicOStreamWrapper& operator=(const BasicOStreamWrapper&);
  56. StreamType& stream_;
  57. };
  58. typedef BasicOStreamWrapper<std::ostream> OStreamWrapper;
  59. typedef BasicOStreamWrapper<std::wostream> WOStreamWrapper;
  60. #ifdef __clang__
  61. RAPIDJSON_DIAG_POP
  62. #endif
  63. RAPIDJSON_NAMESPACE_END
  64. #endif // RAPIDJSON_OSTREAMWRAPPER_H_