MySQL 9.1.0
Source Code Documentation
serializable_size_calculator.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_SIZE_CALCULATOR_H
25#define MYSQL_SERIALIZATION_SERIALIZABLE_SIZE_CALCULATOR_H
26
27#include <tuple>
28#include <unordered_set>
29
31
32/// @file
33/// Experimental API header
34
35/// @addtogroup GroupLibsMysqlSerialization
36/// @{
37
38namespace mysql::serialization {
39
40template <typename Serializable_derived>
41class Serializable;
42
43template <typename Serializer_type, typename Serializable_type>
45 /// @brief returns max size for nested Serializable class
46 /// @return max declared size
47 static constexpr std::size_t get_max_size() {
48 return Serializer_type::template get_max_size<Serializable_type>();
49 }
50};
51
52/// @brief Helper struct used to determine Field_definition declared max size
53template <typename Serializer_type, class T, Field_size S>
55 Field_definition<T, S>> {
56 /// @brief returns max size for Field_definition object
57 /// @return max declared size
58 static constexpr std::size_t get_max_size() {
59 return Serializer_type::template get_max_size<T, S>();
60 }
61};
62
63template <typename Serializer_type, typename... Args>
64struct Serializable_size_calculator;
65
66/// @brief Helper struct used to determine Serializable tuple max declared size
67template <typename Serializer_type, typename... Args>
68struct Serializable_size_calculator<Serializer_type, std::tuple<Args...>> {
69 public:
70 using value_type = std::tuple<Args...>;
71
72 /// @brief returns tuple max declared size
73 /// @return tuple max declared size
74 static constexpr std::size_t get_max_size() {
75 return get_max_size_helper(
76 std::make_index_sequence<std::tuple_size_v<value_type>>{});
77 }
78
79 private:
80 /// @brief Helper function used to sum compile time array of sizes
81 /// @param a Array
82 /// @param i index
83 /// @return summed size
84 template <typename T, std::size_t N>
85 static constexpr T internal_sum_size(T const (&a)[N], std::size_t i = 0U) {
86 return i < N ? (a[i] + internal_sum_size(a, i + 1U)) : T{};
87 }
88
89 /// @brief Additional level of get_max_size_helper, here we get one type from
90 /// the tuple
91 /// @return Max size of tuple declared by the user
92 template <size_t... Is>
93 static constexpr std::size_t get_max_size_helper(std::index_sequence<Is...>) {
94 constexpr std::size_t arr[] = {Serializable_size_calculator_helper<
95 Serializer_type, std::decay_t<decltype(std::get<Is>(
96 std::declval<value_type>()))>>::get_max_size()...};
97 return internal_sum_size(arr);
98 }
99};
100
101} // namespace mysql::serialization
102
103/// @}
104
105#endif // MYSQL_SERIALIZATION_SERIALIZABLE_SIZE_CALCULATOR_H
Field definition provided by classes implementing Serializable interface.
Definition: field_definition.h:45
Experimental API header.
std::atomic< Type > N
Definition: ut0counter.h:225
Definition: archive.h:37
Definition: gcs_xcom_synode.h:64
static constexpr T internal_sum_size(T const (&a)[N], std::size_t i=0U)
Helper function used to sum compile time array of sizes.
Definition: serializable_size_calculator.h:85
static constexpr std::size_t get_max_size_helper(std::index_sequence< Is... >)
Additional level of get_max_size_helper, here we get one type from the tuple.
Definition: serializable_size_calculator.h:93
std::tuple< Args... > value_type
Definition: serializable_size_calculator.h:70
static constexpr std::size_t get_max_size()
returns tuple max declared size
Definition: serializable_size_calculator.h:74
static constexpr std::size_t get_max_size()
returns max size for Field_definition object
Definition: serializable_size_calculator.h:58
Definition: serializable_size_calculator.h:44
static constexpr std::size_t get_max_size()
returns max size for nested Serializable class
Definition: serializable_size_calculator.h:47
Definition: dtoa.cc:595