ostringstream.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // Copyright 2017 The Abseil Authors.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // https://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef Y_ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_
  15. #define Y_ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_
  16. #include <cassert>
  17. #include <ios>
  18. #include <ostream>
  19. #include <streambuf>
  20. #include <util/generic/string.h>
  21. #include <utility>
  22. #include "y_absl/base/config.h"
  23. namespace y_absl {
  24. Y_ABSL_NAMESPACE_BEGIN
  25. namespace strings_internal {
  26. // The same as std::ostringstream but appends to a user-specified TString,
  27. // and is faster. It is ~70% faster to create, ~50% faster to write to, and
  28. // completely free to extract the result TString.
  29. //
  30. // TString s;
  31. // OStringStream strm(&s);
  32. // strm << 42 << ' ' << 3.14; // appends to `s`
  33. //
  34. // The stream object doesn't have to be named. Starting from C++11 operator<<
  35. // works with rvalues of std::ostream.
  36. //
  37. // TString s;
  38. // OStringStream(&s) << 42 << ' ' << 3.14; // appends to `s`
  39. //
  40. // OStringStream is faster to create than std::ostringstream but it's still
  41. // relatively slow. Avoid creating multiple streams where a single stream will
  42. // do.
  43. //
  44. // Creates unnecessary instances of OStringStream: slow.
  45. //
  46. // TString s;
  47. // OStringStream(&s) << 42;
  48. // OStringStream(&s) << ' ';
  49. // OStringStream(&s) << 3.14;
  50. //
  51. // Creates a single instance of OStringStream and reuses it: fast.
  52. //
  53. // TString s;
  54. // OStringStream strm(&s);
  55. // strm << 42;
  56. // strm << ' ';
  57. // strm << 3.14;
  58. //
  59. // Note: flush() has no effect. No reason to call it.
  60. class OStringStream final : public std::ostream {
  61. public:
  62. // The argument can be null, in which case you'll need to call str(p) with a
  63. // non-null argument before you can write to the stream.
  64. //
  65. // The destructor of OStringStream doesn't use the TString. It's OK to
  66. // destroy the TString before the stream.
  67. explicit OStringStream(TString* str)
  68. : std::ostream(&buf_), buf_(str) {}
  69. OStringStream(OStringStream&& that)
  70. : std::ostream(std::move(static_cast<std::ostream&>(that))),
  71. buf_(that.buf_) {
  72. rdbuf(&buf_);
  73. }
  74. OStringStream& operator=(OStringStream&& that) {
  75. std::ostream::operator=(std::move(static_cast<std::ostream&>(that)));
  76. buf_ = that.buf_;
  77. rdbuf(&buf_);
  78. return *this;
  79. }
  80. TString* str() { return buf_.str(); }
  81. const TString* str() const { return buf_.str(); }
  82. void str(TString* str) { buf_.str(str); }
  83. private:
  84. class Streambuf final : public std::streambuf {
  85. public:
  86. explicit Streambuf(TString* str) : str_(str) {}
  87. Streambuf(const Streambuf&) = default;
  88. Streambuf& operator=(const Streambuf&) = default;
  89. TString* str() { return str_; }
  90. const TString* str() const { return str_; }
  91. void str(TString* str) { str_ = str; }
  92. protected:
  93. int_type overflow(int c) override;
  94. std::streamsize xsputn(const char* s, std::streamsize n) override;
  95. private:
  96. TString* str_;
  97. } buf_;
  98. };
  99. } // namespace strings_internal
  100. Y_ABSL_NAMESPACE_END
  101. } // namespace y_absl
  102. #endif // Y_ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_