MySQL 8.4.2
Source Code Documentation
common.h
Go to the documentation of this file.
1/*
2 Copyright (c) 2016, 2024, Oracle and/or its affiliates.
3
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License, version 2.0,
6 as published by the Free Software Foundation.
7
8 This program is designed to work with certain software (including
9 but not limited to OpenSSL) that is licensed under separate terms,
10 as designated in a particular file or component or in included license
11 documentation. The authors of MySQL hereby grant you an additional
12 permission to link the program and your derivative works with the
13 separately licensed software that they have either included with
14 the program or referenced in the documentation.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
24*/
25
26#ifndef MYSQL_HARNESS_COMMON_INCLUDED
27#define MYSQL_HARNESS_COMMON_INCLUDED
28
29#include <cstdlib>
30#include <functional>
31#include <map>
32#include <sstream>
33#include <string>
34#include "harness_export.h"
35
36/**
37 * @defgroup Various operations
38 *
39 * This module contain various utility operations.
40 */
41
42namespace mysql_harness {
43
44/**
45 * Return a truncated version of input string.
46 *
47 * @param str input text
48 * @param max_len maximum length after truncation
49 * @return truncated string
50 */
51HARNESS_EXPORT
52std::string truncate_string(const std::string &str, size_t max_len = 80);
53
54/**
55 * Emit a range of elements using the serial comma.
56 *
57 * This function can be used to output a range of elements using a
58 * serial comma (also known as the Oxford comma). To emit a list of
59 * the first five prime numbers as "The first five prime numbers are
60 * 2, 3, 5, 7, and 11":
61 *
62 * @code
63 * std::vector<int> primes{2, 3, 5, 7, 11};
64 * std::cout << "The first five prime numbers are ";
65 * serial_comma(std::cout, primes.begin(), primes.end());
66 * std::cout << std::endl;
67 * @endcode
68 *
69 * @param out Output stream
70 * @param start Input iterator to start of range.
71 * @param finish Input iterator to one-after-end of range.
72 * @param delim Delimiter to use. Defaults to "and".
73 */
74template <class InputIt>
75void serial_comma(std::ostream &out, InputIt start, InputIt finish,
76 const std::string &delim = "and") {
77 auto elements = std::distance(start, finish);
78 if (elements == 1) {
79 out << *start;
80 } else if (elements == 2) {
81 out << *start++;
82 out << " " << delim << " " << *start;
83 } else {
84 while (elements-- > 0) {
85 out << *start++;
86 if (elements > 0) out << ", ";
87 if (elements == 1) out << delim << " ";
88 }
89 }
90}
91
92/**
93 * Returns string containing list of the elements using the serial comma.
94 *
95 * This function can be used to output a range of elements using a
96 * serial comma (also known as the Oxford comma). To return a list of
97 * the first five prime numbers as "The first five prime numbers are
98 * 2, 3, 5, 7, and 11":
99 *
100 * @code
101 * std::vector<int> primes{2, 3, 5, 7, 11};
102 * std::cout << "The first five prime numbers are "
103 * << serial_comma(primes.begin(), primes.end()) << std::endl;
104 * @endcode
105 *
106 * @param start Input iterator to start of range.
107 * @param finish Input iterator to one-after-end of range.
108 * @param delim Delimiter to use. Defaults to "and".
109 *
110 * @return string containing list of the elements
111 */
112template <class InputIt>
113std::string serial_comma(InputIt start, InputIt finish,
114 const std::string &delim = "and") {
115 std::stringstream out;
116 serial_comma(out, start, finish, delim);
117
118 return out.str();
119}
120
121/**
122 * Gets a Value from std::map for given Key. Returns provided default if the Key
123 * is not in the map.
124 */
125template <class Key, class Value>
126Value get_from_map(const std::map<Key, Value> &map, const Key &key,
127 const Value &default_value) {
128 auto iter = map.find(key);
129 if (iter == map.end()) return default_value;
130 return iter->second;
131}
132
133} // namespace mysql_harness
134
135#endif /* MYSQL_HARNESS_COMMON_INCLUDED */
static void start(mysql_harness::PluginFuncEnv *env)
Definition: http_auth_backend_plugin.cc:180
std::string str(const mysqlrouter::ConfigGenerator::Options::Endpoint &ep)
Definition: config_generator.cc:1081
bool distance(const dd::Spatial_reference_system *srs, const Geometry *g1, const Geometry *g2, double *distance, bool *is_null) noexcept
Computes the distance between two geometries.
Definition: distance.cc:40
std::string_view Key
The key type for the hash structure in HashJoinRowBuffer.
Definition: hash_join_buffer.h:102
Definition: common.h:42
HARNESS_EXPORT std::string truncate_string(const std::string &str, size_t max_len=80)
Return a truncated version of input string.
Definition: common.cc:32
Value get_from_map(const std::map< Key, Value > &map, const Key &key, const Value &default_value)
Gets a Value from std::map for given Key.
Definition: common.h:126
void serial_comma(std::ostream &out, InputIt start, InputIt finish, const std::string &delim="and")
Emit a range of elements using the serial comma.
Definition: common.h:75
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2892
required string key
Definition: replication_asynchronous_connection_failover.proto:60