conversion.hpp 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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 <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,
  239. describe::mod_private
  240. | describe::mod_protected
  241. | boost::describe::mod_inherited>;
  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. #ifdef BOOST_DESCRIBE_CXX14
  263. constexpr
  264. bool
  265. compare_strings(char const* l, char const* r)
  266. {
  267. #if defined(_MSC_VER) && (_MSC_VER <= 1900) && !defined(__clang__)
  268. return *l == *r && ( (*l == 0) | compare_strings(l + 1, r + 1) );
  269. #else
  270. do
  271. {
  272. if( *l != *r )
  273. return false;
  274. if( *l == 0 )
  275. return true;
  276. ++l;
  277. ++r;
  278. } while(true);
  279. #endif
  280. }
  281. template< class L, class R >
  282. struct equal_member_names
  283. : mp11::mp_bool< compare_strings(L::name, R::name) >
  284. {};
  285. template< class T >
  286. using uniquely_named_members = mp11::mp_same<
  287. mp11::mp_unique_if< described_members<T>, equal_member_names >,
  288. described_members<T> >;
  289. #else
  290. // we only check this in C++14, but the template should exist nevertheless
  291. template< class T >
  292. using uniquely_named_members = std::true_type;
  293. #endif // BOOST_DESCRIBE_CXX14
  294. // user conversion (via tag_invoke)
  295. template< class Ctx, class T, class Dir >
  296. using user_conversion_category = mp11::mp_cond<
  297. has_user_conversion3<Ctx, T, Dir>, full_context_conversion_tag,
  298. has_user_conversion2<Ctx, T, Dir>, context_conversion_tag,
  299. has_user_conversion1<T, Dir>, user_conversion_tag>;
  300. // native conversions (constructors and member functions of value)
  301. template< class T >
  302. using native_conversion_category = mp11::mp_cond<
  303. std::is_same<T, value>, value_conversion_tag,
  304. std::is_same<T, array>, array_conversion_tag,
  305. std::is_same<T, object>, object_conversion_tag,
  306. std::is_same<T, string>, string_conversion_tag>;
  307. // generic conversions
  308. template< class T >
  309. using generic_conversion_category = mp11::mp_cond<
  310. std::is_same<T, bool>, bool_conversion_tag,
  311. std::is_integral<T>, integral_conversion_tag,
  312. std::is_floating_point<T>, floating_point_conversion_tag,
  313. is_null_like<T>, null_like_conversion_tag,
  314. is_string_like<T>, string_like_conversion_tag,
  315. is_variant_like<T>, variant_conversion_tag,
  316. is_optional_like<T>, optional_conversion_tag,
  317. is_map_like<T>, map_like_conversion_tag,
  318. is_sequence_like<T>, sequence_conversion_tag,
  319. is_tuple_like<T>, tuple_conversion_tag,
  320. is_described_class<T>, described_class_conversion_tag,
  321. is_described_enum<T>, described_enum_conversion_tag,
  322. is_path_like<T>, path_conversion_tag,
  323. // failed to find a suitable implementation
  324. mp11::mp_true, no_conversion_tag>;
  325. template< class T >
  326. using nested_type = typename T::type;
  327. template< class T1, class T2 >
  328. using conversion_category_impl_helper = mp11::mp_eval_if_not<
  329. std::is_same<detail::no_conversion_tag, T1>,
  330. T1,
  331. mp11::mp_eval_or_q, T1, mp11::mp_quote<nested_type>, T2>;
  332. template< class Ctx, class T, class Dir >
  333. struct conversion_category_impl
  334. {
  335. using type = mp11::mp_fold<
  336. mp11::mp_list<
  337. mp11::mp_defer<user_conversion_category, Ctx, T, Dir>,
  338. mp11::mp_defer<native_conversion_category, T>,
  339. mp11::mp_defer<generic_conversion_category, T>>,
  340. no_conversion_tag,
  341. conversion_category_impl_helper>;
  342. };
  343. template< class Ctx, class T, class Dir >
  344. using conversion_category =
  345. typename conversion_category_impl< Ctx, T, Dir >::type;
  346. template< class T >
  347. using any_conversion_tag = mp11::mp_not<
  348. std::is_same< T, no_conversion_tag > >;
  349. template< class T, class Dir, class... Ctxs >
  350. struct conversion_category_impl< std::tuple<Ctxs...>, T, Dir >
  351. {
  352. using ctxs = mp11::mp_list< remove_cvref<Ctxs>... >;
  353. using cats = mp11::mp_list<
  354. conversion_category<remove_cvref<Ctxs>, T, Dir>... >;
  355. template< class I >
  356. using exists = mp11::mp_less< I, mp11::mp_size<cats> >;
  357. using context2 = mp11::mp_find< cats, full_context_conversion_tag >;
  358. using context1 = mp11::mp_find< cats, context_conversion_tag >;
  359. using context0 = mp11::mp_find< cats, user_conversion_tag >;
  360. using index = mp11::mp_cond<
  361. exists<context2>, context2,
  362. exists<context1>, context1,
  363. exists<context0>, context0,
  364. mp11::mp_true, mp11::mp_find_if< cats, any_conversion_tag > >;
  365. using type = mp11::mp_eval_or<
  366. no_conversion_tag,
  367. mp11::mp_at, cats, index >;
  368. };
  369. struct no_context
  370. {};
  371. template <class T, class Dir>
  372. using can_convert = mp11::mp_not<
  373. std::is_same<
  374. detail::conversion_category<no_context, T, Dir>,
  375. detail::no_conversion_tag>>;
  376. template<class Impl1, class Impl2>
  377. using conversion_round_trips_helper = mp11::mp_or<
  378. std::is_same<Impl1, Impl2>,
  379. std::is_base_of<user_conversion_tag, Impl1>,
  380. std::is_base_of<user_conversion_tag, Impl2>>;
  381. template< class Ctx, class T, class Dir >
  382. using conversion_round_trips = conversion_round_trips_helper<
  383. conversion_category<Ctx, T, Dir>,
  384. conversion_category<Ctx, T, mp11::mp_not<Dir>>>;
  385. template< class T1, class T2 >
  386. struct copy_cref_helper
  387. {
  388. using type = remove_cvref<T2>;
  389. };
  390. template< class T1, class T2 >
  391. using copy_cref = typename copy_cref_helper< T1, T2 >::type;
  392. template< class T1, class T2 >
  393. struct copy_cref_helper<T1 const, T2>
  394. {
  395. using type = remove_cvref<T2> const;
  396. };
  397. template< class T1, class T2 >
  398. struct copy_cref_helper<T1&, T2>
  399. {
  400. using type = copy_cref<T1, T2>&;
  401. };
  402. template< class T1, class T2 >
  403. struct copy_cref_helper<T1&&, T2>
  404. {
  405. using type = copy_cref<T1, T2>&&;
  406. };
  407. template< class Rng, class Traits >
  408. using forwarded_value_helper = mp11::mp_if<
  409. std::is_convertible<
  410. typename Traits::reference,
  411. copy_cref<Rng, typename Traits::value_type> >,
  412. copy_cref<Rng, typename Traits::value_type>,
  413. typename Traits::value_type >;
  414. template< class Rng >
  415. using forwarded_value = forwarded_value_helper<
  416. Rng, iterator_traits< Rng > >;
  417. template< class Ctx, class T, class Dir >
  418. struct supported_context
  419. {
  420. using type = Ctx;
  421. static
  422. type const&
  423. get( Ctx const& ctx ) noexcept
  424. {
  425. return ctx;
  426. }
  427. };
  428. template< class T, class Dir, class... Ctxs >
  429. struct supported_context< std::tuple<Ctxs...>, T, Dir >
  430. {
  431. using Ctx = std::tuple<Ctxs...>;
  432. using impl = conversion_category_impl<Ctx, T, Dir>;
  433. using index = typename impl::index;
  434. using next_supported = supported_context<
  435. mp11::mp_at< typename impl::ctxs, index >, T, Dir >;
  436. using type = typename next_supported::type;
  437. static
  438. type const&
  439. get( Ctx const& ctx ) noexcept
  440. {
  441. return next_supported::get( std::get<index::value>( ctx ) );
  442. }
  443. };
  444. template< class T >
  445. using value_result_type = typename std::decay<
  446. decltype( std::declval<T&>().value() )>::type;
  447. template< class T >
  448. using can_reset = decltype( std::declval<T&>().reset() );
  449. template< class T >
  450. using has_valueless_by_exception =
  451. decltype( std::declval<T const&>().valueless_by_exception() );
  452. } // namespace detail
  453. template <class T>
  454. struct result_for<T, value>
  455. {
  456. using type = system::result< detail::remove_cvref<T> >;
  457. };
  458. template<class T>
  459. struct is_string_like
  460. : std::is_convertible<T, string_view>
  461. { };
  462. template<class T>
  463. struct is_path_like
  464. : mp11::mp_all<
  465. mp11::mp_valid_and_true<detail::is_its_own_value, T>,
  466. mp11::mp_valid_and_true<detail::has_string_type, T>>
  467. { };
  468. template<class T>
  469. struct is_sequence_like
  470. : mp11::mp_all<
  471. mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
  472. mp11::mp_valid_and_true<detail::not_its_own_value, T>,
  473. mp11::mp_valid<detail::begin_iterator_category, T>>
  474. { };
  475. template<class T>
  476. struct is_map_like
  477. : mp11::mp_all<
  478. is_sequence_like<T>,
  479. mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
  480. is_string_like<detail::key_type<T>>,
  481. mp11::mp_valid_and_true<detail::has_unique_keys, T>>
  482. { };
  483. template<class T>
  484. struct is_tuple_like
  485. : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
  486. { };
  487. template<>
  488. struct is_null_like<std::nullptr_t>
  489. : std::true_type
  490. { };
  491. #ifndef BOOST_NO_CXX17_HDR_VARIANT
  492. template<>
  493. struct is_null_like<std::monostate>
  494. : std::true_type
  495. { };
  496. #endif // BOOST_NO_CXX17_HDR_VARIANT
  497. template<class T>
  498. struct is_described_class
  499. : mp11::mp_and<
  500. describe::has_describe_members<T>,
  501. mp11::mp_not< std::is_union<T> >,
  502. mp11::mp_empty<
  503. mp11::mp_eval_or<
  504. mp11::mp_list<>, detail::described_non_public_members, T>>>
  505. { };
  506. template<class T>
  507. struct is_described_enum
  508. : describe::has_describe_enumerators<T>
  509. { };
  510. template<class T>
  511. struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T>
  512. { };
  513. template<class T>
  514. struct is_optional_like
  515. : mp11::mp_and<
  516. mp11::mp_not<std::is_void<
  517. mp11::mp_eval_or<void, detail::value_result_type, T>>>,
  518. mp11::mp_valid<detail::can_reset, T>>
  519. { };
  520. } // namespace json
  521. } // namespace boost
  522. #endif // BOOST_JSON_IMPL_CONVERSION_HPP