row_traits.hpp 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. // Copyright (c) 2019-2024 Ruben Perez Hidalgo (rubenperez038 at gmail dot com)
  3. //
  4. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. //
  7. #ifndef BOOST_MYSQL_DETAIL_TYPING_ROW_TRAITS_HPP
  8. #define BOOST_MYSQL_DETAIL_TYPING_ROW_TRAITS_HPP
  9. #include <boost/mysql/detail/config.hpp>
  10. #ifdef BOOST_MYSQL_CXX14
  11. #include <boost/mysql/client_errc.hpp>
  12. #include <boost/mysql/diagnostics.hpp>
  13. #include <boost/mysql/error_code.hpp>
  14. #include <boost/mysql/field_view.hpp>
  15. #include <boost/mysql/metadata.hpp>
  16. #include <boost/mysql/metadata_collection_view.hpp>
  17. #include <boost/mysql/string_view.hpp>
  18. #include <boost/mysql/detail/config.hpp>
  19. #include <boost/mysql/detail/typing/meta_check_context.hpp>
  20. #include <boost/mysql/detail/typing/pos_map.hpp>
  21. #include <boost/mysql/detail/typing/readable_field_traits.hpp>
  22. #include <boost/assert.hpp>
  23. #include <boost/describe/members.hpp>
  24. #include <boost/mp11/algorithm.hpp>
  25. #include <boost/mp11/list.hpp>
  26. #include <boost/mp11/utility.hpp>
  27. #include <cstddef>
  28. #include <tuple>
  29. #include <type_traits>
  30. #include <utility>
  31. namespace boost {
  32. namespace mysql {
  33. namespace detail {
  34. //
  35. // Base templates. Every StaticRow type must specialize the row_traits class,
  36. // providing the following members:
  37. //
  38. // // type of the actual row to be parsed. This supports marker types, like pfr_by_name
  39. // using underlying_row_type = /* */;
  40. //
  41. // // MP11 type list with the row's member types
  42. // using field_types = /* */;
  43. //
  44. // static constexpr name_table_t name_table() noexcept; // field names
  45. //
  46. // template <class F> /* Apply F to each member */
  47. // static void for_each_member(underlying_row_t<StaticRow>& to, F&& function);
  48. //
  49. //
  50. struct row_traits_is_unspecialized
  51. {
  52. };
  53. template <class T, bool is_describe_struct = describe::has_describe_members<T>::value>
  54. class row_traits : public row_traits_is_unspecialized
  55. {
  56. };
  57. //
  58. // Describe structs
  59. //
  60. // Workaround std::array::data not being constexpr in C++14
  61. template <class T, std::size_t N>
  62. struct array_wrapper
  63. {
  64. T data_[N];
  65. constexpr boost::span<const T> span() const noexcept { return boost::span<const T>(data_); }
  66. };
  67. template <class T>
  68. struct array_wrapper<T, 0>
  69. {
  70. struct
  71. {
  72. } data_; // allow empty brace initialization
  73. constexpr boost::span<const T> span() const noexcept { return boost::span<const T>(); }
  74. };
  75. // Workaround for char_traits::length not being constexpr in C++14
  76. // Only used to retrieve Describe member name lengths
  77. constexpr std::size_t get_length(const char* s) noexcept
  78. {
  79. const char* p = s;
  80. while (*p)
  81. ++p;
  82. return p - s;
  83. }
  84. template <class DescribeStruct>
  85. using row_members = describe::
  86. describe_members<DescribeStruct, describe::mod_public | describe::mod_inherited>;
  87. template <template <class...> class ListType, class... MemberDescriptor>
  88. constexpr array_wrapper<string_view, sizeof...(MemberDescriptor)> get_describe_names(ListType<
  89. MemberDescriptor...>)
  90. {
  91. return {{string_view(MemberDescriptor::name, get_length(MemberDescriptor::name))...}};
  92. }
  93. template <class DescribeStruct>
  94. BOOST_INLINE_CONSTEXPR auto describe_names_storage = get_describe_names(row_members<DescribeStruct>{});
  95. template <class DescribeStruct>
  96. class row_traits<DescribeStruct, true>
  97. {
  98. // clang-format off
  99. template <class D>
  100. using descriptor_to_type = typename
  101. std::remove_reference<decltype(std::declval<DescribeStruct>().*std::declval<D>().pointer)>::type;
  102. // clang-format on
  103. public:
  104. using underlying_row_type = DescribeStruct;
  105. using field_types = mp11::mp_transform<descriptor_to_type, row_members<DescribeStruct>>;
  106. static constexpr name_table_t name_table() noexcept
  107. {
  108. return describe_names_storage<DescribeStruct>.span();
  109. }
  110. template <class F>
  111. static void for_each_member(DescribeStruct& to, F&& function)
  112. {
  113. mp11::mp_for_each<row_members<DescribeStruct>>([function, &to](auto D) { function(to.*D.pointer); });
  114. }
  115. };
  116. //
  117. // Tuples
  118. //
  119. template <class... ReadableField>
  120. class row_traits<std::tuple<ReadableField...>, false>
  121. {
  122. public:
  123. using underlying_row_type = std::tuple<ReadableField...>;
  124. using field_types = std::tuple<ReadableField...>;
  125. static constexpr name_table_t name_table() noexcept { return name_table_t(); }
  126. template <class F>
  127. static void for_each_member(underlying_row_type& to, F&& function)
  128. {
  129. mp11::tuple_for_each(to, std::forward<F>(function));
  130. }
  131. };
  132. //
  133. // Helpers to implement the external interface section
  134. //
  135. // Helpers to check that all the fields satisfy ReadableField
  136. // and produce meaningful error messages, with the offending field type, at least
  137. // Workaround clang 3.6 not liking generic lambdas in the below constexpr function
  138. struct readable_field_checker
  139. {
  140. template <class TypeIdentity>
  141. constexpr void operator()(TypeIdentity) const noexcept
  142. {
  143. using T = typename TypeIdentity::type;
  144. static_assert(
  145. is_readable_field<T>::value,
  146. "You're trying to use an unsupported field type in a row type. Review your row type definitions."
  147. );
  148. }
  149. };
  150. template <class TypeList>
  151. static constexpr bool check_readable_field() noexcept
  152. {
  153. mp11::mp_for_each<mp11::mp_transform<mp11::mp_identity, TypeList>>(readable_field_checker{});
  154. return true;
  155. }
  156. // Centralize where check_readable_field happens
  157. template <class StaticRow>
  158. struct row_traits_with_check : row_traits<StaticRow>
  159. {
  160. static_assert(check_readable_field<typename row_traits<StaticRow>::field_types>(), "");
  161. };
  162. // Meta checking
  163. struct meta_check_field_fn
  164. {
  165. meta_check_context& ctx;
  166. template <class TypeIdentity>
  167. void operator()(TypeIdentity)
  168. {
  169. meta_check_field<typename TypeIdentity::type>(ctx);
  170. }
  171. };
  172. // Useful for testing
  173. template <class ReadableFieldList>
  174. error_code meta_check_impl(
  175. name_table_t name_table,
  176. span<const std::size_t> pos_map,
  177. metadata_collection_view meta,
  178. diagnostics& diag
  179. )
  180. {
  181. BOOST_ASSERT(pos_map.size() == mp11::mp_size<ReadableFieldList>::value);
  182. meta_check_context ctx(pos_map, name_table, meta);
  183. mp11::mp_for_each<mp11::mp_transform<mp11::mp_identity, ReadableFieldList>>(meta_check_field_fn{ctx});
  184. return ctx.check_errors(diag);
  185. }
  186. // Parsing
  187. class parse_context
  188. {
  189. span<const std::size_t> pos_map_;
  190. span<const field_view> fields_;
  191. std::size_t index_{};
  192. error_code ec_;
  193. public:
  194. parse_context(span<const std::size_t> pos_map, span<const field_view> fields) noexcept
  195. : pos_map_(pos_map), fields_(fields)
  196. {
  197. }
  198. template <class ReadableField>
  199. void parse(ReadableField& output)
  200. {
  201. auto ec = readable_field_traits<ReadableField>::parse(
  202. map_field_view(pos_map_, index_++, fields_),
  203. output
  204. );
  205. if (!ec_)
  206. ec_ = ec;
  207. }
  208. error_code error() const noexcept { return ec_; }
  209. };
  210. // Using this instead of a lambda reduces the number of generated instantiations
  211. struct parse_functor
  212. {
  213. parse_context& ctx;
  214. template <class ReadableField>
  215. void operator()(ReadableField& output) const
  216. {
  217. ctx.parse(output);
  218. }
  219. };
  220. //
  221. // External interface. Other Boost.MySQL components should never use row_traits
  222. // directly, but the functions below, instead.
  223. //
  224. template <class T>
  225. BOOST_INLINE_CONSTEXPR bool
  226. is_static_row = !std::is_base_of<row_traits_is_unspecialized, row_traits<T>>::value;
  227. #ifdef BOOST_MYSQL_HAS_CONCEPTS
  228. // Note that static_row only inspects the shape of the row only (i.e. it's a tuple vs. it's nothing we know),
  229. // and not individual fields. These are static_assert-ed in individual row_traits. This gives us an error
  230. // message that contains the offending types, at least.
  231. template <class T>
  232. concept static_row = is_static_row<T>;
  233. #define BOOST_MYSQL_STATIC_ROW ::boost::mysql::detail::static_row
  234. #else
  235. #define BOOST_MYSQL_STATIC_ROW class
  236. #endif
  237. template <BOOST_MYSQL_STATIC_ROW StaticRow>
  238. using underlying_row_t = typename row_traits_with_check<StaticRow>::underlying_row_type;
  239. template <BOOST_MYSQL_STATIC_ROW StaticRow>
  240. constexpr std::size_t get_row_size() noexcept
  241. {
  242. return mp11::mp_size<typename row_traits_with_check<StaticRow>::field_types>::value;
  243. }
  244. template <BOOST_MYSQL_STATIC_ROW StaticRow>
  245. constexpr name_table_t get_row_name_table() noexcept
  246. {
  247. return row_traits_with_check<StaticRow>::name_table();
  248. }
  249. template <BOOST_MYSQL_STATIC_ROW StaticRow>
  250. error_code meta_check(span<const std::size_t> pos_map, metadata_collection_view meta, diagnostics& diag)
  251. {
  252. using field_types = typename row_traits_with_check<StaticRow>::field_types;
  253. BOOST_ASSERT(pos_map.size() == get_row_size<StaticRow>());
  254. return meta_check_impl<field_types>(get_row_name_table<StaticRow>(), pos_map, meta, diag);
  255. }
  256. template <BOOST_MYSQL_STATIC_ROW StaticRow>
  257. error_code parse(
  258. span<const std::size_t> pos_map,
  259. span<const field_view> from,
  260. underlying_row_t<StaticRow>& to
  261. )
  262. {
  263. BOOST_ASSERT(pos_map.size() == get_row_size<StaticRow>());
  264. BOOST_ASSERT(from.size() >= get_row_size<StaticRow>());
  265. parse_context ctx(pos_map, from);
  266. row_traits_with_check<StaticRow>::for_each_member(to, parse_functor{ctx});
  267. return ctx.error();
  268. }
  269. using meta_check_fn_t =
  270. error_code (*)(span<const std::size_t> field_map, metadata_collection_view meta, diagnostics& diag);
  271. // For multi-resultset
  272. template <class... StaticRow>
  273. BOOST_INLINE_CONSTEXPR std::size_t max_num_columns = (std::max)({get_row_size<StaticRow>()...});
  274. BOOST_INLINE_CONSTEXPR std::size_t index_not_found = static_cast<std::size_t>(-1);
  275. template <class UnderlyingRowType, class... RowType>
  276. constexpr std::size_t get_type_index() noexcept
  277. {
  278. using lunique = mp11::mp_unique<mp11::mp_list<underlying_row_t<RowType>...>>;
  279. using index_t = mp11::mp_find<lunique, UnderlyingRowType>;
  280. return index_t::value < mp11::mp_size<lunique>::value ? index_t::value : index_not_found;
  281. }
  282. } // namespace detail
  283. } // namespace mysql
  284. } // namespace boost
  285. #endif // BOOST_MYSQL_CXX14
  286. #endif