MySQL 8.4.2
Source Code Documentation
archive_binary_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 <typename Field_type>
31 static constexpr auto value_size = Field_type::value_size;
33
34 if (is_good() == true) {
35 try {
36 size_t current_size = m_stream.size();
37 size_t field_written_size = Primitive_type_codec<
38 value_type>::template count_write_bytes<value_size>(arg.get());
39 m_stream.resize(m_stream.size() + field_written_size);
40 // and overwrite it with correct, formatted data
41 if (Primitive_type_codec<value_type>::template write_bytes<value_size>(
42 m_stream.data() + current_size, arg.get()) == 0) {
44 __FILE__, __LINE__, "Unable to write data to the stream",
46 }
47 } catch (std::bad_alloc &) {
48 m_error = Serialization_error(__FILE__, __LINE__,
49 "Got bad alloc, unable to insert field",
51 }
52 }
53 return *this;
54}
55
56template <typename Field_type>
58 static constexpr auto value_size = Field_type::value_size;
60
61 if (is_good() == true) {
62 auto bytes_read =
64 value_size>(m_stream.data() + read_pos, m_stream.size() - read_pos,
65 arg.get());
66 if (bytes_read == 0) {
68 __FILE__, __LINE__, "Unable to read data from the stream",
70 return *this;
71 }
72 read_pos += bytes_read;
73 }
74 return *this;
75}
76
77template <class Field_type>
79 auto read_pos_saved = read_pos;
80 this->operator>>(std::forward<Field_type>(field));
81 read_pos = read_pos_saved;
82}
83
84} // namespace mysql::serialization
Binary archive implementation based on vector of bytes.
Definition: archive_binary.h:44
std::size_t read_pos
Read position.
Definition: archive_binary.h:85
void peek(Field_type &&field)
Peeks selected field wrapper (reads data without updating read stream position)
Definition: archive_binary_impl.hpp:78
Archive_binary & operator>>(Field_type &&arg)
Reads argument from this archive.
Definition: archive_binary_impl.hpp:57
std::vector< unsigned char > m_stream
Internal data stream.
Definition: archive_binary.h:84
Archive_binary & operator<<(Field_type &&arg)
Ingests argument into this archive.
Definition: archive_binary_impl.hpp:30
bool is_good() const
Definition: archive.h:190
Serialization_error m_error
Holds information about error.
Definition: archive.h:197
Error used internally in serialization framework.
Definition: serialization_error.h:46
uint16_t value_type
Definition: vt100.h:184
Definition: archive.h:37
@ archive_write_error
Cannot write data to the stream.
@ archive_read_error
Cannot read data from the stream.
Definition: sql_resultset.h:36
This class is to provide functionality to encode/decode the primitive types into/out of defined strea...
Definition: primitive_type_codec.h:113
static int read_bytes(connection_descriptor const *rfd, char *p, uint32_t n, server *s, int64_t *ret)
Reads n bytes from connection rfd without buffering reads.
Definition: xcom_transport.cc:1024