MySQL 9.6.0
Source Code Documentation
encode.h
Go to the documentation of this file.
1// Copyright (c) 2024, 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_H
25#define MYSQL_STRCONV_ENCODE_ENCODE_H
26
27/// @file
28/// Experimental API header
29
30#include <ios> // ios_base
31#include <ostream> // ostream
32#include <string> // string
33#include "mysql/meta/is_specialization.h" // Is_specialization
34#include "mysql/strconv/encode/out_str.h" // Is_out_str_fixed
35#include "mysql/strconv/encode/out_str_write.h" // out_str_write
36#include "mysql/strconv/encode/string_target.h" // Constructible_string_writer
37#include "mysql/strconv/formats/format.h" // class
38#include "mysql/utils/call_and_catch.h" // call_and_catch
39
40/// @addtogroup GroupLibsMysqlStrconv
41/// @{
42
43namespace mysql::strconv {
44
45/// Return the string length of the object.
46///
47/// @param format Description of output format.
48///
49/// @param object Object for which the size of the string representation shall
50/// be computed.
51///
52/// @note If you plan to allocate memory for a null-terminated string, remember
53/// to add 1 byte for the trailing '\0'.
54///
55/// @return Size of the string representation of `object`, not counting any
56/// trailing '\0' characters.
57[[nodiscard]] std::size_t compute_encoded_length(const Is_format auto &format,
58 const auto &object) {
60 counter.write(format, object);
61 return counter.size();
62}
63
64/// Write the string representation of the object to the given string output
65/// wrapper.
66///
67/// This overload is for the case that the output wrapper's resize policy is
68/// `fixed`, i.e., the caller guarantees that the output buffer has enough space
69/// to store the output. Therefore, this function cannot fail and returns void.
70///
71/// param format Description of output format.
72///
73/// param[in,out] out_str Output string wrapper to write to.
74///
75/// param object Object to write.
76///
77/// @note The caller must ensure that @c out is big enough. The length can be
78/// determined using compute_encoded_length(object).
79//
80// Doxygen complains that the parameters are duplicated (maybe mixing up
81// overloads with different constraints). We work around that by using param
82// instead of @param.
83void encode(const Is_format auto &format, const Is_out_str_fixed auto &out_str,
84 const auto &object) {
85 out_str_write(out_str, [&](Is_string_target auto &target) {
86 target.write(format, object);
87 });
88}
89
90/// Write the string representation of the object to the given string output
91/// wrapper.
92///
93/// This overload is for the case that the output wrapper's resize policy is
94/// `growable`. Therefore, this function can fail to allocate memory and has the
95/// `[[nodiscard]]` attribute.
96///
97/// @param format Description of output format.
98///
99/// @param[in,out] out_str Output string wrapper to write to.
100///
101/// @param object Object to write.
102///
103/// @return Return_status::ok on success, Return_status::error on out-of-memory
104/// error.
105///
106/// @note The caller must ensure that @c out is big enough. The length can be
107/// determined using compute_encoded_length(object).
108[[nodiscard]] auto encode(const Is_format auto &format,
109 const Is_out_str_growable auto &out_str,
110 const auto &object) {
111 return out_str_write(out_str, [&](Is_string_target auto &target) {
112 target.write(format, object);
113 });
114}
115
116namespace throwing {
117
118/// Return an std::string object holding the string representation of the given
119/// object.
120///
121/// @tparam String_t Output string type. Defaults to std::string.
122///
123/// @param format Description of output format.
124///
125/// @param object Object to write.
126///
127/// @return String representation of object.
128///
129/// @throws bad_alloc if an out-of-memory condition occurs.
130template <class String_t = std::string>
131[[nodiscard]] String_t encode(const Is_format auto &format,
132 const auto &object) {
133 String_t ret;
136 throw std::bad_alloc();
137 return ret;
138}
139
140} // namespace throwing
141
142/// Return an std::optional<std::string> object holding the string
143/// representation of the object.
144///
145/// @tparam String_t Output string type. Defaults to std::string.
146///
147/// @param format Description of the output format.
148///
149/// @param object Object to write.
150///
151/// @returns std::optional<std::string> object holding the string representation
152/// of @c object, or holding no value if an out-of-memory condition occurred.
153template <class String_t = std::string>
154[[nodiscard]] std::optional<String_t> encode(const Is_format auto &format,
155 const auto &object) {
156 String_t ret;
159 return {};
160 return ret;
161}
162
163/// Wrapper around an std::optional<std::string> object returned from encode,
164/// intended to be passed to operator<<, allowing it to implement
165/// ostream-idiomatic error handling.
166///
167/// @see encode_to_streamable
168template <class String_t = std::string>
170 public:
171 using Opt_string = std::optional<String_t>;
172
173 /// Construct a new Streamable from an expiring optional string.
174 explicit Streamable(Opt_string &&other) : m_opt_string(std::move(other)) {}
175
176 /// Return reference to the optional string stored in this object.
177 const Opt_string &opt_string_ref() const { return m_opt_string; }
178
179 private:
180 /// Stored optional string object.
182};
183
184/// Return a Streamable object holding the result from `encode(format,
185/// object)`. If the Streamable object is passed to operator<<, it will
186/// write the string if any, or handle the allocation error according to the
187/// stream's policy, if the allocation in `encode` failed.
188///
189/// @tparam String_t Output string type. Defaults to std::string.
190///
191/// @param format Description of the output format.
192///
193/// @param object Object to write.
194///
195/// @return Streamable object that wraps the std::optional<std::string> returned
196/// from `encode`.
197///
198/// Example:
199///
200/// @code
201/// // If allocation fails while allocating a temporary string to hold the
202/// // text form of `object`, set the stream status accordingly.
203/// s << encode_to_streamable(Text_format{}, object);
204/// @endcode
205template <class String_t = std::string>
207 const auto &object) {
208 return Streamable<String_t>(encode<String_t>(format, object));
209}
210
211/// Use output operator to write the string representation of a format-tagged
212/// object to the given ostream.
213///
214/// @param[out] out ostream to write to.
215///
216/// @param streamable Streamable object to write.
217///
218/// @returns out.
219///
220/// @throws std::ios_base::failure if streamable does not hold a value and
221/// out.exceptions() has the badbit set.
222///
223/// @note If @c streamable does not hold a value, this sets badbit in out
224/// (regardless if it throws or not), and it does not write to the output.
225std::ostream &operator<<(
226 std::ostream &out,
227 const mysql::meta::Is_specialization<Streamable> auto &streamable) {
228 auto &opt_string_ref = streamable.opt_string_ref();
229 if (opt_string_ref.has_value()) {
230 out << opt_string_ref.value();
231 } else {
232 out.setstate(std::ios_base::badbit);
233 if ((out.exceptions() & std::ios_base::badbit) != 0)
234 throw std::ios_base::failure("Out of memory");
235 }
236 return out;
237}
238
239} // namespace mysql::strconv
240
241// addtogroup GroupLibsMysqlStrconv
242/// @}
243
244#endif // ifndef MYSQL_STRCONV_ENCODE_ENCODE_H
Experimental API header.
Wrapper around an std::optional<std::string> object returned from encode, intended to be passed to op...
Definition: encode.h:169
std::optional< String_t > Opt_string
Definition: encode.h:171
Opt_string m_opt_string
Stored optional string object.
Definition: encode.h:181
Streamable(Opt_string &&other)
Construct a new Streamable from an expiring optional string.
Definition: encode.h:174
const Opt_string & opt_string_ref() const
Return reference to the optional string stored in this object.
Definition: encode.h:177
String_counter subclass that can be instantiated.
Definition: string_counter.h:97
Concept used to determine at compile time whether a given type Test is a template specialization of t...
Definition: is_specialization.h:68
True if Test is a format.
Definition: format.h:42
True if Test is an Output String Wrapper with Resize_policy fixed.
Definition: out_str.h:235
True if Test is an Output String Wrapper with Resize_policy growable.
Definition: out_str.h:240
Concept that holds for String_counter and String_writer.
Definition: string_target.h:111
Experimental API header.
Experimental API header.
uint counter
Definition: mysqlimport.cc:58
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
String_t encode(const Is_format auto &format, const auto &object)
Return an std::string object holding the string representation of the given object.
Definition: encode.h:131
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::ostream & operator<<(std::ostream &out, const mysql::meta::Is_specialization< Streamable > auto &streamable)
Use output operator to write the string representation of a format-tagged object to the given ostream...
Definition: encode.h:225
Streamable< String_t > encode_to_streamable(const Is_format auto &format, const auto &object)
Return a Streamable object holding the result from encode(format, object).
Definition: encode.h:206
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
mysql::utils::Return_status out_str_write(const Out_str_t &out_str, const Producer_counter_t &producer_counter, const Producer_writer_t &producer_writer, const Oom_action_t &oom_action=detail::nop)
Given an Is_out_str object, a String_producer_counter, and a String_producer_writer,...
Definition: out_str_write.h:178
auto out_str_growable(String_t &str)
Return a wrapper around a growable output buffer, represented as a std::string or similar type.
Definition: out_str.h:985
@ ok
operation succeeded
Define std::hash<Gtid>.
Definition: gtid.h:355
Experimental API header.
Experimental API header.
Experimental API header.