conversion.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545
  1. //
  2. // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
  3. // Copyright (c) 2022 Dmitry Arkhipov (grisumbras@yandex.ru)
  4. //
  5. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. //
  8. // Official repository: https://github.com/boostorg/json
  9. //
  10. #ifndef BOOST_JSON_IMPL_CONVERSION_HPP
  11. #define BOOST_JSON_IMPL_CONVERSION_HPP
  12. #include <boost/json/fwd.hpp>
  13. #include <boost/json/value.hpp>
  14. #include <boost/json/string_view.hpp>
  15. #include <boost/describe/enumerators.hpp>
  16. #include <boost/describe/members.hpp>
  17. #include <boost/describe/bases.hpp>
  18. #include <boost/mp11/algorithm.hpp>
  19. #include <boost/mp11/utility.hpp>
  20. #include <iterator>
  21. #include <tuple>
  22. #include <utility>
  23. #ifndef BOOST_NO_CXX17_HDR_VARIANT
  24. # include <variant>
  25. #endif // BOOST_NO_CXX17_HDR_VARIANT
  26. namespace boost {
  27. namespace json {
  28. namespace detail {
  29. #ifdef __cpp_lib_nonmember_container_access
  30. using std::size;
  31. #endif
  32. template<std::size_t I, class T>
  33. using tuple_element_t = typename std::tuple_element<I, T>::type;
  34. template<class T>
  35. using iterator_type = decltype(std::begin(std::declval<T&>()));
  36. template<class T>
  37. using iterator_traits = std::iterator_traits< iterator_type<T> >;
  38. template<class T>
  39. using value_type = typename iterator_traits<T>::value_type;
  40. template<class T>
  41. using mapped_type = tuple_element_t< 1, value_type<T> >;
  42. // had to make the metafunction always succeeding in order to make it work
  43. // with msvc 14.0
  44. template<class T>
  45. using key_type_helper = tuple_element_t< 0, value_type<T> >;
  46. template<class T>
  47. using key_type = mp11::mp_eval_or<
  48. void,
  49. key_type_helper,
  50. T>;
  51. template<class T>
  52. using are_begin_and_end_same = std::is_same<
  53. iterator_type<T>,
  54. decltype(std::end(std::declval<T&>()))>;
  55. template<class T>
  56. using begin_iterator_category = typename std::iterator_traits<
  57. iterator_type<T>>::iterator_category;
  58. template<class T>
  59. using has_positive_tuple_size = mp11::mp_bool<
  60. (std::tuple_size<T>::value > 0) >;
  61. template<class T>
  62. using has_unique_keys = has_positive_tuple_size<decltype(
  63. std::declval<T&>().emplace(
  64. std::declval<value_type<T>>()))>;
  65. template<class T>
  66. struct is_value_type_pair_helper : std::false_type
  67. { };
  68. template<class T1, class T2>
  69. struct is_value_type_pair_helper<std::pair<T1, T2>> : std::true_type
  70. { };
  71. template<class T>
  72. using is_value_type_pair = is_value_type_pair_helper<value_type<T>>;
  73. template<class T>
  74. using has_size_member_helper
  75. = std::is_convertible<decltype(std::declval<T&>().size()), std::size_t>;
  76. template<class T>
  77. using has_size_member = mp11::mp_valid_and_true<has_size_member_helper, T>;
  78. template<class T>
  79. using has_free_size_helper
  80. = std::is_convertible<
  81. decltype(size(std::declval<T const&>())),
  82. std::size_t>;
  83. template<class T>
  84. using has_free_size = mp11::mp_valid_and_true<has_free_size_helper, T>;
  85. template<class T>
  86. using size_implementation = mp11::mp_cond<
  87. has_size_member<T>, mp11::mp_int<3>,
  88. has_free_size<T>, mp11::mp_int<2>,
  89. std::is_array<T>, mp11::mp_int<1>,
  90. mp11::mp_true, mp11::mp_int<0>>;
  91. template<class T>
  92. std::size_t
  93. try_size(T&& cont, mp11::mp_int<3>)
  94. {
  95. return cont.size();
  96. }
  97. template<class T>
  98. std::size_t
  99. try_size(T& cont, mp11::mp_int<2>)
  100. {
  101. return size(cont);
  102. }
  103. template<class T, std::size_t N>
  104. std::size_t
  105. try_size(T(&)[N], mp11::mp_int<1>)
  106. {
  107. return N;
  108. }
  109. template<class T>
  110. std::size_t
  111. try_size(T&, mp11::mp_int<0>)
  112. {
  113. return 0;
  114. }
  115. template<class T>
  116. using has_push_back_helper
  117. = decltype(std::declval<T&>().push_back(std::declval<value_type<T>>()));
  118. template<class T>
  119. using has_push_back = mp11::mp_valid<has_push_back_helper, T>;
  120. template<class T>
  121. using inserter_implementation = mp11::mp_cond<
  122. is_tuple_like<T>, mp11::mp_int<2>,
  123. has_push_back<T>, mp11::mp_int<1>,
  124. mp11::mp_true, mp11::mp_int<0>>;
  125. template<class T>
  126. iterator_type<T>
  127. inserter(
  128. T& target,
  129. mp11::mp_int<2>)
  130. {
  131. return target.begin();
  132. }
  133. template<class T>
  134. std::back_insert_iterator<T>
  135. inserter(
  136. T& target,
  137. mp11::mp_int<1>)
  138. {
  139. return std::back_inserter(target);
  140. }
  141. template<class T>
  142. std::insert_iterator<T>
  143. inserter(
  144. T& target,
  145. mp11::mp_int<0>)
  146. {
  147. return std::inserter( target, target.end() );
  148. }
  149. using value_from_conversion = mp11::mp_true;
  150. using value_to_conversion = mp11::mp_false;
  151. struct user_conversion_tag { };
  152. struct context_conversion_tag : user_conversion_tag { };
  153. struct full_context_conversion_tag : context_conversion_tag { };
  154. struct native_conversion_tag { };
  155. struct value_conversion_tag : native_conversion_tag { };
  156. struct object_conversion_tag : native_conversion_tag { };
  157. struct array_conversion_tag : native_conversion_tag { };
  158. struct string_conversion_tag : native_conversion_tag { };
  159. struct bool_conversion_tag : native_conversion_tag { };
  160. struct number_conversion_tag : native_conversion_tag { };
  161. struct integral_conversion_tag : number_conversion_tag { };
  162. struct floating_point_conversion_tag : number_conversion_tag { };
  163. struct null_like_conversion_tag { };
  164. struct string_like_conversion_tag { };
  165. struct map_like_conversion_tag { };
  166. struct sequence_conversion_tag { };
  167. struct tuple_conversion_tag { };
  168. struct described_class_conversion_tag { };
  169. struct described_enum_conversion_tag { };
  170. struct variant_conversion_tag { };
  171. struct optional_conversion_tag { };
  172. struct no_conversion_tag { };
  173. template<class... Args>
  174. using supports_tag_invoke = decltype(tag_invoke( std::declval<Args>()... ));
  175. template<class T>
  176. using has_user_conversion_from_impl = supports_tag_invoke<
  177. value_from_tag, value&, T&& >;
  178. template<class T>
  179. using has_user_conversion_to_impl = supports_tag_invoke<
  180. value_to_tag<T>, value const& >;
  181. template<class T>
  182. using has_nonthrowing_user_conversion_to_impl = supports_tag_invoke<
  183. try_value_to_tag<T>, value const& >;
  184. template< class T, class Dir >
  185. using has_user_conversion1 = mp11::mp_if<
  186. std::is_same<Dir, value_from_conversion>,
  187. mp11::mp_valid<has_user_conversion_from_impl, T>,
  188. mp11::mp_or<
  189. mp11::mp_valid<has_user_conversion_to_impl, T>,
  190. mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>>>;
  191. template< class Ctx, class T >
  192. using has_context_conversion_from_impl = supports_tag_invoke<
  193. value_from_tag, value&, T&&, Ctx const& >;
  194. template< class Ctx, class T >
  195. using has_context_conversion_to_impl = supports_tag_invoke<
  196. value_to_tag<T>, value const&, Ctx const& >;
  197. template< class Ctx, class T >
  198. using has_nonthrowing_context_conversion_to_impl = supports_tag_invoke<
  199. try_value_to_tag<T>, value const&, Ctx const& >;
  200. template< class Ctx, class T, class Dir >
  201. using has_user_conversion2 = mp11::mp_if<
  202. std::is_same<Dir, value_from_conversion>,
  203. mp11::mp_valid<has_context_conversion_from_impl, Ctx, T>,
  204. mp11::mp_or<
  205. mp11::mp_valid<has_context_conversion_to_impl, Ctx, T>,
  206. mp11::mp_valid<has_nonthrowing_context_conversion_to_impl, Ctx, T>>>;
  207. template< class Ctx, class T >
  208. using has_full_context_conversion_from_impl = supports_tag_invoke<
  209. value_from_tag, value&, T&&, Ctx const&, Ctx const& >;
  210. template< class Ctx, class T >
  211. using has_full_context_conversion_to_impl = supports_tag_invoke<
  212. value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
  213. template< class Ctx, class T >
  214. using has_nonthrowing_full_context_conversion_to_impl = supports_tag_invoke<
  215. try_value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
  216. template< class Ctx, class T, class Dir >
  217. using has_user_conversion3 = mp11::mp_if<
  218. std::is_same<Dir, value_from_conversion>,
  219. mp11::mp_valid<has_full_context_conversion_from_impl, Ctx, T>,
  220. mp11::mp_or<
  221. mp11::mp_valid<has_full_context_conversion_to_impl, Ctx, T>,
  222. mp11::mp_valid<
  223. has_nonthrowing_full_context_conversion_to_impl, Ctx, T>>>;
  224. template< class T >
  225. using described_non_public_members = describe::describe_members<
  226. T, describe::mod_private | describe::mod_protected>;
  227. template< class T >
  228. using described_bases = describe::describe_bases<
  229. T, describe::mod_any_access>;
  230. #if defined(BOOST_MSVC) && BOOST_MSVC < 1920
  231. template< class T >
  232. struct described_member_t_impl;
  233. template< class T, class C >
  234. struct described_member_t_impl<T C::*>
  235. {
  236. using type = T;
  237. };
  238. template< class T, class D >
  239. using described_member_t = remove_cvref<
  240. typename described_member_t_impl<
  241. remove_cvref<decltype(D::pointer)> >::type>;
  242. #else
  243. template< class T, class D >
  244. using described_member_t = remove_cvref<decltype(
  245. std::declval<T&>().* D::pointer )>;
  246. #endif
  247. // user conversion (via tag_invoke)
  248. template< class Ctx, class T, class Dir >
  249. using user_conversion_category = mp11::mp_cond<
  250. has_user_conversion3<Ctx, T, Dir>, full_context_conversion_tag,
  251. has_user_conversion2<Ctx, T, Dir>, context_conversion_tag,
  252. has_user_conversion1<T, Dir>, user_conversion_tag>;
  253. // native conversions (constructors and member functions of value)
  254. template< class T >
  255. using native_conversion_category = mp11::mp_cond<
  256. std::is_same<T, value>, value_conversion_tag,
  257. std::is_same<T, array>, array_conversion_tag,
  258. std::is_same<T, object>, object_conversion_tag,
  259. std::is_same<T, string>, string_conversion_tag>;
  260. // generic conversions
  261. template< class T >
  262. using generic_conversion_category = mp11::mp_cond<
  263. std::is_same<T, bool>, bool_conversion_tag,
  264. std::is_integral<T>, integral_conversion_tag,
  265. std::is_floating_point<T>, floating_point_conversion_tag,
  266. is_null_like<T>, null_like_conversion_tag,
  267. is_string_like<T>, string_like_conversion_tag,
  268. is_map_like<T>, map_like_conversion_tag,
  269. is_sequence_like<T>, sequence_conversion_tag,
  270. is_tuple_like<T>, tuple_conversion_tag,
  271. is_described_class<T>, described_class_conversion_tag,
  272. is_described_enum<T>, described_enum_conversion_tag,
  273. is_variant_like<T>, variant_conversion_tag,
  274. is_optional_like<T>, optional_conversion_tag,
  275. // failed to find a suitable implementation
  276. mp11::mp_true, no_conversion_tag>;
  277. template< class T >
  278. using nested_type = typename T::type;
  279. template< class T1, class T2 >
  280. using conversion_category_impl_helper = mp11::mp_eval_if_not<
  281. std::is_same<detail::no_conversion_tag, T1>,
  282. T1,
  283. mp11::mp_eval_or_q, T1, mp11::mp_quote<nested_type>, T2>;
  284. template< class Ctx, class T, class Dir >
  285. struct conversion_category_impl
  286. {
  287. using type = mp11::mp_fold<
  288. mp11::mp_list<
  289. mp11::mp_defer<user_conversion_category, Ctx, T, Dir>,
  290. mp11::mp_defer<native_conversion_category, T>,
  291. mp11::mp_defer<generic_conversion_category, T>>,
  292. no_conversion_tag,
  293. conversion_category_impl_helper>;
  294. };
  295. template< class Ctx, class T, class Dir >
  296. using conversion_category =
  297. typename conversion_category_impl< Ctx, T, Dir >::type;
  298. template< class T >
  299. using any_conversion_tag = mp11::mp_not<
  300. std::is_same< T, no_conversion_tag > >;
  301. template< class T, class Dir, class... Ctxs >
  302. struct conversion_category_impl< std::tuple<Ctxs...>, T, Dir >
  303. {
  304. using ctxs = mp11::mp_list< remove_cvref<Ctxs>... >;
  305. using cats = mp11::mp_list<
  306. conversion_category<remove_cvref<Ctxs>, T, Dir>... >;
  307. template< class I >
  308. using exists = mp11::mp_less< I, mp11::mp_size<cats> >;
  309. using context2 = mp11::mp_find< cats, full_context_conversion_tag >;
  310. using context1 = mp11::mp_find< cats, context_conversion_tag >;
  311. using context0 = mp11::mp_find< cats, user_conversion_tag >;
  312. using index = mp11::mp_cond<
  313. exists<context2>, context2,
  314. exists<context1>, context1,
  315. exists<context0>, context0,
  316. mp11::mp_true, mp11::mp_find_if< cats, any_conversion_tag > >;
  317. using type = mp11::mp_eval_or<
  318. no_conversion_tag,
  319. mp11::mp_at, cats, index >;
  320. };
  321. struct no_context
  322. {};
  323. struct allow_exceptions
  324. {};
  325. template <class T, class Dir>
  326. using can_convert = mp11::mp_not<
  327. std::is_same<
  328. detail::conversion_category<no_context, T, Dir>,
  329. detail::no_conversion_tag>>;
  330. template<class Impl1, class Impl2>
  331. using conversion_round_trips_helper = mp11::mp_or<
  332. std::is_same<Impl1, Impl2>,
  333. std::is_base_of<user_conversion_tag, Impl1>,
  334. std::is_base_of<user_conversion_tag, Impl2>>;
  335. template< class Ctx, class T, class Dir >
  336. using conversion_round_trips = conversion_round_trips_helper<
  337. conversion_category<Ctx, T, Dir>,
  338. conversion_category<Ctx, T, mp11::mp_not<Dir>>>;
  339. template< class T1, class T2 >
  340. struct copy_cref_helper
  341. {
  342. using type = remove_cvref<T2>;
  343. };
  344. template< class T1, class T2 >
  345. using copy_cref = typename copy_cref_helper< T1, T2 >::type;
  346. template< class T1, class T2 >
  347. struct copy_cref_helper<T1 const, T2>
  348. {
  349. using type = remove_cvref<T2> const;
  350. };
  351. template< class T1, class T2 >
  352. struct copy_cref_helper<T1&, T2>
  353. {
  354. using type = copy_cref<T1, T2>&;
  355. };
  356. template< class T1, class T2 >
  357. struct copy_cref_helper<T1&&, T2>
  358. {
  359. using type = copy_cref<T1, T2>&&;
  360. };
  361. template< class Rng, class Traits >
  362. using forwarded_value_helper = mp11::mp_if<
  363. std::is_convertible<
  364. typename Traits::reference,
  365. copy_cref<Rng, typename Traits::value_type> >,
  366. copy_cref<Rng, typename Traits::value_type>,
  367. typename Traits::value_type >;
  368. template< class Rng >
  369. using forwarded_value = forwarded_value_helper<
  370. Rng, iterator_traits< Rng > >;
  371. template< class Ctx, class T, class Dir >
  372. struct supported_context
  373. {
  374. using type = Ctx;
  375. static
  376. type const&
  377. get( Ctx const& ctx ) noexcept
  378. {
  379. return ctx;
  380. }
  381. };
  382. template< class T, class Dir, class... Ctxs >
  383. struct supported_context< std::tuple<Ctxs...>, T, Dir >
  384. {
  385. using Ctx = std::tuple<Ctxs...>;
  386. using impl = conversion_category_impl<Ctx, T, Dir>;
  387. using index = typename impl::index;
  388. using next_supported = supported_context<
  389. mp11::mp_at< typename impl::ctxs, index >, T, Dir >;
  390. using type = typename next_supported::type;
  391. static
  392. type const&
  393. get( Ctx const& ctx ) noexcept
  394. {
  395. return next_supported::get( std::get<index::value>( ctx ) );
  396. }
  397. };
  398. template< class T >
  399. using value_result_type = typename std::decay<
  400. decltype( std::declval<T&>().value() )>::type;
  401. template< class T >
  402. using can_reset = decltype( std::declval<T&>().reset() );
  403. template< class T >
  404. using has_valueless_by_exception =
  405. decltype( std::declval<T const&>().valueless_by_exception() );
  406. } // namespace detail
  407. template <class T>
  408. struct result_for<T, value>
  409. {
  410. using type = result< detail::remove_cvref<T> >;
  411. };
  412. template<class T>
  413. struct is_string_like
  414. : std::is_convertible<T, string_view>
  415. { };
  416. template<class T>
  417. struct is_sequence_like
  418. : mp11::mp_all<
  419. mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
  420. mp11::mp_valid<detail::begin_iterator_category, T>>
  421. { };
  422. template<class T>
  423. struct is_map_like
  424. : mp11::mp_all<
  425. is_sequence_like<T>,
  426. mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
  427. is_string_like<detail::key_type<T>>,
  428. mp11::mp_valid_and_true<detail::has_unique_keys, T>>
  429. { };
  430. template<class T>
  431. struct is_tuple_like
  432. : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
  433. { };
  434. template<>
  435. struct is_null_like<std::nullptr_t>
  436. : std::true_type
  437. { };
  438. #ifndef BOOST_NO_CXX17_HDR_VARIANT
  439. template<>
  440. struct is_null_like<std::monostate>
  441. : std::true_type
  442. { };
  443. #endif // BOOST_NO_CXX17_HDR_VARIANT
  444. template<class T>
  445. struct is_described_class
  446. : mp11::mp_and<
  447. describe::has_describe_members<T>,
  448. mp11::mp_not< std::is_union<T> >,
  449. mp11::mp_empty<
  450. mp11::mp_eval_or<
  451. mp11::mp_list<>, detail::described_non_public_members, T>>,
  452. mp11::mp_empty<
  453. mp11::mp_eval_or<mp11::mp_list<>, detail::described_bases, T>>>
  454. { };
  455. template<class T>
  456. struct is_described_enum
  457. : describe::has_describe_enumerators<T>
  458. { };
  459. template<class T>
  460. struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T>
  461. { };
  462. template<class T>
  463. struct is_optional_like
  464. : mp11::mp_and<
  465. mp11::mp_not<std::is_void<
  466. mp11::mp_eval_or<void, detail::value_result_type, T>>>,
  467. mp11::mp_valid<detail::can_reset, T>>
  468. { };
  469. } // namespace json
  470. } // namespace boost
  471. #endif // BOOST_JSON_IMPL_CONVERSION_HPP