File indexing completed on 2025-01-18 09:42:38
0001
0002
0003
0004
0005
0006
0007
0008 #ifndef BOOST_MYSQL_DETAIL_TYPING_ROW_TRAITS_HPP
0009 #define BOOST_MYSQL_DETAIL_TYPING_ROW_TRAITS_HPP
0010
0011 #include <boost/mysql/detail/config.hpp>
0012
0013 #ifdef BOOST_MYSQL_CXX14
0014
0015 #include <boost/mysql/client_errc.hpp>
0016 #include <boost/mysql/diagnostics.hpp>
0017 #include <boost/mysql/error_code.hpp>
0018 #include <boost/mysql/field_view.hpp>
0019 #include <boost/mysql/metadata.hpp>
0020 #include <boost/mysql/metadata_collection_view.hpp>
0021 #include <boost/mysql/string_view.hpp>
0022
0023 #include <boost/mysql/detail/config.hpp>
0024 #include <boost/mysql/detail/typing/meta_check_context.hpp>
0025 #include <boost/mysql/detail/typing/pos_map.hpp>
0026 #include <boost/mysql/detail/typing/readable_field_traits.hpp>
0027
0028 #include <boost/assert.hpp>
0029 #include <boost/describe/members.hpp>
0030 #include <boost/mp11/algorithm.hpp>
0031 #include <boost/mp11/utility.hpp>
0032
0033 #include <cstddef>
0034 #include <tuple>
0035 #include <type_traits>
0036 #include <utility>
0037
0038 namespace boost {
0039 namespace mysql {
0040 namespace detail {
0041
0042
0043
0044
0045
0046 struct readable_field_checker
0047 {
0048 template <class TypeIdentity>
0049 constexpr void operator()(TypeIdentity) const noexcept
0050 {
0051 using T = typename TypeIdentity::type;
0052 static_assert(
0053 is_readable_field<T>::value,
0054 "You're trying to use an unsupported field type in a row type. Review your row type definitions."
0055 );
0056 }
0057 };
0058
0059 template <class TypeList>
0060 static constexpr bool check_readable_field() noexcept
0061 {
0062 mp11::mp_for_each<TypeList>(readable_field_checker{});
0063 return true;
0064 }
0065
0066
0067 template <class T, std::size_t N>
0068 struct array_wrapper
0069 {
0070 T data_[N];
0071
0072 constexpr boost::span<const T> span() const noexcept { return boost::span<const T>(data_); }
0073 };
0074
0075 template <class T>
0076 struct array_wrapper<T, 0>
0077 {
0078 struct
0079 {
0080 } data_;
0081
0082 constexpr boost::span<const T> span() const noexcept { return boost::span<const T>(); }
0083 };
0084
0085
0086
0087 constexpr std::size_t get_length(const char* s) noexcept
0088 {
0089 const char* p = s;
0090 while (*p)
0091 ++p;
0092 return p - s;
0093 }
0094
0095
0096 class parse_functor
0097 {
0098 span<const std::size_t> pos_map_;
0099 span<const field_view> fields_;
0100 std::size_t index_{};
0101 error_code ec_;
0102
0103 public:
0104 parse_functor(span<const std::size_t> pos_map, span<const field_view> fields) noexcept
0105 : pos_map_(pos_map), fields_(fields)
0106 {
0107 }
0108
0109 template <class ReadableField>
0110 void operator()(ReadableField& output)
0111 {
0112 auto ec = readable_field_traits<ReadableField>::parse(
0113 map_field_view(pos_map_, index_++, fields_),
0114 output
0115 );
0116 if (!ec_)
0117 ec_ = ec;
0118 }
0119
0120 error_code error() const noexcept { return ec_; }
0121 };
0122
0123
0124 template <class T, bool is_describe_struct = boost::describe::has_describe_members<T>::value>
0125 class row_traits;
0126
0127
0128 template <class DescribeStruct>
0129 using row_members = boost::describe::
0130 describe_members<DescribeStruct, boost::describe::mod_public | boost::describe::mod_inherited>;
0131
0132 template <class MemberDescriptor>
0133 constexpr string_view get_member_name(MemberDescriptor d) noexcept
0134 {
0135 return string_view(d.name, get_length(d.name));
0136 }
0137
0138 template <template <class...> class ListType, class... MemberDescriptor>
0139 constexpr array_wrapper<string_view, sizeof...(MemberDescriptor)> get_describe_names(ListType<
0140 MemberDescriptor...>)
0141 {
0142 return {{get_member_name(MemberDescriptor())...}};
0143 }
0144
0145 template <class DescribeStruct>
0146 constexpr auto describe_names_storage = get_describe_names(row_members<DescribeStruct>{});
0147
0148 template <class DescribeStruct>
0149 class row_traits<DescribeStruct, true>
0150 {
0151 using members = row_members<DescribeStruct>;
0152
0153 template <class D>
0154 struct descriptor_to_type
0155 {
0156 using helper = decltype(std::declval<DescribeStruct>().*std::declval<D>().pointer);
0157 using type = typename std::remove_reference<helper>::type;
0158 };
0159
0160 using member_types = mp11::mp_transform<descriptor_to_type, members>;
0161
0162 static_assert(check_readable_field<member_types>(), "");
0163
0164 public:
0165 using types = member_types;
0166
0167 static constexpr std::size_t size() noexcept { return boost::mp11::mp_size<members>::value; }
0168
0169 static constexpr name_table_t name_table() noexcept
0170 {
0171 return describe_names_storage<DescribeStruct>.span();
0172 }
0173
0174 static void parse(parse_functor& parser, DescribeStruct& to)
0175 {
0176 boost::mp11::mp_for_each<members>([&](auto D) { parser(to.*D.pointer); });
0177 }
0178 };
0179
0180
0181 template <class T>
0182 struct is_tuple : std::false_type
0183 {
0184 };
0185 template <class... T>
0186 struct is_tuple<std::tuple<T...>> : std::true_type
0187 {
0188 };
0189
0190 template <class... ReadableField>
0191 class row_traits<std::tuple<ReadableField...>, false>
0192 {
0193 using tuple_type = std::tuple<ReadableField...>;
0194 using field_types = boost::mp11::mp_list<boost::mp11::mp_identity<ReadableField>...>;
0195
0196 static_assert(check_readable_field<field_types>(), "");
0197
0198 public:
0199 using types = field_types;
0200 static constexpr std::size_t size() noexcept { return std::tuple_size<tuple_type>::value; }
0201 static constexpr name_table_t name_table() noexcept { return name_table_t(); }
0202 static void parse(parse_functor& parser, tuple_type& to) { boost::mp11::tuple_for_each(to, parser); }
0203 };
0204
0205
0206
0207
0208 template <class T>
0209 struct is_static_row
0210 {
0211 static constexpr bool value = is_tuple<T>::value || describe::has_describe_members<T>::value;
0212 };
0213
0214 #ifdef BOOST_MYSQL_HAS_CONCEPTS
0215
0216 template <class T>
0217 concept static_row = is_static_row<T>::value;
0218
0219 #define BOOST_MYSQL_STATIC_ROW ::boost::mysql::detail::static_row
0220
0221 #else
0222 #define BOOST_MYSQL_STATIC_ROW class
0223 #endif
0224
0225
0226 template <BOOST_MYSQL_STATIC_ROW StaticRow>
0227 constexpr std::size_t get_row_size()
0228 {
0229 return row_traits<StaticRow>::size();
0230 }
0231
0232 template <BOOST_MYSQL_STATIC_ROW StaticRow>
0233 constexpr name_table_t get_row_name_table()
0234 {
0235 return row_traits<StaticRow>::name_table();
0236 }
0237
0238 template <BOOST_MYSQL_STATIC_ROW StaticRow>
0239 error_code meta_check(span<const std::size_t> pos_map, metadata_collection_view meta, diagnostics& diag)
0240 {
0241 using fields = typename row_traits<StaticRow>::types;
0242 BOOST_ASSERT(pos_map.size() == get_row_size<StaticRow>());
0243 return meta_check_field_type_list<fields>(pos_map, get_row_name_table<StaticRow>(), meta, diag);
0244 }
0245
0246 template <BOOST_MYSQL_STATIC_ROW StaticRow>
0247 error_code parse(span<const std::size_t> pos_map, span<const field_view> from, StaticRow& to)
0248 {
0249 BOOST_ASSERT(pos_map.size() == get_row_size<StaticRow>());
0250 BOOST_ASSERT(from.size() >= get_row_size<StaticRow>());
0251 parse_functor ctx(pos_map, from);
0252 row_traits<StaticRow>::parse(ctx, to);
0253 return ctx.error();
0254 }
0255
0256 using meta_check_fn_t =
0257 error_code (*)(span<const std::size_t> field_map, metadata_collection_view meta, diagnostics& diag);
0258
0259
0260 template <class... StaticRow>
0261 constexpr std::size_t max_num_columns = (std::max)({get_row_size<StaticRow>()...});
0262
0263 }
0264 }
0265 }
0266
0267 #endif
0268
0269 #endif