Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-09-18 08:47:34

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, 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 // user conversion (via tag_invoke)
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 // native conversions (constructors and member functions of value)
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 // generic conversions
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     // failed to find a suitable implementation
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 template <class T, class Dir>
0391 using can_convert = mp11::mp_not<
0392     std::is_same<
0393         detail::conversion_category<no_context, T, Dir>,
0394         detail::no_conversion_tag>>;
0395 
0396 template<class Impl1, class Impl2>
0397 using conversion_round_trips_helper = mp11::mp_or<
0398     std::is_same<Impl1, Impl2>,
0399     std::is_base_of<user_conversion_tag, Impl1>,
0400     std::is_base_of<user_conversion_tag, Impl2>>;
0401 template< class Ctx, class T, class Dir >
0402 using conversion_round_trips  = conversion_round_trips_helper<
0403     conversion_category<Ctx, T, Dir>,
0404     conversion_category<Ctx, T, mp11::mp_not<Dir>>>;
0405 
0406 template< class T1, class T2 >
0407 struct copy_cref_helper
0408 {
0409     using type = remove_cvref<T2>;
0410 };
0411 template< class T1, class T2 >
0412 using copy_cref = typename copy_cref_helper< T1, T2 >::type;
0413 
0414 template< class T1, class T2 >
0415 struct copy_cref_helper<T1 const, T2>
0416 {
0417     using type = remove_cvref<T2> const;
0418 };
0419 template< class T1, class T2 >
0420 struct copy_cref_helper<T1&, T2>
0421 {
0422     using type = copy_cref<T1, T2>&;
0423 };
0424 template< class T1, class T2 >
0425 struct copy_cref_helper<T1&&, T2>
0426 {
0427     using type = copy_cref<T1, T2>&&;
0428 };
0429 
0430 template< class Rng, class Traits >
0431 using forwarded_value_helper = mp11::mp_if<
0432     std::is_convertible<
0433         typename Traits::reference,
0434         copy_cref<Rng, typename Traits::value_type> >,
0435     copy_cref<Rng, typename Traits::value_type>,
0436     typename Traits::value_type >;
0437 
0438 template< class Rng >
0439 using forwarded_value = forwarded_value_helper<
0440     Rng, iterator_traits< Rng > >;
0441 
0442 template< class Ctx, class T, class Dir >
0443 struct supported_context
0444 {
0445     using type = Ctx;
0446 
0447     static
0448     type const&
0449     get( Ctx const& ctx ) noexcept
0450     {
0451         return ctx;
0452     }
0453 };
0454 
0455 template< class T, class Dir, class... Ctxs >
0456 struct supported_context< std::tuple<Ctxs...>, T, Dir >
0457 {
0458     using Ctx = std::tuple<Ctxs...>;
0459     using impl = conversion_category_impl<Ctx, T, Dir>;
0460     using index = typename impl::index;
0461     using next_supported = supported_context<
0462         mp11::mp_at< typename impl::ctxs, index >, T, Dir >;
0463     using type = typename next_supported::type;
0464 
0465     static
0466     type const&
0467     get( Ctx const& ctx ) noexcept
0468     {
0469         return next_supported::get( std::get<index::value>( ctx ) );
0470     }
0471 };
0472 
0473 template< class T >
0474 using value_result_type = typename std::decay<
0475     decltype( std::declval<T&>().value() )>::type;
0476 
0477 template< class T >
0478 using can_reset = decltype( std::declval<T&>().reset() );
0479 
0480 template< class T >
0481 using has_valueless_by_exception =
0482     decltype( std::declval<T const&>().valueless_by_exception() );
0483 
0484 } // namespace detail
0485 
0486 template <class T>
0487 struct result_for<T, value>
0488 {
0489     using type = system::result< detail::remove_cvref<T> >;
0490 };
0491 
0492 template<class T>
0493 struct is_string_like
0494     : std::is_convertible<T, string_view>
0495 { };
0496 
0497 template<class T>
0498 struct is_path_like
0499     : mp11::mp_all<
0500         mp11::mp_valid_and_true<detail::is_its_own_value, T>,
0501         mp11::mp_valid_and_true<detail::has_string_type, T>>
0502 { };
0503 template<class T>
0504 struct is_sequence_like
0505     : mp11::mp_all<
0506         mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
0507         mp11::mp_valid_and_true<detail::not_its_own_value, T>,
0508         mp11::mp_valid<detail::begin_iterator_category, T>>
0509 { };
0510 
0511 template<class T>
0512 struct is_map_like
0513     : mp11::mp_all<
0514         is_sequence_like<T>,
0515         mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
0516         is_string_like<detail::key_type<T>>,
0517         mp11::mp_valid_and_true<detail::has_unique_keys, T>>
0518 { };
0519 
0520 template<class T>
0521 struct is_tuple_like
0522     : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
0523 { };
0524 
0525 template<>
0526 struct is_null_like<std::nullptr_t>
0527     : std::true_type
0528 { };
0529 
0530 #ifndef BOOST_NO_CXX17_HDR_VARIANT
0531 template<>
0532 struct is_null_like<std::monostate>
0533     : std::true_type
0534 { };
0535 #endif // BOOST_NO_CXX17_HDR_VARIANT
0536 
0537 template<class T>
0538 struct is_described_class
0539     : mp11::mp_and<
0540         describe::has_describe_members<T>,
0541         mp11::mp_not< std::is_union<T> >,
0542         mp11::mp_empty<
0543             mp11::mp_eval_or<
0544                 mp11::mp_list<>, detail::described_non_public_members, T>>,
0545         mp11::mp_empty<
0546             mp11::mp_eval_or<mp11::mp_list<>, detail::described_bases, T>>>
0547 { };
0548 
0549 template<class T>
0550 struct is_described_enum
0551     : describe::has_describe_enumerators<T>
0552 { };
0553 
0554 template<class T>
0555 struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T>
0556 { };
0557 
0558 template<class T>
0559 struct is_optional_like
0560     : mp11::mp_and<
0561         mp11::mp_not<std::is_void<
0562             mp11::mp_eval_or<void, detail::value_result_type, T>>>,
0563         mp11::mp_valid<detail::can_reset, T>>
0564 { };
0565 
0566 } // namespace json
0567 } // namespace boost
0568 
0569 #endif // BOOST_JSON_IMPL_CONVERSION_HPP