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