Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:52:43

0001 //
0002 // Copyright (c) 2019-2025 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
0003 //
0004 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0005 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0006 //
0007 
0008 #ifndef BOOST_MYSQL_IMPL_PFR_HPP
0009 #define BOOST_MYSQL_IMPL_PFR_HPP
0010 
0011 #pragma once
0012 
0013 #include <boost/config.hpp>
0014 
0015 // Silence MSVC 14.1 warnings caused by https://github.com/boostorg/pfr/issues/167
0016 #if defined(BOOST_MSVC) && BOOST_MSVC < 1920
0017 #pragma warning(push)
0018 #pragma warning(disable : 4100)
0019 #endif
0020 
0021 #include <boost/mysql/pfr.hpp>
0022 #include <boost/mysql/string_view.hpp>
0023 
0024 #include <boost/mysql/detail/typing/row_traits.hpp>
0025 
0026 #include <boost/pfr/core.hpp>
0027 #include <boost/pfr/core_name.hpp>
0028 #include <boost/pfr/traits.hpp>
0029 
0030 #include <type_traits>
0031 #include <utility>
0032 
0033 #if BOOST_PFR_CORE_NAME_ENABLED
0034 #include <array>
0035 #include <cstddef>
0036 #include <string_view>
0037 #endif
0038 
0039 namespace boost {
0040 namespace mysql {
0041 namespace detail {
0042 
0043 // Not all types reflected by PFR are acceptable for us - this function performs this checking
0044 template <class T>
0045 constexpr bool is_pfr_reflectable() noexcept
0046 {
0047     return std::is_class<T>::value && !std::is_const<T>::value
0048     // is_implicitly_reflectable_v returns always false when implicit reflection
0049     // (which requires structured bindings and C++17) is not available
0050 #if BOOST_PFR_ENABLE_IMPLICIT_REFLECTION
0051            && pfr::is_implicitly_reflectable_v<T, struct mysql_tag>
0052 #endif
0053         ;
0054 }
0055 
0056 template <class T>
0057 using pfr_fields_t = decltype(pfr::structure_to_tuple(std::declval<const T&>()));
0058 
0059 #if BOOST_PFR_CORE_NAME_ENABLED
0060 
0061 // PFR field names use std::string_view
0062 template <std::size_t N>
0063 constexpr std::array<string_view, N> to_name_table_storage(std::array<std::string_view, N> input) noexcept
0064 {
0065     std::array<string_view, N> res;
0066     for (std::size_t i = 0; i < N; ++i)
0067         res[i] = input[i];
0068     return res;
0069 }
0070 
0071 // Workaround for https://github.com/boostorg/pfr/issues/165
0072 constexpr std::array<string_view, 0u> to_name_table_storage(std::array<std::nullptr_t, 0u>) noexcept
0073 {
0074     return {};
0075 }
0076 
0077 template <class T>
0078 constexpr inline auto pfr_names_storage = to_name_table_storage(pfr::names_as_array<T>());
0079 
0080 template <class T>
0081 class row_traits<pfr_by_name<T>, false>
0082 {
0083     static_assert(
0084         is_pfr_reflectable<T>(),
0085         "T needs to be a non-const object type that supports PFR reflection"
0086     );
0087 
0088 public:
0089     using underlying_row_type = T;
0090     using field_types = pfr_fields_t<T>;
0091 
0092     static constexpr name_table_t name_table() noexcept { return pfr_names_storage<T>; }
0093 
0094     template <class F>
0095     static void for_each_member(T& to, F&& function)
0096     {
0097         pfr::for_each_field(to, std::forward<F>(function));
0098     }
0099 };
0100 
0101 #endif
0102 
0103 template <class T>
0104 class row_traits<pfr_by_position<T>, false>
0105 {
0106     static_assert(
0107         is_pfr_reflectable<T>(),
0108         "T needs to be a non-const object type that supports PFR reflection"
0109     );
0110 
0111 public:
0112     using underlying_row_type = T;
0113     using field_types = pfr_fields_t<T>;
0114 
0115     static constexpr name_table_t name_table() noexcept { return {}; }
0116 
0117     template <class F>
0118     static void for_each_member(T& to, F&& function)
0119     {
0120         pfr::for_each_field(to, std::forward<F>(function));
0121     }
0122 };
0123 
0124 }  // namespace detail
0125 }  // namespace mysql
0126 }  // namespace boost
0127 
0128 #if defined(BOOST_MSVC) && BOOST_MSVC < 1920
0129 #pragma warning(pop)
0130 #endif
0131 
0132 #endif