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