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