ostringstream.h 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_
  15. #define ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_
  16. #include <cassert>
  17. #include <ostream>
  18. #include <streambuf>
  19. #include <string>
  20. #include "absl/base/port.h"
  21. namespace absl {
  22. ABSL_NAMESPACE_BEGIN
  23. namespace strings_internal {
  24. // The same as std::ostringstream but appends to a user-specified std::string,
  25. // and is faster. It is ~70% faster to create, ~50% faster to write to, and
  26. // completely free to extract the result std::string.
  27. //
  28. // std::string s;
  29. // OStringStream strm(&s);
  30. // strm << 42 << ' ' << 3.14; // appends to `s`
  31. //
  32. // The stream object doesn't have to be named. Starting from C++11 operator<<
  33. // works with rvalues of std::ostream.
  34. //
  35. // std::string s;
  36. // OStringStream(&s) << 42 << ' ' << 3.14; // appends to `s`
  37. //
  38. // OStringStream is faster to create than std::ostringstream but it's still
  39. // relatively slow. Avoid creating multiple streams where a single stream will
  40. // do.
  41. //
  42. // Creates unnecessary instances of OStringStream: slow.
  43. //
  44. // std::string s;
  45. // OStringStream(&s) << 42;
  46. // OStringStream(&s) << ' ';
  47. // OStringStream(&s) << 3.14;
  48. //
  49. // Creates a single instance of OStringStream and reuses it: fast.
  50. //
  51. // std::string s;
  52. // OStringStream strm(&s);
  53. // strm << 42;
  54. // strm << ' ';
  55. // strm << 3.14;
  56. //
  57. // Note: flush() has no effect. No reason to call it.
  58. class OStringStream : private std::basic_streambuf<char>, public std::ostream {
  59. public:
  60. // The argument can be null, in which case you'll need to call str(p) with a
  61. // non-null argument before you can write to the stream.
  62. //
  63. // The destructor of OStringStream doesn't use the std::string. It's OK to
  64. // destroy the std::string before the stream.
  65. explicit OStringStream(std::string* s) : std::ostream(this), s_(s) {}
  66. std::string* str() { return s_; }
  67. const std::string* str() const { return s_; }
  68. void str(std::string* s) { s_ = s; }
  69. private:
  70. using Buf = std::basic_streambuf<char>;
  71. Buf::int_type overflow(int c) override;
  72. std::streamsize xsputn(const char* s, std::streamsize n) override;
  73. std::string* s_;
  74. };
  75. } // namespace strings_internal
  76. ABSL_NAMESPACE_END
  77. } // namespace absl
  78. #endif // ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_