MySQL 9.6.0
Source Code Documentation
string_counter.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_COUNTER_H
25#define MYSQL_STRCONV_ENCODE_STRING_COUNTER_H
26
27/// @file
28/// Experimental API header
29
30#include <string_view> // string_view
31#include "mysql/strconv/encode/string_target.h" // String_target_interface
32
33/// @addtogroup GroupLibsMysqlStrconv
34/// @{
35
36namespace mysql::strconv {
37
38/// Class that serves as the target for encode(..., Is_string_target), which
39/// never writes anything and only stores the size.
40class String_counter : public detail::String_target_interface<String_counter> {
41 protected:
42 /// Construct a new object.
43 ///
44 /// This is hidden from user code. These object are only meant to be created
45 /// internally by the framework.
46 String_counter() = default;
47
48 public:
50
51 // Allow move, but not copy.
52 //
53 // Deleting copy semantics protects against the mistake of making a
54 // `encode_impl` function take its second parameter by value.
55 String_counter(const String_counter &) = delete;
57 String_counter &operator=(const String_counter &) = delete;
58 String_counter &operator=(String_counter &&) noexcept = default;
59 ~String_counter() = default;
60
61 /// Increment the size by `sv.size()`.
62 void write_raw(const std::string_view &sv) { advance(sv.size()); }
63
64 /// Increment the size by 1.
65 void write_char(int) { advance(1); }
66
67 /// Increment the size by the size of the given object.
68 void write(const Is_format auto &format, const auto &object) {
70 }
71
72 /// Increment the size by the size of the given string. This overload enables
73 /// writing string literals directly.
74 void write(const Is_format auto &format, const std::string_view &sv) {
76 }
77
78 /// Increment the size by `size`.
79 void advance(std::size_t size) { m_size += size; }
80
81 /// Return the current size.
82 [[nodiscard]] std::size_t size() const { return m_size; }
83
84 private:
85 /// The current size.
86 std::size_t m_size{0};
87}; // class String_counter
88
89} // namespace mysql::strconv
90
91namespace mysql::strconv::detail {
92
93/// String_counter subclass that can be instantiated.
94///
95/// We hide this in the detail namespace because the class is not supposed to be
96/// instantiated in user code, only in this framework.
98 public:
100};
101
102static_assert(!std::copy_constructible<Constructible_string_counter>);
103static_assert(!std::copy_constructible<String_counter>);
104static_assert(std::movable<Constructible_string_counter>);
105static_assert(std::movable<String_counter>);
106
107} // namespace mysql::strconv::detail
108
109// addtogroup GroupLibsMysqlStrconv
110/// @}
111
112#endif // ifndef MYSQL_STRCONV_ENCODE_STRING_COUNTER_H
Class that serves as the target for encode(..., Is_string_target), which never writes anything and on...
Definition: string_counter.h:40
std::size_t m_size
The current size.
Definition: string_counter.h:86
void write(const Is_format auto &format, const auto &object)
Increment the size by the size of the given object.
Definition: string_counter.h:68
void write_raw(const std::string_view &sv)
Increment the size by sv.size().
Definition: string_counter.h:62
String_counter()=default
Construct a new object.
void write(const Is_format auto &format, const std::string_view &sv)
Increment the size by the size of the given string.
Definition: string_counter.h:74
void write_char(int)
Increment the size by 1.
Definition: string_counter.h:65
void advance(std::size_t size)
Increment the size by size.
Definition: string_counter.h:79
std::size_t size() const
Return the current size.
Definition: string_counter.h:82
static constexpr Target_type target_type
Definition: string_counter.h:49
String_counter(String_counter &&) noexcept=default
String_counter(const String_counter &)=delete
String_counter subclass that can be instantiated.
Definition: string_counter.h:97
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
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
noexcept
The return type for any call_and_catch(f, args...) call where f(args...) returns Type.
Definition: call_and_catch.h:76
Define std::hash<Gtid>.
Definition: gtid.h:355
Experimental API header.