MySQL 9.1.0
Source Code Documentation
targeted_stringstream.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2024, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24#ifndef RAII_TARGETED_STRINGSTREAM_H_INCLUDED
25#define RAII_TARGETED_STRINGSTREAM_H_INCLUDED
26
27#include <functional>
28#include <sstream>
29#include <string>
30
31namespace raii {
32
33/// Like std::stringstream, copying to a given target string at
34/// destruction.
35///
36/// This is a convenience helper, allowing the use of:
37/// @code
38/// Targeted_stringstream(s) << foo << bar << baz;
39/// @endcode
40/// instead of:
41/// @code
42/// std::stringstream stream;
43/// stream << foo << bar << baz;
44/// s.assign(stream.str());
45/// @endcode
46/// This can, for instance, be used by a class to export a stream
47/// interface to update private string members.
49 public:
50 explicit Targeted_stringstream(
51 std::string &target, const std::string &suffix = "",
52 const std::function<void(const std::string &)> &callback = nullptr);
58 template <class T>
60 const T &value);
61 template <class T>
63 const T &value);
64
65 private:
67 std::string &m_target;
68 std::string m_suffix;
70 std::function<void(std::string &)> m_callback;
71};
72
73template <class T>
75 const T &value) {
76 stream.m_stream << value;
77 return stream;
78}
79
80template <class T>
82 const T &value) {
83 stream.m_stream << value;
84 return stream;
85}
86
87} // namespace raii
88
89#endif // ifndef RAII_TARGETED_STRINGSTREAM_H_INCLUDED
Like std::stringstream, copying to a given target string at destruction.
Definition: targeted_stringstream.h:48
bool m_active
Definition: targeted_stringstream.h:66
friend Targeted_stringstream & operator<<(Targeted_stringstream &stream, const T &value)
Definition: targeted_stringstream.h:81
std::ostringstream m_stream
Definition: targeted_stringstream.h:69
Targeted_stringstream & operator=(const Targeted_stringstream &)=delete
std::function< void(std::string &)> m_callback
Definition: targeted_stringstream.h:70
std::string m_suffix
Definition: targeted_stringstream.h:68
std::string & m_target
Definition: targeted_stringstream.h:67
Targeted_stringstream(std::string &target, const std::string &suffix="", const std::function< void(const std::string &)> &callback=nullptr)
Definition: targeted_stringstream.cc:28
~Targeted_stringstream()
Definition: targeted_stringstream.cc:57
Targeted_stringstream(const Targeted_stringstream &)=delete
Definition: sentry.h:29
Targeted_stringstream & operator<<(Targeted_stringstream &&stream, const T &value)
Definition: targeted_stringstream.h:74
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2872