MySQL 9.6.0
Source Code Documentation
encode_debug.h
Go to the documentation of this file.
1// Copyright (c) 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#ifndef MYSQL_STRCONV_ENCODE_ENCODE_DEBUG_H
25#define MYSQL_STRCONV_ENCODE_ENCODE_DEBUG_H
26
27/// @file
28/// Experimental API header
29
30#include "mysql/strconv/encode/concat.h" // concat
31#include "mysql/strconv/encode/encode.h" // compute_encoded_length
32#include "mysql/strconv/formats/debug_format.h" // Debug_format
33
34/// @addtogroup GroupLibsMysqlStrconv
35/// @{
36
37namespace mysql::strconv {
38
39/// Return the string length of the object, using Debug_format.
40///
41/// @note If you plan to allocate memory for a null-terminated string, remember
42/// to add 1 byte for the trailing '\0'.
43template <class Object_t>
44[[nodiscard]] std::size_t compute_encoded_length_debug(const Object_t &object) {
45 return compute_encoded_length(Debug_format{}, object);
46}
47
48/// Write the string representation of the object to the given string output
49/// wrapper, using Debug_format.
50///
51/// This overload is for the case that the output wrapper's resize policy is
52/// `fixed`, i.e., the caller guarantees that the output buffer has enough space
53/// to store the output. Therefore, this function cannot fail and does not have
54/// the `[[nodiscard]]` attribute.
55///
56/// @tparam Out_str_t Type of output string wrapper to write to.
57///
58/// @tparam Object_t Type of object.
59///
60/// param[in,out] out_str Output string wrapper to write to.
61///
62/// param object Object to write.
63///
64/// @return Return_status::ok on success, Return_status::error on allocation
65/// failure.
66///
67/// @note The caller must ensure that @c out is big enough. The length can be
68/// determined using compute_encoded_length(object).
69//
70// Doxygen complains that the parameters are duplicated (maybe mixing up
71// overloads with different constraints). We work around that by using param
72// instead of @param.
73template <Is_out_str_fixed Out_str_t, class Object_t>
74auto encode_debug(Out_str_t out_str, const Object_t &object) {
75 return encode(Debug_format{}, out_str, object);
76}
77
78/// Write the string representation of the object to the given string output
79/// wrapper, using Debug_format.
80///
81/// This overload is for the case that the output wrapper's resize policy is
82/// `growable`. Therefore, this function can fail to allocate memory and has the
83/// `[[nodiscard]]` attribute.
84///
85/// @tparam Out_str_t Output string wrapper to write to.
86///
87/// @tparam Object_t Type of object.
88///
89/// @param[in,out] out_str Output string wrapper to write to.
90///
91/// @param object Object to write.
92///
93/// @return Return_status::ok on success, Return_status::error on allocation
94/// failure.
95///
96/// @note The caller must ensure that @c out is big enough. The length can be
97/// determined using compute_encoded_length(object).
98template <Is_out_str_growable Out_str_t, class Object_t>
99[[nodiscard]] auto encode_debug(Out_str_t out_str, const Object_t &object) {
100 return encode(Debug_format{}, out_str, object);
101}
102
103namespace throwing {
104
105/// Return an std::string object holding the string representation of the given
106/// object, using Debug_format.
107///
108/// @tparam String_t Output string type. Defaults to std::string.
109///
110/// @tparam Object_t Type of object.
111///
112/// @param object Object to write.
113///
114/// @return String representation of object.
115///
116/// @throws bad_alloc if an out-of-memory condition occurs.
117template <class String_t = std::string, class Object_t>
118[[nodiscard]] String_t encode_debug(const Object_t &object) {
119 return mysql::strconv::throwing::encode<String_t>(Debug_format{}, object);
120}
121
122/// Return an std::string object holding the concatenated string representations
123/// of the objects, using Debug_format.
124///
125/// @tparam String_t Output string type. Defaults to std::string.
126///
127/// @tparam Objects_t Types of objects.
128///
129/// @param objects Objects to write.
130///
131/// @return Concatenated string representations of objects.
132///
133/// @throws bad_alloc if an out-of-memory condition occurs.
134template <class String_t = std::string, class... Objects_t>
135[[nodiscard]] String_t concat_debug(const Objects_t &...objects) {
136 return mysql::strconv::throwing::concat<String_t>(Debug_format{}, objects...);
137}
138
139} // namespace throwing
140
141/// Return an std::optional<std::string> object holding the string
142/// representation of the object, using Debug_format.
143///
144/// @tparam String_t Output string type. Defaults to std::string.
145///
146/// @tparam Object_t Type of object.
147///
148/// @param object Object to write.
149///
150/// @returns std::optional<std::string> object holding the string representation
151/// of @c object, or holding no value if an out-of-memory condition occurred.
152template <class String_t = std::string, class Object_t>
153[[nodiscard]] std::optional<String_t> encode_debug(const Object_t &object) {
154 return encode<String_t>(Debug_format{}, object);
155}
156
157/// Return an std::optional<std::string> object holding the concatenated string
158/// representations of the objects, using Debug_format.
159///
160/// @tparam String_t Output string type. Defaults to std::string.
161///
162/// @tparam Objects_t Types of objects.
163///
164/// @param objects Objects to write.
165///
166/// @returns std::optional<std::string> object holding the concatenated string
167/// representations of @c objects, or holding no value if an out-of-memory
168/// condition occurred.
169template <class String_t = std::string, class... Objects_t>
170[[nodiscard]] auto concat_debug(const Objects_t &...objects) {
171 return concat<String_t>(Debug_format{}, objects...);
172}
173
174} // namespace mysql::strconv
175
176// addtogroup GroupLibsMysqlStrconv
177/// @}
178
179#endif // ifndef MYSQL_STRCONV_ENCODE_ENCODE_DEBUG_H
Experimental API header.
String_t concat_debug(const Objects_t &...objects)
Return an std::string object holding the concatenated string representations of the objects,...
Definition: encode_debug.h:135
String_t encode_debug(const Object_t &object)
Return an std::string object holding the string representation of the given object,...
Definition: encode_debug.h:118
Definition: gtid_binary_format.h:41
void encode(const Debug_format &format, Is_string_target auto &target, const Repeat &repeat)
Definition: debug_repeat.h:39
std::size_t compute_encoded_length_debug(const Object_t &object)
Return the string length of the object, using Debug_format.
Definition: encode_debug.h:44
std::size_t compute_encoded_length(const Is_format auto &format, const auto &object)
Return the string length of the object.
Definition: encode.h:57
auto concat_debug(const Objects_t &...objects)
Return an std::optional<std::string> object holding the concatenated string representations of the ob...
Definition: encode_debug.h:170
auto encode_debug(Out_str_t out_str, const Object_t &object)
Write the string representation of the object to the given string output wrapper, using Debug_format.
Definition: encode_debug.h:74
Experimental API header.
Experimental API header.
Format tag to identify debug format.
Definition: debug_format.h:44