MySQL 9.6.0
Source Code Documentation
concat.h
Go to the documentation of this file.
1/* Copyright (c) 2023, 2025, 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/// @file
25///
26/// Convenience function that concatenates arbitrary arguments, by
27/// feeding them to an ostringstream.
28
29#ifndef MYSQL_UTILS_CONCAT_H
30#define MYSQL_UTILS_CONCAT_H
31
32#include <sstream> // ostringstream
33#include <string> // string
34#include <utility> // ignore
35#include "mysql/utils/call_and_catch.h" // call_and_catch
36
37/// @addtogroup GroupLibsMysqlUtils
38/// @{
39
41
42/// Stream all the arguments to a stringstream and return the resulting string.
43///
44/// @tparam Args_t types of the arguments.
45///
46/// @param args Arguments to concatenate.
47///
48/// @return The resulting std::string.
49///
50/// @throws bad_alloc if an out-of-memory condition occurs.
51template <class... Args_t>
52[[nodiscard]] std::string concat(Args_t &&...args) {
54 // std::ignore required to workaround
55 // https://github.com/llvm/llvm-project/issues/140205
56 std::ignore = (out << ... << std::forward<Args_t>(args));
57 return out.str();
58}
59
60} // namespace mysql::utils::throwing
61
62namespace mysql::utils {
63
64/// Stream all the arguments to a stringstream and return the resulting string.
65///
66/// @param args Arguments to concatenate.
67///
68/// @return std::optional<std::string>, holding a value on success, or holding
69/// no value if an out-of-memory condition occurs.
70template <class... Args_t>
71[[nodiscard]] std::optional<std::string> concat(Args_t &&...args) noexcept {
72 return call_and_catch(
73 [&] { return throwing::concat(std::forward<Args_t>(args)...); });
74}
75
76} // namespace mysql::utils
77/// @}
78
79#endif // MYSQL_UTILS_CONCAT_H
Experimental API header.
Definition: concat.h:40
std::string concat(Args_t &&...args)
Stream all the arguments to a stringstream and return the resulting string.
Definition: concat.h:52
Definition: gtid_format.h:47
std::optional< std::string > concat(Args_t &&...args) noexcept
Stream all the arguments to a stringstream and return the resulting string.
Definition: concat.h:71
std::basic_ostringstream< char, std::char_traits< char >, ut::allocator< char > > ostringstream
Specialization of basic_ostringstream which uses ut::allocator.
Definition: ut0new.h:2876