summaryrefslogtreecommitdiff
path: root/absl/strings/internal/ostringstream.h
diff options
context:
space:
mode:
authorGravatar Andy Getzendanner <durandal@google.com>2022-09-14 09:23:31 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2022-09-14 09:24:31 -0700
commitd423ac0ef052bd7b6fc53fd1a026a44e1713d993 (patch)
tree8169581dc63f2d180cf4c57be960de0178a0d171 /absl/strings/internal/ostringstream.h
parent4832049e5c050cd4b50182d0fc061b99bf64b4b6 (diff)
Implement correct move constructor and assignment for absl::strings_internal::OStringStream, and mark that class final.
This should be explicit per https://google.github.io/styleguide/cppguide.html#Copyable_Movable_Types, and is currently hard to infer due to multiple inheritance. PiperOrigin-RevId: 474311032 Change-Id: I72d7adcb9f7a991c19c26bc7083a4df31de5da49
Diffstat (limited to 'absl/strings/internal/ostringstream.h')
-rw-r--r--absl/strings/internal/ostringstream.h45
1 files changed, 35 insertions, 10 deletions
diff --git a/absl/strings/internal/ostringstream.h b/absl/strings/internal/ostringstream.h
index d25d6047..c0e237db 100644
--- a/absl/strings/internal/ostringstream.h
+++ b/absl/strings/internal/ostringstream.h
@@ -16,11 +16,13 @@
#define ABSL_STRINGS_INTERNAL_OSTRINGSTREAM_H_
#include <cassert>
+#include <ios>
#include <ostream>
#include <streambuf>
#include <string>
+#include <utility>
-#include "absl/base/port.h"
+#include "absl/base/config.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
@@ -60,26 +62,49 @@ namespace strings_internal {
// strm << 3.14;
//
// Note: flush() has no effect. No reason to call it.
-class OStringStream : private std::basic_streambuf<char>, public std::ostream {
+class OStringStream final : public std::ostream {
public:
// The argument can be null, in which case you'll need to call str(p) with a
// non-null argument before you can write to the stream.
//
// The destructor of OStringStream doesn't use the std::string. It's OK to
// destroy the std::string before the stream.
- explicit OStringStream(std::string* s) : std::ostream(this), s_(s) {}
+ explicit OStringStream(std::string* str)
+ : std::ostream(&buf_), buf_(str) {}
+ OStringStream(OStringStream&& that)
+ : std::ostream(std::move(static_cast<std::ostream&>(that))),
+ buf_(that.buf_) {
+ rdbuf(&buf_);
+ }
+ OStringStream& operator=(OStringStream&& that) {
+ std::ostream::operator=(std::move(static_cast<std::ostream&>(that)));
+ buf_ = that.buf_;
+ rdbuf(&buf_);
+ return *this;
+ }
- std::string* str() { return s_; }
- const std::string* str() const { return s_; }
- void str(std::string* s) { s_ = s; }
+ std::string* str() { return buf_.str(); }
+ const std::string* str() const { return buf_.str(); }
+ void str(std::string* str) { buf_.str(str); }
private:
- using Buf = std::basic_streambuf<char>;
+ class Streambuf final : public std::streambuf {
+ public:
+ explicit Streambuf(std::string* str) : str_(str) {}
+ Streambuf(const Streambuf&) = default;
+ Streambuf& operator=(const Streambuf&) = default;
- Buf::int_type overflow(int c) override;
- std::streamsize xsputn(const char* s, std::streamsize n) override;
+ std::string* str() { return str_; }
+ const std::string* str() const { return str_; }
+ void str(std::string* str) { str_ = str; }
- std::string* s_;
+ protected:
+ int_type overflow(int c) override;
+ std::streamsize xsputn(const char* s, std::streamsize n) override;
+
+ private:
+ std::string* str_;
+ } buf_;
};
} // namespace strings_internal