File indexing completed on 2025-01-18 09:39:01
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011 #ifndef BOOST_JSON_IMPL_CONVERSION_HPP
0012 #define BOOST_JSON_IMPL_CONVERSION_HPP
0013
0014 #include <boost/json/fwd.hpp>
0015 #include <boost/json/value.hpp>
0016 #include <boost/json/string_view.hpp>
0017 #include <boost/describe/enumerators.hpp>
0018 #include <boost/describe/members.hpp>
0019 #include <boost/describe/bases.hpp>
0020 #include <boost/mp11/algorithm.hpp>
0021 #include <boost/mp11/utility.hpp>
0022
0023 #include <iterator>
0024 #include <tuple>
0025 #include <utility>
0026 #ifndef BOOST_NO_CXX17_HDR_VARIANT
0027 # include <variant>
0028 #endif
0029
0030 namespace boost {
0031 namespace json {
0032 namespace detail {
0033
0034 #ifdef __cpp_lib_nonmember_container_access
0035 using std::size;
0036 #endif
0037
0038 template<std::size_t I, class T>
0039 using tuple_element_t = typename std::tuple_element<I, T>::type;
0040
0041 template<class T>
0042 using iterator_type = decltype(std::begin(std::declval<T&>()));
0043 template<class T>
0044 using iterator_traits = std::iterator_traits< iterator_type<T> >;
0045
0046 template<class T>
0047 using value_type = typename iterator_traits<T>::value_type;
0048 template<class T>
0049 using mapped_type = tuple_element_t< 1, value_type<T> >;
0050
0051
0052
0053 template<class T>
0054 using key_type_helper = tuple_element_t< 0, value_type<T> >;
0055 template<class T>
0056 using key_type = mp11::mp_eval_or<
0057 void,
0058 key_type_helper,
0059 T>;
0060
0061 template<class T>
0062 using are_begin_and_end_same = std::is_same<
0063 iterator_type<T>,
0064 decltype(std::end(std::declval<T&>()))>;
0065
0066 template<class T>
0067 using begin_iterator_category = typename std::iterator_traits<
0068 iterator_type<T>>::iterator_category;
0069
0070 template<class T>
0071 using has_positive_tuple_size = mp11::mp_bool<
0072 (std::tuple_size<T>::value > 0) >;
0073
0074 template<class T>
0075 using has_unique_keys = has_positive_tuple_size<decltype(
0076 std::declval<T&>().emplace(
0077 std::declval<value_type<T>>()))>;
0078
0079 template<class T>
0080 struct is_value_type_pair_helper : std::false_type
0081 { };
0082 template<class T1, class T2>
0083 struct is_value_type_pair_helper<std::pair<T1, T2>> : std::true_type
0084 { };
0085 template<class T>
0086 using is_value_type_pair = is_value_type_pair_helper<value_type<T>>;
0087
0088 template<class T>
0089 using has_size_member_helper
0090 = std::is_convertible<decltype(std::declval<T&>().size()), std::size_t>;
0091 template<class T>
0092 using has_size_member = mp11::mp_valid_and_true<has_size_member_helper, T>;
0093 template<class T>
0094 using has_free_size_helper
0095 = std::is_convertible<
0096 decltype(size(std::declval<T const&>())),
0097 std::size_t>;
0098 template<class T>
0099 using has_free_size = mp11::mp_valid_and_true<has_free_size_helper, T>;
0100 template<class T>
0101 using size_implementation = mp11::mp_cond<
0102 has_size_member<T>, mp11::mp_int<3>,
0103 has_free_size<T>, mp11::mp_int<2>,
0104 std::is_array<T>, mp11::mp_int<1>,
0105 mp11::mp_true, mp11::mp_int<0>>;
0106
0107 template<class T>
0108 std::size_t
0109 try_size(T&& cont, mp11::mp_int<3>)
0110 {
0111 return cont.size();
0112 }
0113
0114 template<class T>
0115 std::size_t
0116 try_size(T& cont, mp11::mp_int<2>)
0117 {
0118 return size(cont);
0119 }
0120
0121 template<class T, std::size_t N>
0122 std::size_t
0123 try_size(T(&)[N], mp11::mp_int<1>)
0124 {
0125 return N;
0126 }
0127
0128 template<class T>
0129 std::size_t
0130 try_size(T&, mp11::mp_int<0>)
0131 {
0132 return 0;
0133 }
0134
0135 template<class T>
0136 using has_push_back_helper
0137 = decltype(std::declval<T&>().push_back(std::declval<value_type<T>>()));
0138 template<class T>
0139 using has_push_back = mp11::mp_valid<has_push_back_helper, T>;
0140 template<class T>
0141 using inserter_implementation = mp11::mp_cond<
0142 is_tuple_like<T>, mp11::mp_int<2>,
0143 has_push_back<T>, mp11::mp_int<1>,
0144 mp11::mp_true, mp11::mp_int<0>>;
0145
0146 template<class T>
0147 iterator_type<T>
0148 inserter(
0149 T& target,
0150 mp11::mp_int<2>)
0151 {
0152 return target.begin();
0153 }
0154
0155 template<class T>
0156 std::back_insert_iterator<T>
0157 inserter(
0158 T& target,
0159 mp11::mp_int<1>)
0160 {
0161 return std::back_inserter(target);
0162 }
0163
0164 template<class T>
0165 std::insert_iterator<T>
0166 inserter(
0167 T& target,
0168 mp11::mp_int<0>)
0169 {
0170 return std::inserter( target, target.end() );
0171 }
0172
0173 using value_from_conversion = mp11::mp_true;
0174 using value_to_conversion = mp11::mp_false;
0175
0176 struct user_conversion_tag { };
0177 struct context_conversion_tag : user_conversion_tag { };
0178 struct full_context_conversion_tag : context_conversion_tag { };
0179 struct native_conversion_tag { };
0180 struct value_conversion_tag : native_conversion_tag { };
0181 struct object_conversion_tag : native_conversion_tag { };
0182 struct array_conversion_tag : native_conversion_tag { };
0183 struct string_conversion_tag : native_conversion_tag { };
0184 struct bool_conversion_tag : native_conversion_tag { };
0185 struct number_conversion_tag : native_conversion_tag { };
0186 struct integral_conversion_tag : number_conversion_tag { };
0187 struct floating_point_conversion_tag : number_conversion_tag { };
0188 struct null_like_conversion_tag { };
0189 struct string_like_conversion_tag { };
0190 struct map_like_conversion_tag { };
0191 struct sequence_conversion_tag { };
0192 struct tuple_conversion_tag { };
0193 struct described_class_conversion_tag { };
0194 struct described_enum_conversion_tag { };
0195 struct variant_conversion_tag { };
0196 struct optional_conversion_tag { };
0197 struct no_conversion_tag { };
0198
0199 template<class... Args>
0200 using supports_tag_invoke = decltype(tag_invoke( std::declval<Args>()... ));
0201
0202 template<class T>
0203 using has_user_conversion_from_impl = supports_tag_invoke<
0204 value_from_tag, value&, T&& >;
0205 template<class T>
0206 using has_user_conversion_to_impl = supports_tag_invoke<
0207 value_to_tag<T>, value const& >;
0208 template<class T>
0209 using has_nonthrowing_user_conversion_to_impl = supports_tag_invoke<
0210 try_value_to_tag<T>, value const& >;
0211 template< class T, class Dir >
0212 using has_user_conversion1 = mp11::mp_if<
0213 std::is_same<Dir, value_from_conversion>,
0214 mp11::mp_valid<has_user_conversion_from_impl, T>,
0215 mp11::mp_or<
0216 mp11::mp_valid<has_user_conversion_to_impl, T>,
0217 mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>>>;
0218
0219 template< class Ctx, class T >
0220 using has_context_conversion_from_impl = supports_tag_invoke<
0221 value_from_tag, value&, T&&, Ctx const& >;
0222 template< class Ctx, class T >
0223 using has_context_conversion_to_impl = supports_tag_invoke<
0224 value_to_tag<T>, value const&, Ctx const& >;
0225 template< class Ctx, class T >
0226 using has_nonthrowing_context_conversion_to_impl = supports_tag_invoke<
0227 try_value_to_tag<T>, value const&, Ctx const& >;
0228 template< class Ctx, class T, class Dir >
0229 using has_user_conversion2 = mp11::mp_if<
0230 std::is_same<Dir, value_from_conversion>,
0231 mp11::mp_valid<has_context_conversion_from_impl, Ctx, T>,
0232 mp11::mp_or<
0233 mp11::mp_valid<has_context_conversion_to_impl, Ctx, T>,
0234 mp11::mp_valid<has_nonthrowing_context_conversion_to_impl, Ctx, T>>>;
0235
0236 template< class Ctx, class T >
0237 using has_full_context_conversion_from_impl = supports_tag_invoke<
0238 value_from_tag, value&, T&&, Ctx const&, Ctx const& >;
0239 template< class Ctx, class T >
0240 using has_full_context_conversion_to_impl = supports_tag_invoke<
0241 value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
0242 template< class Ctx, class T >
0243 using has_nonthrowing_full_context_conversion_to_impl = supports_tag_invoke<
0244 try_value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
0245 template< class Ctx, class T, class Dir >
0246 using has_user_conversion3 = mp11::mp_if<
0247 std::is_same<Dir, value_from_conversion>,
0248 mp11::mp_valid<has_full_context_conversion_from_impl, Ctx, T>,
0249 mp11::mp_or<
0250 mp11::mp_valid<has_full_context_conversion_to_impl, Ctx, T>,
0251 mp11::mp_valid<
0252 has_nonthrowing_full_context_conversion_to_impl, Ctx, T>>>;
0253
0254 template< class T >
0255 using described_non_public_members = describe::describe_members<
0256 T, describe::mod_private | describe::mod_protected>;
0257 template< class T >
0258 using described_bases = describe::describe_bases<
0259 T, describe::mod_any_access>;
0260
0261 #if defined(BOOST_MSVC) && BOOST_MSVC < 1920
0262
0263 template< class T >
0264 struct described_member_t_impl;
0265
0266 template< class T, class C >
0267 struct described_member_t_impl<T C::*>
0268 {
0269 using type = T;
0270 };
0271
0272 template< class T, class D >
0273 using described_member_t = remove_cvref<
0274 typename described_member_t_impl<
0275 remove_cvref<decltype(D::pointer)> >::type>;
0276
0277 #else
0278
0279 template< class T, class D >
0280 using described_member_t = remove_cvref<decltype(
0281 std::declval<T&>().* D::pointer )>;
0282
0283 #endif
0284
0285
0286 template< class Ctx, class T, class Dir >
0287 using user_conversion_category = mp11::mp_cond<
0288 has_user_conversion3<Ctx, T, Dir>, full_context_conversion_tag,
0289 has_user_conversion2<Ctx, T, Dir>, context_conversion_tag,
0290 has_user_conversion1<T, Dir>, user_conversion_tag>;
0291
0292
0293 template< class T >
0294 using native_conversion_category = mp11::mp_cond<
0295 std::is_same<T, value>, value_conversion_tag,
0296 std::is_same<T, array>, array_conversion_tag,
0297 std::is_same<T, object>, object_conversion_tag,
0298 std::is_same<T, string>, string_conversion_tag>;
0299
0300
0301 template< class T >
0302 using generic_conversion_category = mp11::mp_cond<
0303 std::is_same<T, bool>, bool_conversion_tag,
0304 std::is_integral<T>, integral_conversion_tag,
0305 std::is_floating_point<T>, floating_point_conversion_tag,
0306 is_null_like<T>, null_like_conversion_tag,
0307 is_string_like<T>, string_like_conversion_tag,
0308 is_map_like<T>, map_like_conversion_tag,
0309 is_sequence_like<T>, sequence_conversion_tag,
0310 is_tuple_like<T>, tuple_conversion_tag,
0311 is_described_class<T>, described_class_conversion_tag,
0312 is_described_enum<T>, described_enum_conversion_tag,
0313 is_variant_like<T>, variant_conversion_tag,
0314 is_optional_like<T>, optional_conversion_tag,
0315
0316 mp11::mp_true, no_conversion_tag>;
0317
0318 template< class T >
0319 using nested_type = typename T::type;
0320 template< class T1, class T2 >
0321 using conversion_category_impl_helper = mp11::mp_eval_if_not<
0322 std::is_same<detail::no_conversion_tag, T1>,
0323 T1,
0324 mp11::mp_eval_or_q, T1, mp11::mp_quote<nested_type>, T2>;
0325 template< class Ctx, class T, class Dir >
0326 struct conversion_category_impl
0327 {
0328 using type = mp11::mp_fold<
0329 mp11::mp_list<
0330 mp11::mp_defer<user_conversion_category, Ctx, T, Dir>,
0331 mp11::mp_defer<native_conversion_category, T>,
0332 mp11::mp_defer<generic_conversion_category, T>>,
0333 no_conversion_tag,
0334 conversion_category_impl_helper>;
0335 };
0336 template< class Ctx, class T, class Dir >
0337 using conversion_category =
0338 typename conversion_category_impl< Ctx, T, Dir >::type;
0339
0340 template< class T >
0341 using any_conversion_tag = mp11::mp_not<
0342 std::is_same< T, no_conversion_tag > >;
0343
0344 template< class T, class Dir, class... Ctxs >
0345 struct conversion_category_impl< std::tuple<Ctxs...>, T, Dir >
0346 {
0347 using ctxs = mp11::mp_list< remove_cvref<Ctxs>... >;
0348 using cats = mp11::mp_list<
0349 conversion_category<remove_cvref<Ctxs>, T, Dir>... >;
0350
0351 template< class I >
0352 using exists = mp11::mp_less< I, mp11::mp_size<cats> >;
0353
0354 using context2 = mp11::mp_find< cats, full_context_conversion_tag >;
0355 using context1 = mp11::mp_find< cats, context_conversion_tag >;
0356 using context0 = mp11::mp_find< cats, user_conversion_tag >;
0357 using index = mp11::mp_cond<
0358 exists<context2>, context2,
0359 exists<context1>, context1,
0360 exists<context0>, context0,
0361 mp11::mp_true, mp11::mp_find_if< cats, any_conversion_tag > >;
0362 using type = mp11::mp_eval_or<
0363 no_conversion_tag,
0364 mp11::mp_at, cats, index >;
0365 };
0366
0367 struct no_context
0368 {};
0369
0370 struct allow_exceptions
0371 {};
0372
0373 template <class T, class Dir>
0374 using can_convert = mp11::mp_not<
0375 std::is_same<
0376 detail::conversion_category<no_context, T, Dir>,
0377 detail::no_conversion_tag>>;
0378
0379 template<class Impl1, class Impl2>
0380 using conversion_round_trips_helper = mp11::mp_or<
0381 std::is_same<Impl1, Impl2>,
0382 std::is_base_of<user_conversion_tag, Impl1>,
0383 std::is_base_of<user_conversion_tag, Impl2>>;
0384 template< class Ctx, class T, class Dir >
0385 using conversion_round_trips = conversion_round_trips_helper<
0386 conversion_category<Ctx, T, Dir>,
0387 conversion_category<Ctx, T, mp11::mp_not<Dir>>>;
0388
0389 template< class T1, class T2 >
0390 struct copy_cref_helper
0391 {
0392 using type = remove_cvref<T2>;
0393 };
0394 template< class T1, class T2 >
0395 using copy_cref = typename copy_cref_helper< T1, T2 >::type;
0396
0397 template< class T1, class T2 >
0398 struct copy_cref_helper<T1 const, T2>
0399 {
0400 using type = remove_cvref<T2> const;
0401 };
0402 template< class T1, class T2 >
0403 struct copy_cref_helper<T1&, T2>
0404 {
0405 using type = copy_cref<T1, T2>&;
0406 };
0407 template< class T1, class T2 >
0408 struct copy_cref_helper<T1&&, T2>
0409 {
0410 using type = copy_cref<T1, T2>&&;
0411 };
0412
0413 template< class Rng, class Traits >
0414 using forwarded_value_helper = mp11::mp_if<
0415 std::is_convertible<
0416 typename Traits::reference,
0417 copy_cref<Rng, typename Traits::value_type> >,
0418 copy_cref<Rng, typename Traits::value_type>,
0419 typename Traits::value_type >;
0420
0421 template< class Rng >
0422 using forwarded_value = forwarded_value_helper<
0423 Rng, iterator_traits< Rng > >;
0424
0425 template< class Ctx, class T, class Dir >
0426 struct supported_context
0427 {
0428 using type = Ctx;
0429
0430 static
0431 type const&
0432 get( Ctx const& ctx ) noexcept
0433 {
0434 return ctx;
0435 }
0436 };
0437
0438 template< class T, class Dir, class... Ctxs >
0439 struct supported_context< std::tuple<Ctxs...>, T, Dir >
0440 {
0441 using Ctx = std::tuple<Ctxs...>;
0442 using impl = conversion_category_impl<Ctx, T, Dir>;
0443 using index = typename impl::index;
0444 using next_supported = supported_context<
0445 mp11::mp_at< typename impl::ctxs, index >, T, Dir >;
0446 using type = typename next_supported::type;
0447
0448 static
0449 type const&
0450 get( Ctx const& ctx ) noexcept
0451 {
0452 return next_supported::get( std::get<index::value>( ctx ) );
0453 }
0454 };
0455
0456 template< class T >
0457 using value_result_type = typename std::decay<
0458 decltype( std::declval<T&>().value() )>::type;
0459
0460 template< class T >
0461 using can_reset = decltype( std::declval<T&>().reset() );
0462
0463 template< class T >
0464 using has_valueless_by_exception =
0465 decltype( std::declval<T const&>().valueless_by_exception() );
0466
0467 }
0468
0469 template <class T>
0470 struct result_for<T, value>
0471 {
0472 using type = result< detail::remove_cvref<T> >;
0473 };
0474
0475 template<class T>
0476 struct is_string_like
0477 : std::is_convertible<T, string_view>
0478 { };
0479
0480 template<class T>
0481 struct is_sequence_like
0482 : mp11::mp_all<
0483 mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
0484 mp11::mp_valid<detail::begin_iterator_category, T>>
0485 { };
0486
0487 template<class T>
0488 struct is_map_like
0489 : mp11::mp_all<
0490 is_sequence_like<T>,
0491 mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
0492 is_string_like<detail::key_type<T>>,
0493 mp11::mp_valid_and_true<detail::has_unique_keys, T>>
0494 { };
0495
0496 template<class T>
0497 struct is_tuple_like
0498 : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
0499 { };
0500
0501 template<>
0502 struct is_null_like<std::nullptr_t>
0503 : std::true_type
0504 { };
0505
0506 #ifndef BOOST_NO_CXX17_HDR_VARIANT
0507 template<>
0508 struct is_null_like<std::monostate>
0509 : std::true_type
0510 { };
0511 #endif
0512
0513 template<class T>
0514 struct is_described_class
0515 : mp11::mp_and<
0516 describe::has_describe_members<T>,
0517 mp11::mp_not< std::is_union<T> >,
0518 mp11::mp_empty<
0519 mp11::mp_eval_or<
0520 mp11::mp_list<>, detail::described_non_public_members, T>>,
0521 mp11::mp_empty<
0522 mp11::mp_eval_or<mp11::mp_list<>, detail::described_bases, T>>>
0523 { };
0524
0525 template<class T>
0526 struct is_described_enum
0527 : describe::has_describe_enumerators<T>
0528 { };
0529
0530 template<class T>
0531 struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T>
0532 { };
0533
0534 template<class T>
0535 struct is_optional_like
0536 : mp11::mp_and<
0537 mp11::mp_not<std::is_void<
0538 mp11::mp_eval_or<void, detail::value_result_type, T>>>,
0539 mp11::mp_valid<detail::can_reset, T>>
0540 { };
0541
0542 }
0543 }
0544
0545 #endif