MySQL 9.1.0
Source Code Documentation
field_definition.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_FIELD_DEFINITION_H
25#define MYSQL_SERIALIZATION_FIELD_DEFINITION_H
26
27#include <functional>
28
33
34/// @file
35/// Experimental API header
36
37/// @addtogroup GroupLibsMysqlSerialization
38/// @{
39
40namespace mysql::serialization {
41
42/// @brief Field definition provided by classes implementing Serializable
43/// interface
44template <class Field_type, Field_size defined_field_size = sizeof(Field_type)>
46 public:
48 using Field_type_ref = std::reference_wrapper<Field_type>;
49
50 /// @brief Constructs field definition object
51 /// @param[in] field Reference to described field object
52 /// externally, serializer may ignore this value or use it to hide some fields
53 /// @param[in] encode_predicate Function that is called for optional fields,
54 /// defines whether field will be encoded
55 /// @param[in] unknown_field_policy What decoder should do in case this
56 /// field definition is unknown to the decoder (error / ignore)
57 /// @param[in] field_missing_functor Function that is called for this
58 /// field in case it is not available in encoded data
60 Unknown_field_policy unknown_field_policy,
61 Field_missing_functor field_missing_functor)
62 : Field_definition(field, encode_predicate, unknown_field_policy) {
63 m_field_missing_functor = field_missing_functor;
64 }
65
66 /// @brief Constructs field definition object
67 /// @param[in] field Reference to described field object
68 /// @param[in] unknown_field_policy What decoder should do in case this
69 /// field definition is unknown to the decoder (error / ignore)
71 Field_type &field,
73 : m_ref(field), m_unknown_field_policy(unknown_field_policy) {
74 m_encode_predicate = Field_encode_predicate([]() -> bool { return true; });
76 }
77
78 /// @brief Constructs field definition object
79 /// @param[in] field Reference to described field object
80 /// @param[in] unknown_field_policy What decoder should do in case this
81 /// field definition is unknown to the decoder (error / ignore)
82 /// @param[in] encode_predicate Function that is called for optional fields,
83 /// defines whether field will be encoded
85 Field_type &field, Field_encode_predicate encode_predicate,
87 : Field_definition(field, unknown_field_policy) {
88 m_encode_predicate = encode_predicate;
89 }
90
91 /// @brief Constructs field definition object
92 /// @param[in] field Reference to described field object
93 /// @param[in] field_missing_functor Function that is called for this
94 /// field in case it is not available in encoded data
96 Field_missing_functor field_missing_functor)
97 : Field_definition(field) {
98 m_field_missing_functor = field_missing_functor;
99 }
100
101 Field_type &get_ref() { return m_ref.get(); }
102 const Field_type &get_ref() const { return m_ref.get(); }
103
104 /// @brief Runs "field missing functor" in case field is not available in the
105 /// provided encoded data
107 /// @brief Runs encode predicate functor
108 /// @retval true Encode field
109 /// @retval false Do not encode field
110 bool run_encode_predicate() const { return m_encode_predicate(); }
111
112 /// @brief Indicates whether this field can be ignored by decoders that
113 /// do not know this field
114 /// @retval true Decoders that do not recognize this field may ignore it
115 /// @retval false Decoders that do not recognize this field should generate
116 /// an error if they find this field in the packet
117 bool is_field_ignorable() const {
119 }
120
121 protected:
122 Field_type_ref m_ref; ///< Field object reference
123
124 /// Defines what a decoder should do if the field is present but the decoder
125 /// is a version that does not know how to interpret the field.
126 /// Typically the encoder writes this information to the packet as a
127 /// directive that instructs old decoders or third-party decoders how to
128 /// handle the field, if the message definition that they are aware of
129 /// does not include the field.
131
132 /// Defines whether field will be encoded
134
135 /// Function that is called for this field during decoding in case
136 /// field is missing in the encoded message
138};
139
140} // namespace mysql::serialization
141
142/// @}
143
144#endif // MYSQL_SERIALIZATION_FIELD_DEFINITION_H
Field definition provided by classes implementing Serializable interface.
Definition: field_definition.h:45
bool is_field_ignorable() const
Indicates whether this field can be ignored by decoders that do not know this field.
Definition: field_definition.h:117
Field_encode_predicate m_encode_predicate
Defines whether field will be encoded.
Definition: field_definition.h:133
Field_definition(Field_type &field, Field_encode_predicate encode_predicate, Unknown_field_policy unknown_field_policy, Field_missing_functor field_missing_functor)
Constructs field definition object.
Definition: field_definition.h:59
Field_type & get_ref()
Definition: field_definition.h:101
Field_definition(Field_type &field, Unknown_field_policy unknown_field_policy=Unknown_field_policy::ignore)
Constructs field definition object.
Definition: field_definition.h:70
Field_definition(Field_type &field, Field_encode_predicate encode_predicate, Unknown_field_policy unknown_field_policy=Unknown_field_policy::ignore)
Constructs field definition object.
Definition: field_definition.h:84
Unknown_field_policy m_unknown_field_policy
Defines what a decoder should do if the field is present but the decoder is a version that does not k...
Definition: field_definition.h:130
Field_definition(Field_type &field, Field_missing_functor field_missing_functor)
Constructs field definition object.
Definition: field_definition.h:95
void run_field_missing() const
Runs "field missing functor" in case field is not available in the provided encoded data.
Definition: field_definition.h:106
Field_missing_functor m_field_missing_functor
Function that is called for this field during decoding in case field is missing in the encoded messag...
Definition: field_definition.h:137
bool run_encode_predicate() const
Runs encode predicate functor.
Definition: field_definition.h:110
std::reference_wrapper< Field_type > Field_type_ref
Definition: field_definition.h:48
const Field_type & get_ref() const
Definition: field_definition.h:102
Field_type_ref m_ref
Field object reference.
Definition: field_definition.h:122
Experimental API header.
Definition: archive.h:37
Field_encode_functor_type Field_encode_predicate
Type of the predicate telling serializer whether field is provided or not.
Definition: field_functor.h:47
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
Field_decode_functor_type Field_missing_functor
Type of the predicate telling serializer what to do in case field is not provided.
Definition: field_functor.h:43
Experimental API header Defines tags supported by the serializable, used for internal dispatching.
Experimental API header.
Definition: sql_resultset.h:36
Used to distinguish between Serializable and Field_definition types.
Definition: serializable_type_tags.h:43
Experimental API header.