ostreamwrapper.h 2.3 KB

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