TLA Line data Source code
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 :
11 : #ifndef BOOST_JSON_IMPL_CONVERSION_HPP
12 : #define BOOST_JSON_IMPL_CONVERSION_HPP
13 :
14 : #include <boost/json/fwd.hpp>
15 : #include <boost/json/string_view.hpp>
16 : #include <boost/describe/enumerators.hpp>
17 : #include <boost/describe/members.hpp>
18 : #include <boost/describe/bases.hpp>
19 : #include <boost/mp11/algorithm.hpp>
20 : #include <boost/mp11/utility.hpp>
21 : #include <boost/system/result.hpp>
22 :
23 : #include <iterator>
24 : #include <tuple>
25 : #include <utility>
26 : #ifndef BOOST_NO_CXX17_HDR_VARIANT
27 : # include <variant>
28 : #endif // BOOST_NO_CXX17_HDR_VARIANT
29 :
30 : namespace boost {
31 : namespace json {
32 :
33 : class value_ref;
34 :
35 : namespace detail {
36 :
37 : #ifdef __cpp_lib_nonmember_container_access
38 : using std::size;
39 : #endif
40 :
41 : template<std::size_t I, class T>
42 : using tuple_element_t = typename std::tuple_element<I, T>::type;
43 :
44 : template<class T>
45 : using iterator_type = decltype(std::begin(std::declval<T&>()));
46 : template<class T>
47 : using iterator_traits = std::iterator_traits< iterator_type<T> >;
48 :
49 : template<class T>
50 : using value_type = typename iterator_traits<T>::value_type;
51 : template<class T>
52 : using mapped_type = tuple_element_t< 1, value_type<T> >;
53 :
54 : // had to make the metafunction always succeeding in order to make it work
55 : // with msvc 14.0
56 : template<class T>
57 : using key_type_helper = tuple_element_t< 0, value_type<T> >;
58 : template<class T>
59 : using key_type = mp11::mp_eval_or<
60 : void,
61 : key_type_helper,
62 : T>;
63 :
64 : template<class T>
65 : using are_begin_and_end_same = std::is_same<
66 : iterator_type<T>,
67 : decltype(std::end(std::declval<T&>()))>;
68 :
69 : // msvc 14.0 gets confused when std::is_same is used directly
70 : template<class A, class B>
71 : using is_same_msvc_140 = std::is_same<A, B>;
72 : template<class T>
73 : using is_its_own_value = is_same_msvc_140<value_type<T>, T>;
74 :
75 : template<class T>
76 : using not_its_own_value = mp11::mp_not< is_its_own_value<T> >;
77 :
78 : template<class T>
79 : using begin_iterator_category = typename std::iterator_traits<
80 : iterator_type<T>>::iterator_category;
81 :
82 : template<class T>
83 : using has_positive_tuple_size = mp11::mp_bool<
84 : (std::tuple_size<T>::value > 0) >;
85 :
86 : template<class T>
87 : using has_unique_keys = has_positive_tuple_size<decltype(
88 : std::declval<T&>().emplace(
89 : std::declval<value_type<T>>()))>;
90 :
91 : template<class T>
92 : using has_string_type = std::is_same<
93 : typename T::string_type, std::basic_string<typename T::value_type> >;
94 :
95 : template<class T>
96 : struct is_value_type_pair_helper : std::false_type
97 : { };
98 : template<class T1, class T2>
99 : struct is_value_type_pair_helper<std::pair<T1, T2>> : std::true_type
100 : { };
101 : template<class T>
102 : using is_value_type_pair = is_value_type_pair_helper<value_type<T>>;
103 :
104 : template<class T>
105 : using has_size_member_helper
106 : = std::is_convertible<decltype(std::declval<T&>().size()), std::size_t>;
107 : template<class T>
108 : using has_size_member = mp11::mp_valid_and_true<has_size_member_helper, T>;
109 : template<class T>
110 : using has_free_size_helper
111 : = std::is_convertible<
112 : decltype(size(std::declval<T const&>())),
113 : std::size_t>;
114 : template<class T>
115 : using has_free_size = mp11::mp_valid_and_true<has_free_size_helper, T>;
116 : template<class T>
117 : using size_implementation = mp11::mp_cond<
118 : has_size_member<T>, mp11::mp_int<3>,
119 : has_free_size<T>, mp11::mp_int<2>,
120 : std::is_array<T>, mp11::mp_int<1>,
121 : mp11::mp_true, mp11::mp_int<0>>;
122 :
123 : template<class T>
124 : std::size_t
125 HIT 141 : try_size(T&& cont, mp11::mp_int<3>)
126 : {
127 141 : return cont.size();
128 : }
129 :
130 : template<class T>
131 : std::size_t
132 1 : try_size(T& cont, mp11::mp_int<2>)
133 : {
134 1 : return size(cont);
135 : }
136 :
137 : template<class T, std::size_t N>
138 : std::size_t
139 1 : try_size(T(&)[N], mp11::mp_int<1>)
140 : {
141 1 : return N;
142 : }
143 :
144 : template<class T>
145 : std::size_t
146 7 : try_size(T&, mp11::mp_int<0>)
147 : {
148 7 : return 0;
149 : }
150 :
151 : template<class T>
152 : using has_push_back_helper
153 : = decltype(std::declval<T&>().push_back(std::declval<value_type<T>>()));
154 : template<class T>
155 : using has_push_back = mp11::mp_valid<has_push_back_helper, T>;
156 : template<class T>
157 : using inserter_implementation = mp11::mp_cond<
158 : is_tuple_like<T>, mp11::mp_int<2>,
159 : has_push_back<T>, mp11::mp_int<1>,
160 : mp11::mp_true, mp11::mp_int<0>>;
161 :
162 : template<class T>
163 : iterator_type<T>
164 56 : inserter(
165 : T& target,
166 : mp11::mp_int<2>)
167 : {
168 56 : return target.begin();
169 : }
170 :
171 : template<class T>
172 : std::back_insert_iterator<T>
173 569 : inserter(
174 : T& target,
175 : mp11::mp_int<1>)
176 : {
177 569 : return std::back_inserter(target);
178 : }
179 :
180 : template<class T>
181 : std::insert_iterator<T>
182 62 : inserter(
183 : T& target,
184 : mp11::mp_int<0>)
185 : {
186 62 : return std::inserter( target, target.end() );
187 : }
188 :
189 : using value_from_conversion = mp11::mp_true;
190 : using value_to_conversion = mp11::mp_false;
191 :
192 : using user_category = std::integral_constant<
193 : conversion_category, conversion_category::user>;
194 : using user_context_category = std::integral_constant<
195 : conversion_category, conversion_category::user_context>;
196 : using user_full_context_category = std::integral_constant<
197 : conversion_category, conversion_category::user_full_context>;
198 : using json_value_category = std::integral_constant<
199 : conversion_category, conversion_category::json_value>;
200 : using json_object_category = std::integral_constant<
201 : conversion_category, conversion_category::json_object>;
202 : using json_array_category = std::integral_constant<
203 : conversion_category, conversion_category::json_array>;
204 : using json_string_category = std::integral_constant<
205 : conversion_category, conversion_category::json_string>;
206 : using json_value_ref_category = std::integral_constant<
207 : conversion_category, conversion_category::json_value_ref>;
208 :
209 : template< class Cat >
210 : using is_user_conversion = mp11::mp_bool<
211 : Cat::value == conversion_category::user
212 : || Cat::value == conversion_category::user_context
213 : || Cat::value == conversion_category::user_full_context>;
214 :
215 : template< class Cat >
216 : using is_native_conversion = mp11::mp_bool<
217 : Cat::value == conversion_category::user
218 : || Cat::value == conversion_category::json_value
219 : || Cat::value == conversion_category::json_object
220 : || Cat::value == conversion_category::json_array
221 : || Cat::value == conversion_category::json_string
222 : || Cat::value == conversion_category::json_value_ref
223 : || Cat::value == conversion_category::boolean
224 : || Cat::value == conversion_category::integer
225 : || Cat::value == conversion_category::floating_point>;
226 :
227 : template<class... Args>
228 : using supports_tag_invoke = decltype(tag_invoke( std::declval<Args>()... ));
229 :
230 : template<class T>
231 : using has_user_conversion_from_impl = supports_tag_invoke<
232 : value_from_tag, value&, T&& >;
233 : template<class T>
234 : using has_user_conversion_to_impl = supports_tag_invoke<
235 : value_to_tag<T>, value const& >;
236 : template<class T>
237 : using has_nonthrowing_user_conversion_to_impl = supports_tag_invoke<
238 : try_value_to_tag<T>, value const& >;
239 : template< class T, class Dir >
240 : using has_user_conversion1 = mp11::mp_if<
241 : std::is_same<Dir, value_from_conversion>,
242 : mp11::mp_valid<has_user_conversion_from_impl, T>,
243 : mp11::mp_or<
244 : mp11::mp_valid<has_user_conversion_to_impl, T>,
245 : mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>>>;
246 :
247 : template< class Ctx, class T >
248 : using has_context_conversion_from_impl = supports_tag_invoke<
249 : value_from_tag, value&, T&&, Ctx const& >;
250 : template< class Ctx, class T >
251 : using has_context_conversion_to_impl = supports_tag_invoke<
252 : value_to_tag<T>, value const&, Ctx const& >;
253 : template< class Ctx, class T >
254 : using has_nonthrowing_context_conversion_to_impl = supports_tag_invoke<
255 : try_value_to_tag<T>, value const&, Ctx const& >;
256 : template< class Ctx, class T, class Dir >
257 : using has_user_conversion2 = mp11::mp_if<
258 : std::is_same<Dir, value_from_conversion>,
259 : mp11::mp_valid<has_context_conversion_from_impl, Ctx, T>,
260 : mp11::mp_or<
261 : mp11::mp_valid<has_context_conversion_to_impl, Ctx, T>,
262 : mp11::mp_valid<has_nonthrowing_context_conversion_to_impl, Ctx, T>>>;
263 :
264 : template< class Ctx, class T >
265 : using has_full_context_conversion_from_impl = supports_tag_invoke<
266 : value_from_tag, value&, T&&, Ctx const&, Ctx const& >;
267 : template< class Ctx, class T >
268 : using has_full_context_conversion_to_impl = supports_tag_invoke<
269 : value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
270 : template< class Ctx, class T >
271 : using has_nonthrowing_full_context_conversion_to_impl = supports_tag_invoke<
272 : try_value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
273 : template< class Ctx, class T, class Dir >
274 : using has_user_conversion3 = mp11::mp_if<
275 : std::is_same<Dir, value_from_conversion>,
276 : mp11::mp_valid<has_full_context_conversion_from_impl, Ctx, T>,
277 : mp11::mp_or<
278 : mp11::mp_valid<has_full_context_conversion_to_impl, Ctx, T>,
279 : mp11::mp_valid<
280 : has_nonthrowing_full_context_conversion_to_impl, Ctx, T>>>;
281 :
282 : template< class T >
283 : using described_non_public_members = describe::describe_members<
284 : T,
285 : describe::mod_private
286 : | describe::mod_protected
287 : | boost::describe::mod_inherited>;
288 :
289 : #if defined(BOOST_MSVC) && BOOST_MSVC < 1920
290 :
291 : template< class T >
292 : struct described_member_t_impl;
293 :
294 : template< class T, class C >
295 : struct described_member_t_impl<T C::*>
296 : {
297 : using type = T;
298 : };
299 :
300 : template< class T, class D >
301 : using described_member_t = remove_cvref<
302 : typename described_member_t_impl<
303 : remove_cvref<decltype(D::pointer)> >::type>;
304 :
305 : #else
306 :
307 : template< class T, class D >
308 : using described_member_t = remove_cvref<decltype(
309 : std::declval<T&>().* D::pointer )>;
310 :
311 : #endif
312 :
313 : template< class T >
314 : using described_members = describe::describe_members<
315 : T, describe::mod_any_access | describe::mod_inherited>;
316 :
317 : #ifdef BOOST_DESCRIBE_CXX14
318 :
319 : constexpr
320 : bool
321 : compare_strings(char const* l, char const* r)
322 : {
323 : #if defined(_MSC_VER) && (_MSC_VER <= 1900) && !defined(__clang__)
324 : return *l == *r && ( (*l == 0) | compare_strings(l + 1, r + 1) );
325 : #else
326 : do
327 : {
328 : if( *l != *r )
329 : return false;
330 : if( *l == 0 )
331 : return true;
332 : ++l;
333 : ++r;
334 : } while(true);
335 : #endif
336 : }
337 :
338 : template< class L, class R >
339 : struct equal_member_names
340 : : mp11::mp_bool< compare_strings(L::name, R::name) >
341 : {};
342 :
343 : template< class T >
344 : using uniquely_named_members = mp11::mp_same<
345 : mp11::mp_unique_if< described_members<T>, equal_member_names >,
346 : described_members<T> >;
347 :
348 : #else
349 :
350 : // we only check this in C++14, but the template should exist nevertheless
351 : template< class T >
352 : using uniquely_named_members = std::true_type;
353 :
354 : #endif // BOOST_DESCRIBE_CXX14
355 :
356 : // user conversion (via tag_invoke)
357 : template< class Ctx, class T, class Dir >
358 : using user_conversion_category = mp11::mp_cond<
359 : has_user_conversion3<Ctx, T, Dir>, user_full_context_category,
360 : has_user_conversion2<Ctx, T, Dir>, user_context_category,
361 : has_user_conversion1<T, Dir>, user_category>;
362 :
363 : // native conversions (constructors and member functions of value)
364 : template< class T >
365 : using native_conversion_category = mp11::mp_cond<
366 : std::is_same<T, value_ref>, json_value_ref_category,
367 : std::is_same<T, value>, json_value_category,
368 : std::is_same<T, array>, json_array_category,
369 : std::is_same<T, object>, json_object_category,
370 : std::is_same<T, string>, json_string_category>;
371 :
372 : template< class T >
373 : using conversion_category_t = std::integral_constant<
374 : conversion_category, conversion_category_of<T>::value>;
375 :
376 : template< class T >
377 : using nested_type = typename T::type;
378 : template< class T1, class T2 >
379 : using extended_conversion_category_impl_helper = mp11::mp_eval_if_not<
380 : std::is_same<unknown_category, T1>,
381 : T1,
382 : mp11::mp_eval_or_q, T1, mp11::mp_quote<nested_type>, T2>;
383 : template< class Ctx, class T, class Dir >
384 : struct extended_conversion_category_impl
385 : {
386 : using type = mp11::mp_fold<
387 : mp11::mp_list<
388 : mp11::mp_defer<user_conversion_category, Ctx, T, Dir>,
389 : mp11::mp_defer<native_conversion_category, T>,
390 : mp11::mp_defer<conversion_category_t, T>>,
391 : unknown_category,
392 : extended_conversion_category_impl_helper>;
393 : };
394 : template< class Ctx, class T, class Dir >
395 : using extended_conversion_category =
396 : typename extended_conversion_category_impl<Ctx, T, Dir>::type;
397 :
398 : template< class T >
399 : using any_conversion_tag = mp11::mp_not< std::is_same<T, unknown_category> >;
400 :
401 : template< class T, class Dir, class... Ctxs >
402 : struct extended_conversion_category_impl< std::tuple<Ctxs...>, T, Dir >
403 : {
404 : using ctxs = mp11::mp_list< remove_cvref<Ctxs>... >;
405 : using cats = mp11::mp_list<
406 : extended_conversion_category<remove_cvref<Ctxs>, T, Dir>... >;
407 :
408 : template< class I >
409 : using exists = mp11::mp_less< I, mp11::mp_size<cats> >;
410 :
411 : using context2 = mp11::mp_find< cats, user_full_context_category >;
412 : using context1 = mp11::mp_find< cats, user_context_category >;
413 : using context0 = mp11::mp_find< cats, user_category >;
414 : using index = mp11::mp_cond<
415 : exists<context2>, context2,
416 : exists<context1>, context1,
417 : exists<context0>, context0,
418 : mp11::mp_true, mp11::mp_find_if< cats, any_conversion_tag > >;
419 : using type = mp11::mp_eval_or<
420 : unknown_category,
421 : mp11::mp_at, cats, index >;
422 : };
423 :
424 : struct no_context
425 : {};
426 :
427 : template <class T, class Dir>
428 : using can_convert = mp11::mp_not<
429 : std::is_same<
430 : detail::extended_conversion_category<no_context, T, Dir>,
431 : unknown_category>>;
432 :
433 : template<class Impl1, class Impl2>
434 : using conversion_round_trips_helper = mp11::mp_or<
435 : std::is_same<Impl1, Impl2>,
436 : is_user_conversion<Impl1>,
437 : is_user_conversion<Impl2>>;
438 : template< class Ctx, class T, class Dir >
439 : using conversion_round_trips = conversion_round_trips_helper<
440 : extended_conversion_category<Ctx, T, Dir>,
441 : extended_conversion_category<Ctx, T, mp11::mp_not<Dir>>>;
442 :
443 : template< class T1, class T2 >
444 : struct copy_cref_helper
445 : {
446 : using type = remove_cvref<T2>;
447 : };
448 : template< class T1, class T2 >
449 : using copy_cref = typename copy_cref_helper< T1, T2 >::type;
450 :
451 : template< class T1, class T2 >
452 : struct copy_cref_helper<T1 const, T2>
453 : {
454 : using type = remove_cvref<T2> const;
455 : };
456 : template< class T1, class T2 >
457 : struct copy_cref_helper<T1&, T2>
458 : {
459 : using type = copy_cref<T1, T2>&;
460 : };
461 : template< class T1, class T2 >
462 : struct copy_cref_helper<T1&&, T2>
463 : {
464 : using type = copy_cref<T1, T2>&&;
465 : };
466 :
467 : template< class Rng, class Traits >
468 : using forwarded_value_helper = mp11::mp_if<
469 : std::is_convertible<
470 : typename Traits::reference,
471 : copy_cref<Rng, typename Traits::value_type> >,
472 : copy_cref<Rng, typename Traits::value_type>,
473 : typename Traits::value_type >;
474 :
475 : template< class Rng >
476 : using forwarded_value = forwarded_value_helper<
477 : Rng, iterator_traits< Rng > >;
478 :
479 : template< class Ctx, class T, class Dir >
480 : struct supported_context
481 : {
482 : using type = Ctx;
483 :
484 : static
485 : type const&
486 32 : get( Ctx const& ctx ) noexcept
487 : {
488 32 : return ctx;
489 : }
490 : };
491 :
492 : template< class T, class Dir, class... Ctxs >
493 : struct supported_context< std::tuple<Ctxs...>, T, Dir >
494 : {
495 : using Ctx = std::tuple<Ctxs...>;
496 : using impl = extended_conversion_category_impl<Ctx, T, Dir>;
497 : using index = typename impl::index;
498 : using next_supported = supported_context<
499 : mp11::mp_at< typename impl::ctxs, index >, T, Dir >;
500 : using type = typename next_supported::type;
501 :
502 : static
503 : type const&
504 19 : get( Ctx const& ctx ) noexcept
505 : {
506 19 : return next_supported::get( std::get<index::value>( ctx ) );
507 : }
508 : };
509 :
510 : template< class T >
511 : using value_result_type = typename std::decay<
512 : decltype( std::declval<T&>().value() )>::type;
513 :
514 : template< class T >
515 : using can_reset = decltype( std::declval<T&>().reset() );
516 :
517 : template< class T >
518 : using has_valueless_by_exception =
519 : decltype( std::declval<T const&>().valueless_by_exception() );
520 :
521 : } // namespace detail
522 :
523 : template <class T>
524 : struct result_for<T, value>
525 : {
526 : using type = system::result< detail::remove_cvref<T> >;
527 : };
528 :
529 : template<class T>
530 : struct is_string_like
531 : : std::is_convertible<T, string_view>
532 : { };
533 :
534 : template<class T>
535 : struct is_path_like
536 : : mp11::mp_all<
537 : mp11::mp_valid_and_true<detail::is_its_own_value, T>,
538 : mp11::mp_valid_and_true<detail::has_string_type, T>>
539 : { };
540 : template<class T>
541 : struct is_sequence_like
542 : : mp11::mp_all<
543 : mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
544 : mp11::mp_valid_and_true<detail::not_its_own_value, T>,
545 : mp11::mp_valid<detail::begin_iterator_category, T>>
546 : { };
547 :
548 : template<class T>
549 : struct is_map_like
550 : : mp11::mp_all<
551 : is_sequence_like<T>,
552 : mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
553 : is_string_like<detail::key_type<T>>,
554 : mp11::mp_valid_and_true<detail::has_unique_keys, T>>
555 : { };
556 :
557 : template<class T>
558 : struct is_tuple_like
559 : : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
560 : { };
561 :
562 : template<>
563 : struct is_null_like<std::nullptr_t>
564 : : std::true_type
565 : { };
566 :
567 : #ifndef BOOST_NO_CXX17_HDR_VARIANT
568 : template<>
569 : struct is_null_like<std::monostate>
570 : : std::true_type
571 : { };
572 : #endif // BOOST_NO_CXX17_HDR_VARIANT
573 :
574 : template<class T>
575 : struct is_described_class
576 : : mp11::mp_and<
577 : describe::has_describe_members<T>,
578 : mp11::mp_not< std::is_union<T> >,
579 : mp11::mp_empty<
580 : mp11::mp_eval_or<
581 : mp11::mp_list<>, detail::described_non_public_members, T>>>
582 : { };
583 :
584 : template<class T>
585 : struct is_described_enum
586 : : describe::has_describe_enumerators<T>
587 : { };
588 :
589 : template<class T>
590 : struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T>
591 : { };
592 :
593 : template<class T>
594 : struct is_optional_like
595 : : mp11::mp_and<
596 : mp11::mp_not<std::is_void<
597 : mp11::mp_eval_or<void, detail::value_result_type, T>>>,
598 : mp11::mp_valid<detail::can_reset, T>>
599 : { };
600 :
601 : } // namespace json
602 : } // namespace boost
603 :
604 : #endif // BOOST_JSON_IMPL_CONVERSION_HPP
|