MySQL 8.4.0
Source Code Documentation
serializable_impl.hpp
Go to the documentation of this file.
1// Copyright (c) 2023, 2024, 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/// @file
25/// Experimental API header
26
27namespace mysql::serialization {
28
29template <class Derived_serializable_type>
30template <typename Serializer_type>
32 std::size_t calculated_size = 0;
33 auto add_size_s = [&calculated_size](const auto &field,
34 auto field_id) -> auto {
35 calculated_size += Serializer_type::get_size_serializable(field_id, field);
36 };
37 auto add_size_f = [&calculated_size](const auto &field,
38 auto field_id) -> auto {
39 calculated_size += Serializer_type::get_size_field_def(field_id, field);
40 };
41 do_for_each_field(add_size_s, add_size_f);
42 return calculated_size;
43}
44
45template <class Derived_serializable_type>
46template <class Serializable_functor_type, class Field_functor_type>
48 Serializable_functor_type &&func_s, Field_functor_type &&func_f) {
49 using Tuple_type =
50 decltype(std::declval<Derived_serializable_type>().define_fields());
51 do_for_each_field(
52 std::forward<Serializable_functor_type>(func_s),
53 std::forward<Field_functor_type>(func_f),
54 static_cast<Derived_serializable_type *>(this)->define_fields(),
55 std::make_index_sequence<std::tuple_size_v<Tuple_type>>{});
56}
57
58template <class Derived_serializable_type>
59template <class Serializable_functor_type, class Field_functor_type>
61 Serializable_functor_type &&func_s, Field_functor_type &&func_f) const {
62 using Tuple_type =
63 decltype(std::declval<const Derived_serializable_type>().define_fields());
64 const auto *derived_ptr =
65 static_cast<const Derived_serializable_type *>(this);
66 do_for_each_field(std::forward<Serializable_functor_type>(func_s),
67 std::forward<Field_functor_type>(func_f),
68 (derived_ptr)->define_fields(),
69 std::make_index_sequence<std::tuple_size_v<Tuple_type>>{});
70}
71
72template <class Derived_serializable_type>
73template <class Serializable_functor_type, class Field_functor_type,
74 class Tuple_type, std::size_t... Is>
76 Serializable_functor_type &&func_s, Field_functor_type &&func_f,
77 Tuple_type &&tuple, std::index_sequence<Is...>) {
78 (do_for_one_field(std::forward<Serializable_functor_type>(func_s),
79 std::forward<Field_functor_type>(func_f),
80 std::get<Is>(tuple), Is),
81 ...);
82}
83
84template <class Derived_serializable_type>
85template <class Serializable_functor_type, class Field_functor_type,
86 class Field_type>
88 Serializable_functor_type &&func_s, Field_functor_type &&func_f,
89 Field_type &field, std::size_t field_id) {
90 using Current_field_type_bare = typename std::decay<Field_type>::type;
91 using Current_tag_type = typename Current_field_type_bare::Tag;
92 do_for_one_field(std::forward<Serializable_functor_type>(func_s),
93 std::forward<Field_functor_type>(func_f), field, field_id,
94 Current_tag_type{});
96
97template <class Derived_serializable_type>
98template <class Serializable_functor_type, class Field_functor_type,
99 class Field_type>
101 Serializable_functor_type &&func_s, Field_functor_type &&,
102 Field_type &field, std::size_t field_id, Serializable_tag) {
103 func_s(field, field_id);
104 // we only call only function for serializable
106
107template <class Derived_serializable_type>
108template <class Serializable_functor_type, class Field_functor_type,
109 class Field_type>
111 Serializable_functor_type &&, Field_functor_type &&func_f,
112 Field_type &field, std::size_t field_id, Field_definition_tag) {
113 func_f(field, field_id);
114}
115
116template <class Derived_serializable_type>
117template <class Serializable_functor_type, class Field_functor_type,
118 class Tuple_type, std::size_t... Is>
120 Serializable_functor_type &&func_s, Field_functor_type &&func_f,
121 Tuple_type &&tuple, std::index_sequence<Is...>) const {
122 (do_for_one_field(std::forward<Serializable_functor_type>(func_s),
123 std::forward<Field_functor_type>(func_f),
124 std::get<Is>(tuple), Is),
125 ...);
126}
127
128template <class Derived_serializable_type>
129template <class Serializable_functor_type, class Field_functor_type,
132 Serializable_functor_type &&func_s, Field_functor_type &&func_f,
133 const Field_type &field, std::size_t field_id) const {
134 using Current_field_type_bare = typename std::decay<Field_type>::type;
135 using Current_tag_type = typename Current_field_type_bare::Tag;
136 do_for_one_field(std::forward<Serializable_functor_type>(func_s),
137 std::forward<Field_functor_type>(func_f), field, field_id,
138 Current_tag_type{});
139}
140
141template <class Derived_serializable_type>
142template <class Serializable_functor_type, class Field_functor_type,
143 class Field_type>
145 Serializable_functor_type &&func_s, Field_functor_type &&,
146 const Field_type &field, std::size_t field_id, Serializable_tag) const {
147 func_s(field, field_id);
148 // we only call only function for serializable
149}
150
151template <class Derived_serializable_type>
152template <class Serializable_functor_type, class Field_functor_type,
153 class Field_type>
155 Serializable_functor_type &&, Field_functor_type &&func_f,
156 const Field_type &field, std::size_t field_id, Field_definition_tag) const {
157 func_f(field, field_id);
158}
159
160template <class Derived_serializable_type>
162 bool is_provided = false;
163 auto func_is_provided_s = [&is_provided](const auto &serializable,
164 const auto &) -> void {
165 is_provided = serializable.is_any_field_provided();
166 };
167 auto func_is_provided_f = [&is_provided](const auto &field,
168 const auto &) -> void {
169 if (field.run_encode_predicate()) {
170 is_provided = true;
171 }
172 };
173 using Tuple_type =
174 decltype(std::declval<Derived_serializable_type>().define_fields());
175 do_for_each_field(
176 func_is_provided_s, func_is_provided_f,
177 static_cast<const Derived_serializable_type *>(this)->define_fields(),
178 std::make_index_sequence<std::tuple_size_v<Tuple_type>>{});
179 return is_provided;
180}
182} // namespace mysql::serialization
std::size_t get_size_internal() const
Returns serializable object fields size, internal function (without serializable metadata size)
Definition: serializable_impl.hpp:31
bool is_any_field_provided() const
Performs iteration over all of the serializable fields and checks whether any of the fields in this s...
Definition: serializable_impl.hpp:161
void do_for_one_field(Serializable_functor_type &&func_s, Field_functor_type &&func_f, Field_type &field, std::size_t field_id)
do_for_each_field helper
Definition: serializable_impl.hpp:87
void do_for_each_field(Serializable_functor_type &&func_s, Field_functor_type &&func_f)
calls functor for each field
Definition: serializable_impl.hpp:47
Definition: archive.h:37
required string type
Definition: replication_group_member_actions.proto:34
static const LEX_CSTRING field_id
Definition: sql_show_processlist.cc:49
Definition: sql_resultset.h:36
Used to distinguish between Serializable and Field_definition types.
Definition: serializable_type_tags.h:43
Used to distinguish between Serializable and Field_definition types.
Definition: serializable_type_tags.h:40