Back to home page

EIC code displayed by LXR

 
 

    


Warning, file /include/boost/mysql/impl/pfr.hpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).

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 template <class T>
0072 constexpr inline std::size_t pfr_row_size_v = mp11::mp_size<pfr_fields_t<T>>::value;
0073 
0074 template <class T>
0075 constexpr std::array<string_view, pfr_row_size_v<T>> create_pfr_name_table() noexcept
0076 {
0077     // Some MSVC compilers have trouble with pfr::names_as_array<T>() when
0078     // the row type is empty
0079     if constexpr (pfr_row_size_v<T> == 0u)
0080     {
0081         return {};
0082     }
0083     else
0084     {
0085         return to_name_table_storage(pfr::names_as_array<T>());
0086     }
0087 }
0088 
0089 template <class T>
0090 constexpr inline auto pfr_names_storage = create_pfr_name_table<T>();
0091 
0092 template <class T>
0093 class row_traits<pfr_by_name<T>, false>
0094 {
0095     static_assert(
0096         is_pfr_reflectable<T>(),
0097         "T needs to be a non-const object type that supports PFR reflection"
0098     );
0099 
0100 public:
0101     using underlying_row_type = T;
0102     using field_types = pfr_fields_t<T>;
0103 
0104     static constexpr name_table_t name_table() noexcept { return pfr_names_storage<T>; }
0105 
0106     template <class F>
0107     static void for_each_member(T& to, F&& function)
0108     {
0109         pfr::for_each_field(to, std::forward<F>(function));
0110     }
0111 };
0112 
0113 #endif
0114 
0115 template <class T>
0116 class row_traits<pfr_by_position<T>, false>
0117 {
0118     static_assert(
0119         is_pfr_reflectable<T>(),
0120         "T needs to be a non-const object type that supports PFR reflection"
0121     );
0122 
0123 public:
0124     using underlying_row_type = T;
0125     using field_types = pfr_fields_t<T>;
0126 
0127     static constexpr name_table_t name_table() noexcept { return {}; }
0128 
0129     template <class F>
0130     static void for_each_member(T& to, F&& function)
0131     {
0132         pfr::for_each_field(to, std::forward<F>(function));
0133     }
0134 };
0135 
0136 }  // namespace detail
0137 }  // namespace mysql
0138 }  // namespace boost
0139 
0140 #if defined(BOOST_MSVC) && BOOST_MSVC < 1920
0141 #pragma warning(pop)
0142 #endif
0143 
0144 #endif