MySQL 9.2.0
Source Code Documentation
All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages Concepts
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 <algorithm>
30#include <concepts>
31#include <cstdlib>
32#include <functional>
33#include <map>
34#include <sstream>
35#include <string>
36#include "harness_export.h"
37
38/**
39 * @defgroup Various operations
40 *
41 * This module contain various utility operations.
42 */
43
44namespace mysql_harness {
45
46/**
47 * Return a truncated version of input string.
48 *
49 * @param str input text
50 * @param max_len maximum length after truncation
51 * @return truncated string
52 */
53HARNESS_EXPORT
54std::string truncate_string(const std::string &str, size_t max_len = 80);
55
56/**
57 * Emit a range of elements using the serial comma.
58 *
59 * This function can be used to output a range of elements using a
60 * serial comma (also known as the Oxford comma). To emit a list of
61 * the first five prime numbers as "The first five prime numbers are
62 * 2, 3, 5, 7, and 11":
63 *
64 * @code
65 * std::vector<int> primes{2, 3, 5, 7, 11};
66 * std::cout << "The first five prime numbers are ";
67 * serial_comma(std::cout, primes.begin(), primes.end());
68 * std::cout << std::endl;
69 * @endcode
70 *
71 * @param out Output stream
72 * @param start Input iterator to start of range.
73 * @param finish Input iterator to one-after-end of range.
74 * @param delim Delimiter to use. Defaults to "and".
75 */
76template <class InputIt>
77void serial_comma(std::ostream &out, InputIt start, InputIt finish,
78 const std::string &delim = "and") {
79 auto elements = std::distance(start, finish);
80 if (elements == 1) {
81 out << *start;
82 } else if (elements == 2) {
83 out << *start++;
84 out << " " << delim << " " << *start;
85 } else {
86 while (elements-- > 0) {
87 out << *start++;
88 if (elements > 0) out << ", ";
89 if (elements == 1) out << delim << " ";
90 }
91 }
92}
93
94/**
95 * Returns string containing list of the elements using the serial comma.
96 *
97 * This function can be used to output a range of elements using a
98 * serial comma (also known as the Oxford comma). To return a list of
99 * the first five prime numbers as "The first five prime numbers are
100 * 2, 3, 5, 7, and 11":
101 *
102 * @code
103 * std::vector<int> primes{2, 3, 5, 7, 11};
104 * std::cout << "The first five prime numbers are "
105 * << serial_comma(primes.begin(), primes.end()) << std::endl;
106 * @endcode
107 *
108 * @param start Input iterator to start of range.
109 * @param finish Input iterator to one-after-end of range.
110 * @param delim Delimiter to use. Defaults to "and".
111 *
112 * @return string containing list of the elements
113 */
114template <class InputIt>
115std::string serial_comma(InputIt start, InputIt finish,
116 const std::string &delim = "and") {
117 std::stringstream out;
118 serial_comma(out, start, finish, delim);
119
120 return out.str();
121}
122
123/**
124 * Gets a Value from std::map for given Key. Returns provided default if the Key
125 * is not in the map.
126 */
127template <class Key, class Value>
128Value get_from_map(const std::map<Key, Value> &map, const Key &key,
129 const Value &default_value) {
130 auto iter = map.find(key);
131 if (iter == map.end()) return default_value;
132 return iter->second;
133}
134
135} // namespace mysql_harness
136
137#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:1117
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:108
Definition: common.h:44
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:128
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:77
std::map< Key, Value, Compare, ut::allocator< std::pair< const Key, Value > > > map
Specialization of map which uses ut_allocator.
Definition: ut0new.h:2894
required string key
Definition: replication_asynchronous_connection_failover.proto:60