MySQL 9.6.0
Source Code Documentation
string_writer.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_STRING_WRITER_H
25#define MYSQL_STRCONV_ENCODE_STRING_WRITER_H
26
27/// @file
28/// Experimental API header
29
30#include <cassert> // assert
31#include <cstring> // memcpy
32#include <string_view> // string_view
33#include "mysql/strconv/encode/out_str.h" // Is_out_str
34#include "mysql/strconv/encode/string_target.h" // String_target_interface
35
36/// @addtogroup GroupLibsMysqlStrconv
37/// @{
38
39namespace mysql::strconv {
40
41/// Class that serves as the target for encode(..., Is_string_target), which
42/// writes to a `char *` buffer without bounds checking.
43class String_writer : public detail::String_target_interface<String_writer> {
44 protected:
45 /// Construct a new object backed by the given buffer. The caller must ensure
46 /// that the buffer has space for everything that will be written to it.
47 ///
48 /// This is hidden from user code. These objects are only meant to be
49 /// created internally by the framework.
50 ///
51 /// @param first Pointer to the first character of the output buffer.
52 ///
53 /// @param last Pointer to the one-past-last character of the output buffer.
54 String_writer(char *first, char *last) : m_pos(first), m_end(last) {}
55
56 public:
58
59 // Allow move, but not copy.
60 //
61 // Deleting copy semantics protects against the mistake of making a
62 // `encode_impl` function take its second parameter by value. And it does
63 // not make sense to have two `String_writers` pointing to the same back-end
64 // buffer.
65 String_writer(const String_writer &) = delete;
67 String_writer &operator=(const String_writer &) = delete;
68 String_writer &operator=(String_writer &&) noexcept = default;
69 ~String_writer() = default;
70
71 /// Append a string_view to the buffer, unformatted.
72 ///
73 /// @param sv String to write.
74 void write_raw(const std::string_view &sv) {
75 assert(remaining_size() >= sv.size());
76 std::memcpy(m_pos, sv.data(), sv.size());
77 advance(sv.size());
78 }
79
80 /// Append a single character to the buffer.
81 ///
82 /// @param ch Character to write.
83 void write_char(int ch) {
84 assert(m_end > m_pos);
85 *m_pos = ch;
86 advance(1);
87 }
88
89 /// Write the given object to this String_writer.
90 void write(const Is_format auto &format, const auto &object) {
91 assert(remaining_size() >= compute_encoded_length(format, object));
93 }
94
95 /// Write the given string_view to this String_writer.
96 void write(const Is_format auto &format, const std::string_view &sv) {
99 }
100
101 /// Move the position `size` bytes forward without writing anything.
102 ///
103 /// @note The characters are left uninitialized. The caller must initialize
104 /// them.
105 void advance(std::size_t size) {
106 assert(remaining_size() >= size);
107 m_pos += size;
108 }
109
110 /// Return the current write position as a char *.
111 [[nodiscard]] char *pos() { return m_pos; }
112
113 /// Return the current write position as a const char *.
114 [[nodiscard]] const char *pos() const { return m_pos; }
115
116 /// Return the current write position as an unsigned char *.
117 [[nodiscard]] unsigned char *upos() {
118 return reinterpret_cast<unsigned char *>(m_pos);
119 }
120
121 /// Return the current write position as a const unsigned char *.
122 [[nodiscard]] const unsigned char *upos() const {
123 return reinterpret_cast<unsigned char *>(m_pos);
124 }
125
126 /// Return the current write position as a std::byte *.
127 [[nodiscard]] std::byte *bpos() {
128 return reinterpret_cast<std::byte *>(m_pos);
129 }
130
131 /// Return the current write position as a const std::byte *.
132 [[nodiscard]] const std::byte *bpos() const {
133 return reinterpret_cast<std::byte *>(m_pos);
134 }
135
136 /// Return the buffer end as a char *.
137 [[nodiscard]] char *end() { return m_end; }
138
139 /// Return the buffer end as a const char *.
140 [[nodiscard]] const char *end() const { return m_end; }
141
142 /// Return the buffer end as an unsigned char *.
143 [[nodiscard]] unsigned char *uend() {
144 return reinterpret_cast<unsigned char *>(m_end);
145 }
146
147 /// Return the buffer end as a const unsigned char *.
148 [[nodiscard]] const unsigned char *uend() const {
149 return reinterpret_cast<unsigned char *>(m_end);
150 }
151
152 /// Return the buffer end as a std::byte *.
153 [[nodiscard]] std::byte *bend() {
154 return reinterpret_cast<std::byte *>(m_end);
155 }
156
157 /// Return the buffer end as a const std::byte *.
158 [[nodiscard]] const std::byte *bend() const {
159 return reinterpret_cast<std::byte *>(m_end);
160 }
161
162 // Returning the distance from the current position to the end of the buffer.
163 [[nodiscard]] std::size_t remaining_size() const {
164 return std::size_t(m_end - m_pos);
165 }
166
167 private:
168 /// The current write position.
169 char *m_pos;
170 /// End of buffer.
171 char *m_end;
172}; // class String_writer
173
174} // namespace mysql::strconv
175
176namespace mysql::strconv::detail {
177
178/// String_writer subclass that can be instantiated.
179///
180/// We hide this in the detail namespace because the class is not supposed to be
181/// instantiated in user code, only in this framework.
183 public:
184 explicit Constructible_string_writer(const Is_out_str auto &out_str)
185 : String_writer(out_str.data(), out_str.end()) {}
186};
187
188static_assert(!std::copy_constructible<Constructible_string_writer>);
189static_assert(!std::copy_constructible<String_writer>);
190static_assert(std::movable<Constructible_string_writer>);
191static_assert(std::movable<String_writer>);
192
193} // namespace mysql::strconv::detail
194
195// addtogroup GroupLibsMysqlStrconv
196/// @}
197
198#endif // ifndef MYSQL_STRCONV_ENCODE_STRING_WRITER_H
Class that serves as the target for encode(..., Is_string_target), which writes to a char * buffer wi...
Definition: string_writer.h:43
const char * end() const
Return the buffer end as a const char *.
Definition: string_writer.h:140
void advance(std::size_t size)
Move the position size bytes forward without writing anything.
Definition: string_writer.h:105
void write_raw(const std::string_view &sv)
Append a string_view to the buffer, unformatted.
Definition: string_writer.h:74
unsigned char * uend()
Return the buffer end as an unsigned char *.
Definition: string_writer.h:143
const std::byte * bend() const
Return the buffer end as a const std::byte *.
Definition: string_writer.h:158
std::byte * bend()
Return the buffer end as a std::byte *.
Definition: string_writer.h:153
char * m_pos
The current write position.
Definition: string_writer.h:169
void write_char(int ch)
Append a single character to the buffer.
Definition: string_writer.h:83
char * m_end
End of buffer.
Definition: string_writer.h:171
void write(const Is_format auto &format, const std::string_view &sv)
Write the given string_view to this String_writer.
Definition: string_writer.h:96
const std::byte * bpos() const
Return the current write position as a const std::byte *.
Definition: string_writer.h:132
String_writer(const String_writer &)=delete
static constexpr Target_type target_type
Definition: string_writer.h:57
String_writer(String_writer &&) noexcept=default
String_writer(char *first, char *last)
Construct a new object backed by the given buffer.
Definition: string_writer.h:54
char * pos()
Return the current write position as a char *.
Definition: string_writer.h:111
unsigned char * upos()
Return the current write position as an unsigned char *.
Definition: string_writer.h:117
std::size_t remaining_size() const
Definition: string_writer.h:163
char * end()
Return the buffer end as a char *.
Definition: string_writer.h:137
void write(const Is_format auto &format, const auto &object)
Write the given object to this String_writer.
Definition: string_writer.h:90
const char * pos() const
Return the current write position as a const char *.
Definition: string_writer.h:114
const unsigned char * uend() const
Return the buffer end as a const unsigned char *.
Definition: string_writer.h:148
const unsigned char * upos() const
Return the current write position as a const unsigned char *.
Definition: string_writer.h:122
std::byte * bpos()
Return the current write position as a std::byte *.
Definition: string_writer.h:127
String_writer subclass that can be instantiated.
Definition: string_writer.h:182
Constructible_string_writer(const Is_out_str auto &out_str)
Definition: string_writer.h:184
CRTP base class providing common helpers needed by String_writer and String_counter,...
Definition: string_target.h:61
void resolve_format_and_write(const Is_format auto &format, const Object_t &object)
Resolve the format, using the rules to deduce format based on default format and parent format,...
Definition: string_target.h:90
True if Test is a format.
Definition: format.h:42
True if Test is an Output String Wrapper, i.e., derived from Out_str_base.
Definition: out_str.h:219
unsigned char byte
Blob class.
Definition: common.h:151
std::string format(const routing_guidelines::Session_info &session_info, bool extended_session_info)
Definition: dest_metadata_cache.cc:170
Definition: gtid_binary_format_conv.h:252
Definition: gtid_binary_format.h:41
Target_type
Definition: string_target.h:107
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
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
size_t size(const char *const c)
Definition: base64.h:46
Define std::hash<Gtid>.
Definition: gtid.h:355
Experimental API header.
Experimental API header.