MySQL 9.1.0
Source Code Documentation
serializable.h
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#ifndef MYSQL_SERIALIZATION_SERIALIZABLE_H
25#define MYSQL_SERIALIZATION_SERIALIZABLE_H
26
27#include <tuple>
28#include <unordered_set>
29
36
37/// @file
38/// Experimental API header
39
40/// @addtogroup GroupLibsMysqlSerialization
41/// @{
42
43namespace mysql::serialization {
44
45// forward declarations
46
47template <typename Serializer_type, typename... Args>
49
50/// @brief Interface for serializable data structures. Classes that implement
51/// serializable interface may be serialized automatically by the serializer
52/// @note To be implemented by derived:
53/// decltype(auto) define_fields { return std::make_tuple(...); } const and
54/// non-const version
55/// @details Please have a look at examples enclosed in unittest
56/// The base class provides:
57/// - methods to traverse through fields defined by the user in the
58/// Derived_serializable_type
59/// - methods to compute a compile-time upper bound on the size of message
60/// defined by the user in Derived_serializable_type
61/// - methods to compute the exact size of a particular message at runtime,
62/// taking into account the current values of fields defined by the user
63/// in the Derived_serializable_type
64/// Derived class should provide:
65/// - decltype(auto) define_fields { return std::make_tuple(...); } const
66/// defines fields to be encoded. To define fields,
67/// helpers defined in the "field_definition_helpers.h" shall be used.
68/// Sufficient information about the field is the field reference. The
69/// message designer can also define:
70/// - fixed size of an integer / upper bound of the string length
71/// - encode predicate, which will be called by the encoding functions
72/// to determine whether field should be encoded
73/// - an unknown_field_policy, defined by the encoder and applied by the
74/// decoder in case decoder does not recognize a field
75/// - decltype(auto) define_fields { return std::make_tuple(...); }
76/// defines fields to be decoded. To define fields,
77/// helpers defined in the "field_definition_helpers.h" shall be used.
78/// Sufficient information about the field is the field reference. The
79/// message designer can also define:
80/// - fixed size of an integer / upper bound of the string length
81/// - field missing functor, which will be called by the decoding functions
82/// in case field is not encoded in the message
83/// - an unknown_field_policy, defined by the encoder and applied by the
84/// decoder in case decoder does not recognize a field
85template <class Derived_serializable_type>
87 public:
88 /// @brief calls functor for each field
89 /// @tparam Serializable_functor_type Type of functor to be applied on the
90 /// serializable
91 /// @tparam Field_functor_type Type of functor to be applied on the field
92 /// @param func_s Functor to be called for each serializable field
93 /// @param func_f Functor to be called for each non-serializable field
94 template <class Serializable_functor_type, class Field_functor_type>
95 void do_for_each_field(Serializable_functor_type &&func_s,
96 Field_functor_type &&func_f);
97
98 /// @brief calls functor for each field, const version
99 /// @tparam Serializable_functor_type Type of functor to be applied on the
100 /// serializable
101 /// @tparam Field_functor_type Type of functor to be applied on the field
102 /// @param func_s Functor to be called for each serializable field
103 /// @param func_f Functor to be called for each non-serializable field
104 template <class Serializable_functor_type, class Field_functor_type>
105 void do_for_each_field(Serializable_functor_type &&func_s,
106 Field_functor_type &&func_f) const;
107
108 // available for serializer derived
109
110 /// @brief This function calculates last field id of this type
111 /// @return last field id
112 static constexpr Field_id_type get_last_field_id() {
113 using Tuple_type_captured =
114 decltype(std::declval<Derived_serializable_type>().define_fields());
115 return std::tuple_size_v<Tuple_type_captured> - 1;
116 }
117
118 /// @brief Returns serializable object fields size, internal function
119 /// (without serializable metadata size)
120 /// @tparam Serializer_type Type of serializer used to format fields
121 /// overhead added by serializer, including overhead of nested types
122 /// @returns Serializable internal size
123 template <typename Serializer_type>
124 std::size_t get_size_internal() const;
125
126 /// @brief Performs iteration over all of the serializable fields and checks
127 /// whether any of the fields in this serializable is provided
128 /// @return True in case any of the fields in all levels, starting from this
129 /// one, is provided
130 bool is_any_field_provided() const;
131
132 /// @brief Returns serializable object fields maximum size, internal function
133 /// (without serializable metadata size)
134 /// @tparam Serializer_type Type of serializer used to format fields
135 /// object will have this maximum size
136 /// @return This class maximum declared size
137 template <typename Serializer_type>
138 static constexpr std::size_t get_max_size_internal() {
139 using Tuple_type_captured =
140 decltype(std::declval<Derived_serializable_type>().define_fields());
141 return Serializable_size_calculator<Serializer_type,
142 Tuple_type_captured>::get_max_size();
143 }
144
145 /// @brief Sets unknown field policy for this object
146 /// @param policy Chosen policy, determines what decoder should do in case it
147 /// encounters this object in the stream but this object definition is
148 /// unknown to the decoder
150 m_unknown_field_policy = policy;
151 }
152 bool is_ignorable() const {
154 }
155
156 protected:
158
159 template <class Serializer_derived_type, class Archive_type>
160 friend class Serializer;
161 template <class Derived_serializable_type_current>
162 friend class Serializable;
163
164 /// @brief do_for_each_field helper
165 template <class Serializable_functor_type, class Field_functor_type,
166 class Tuple_type, std::size_t... Is>
167 void do_for_each_field(Serializable_functor_type &&func_s,
168 Field_functor_type &&func_f, Tuple_type &&tuple,
169 std::index_sequence<Is...>);
170
171 /// @brief do_for_each_field helper
172 template <class Serializable_functor_type, class Field_functor_type,
173 class Field_type>
174 void do_for_one_field(Serializable_functor_type &&func_s,
175 Field_functor_type &&func_f, Field_type &field,
176 std::size_t field_id);
177
178 /// @brief do_for_each_field helper
179 template <class Serializable_functor_type, class Field_functor_type,
180 class Field_type>
181 void do_for_one_field(Serializable_functor_type &&func_s,
182 Field_functor_type &&func_f, Field_type &field,
183 std::size_t field_id, Serializable_tag);
184
185 /// @brief do_for_each_field helper
186 template <class Serializable_functor_type, class Field_functor_type,
187 class Field_type>
188 void do_for_one_field(Serializable_functor_type &&,
189 Field_functor_type &&func_f, Field_type &field,
190 std::size_t field_id, Field_definition_tag);
191
192 /// @brief do_for_each_field (const) helper
193 template <class Serializable_functor_type, class Field_functor_type,
194 class Tuple_type, std::size_t... Is>
195 void do_for_each_field(Serializable_functor_type &&func_s,
196 Field_functor_type &&func_f, Tuple_type &&tuple,
197 std::index_sequence<Is...>) const;
198
199 /// @brief do_for_each_field (const) helper
200 template <class Serializable_functor_type, class Field_functor_type,
201 class Field_type>
202 void do_for_one_field(Serializable_functor_type &&func_s,
203 Field_functor_type &&func_f, const Field_type &field,
204 std::size_t field_id) const;
205
206 /// @brief do_for_each_field (const) helper
207 template <class Serializable_functor_type, class Field_functor_type,
208 class Field_type>
209 void do_for_one_field(Serializable_functor_type &&func_s,
210 Field_functor_type &&func_f, const Field_type &field,
211 std::size_t field_id, Serializable_tag) const;
212
213 /// @brief do_for_each_field (const) helper
214 template <class Serializable_functor_type, class Field_functor_type,
215 class Field_type>
216 void do_for_one_field(Serializable_functor_type &&,
217 Field_functor_type &&func_f, const Field_type &field,
218 std::size_t field_id, Field_definition_tag) const;
219
220 // copy-move semantics
221 Serializable() = default;
222 Serializable(const Serializable &) = default;
226 /// @brief Creation of Serializable objects is prohibited
227 virtual ~Serializable() = default;
228
229 private:
230 /// Unknown field policy for this serializable object
232};
233
234} // namespace mysql::serialization
235
236/// @}
237
239
240#endif // MYSQL_SERIALIZATION_SERIALIZABLE_H
Interface for serializable data structures.
Definition: serializable.h:86
Unknown_field_policy m_unknown_field_policy
Unknown field policy for this serializable object.
Definition: serializable.h:231
std::size_t get_size_internal() const
Returns serializable object fields size, internal function (without serializable metadata size)
Definition: serializable_impl.hpp:31
static constexpr std::size_t get_max_size_internal()
Returns serializable object fields maximum size, internal function (without serializable metadata siz...
Definition: serializable.h:138
Serializable & operator=(Serializable &&)=default
bool is_ignorable() const
Definition: serializable.h:152
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
Serializable & operator=(const Serializable &)=default
static constexpr Field_id_type get_last_field_id()
This function calculates last field id of this type.
Definition: serializable.h:112
void set_unknown_field_policy(const Unknown_field_policy &policy)
Sets unknown field policy for this object.
Definition: serializable.h:149
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
virtual ~Serializable()=default
Creation of Serializable objects is prohibited.
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
Serializable(const Serializable &)=default
Serializable(Serializable &&)=default
Interface for serializer.
Definition: serializer.h:62
Experimental API header.
Experimental API header.
Definition: archive.h:37
uint64_t Field_id_type
Type for field_id assigned to each field in the.
Definition: serialization_types.h:42
Unknown_field_policy
Represents 'unknown field' policy - decision what needs to be done in case unknown field is encounter...
Definition: unknown_field_policy.h:39
Experimental API header.
Experimental API header.
Experimental API header Defines tags supported by the serializable, used for internal dispatching.
Experimental API header.
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
Experimental API header.