conversion.hpp 17 KB

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