conversion.hpp 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  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 <utility>
  22. #ifndef BOOST_NO_CXX17_HDR_VARIANT
  23. # include <variant>
  24. #endif // BOOST_NO_CXX17_HDR_VARIANT
  25. namespace boost {
  26. namespace json {
  27. namespace detail {
  28. #ifdef __cpp_lib_nonmember_container_access
  29. using std::size;
  30. #endif
  31. template<std::size_t I, class T>
  32. using tuple_element_t = typename std::tuple_element<I, T>::type;
  33. template<class T>
  34. using iterator_type = decltype(std::begin(std::declval<T&>()));
  35. template<class T>
  36. using iterator_traits = std::iterator_traits< iterator_type<T> >;
  37. template<class T>
  38. using value_type = typename iterator_traits<T>::value_type;
  39. template<class T>
  40. using mapped_type = tuple_element_t< 1, value_type<T> >;
  41. // had to make the metafunction always succeeding in order to make it work
  42. // with msvc 14.0
  43. template<class T>
  44. using key_type_helper = tuple_element_t< 0, value_type<T> >;
  45. template<class T>
  46. using key_type = mp11::mp_eval_or<
  47. void,
  48. key_type_helper,
  49. T>;
  50. template<class T>
  51. using are_begin_and_end_same = std::is_same<
  52. iterator_type<T>,
  53. decltype(std::end(std::declval<T&>()))>;
  54. template<class T>
  55. using begin_iterator_category = typename std::iterator_traits<
  56. iterator_type<T>>::iterator_category;
  57. template<class T>
  58. using has_positive_tuple_size = mp11::mp_bool<
  59. (std::tuple_size<T>::value > 0) >;
  60. template<class T>
  61. using has_unique_keys = has_positive_tuple_size<decltype(
  62. std::declval<T&>().emplace(
  63. std::declval<value_type<T>>()))>;
  64. template<class T>
  65. struct is_value_type_pair_helper : std::false_type
  66. { };
  67. template<class T1, class T2>
  68. struct is_value_type_pair_helper<std::pair<T1, T2>> : std::true_type
  69. { };
  70. template<class T>
  71. using is_value_type_pair = is_value_type_pair_helper<value_type<T>>;
  72. template<class T>
  73. using has_size_member_helper
  74. = std::is_convertible<decltype(std::declval<T&>().size()), std::size_t>;
  75. template<class T>
  76. using has_size_member = mp11::mp_valid_and_true<has_size_member_helper, T>;
  77. template<class T>
  78. using has_free_size_helper
  79. = std::is_convertible<
  80. decltype(size(std::declval<T const&>())),
  81. std::size_t>;
  82. template<class T>
  83. using has_free_size = mp11::mp_valid_and_true<has_free_size_helper, T>;
  84. template<class T>
  85. using size_implementation = mp11::mp_cond<
  86. has_size_member<T>, mp11::mp_int<3>,
  87. has_free_size<T>, mp11::mp_int<2>,
  88. std::is_array<T>, mp11::mp_int<1>,
  89. mp11::mp_true, mp11::mp_int<0>>;
  90. template<class T>
  91. std::size_t
  92. try_size(T&& cont, mp11::mp_int<3>)
  93. {
  94. return cont.size();
  95. }
  96. template<class T>
  97. std::size_t
  98. try_size(T& cont, mp11::mp_int<2>)
  99. {
  100. return size(cont);
  101. }
  102. template<class T, std::size_t N>
  103. std::size_t
  104. try_size(T(&)[N], mp11::mp_int<1>)
  105. {
  106. return N;
  107. }
  108. template<class T>
  109. std::size_t
  110. try_size(T&, mp11::mp_int<0>)
  111. {
  112. return 0;
  113. }
  114. using value_from_conversion = mp11::mp_true;
  115. using value_to_conversion = mp11::mp_false;
  116. struct user_conversion_tag { };
  117. struct native_conversion_tag { };
  118. struct value_conversion_tag : native_conversion_tag { };
  119. struct object_conversion_tag : native_conversion_tag { };
  120. struct array_conversion_tag : native_conversion_tag { };
  121. struct string_conversion_tag : native_conversion_tag { };
  122. struct bool_conversion_tag : native_conversion_tag { };
  123. struct number_conversion_tag : native_conversion_tag { };
  124. struct null_like_conversion_tag { };
  125. struct string_like_conversion_tag { };
  126. struct map_like_conversion_tag { };
  127. struct sequence_conversion_tag { };
  128. struct tuple_conversion_tag { };
  129. struct described_class_conversion_tag { };
  130. struct described_enum_conversion_tag { };
  131. struct no_conversion_tag { };
  132. template<class T>
  133. using has_user_conversion_from_impl
  134. = decltype(tag_invoke(
  135. value_from_tag(), std::declval<value&>(), std::declval<T&&>()));
  136. template<class T>
  137. using has_user_conversion_to_impl
  138. = decltype(tag_invoke(value_to_tag<T>(), std::declval<value const &>()));
  139. template<class T>
  140. using has_nonthrowing_user_conversion_to_impl
  141. = decltype(tag_invoke(
  142. try_value_to_tag<T>(), std::declval<value const&>() ));
  143. template<class T, class Dir>
  144. using has_user_conversion = mp11::mp_if<
  145. std::is_same<Dir, value_from_conversion>,
  146. mp11::mp_valid<has_user_conversion_from_impl, T>,
  147. mp11::mp_or<
  148. mp11::mp_valid<has_user_conversion_to_impl, T>,
  149. mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>>>;
  150. template< class T >
  151. using described_non_public_members = describe::describe_members<
  152. T, describe::mod_private | describe::mod_protected>;
  153. template< class T >
  154. using described_bases = describe::describe_bases<
  155. T, describe::mod_any_access>;
  156. template<class T, class Dir>
  157. using conversion_implementation = mp11::mp_cond<
  158. // user conversion (via tag_invoke)
  159. has_user_conversion<T, Dir>, user_conversion_tag,
  160. // native conversions (constructors and member functions of value)
  161. std::is_same<T, value>, value_conversion_tag,
  162. std::is_same<T, array>, array_conversion_tag,
  163. std::is_same<T, object>, object_conversion_tag,
  164. std::is_same<T, string>, string_conversion_tag,
  165. std::is_same<T, bool>, bool_conversion_tag,
  166. std::is_arithmetic<T>, number_conversion_tag,
  167. // generic conversions
  168. is_null_like<T>, null_like_conversion_tag,
  169. is_string_like<T>, string_like_conversion_tag,
  170. is_map_like<T>, map_like_conversion_tag,
  171. is_sequence_like<T>, sequence_conversion_tag,
  172. is_tuple_like<T>, tuple_conversion_tag,
  173. is_described_class<T>, described_class_conversion_tag,
  174. is_described_enum<T>, described_enum_conversion_tag,
  175. // failed to find a suitable implementation
  176. mp11::mp_true, no_conversion_tag>;
  177. template <class T, class Dir>
  178. using can_convert = mp11::mp_not<
  179. std::is_same<
  180. detail::conversion_implementation<T, Dir>,
  181. detail::no_conversion_tag>>;
  182. template<class T>
  183. using value_from_implementation
  184. = conversion_implementation<T, value_from_conversion>;
  185. template<class T>
  186. using value_to_implementation
  187. = conversion_implementation<T, value_to_conversion>;
  188. template<class Impl1, class Impl2>
  189. using conversion_round_trips_helper = mp11::mp_or<
  190. std::is_same<Impl1, Impl2>,
  191. std::is_same<user_conversion_tag, Impl1>,
  192. std::is_same<user_conversion_tag, Impl2>>;
  193. template<class T, class Dir>
  194. using conversion_round_trips = conversion_round_trips_helper<
  195. conversion_implementation<T, Dir>,
  196. conversion_implementation<T, mp11::mp_not<Dir>>>;
  197. template< class T1, class T2 >
  198. struct copy_cref_helper
  199. {
  200. using type = remove_cvref<T2>;
  201. };
  202. template< class T1, class T2 >
  203. using copy_cref = typename copy_cref_helper< T1, T2 >::type;
  204. template< class T1, class T2 >
  205. struct copy_cref_helper<T1 const, T2>
  206. {
  207. using type = remove_cvref<T2> const;
  208. };
  209. template< class T1, class T2 >
  210. struct copy_cref_helper<T1&, T2>
  211. {
  212. using type = copy_cref<T1, T2>&;
  213. };
  214. template< class T1, class T2 >
  215. struct copy_cref_helper<T1&&, T2>
  216. {
  217. using type = copy_cref<T1, T2>&&;
  218. };
  219. template< class Rng, class Traits >
  220. using forwarded_value_helper = mp11::mp_if<
  221. std::is_convertible<
  222. typename Traits::reference,
  223. copy_cref<Rng, typename Traits::value_type> >,
  224. copy_cref<Rng, typename Traits::value_type>,
  225. typename Traits::value_type >;
  226. template< class Rng >
  227. using forwarded_value = forwarded_value_helper<
  228. Rng, iterator_traits< Rng > >;
  229. } // namespace detail
  230. template <class T>
  231. struct result_for<T, value>
  232. {
  233. using type = result< detail::remove_cvref<T> >;
  234. };
  235. template<class T>
  236. struct is_string_like
  237. : std::is_convertible<T, string_view>
  238. { };
  239. template<class T>
  240. struct is_sequence_like
  241. : mp11::mp_all<
  242. mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
  243. mp11::mp_valid<detail::begin_iterator_category, T>>
  244. { };
  245. template<class T>
  246. struct is_map_like
  247. : mp11::mp_all<
  248. is_sequence_like<T>,
  249. mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
  250. is_string_like<detail::key_type<T>>,
  251. mp11::mp_valid_and_true<detail::has_unique_keys, T>>
  252. { };
  253. template<class T>
  254. struct is_tuple_like
  255. : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
  256. { };
  257. template<>
  258. struct is_null_like<std::nullptr_t>
  259. : std::true_type
  260. { };
  261. #ifndef BOOST_NO_CXX17_HDR_VARIANT
  262. template<>
  263. struct is_null_like<std::monostate>
  264. : std::true_type
  265. { };
  266. #endif // BOOST_NO_CXX17_HDR_VARIANT
  267. template<class T>
  268. struct is_described_class
  269. : mp11::mp_and<
  270. describe::has_describe_members<T>,
  271. mp11::mp_not< std::is_union<T> >,
  272. mp11::mp_empty<
  273. mp11::mp_eval_or<
  274. mp11::mp_list<>, detail::described_non_public_members, T>>,
  275. mp11::mp_empty<
  276. mp11::mp_eval_or<mp11::mp_list<>, detail::described_bases, T>>>
  277. { };
  278. template<class T>
  279. struct is_described_enum
  280. : describe::has_describe_enumerators<T>
  281. { };
  282. } // namespace json
  283. } // namespace boost
  284. #endif // BOOST_JSON_IMPL_CONVERSION_HPP