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


grpc
Author(s):
autogenerated on Fri May 16 2025 02:59:36