// Copyright (c) 2023, 2024, Oracle and/or its affiliates. // // This program is free software; you can redistribute it and/or modify // it under the terms of the GNU General Public License, version 2.0, // as published by the Free Software Foundation. // // This program is designed to work with certain software (including // but not limited to OpenSSL) that is licensed under separate terms, // as designated in a particular file or component or in included license // documentation. The authors of MySQL hereby grant you an additional // permission to link the program and your derivative works with the // separately licensed software that they have either included with // the program or referenced in the documentation. // // This program is distributed in the hope that it will be useful, // but WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License, version 2.0, for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. #ifndef MYSQL_SERIALIZATION_SERIALIZABLE_SIZE_CALCULATOR_H #define MYSQL_SERIALIZATION_SERIALIZABLE_SIZE_CALCULATOR_H #include #include #include "mysql/serialization/field_definition.h" #include "mysql/serialization/serializable.h" /// @file /// Experimental API header /// @addtogroup GroupLibsMysqlSerialization /// @{ namespace mysql::serialization { template class Serializable; template struct Serializable_size_calculator_helper { /// @brief returns max size for nested Serializable class /// @return max declared size static constexpr std::size_t get_max_size() { return Serializer_type::template get_max_size(); } }; /// @brief Helper struct used to determine Field_definition declared max size template struct Serializable_size_calculator_helper> { /// @brief returns max size for Field_definition object /// @return max declared size static constexpr std::size_t get_max_size() { return Serializer_type::template get_max_size(); } }; template struct Serializable_size_calculator; /// @brief Helper struct used to determine Serializable tuple max declared size template struct Serializable_size_calculator> { public: using value_type = std::tuple; /// @brief returns tuple max declared size /// @return tuple max declared size static constexpr std::size_t get_max_size() { return get_max_size_helper( std::make_index_sequence>{}); } private: /// @brief Helper function used to sum compile time array of sizes /// @param a Array /// @param i index /// @return summed size template static constexpr T internal_sum_size(T const (&a)[N], std::size_t i = 0U) { return i < N ? (a[i] + internal_sum_size(a, i + 1U)) : T{}; } /// @brief Additional level of get_max_size_helper, here we get one type from /// the tuple /// @return Max size of tuple declared by the user template static constexpr std::size_t get_max_size_helper(std::index_sequence) { constexpr std::size_t arr[] = {Serializable_size_calculator_helper< Serializer_type, std::decay_t( std::declval()))>>::get_max_size()...}; return internal_sum_size(arr); } }; } // namespace mysql::serialization /// @} #endif // MYSQL_SERIALIZATION_SERIALIZABLE_SIZE_CALCULATOR_H