Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-08-18 08:54:41

0001 //
0002 // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
0003 // Copyright (c) 2022 Dmitry Arkhipov (grisumbras@yandex.ru)
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // Official repository: https://github.com/boostorg/json
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 // BOOST_NO_CXX17_HDR_VARIANT
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 // had to make the metafunction always succeeding in order to make it work
0053 // with msvc 14.0
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 // msvc 14.0 gets confused when std::is_same is used directly
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,
0272     describe::mod_private
0273         | describe::mod_protected
0274         | boost::describe::mod_inherited>;
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 #ifdef BOOST_DESCRIBE_CXX14
0305 
0306 constexpr
0307 bool
0308 compare_strings(char const* l, char const* r)
0309 {
0310 #if defined(_MSC_VER) && (_MSC_VER <= 1900) && !defined(__clang__)
0311     return *l == *r && ( (*l == 0) | compare_strings(l + 1, r + 1) );
0312 #else
0313     do
0314     {
0315         if( *l != *r )
0316             return false;
0317         if( *l == 0 )
0318             return true;
0319         ++l;
0320         ++r;
0321     } while(true);
0322 #endif
0323 }
0324 
0325 template< class L, class R >
0326 struct equal_member_names
0327     : mp11::mp_bool< compare_strings(L::name, R::name) >
0328 {};
0329 
0330 template< class T >
0331 using uniquely_named_members = mp11::mp_same<
0332     mp11::mp_unique_if< described_members<T>, equal_member_names >,
0333     described_members<T> >;
0334 
0335 #else
0336 
0337 // we only check this in C++14, but the template should exist nevertheless
0338 template< class T >
0339 using uniquely_named_members = std::true_type;
0340 
0341 #endif // BOOST_DESCRIBE_CXX14
0342 
0343 // user conversion (via tag_invoke)
0344 template< class Ctx, class T, class Dir >
0345 using user_conversion_category = mp11::mp_cond<
0346     has_user_conversion3<Ctx, T, Dir>, full_context_conversion_tag,
0347     has_user_conversion2<Ctx, T, Dir>, context_conversion_tag,
0348     has_user_conversion1<T, Dir>,      user_conversion_tag>;
0349 
0350 // native conversions (constructors and member functions of value)
0351 template< class T >
0352 using native_conversion_category = mp11::mp_cond<
0353     std::is_same<T, value>,  value_conversion_tag,
0354     std::is_same<T, array>,  array_conversion_tag,
0355     std::is_same<T, object>, object_conversion_tag,
0356     std::is_same<T, string>, string_conversion_tag>;
0357 
0358 // generic conversions
0359 template< class T >
0360 using generic_conversion_category = mp11::mp_cond<
0361     std::is_same<T, bool>,     bool_conversion_tag,
0362     std::is_integral<T>,       integral_conversion_tag,
0363     std::is_floating_point<T>, floating_point_conversion_tag,
0364     is_null_like<T>,           null_like_conversion_tag,
0365     is_string_like<T>,         string_like_conversion_tag,
0366     is_variant_like<T>,        variant_conversion_tag,
0367     is_optional_like<T>,       optional_conversion_tag,
0368     is_map_like<T>,            map_like_conversion_tag,
0369     is_sequence_like<T>,       sequence_conversion_tag,
0370     is_tuple_like<T>,          tuple_conversion_tag,
0371     is_described_class<T>,     described_class_conversion_tag,
0372     is_described_enum<T>,      described_enum_conversion_tag,
0373     is_path_like<T>,           path_conversion_tag,
0374     // failed to find a suitable implementation
0375     mp11::mp_true,             no_conversion_tag>;
0376 
0377 template< class T >
0378 using nested_type = typename T::type;
0379 template< class T1, class T2 >
0380 using conversion_category_impl_helper = mp11::mp_eval_if_not<
0381     std::is_same<detail::no_conversion_tag, T1>,
0382     T1,
0383     mp11::mp_eval_or_q, T1, mp11::mp_quote<nested_type>, T2>;
0384 template< class Ctx, class T, class Dir >
0385 struct conversion_category_impl
0386 {
0387     using type = mp11::mp_fold<
0388         mp11::mp_list<
0389             mp11::mp_defer<user_conversion_category, Ctx, T, Dir>,
0390             mp11::mp_defer<native_conversion_category, T>,
0391             mp11::mp_defer<generic_conversion_category, T>>,
0392         no_conversion_tag,
0393         conversion_category_impl_helper>;
0394 };
0395 template< class Ctx, class T, class Dir >
0396 using conversion_category =
0397     typename conversion_category_impl< Ctx, T, Dir >::type;
0398 
0399 template< class T >
0400 using any_conversion_tag = mp11::mp_not<
0401     std::is_same< T, no_conversion_tag > >;
0402 
0403 template< class T, class Dir, class... Ctxs >
0404 struct conversion_category_impl< std::tuple<Ctxs...>, T, Dir >
0405 {
0406     using ctxs = mp11::mp_list< remove_cvref<Ctxs>... >;
0407     using cats = mp11::mp_list<
0408         conversion_category<remove_cvref<Ctxs>, T, Dir>... >;
0409 
0410     template< class I >
0411     using exists = mp11::mp_less< I, mp11::mp_size<cats> >;
0412 
0413     using context2 = mp11::mp_find< cats, full_context_conversion_tag >;
0414     using context1 = mp11::mp_find< cats, context_conversion_tag >;
0415     using context0 = mp11::mp_find< cats, user_conversion_tag >;
0416     using index = mp11::mp_cond<
0417         exists<context2>, context2,
0418         exists<context1>, context1,
0419         exists<context0>, context0,
0420         mp11::mp_true, mp11::mp_find_if< cats, any_conversion_tag > >;
0421     using type = mp11::mp_eval_or<
0422         no_conversion_tag,
0423         mp11::mp_at, cats, index >;
0424 };
0425 
0426 struct no_context
0427 {};
0428 
0429 template <class T, class Dir>
0430 using can_convert = mp11::mp_not<
0431     std::is_same<
0432         detail::conversion_category<no_context, T, Dir>,
0433         detail::no_conversion_tag>>;
0434 
0435 template<class Impl1, class Impl2>
0436 using conversion_round_trips_helper = mp11::mp_or<
0437     std::is_same<Impl1, Impl2>,
0438     std::is_base_of<user_conversion_tag, Impl1>,
0439     std::is_base_of<user_conversion_tag, Impl2>>;
0440 template< class Ctx, class T, class Dir >
0441 using conversion_round_trips  = conversion_round_trips_helper<
0442     conversion_category<Ctx, T, Dir>,
0443     conversion_category<Ctx, T, mp11::mp_not<Dir>>>;
0444 
0445 template< class T1, class T2 >
0446 struct copy_cref_helper
0447 {
0448     using type = remove_cvref<T2>;
0449 };
0450 template< class T1, class T2 >
0451 using copy_cref = typename copy_cref_helper< T1, T2 >::type;
0452 
0453 template< class T1, class T2 >
0454 struct copy_cref_helper<T1 const, T2>
0455 {
0456     using type = remove_cvref<T2> const;
0457 };
0458 template< class T1, class T2 >
0459 struct copy_cref_helper<T1&, T2>
0460 {
0461     using type = copy_cref<T1, T2>&;
0462 };
0463 template< class T1, class T2 >
0464 struct copy_cref_helper<T1&&, T2>
0465 {
0466     using type = copy_cref<T1, T2>&&;
0467 };
0468 
0469 template< class Rng, class Traits >
0470 using forwarded_value_helper = mp11::mp_if<
0471     std::is_convertible<
0472         typename Traits::reference,
0473         copy_cref<Rng, typename Traits::value_type> >,
0474     copy_cref<Rng, typename Traits::value_type>,
0475     typename Traits::value_type >;
0476 
0477 template< class Rng >
0478 using forwarded_value = forwarded_value_helper<
0479     Rng, iterator_traits< Rng > >;
0480 
0481 template< class Ctx, class T, class Dir >
0482 struct supported_context
0483 {
0484     using type = Ctx;
0485 
0486     static
0487     type const&
0488     get( Ctx const& ctx ) noexcept
0489     {
0490         return ctx;
0491     }
0492 };
0493 
0494 template< class T, class Dir, class... Ctxs >
0495 struct supported_context< std::tuple<Ctxs...>, T, Dir >
0496 {
0497     using Ctx = std::tuple<Ctxs...>;
0498     using impl = conversion_category_impl<Ctx, T, Dir>;
0499     using index = typename impl::index;
0500     using next_supported = supported_context<
0501         mp11::mp_at< typename impl::ctxs, index >, T, Dir >;
0502     using type = typename next_supported::type;
0503 
0504     static
0505     type const&
0506     get( Ctx const& ctx ) noexcept
0507     {
0508         return next_supported::get( std::get<index::value>( ctx ) );
0509     }
0510 };
0511 
0512 template< class T >
0513 using value_result_type = typename std::decay<
0514     decltype( std::declval<T&>().value() )>::type;
0515 
0516 template< class T >
0517 using can_reset = decltype( std::declval<T&>().reset() );
0518 
0519 template< class T >
0520 using has_valueless_by_exception =
0521     decltype( std::declval<T const&>().valueless_by_exception() );
0522 
0523 } // namespace detail
0524 
0525 template <class T>
0526 struct result_for<T, value>
0527 {
0528     using type = system::result< detail::remove_cvref<T> >;
0529 };
0530 
0531 template<class T>
0532 struct is_string_like
0533     : std::is_convertible<T, string_view>
0534 { };
0535 
0536 template<class T>
0537 struct is_path_like
0538     : mp11::mp_all<
0539         mp11::mp_valid_and_true<detail::is_its_own_value, T>,
0540         mp11::mp_valid_and_true<detail::has_string_type, T>>
0541 { };
0542 template<class T>
0543 struct is_sequence_like
0544     : mp11::mp_all<
0545         mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
0546         mp11::mp_valid_and_true<detail::not_its_own_value, T>,
0547         mp11::mp_valid<detail::begin_iterator_category, T>>
0548 { };
0549 
0550 template<class T>
0551 struct is_map_like
0552     : mp11::mp_all<
0553         is_sequence_like<T>,
0554         mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
0555         is_string_like<detail::key_type<T>>,
0556         mp11::mp_valid_and_true<detail::has_unique_keys, T>>
0557 { };
0558 
0559 template<class T>
0560 struct is_tuple_like
0561     : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
0562 { };
0563 
0564 template<>
0565 struct is_null_like<std::nullptr_t>
0566     : std::true_type
0567 { };
0568 
0569 #ifndef BOOST_NO_CXX17_HDR_VARIANT
0570 template<>
0571 struct is_null_like<std::monostate>
0572     : std::true_type
0573 { };
0574 #endif // BOOST_NO_CXX17_HDR_VARIANT
0575 
0576 template<class T>
0577 struct is_described_class
0578     : mp11::mp_and<
0579         describe::has_describe_members<T>,
0580         mp11::mp_not< std::is_union<T> >,
0581         mp11::mp_empty<
0582             mp11::mp_eval_or<
0583                 mp11::mp_list<>, detail::described_non_public_members, T>>>
0584 { };
0585 
0586 template<class T>
0587 struct is_described_enum
0588     : describe::has_describe_enumerators<T>
0589 { };
0590 
0591 template<class T>
0592 struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T>
0593 { };
0594 
0595 template<class T>
0596 struct is_optional_like
0597     : mp11::mp_and<
0598         mp11::mp_not<std::is_void<
0599             mp11::mp_eval_or<void, detail::value_result_type, T>>>,
0600         mp11::mp_valid<detail::can_reset, T>>
0601 { };
0602 
0603 } // namespace json
0604 } // namespace boost
0605 
0606 #endif // BOOST_JSON_IMPL_CONVERSION_HPP