GCC Code Coverage Report


Directory: libs/json/include/boost/json/
File: impl/conversion.hpp
Date: 2025-12-23 17:22:01
Exec Total Coverage
Lines: 18 18 100.0%
Functions: 55 59 93.2%
Branches: 0 0 -%

Line Branch Exec Source
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 276 try_size(T&& cont, mp11::mp_int<3>)
124 {
125 276 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 14 try_size(T&, mp11::mp_int<0>)
145 {
146 14 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 55 inserter(
163 T& target,
164 mp11::mp_int<2>)
165 {
166 55 return target.begin();
167 }
168
169 template<class T>
170 std::back_insert_iterator<T>
171 557 inserter(
172 T& target,
173 mp11::mp_int<1>)
174 {
175 557 return std::back_inserter(target);
176 }
177
178 template<class T>
179 std::insert_iterator<T>
180 84 inserter(
181 T& target,
182 mp11::mp_int<0>)
183 {
184 84 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 struct native_conversion_tag { };
194 struct value_conversion_tag : native_conversion_tag { };
195 struct object_conversion_tag : native_conversion_tag { };
196 struct array_conversion_tag : native_conversion_tag { };
197 struct string_conversion_tag : native_conversion_tag { };
198 struct bool_conversion_tag : native_conversion_tag { };
199 struct number_conversion_tag : native_conversion_tag { };
200 struct integral_conversion_tag : number_conversion_tag { };
201 struct floating_point_conversion_tag : number_conversion_tag { };
202 struct null_like_conversion_tag { };
203 struct string_like_conversion_tag { };
204 struct map_like_conversion_tag { };
205 struct path_conversion_tag { };
206 struct sequence_conversion_tag { };
207 struct tuple_conversion_tag { };
208 struct described_class_conversion_tag { };
209 struct described_enum_conversion_tag { };
210 struct variant_conversion_tag { };
211 struct optional_conversion_tag { };
212 struct no_conversion_tag { };
213
214 template<class... Args>
215 using supports_tag_invoke = decltype(tag_invoke( std::declval<Args>()... ));
216
217 template<class T>
218 using has_user_conversion_from_impl = supports_tag_invoke<
219 value_from_tag, value&, T&& >;
220 template<class T>
221 using has_user_conversion_to_impl = supports_tag_invoke<
222 value_to_tag<T>, value const& >;
223 template<class T>
224 using has_nonthrowing_user_conversion_to_impl = supports_tag_invoke<
225 try_value_to_tag<T>, value const& >;
226 template< class T, class Dir >
227 using has_user_conversion1 = mp11::mp_if<
228 std::is_same<Dir, value_from_conversion>,
229 mp11::mp_valid<has_user_conversion_from_impl, T>,
230 mp11::mp_or<
231 mp11::mp_valid<has_user_conversion_to_impl, T>,
232 mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>>>;
233
234 template< class Ctx, class T >
235 using has_context_conversion_from_impl = supports_tag_invoke<
236 value_from_tag, value&, T&&, Ctx const& >;
237 template< class Ctx, class T >
238 using has_context_conversion_to_impl = supports_tag_invoke<
239 value_to_tag<T>, value const&, Ctx const& >;
240 template< class Ctx, class T >
241 using has_nonthrowing_context_conversion_to_impl = supports_tag_invoke<
242 try_value_to_tag<T>, value const&, Ctx const& >;
243 template< class Ctx, class T, class Dir >
244 using has_user_conversion2 = mp11::mp_if<
245 std::is_same<Dir, value_from_conversion>,
246 mp11::mp_valid<has_context_conversion_from_impl, Ctx, T>,
247 mp11::mp_or<
248 mp11::mp_valid<has_context_conversion_to_impl, Ctx, T>,
249 mp11::mp_valid<has_nonthrowing_context_conversion_to_impl, Ctx, T>>>;
250
251 template< class Ctx, class T >
252 using has_full_context_conversion_from_impl = supports_tag_invoke<
253 value_from_tag, value&, T&&, Ctx const&, Ctx const& >;
254 template< class Ctx, class T >
255 using has_full_context_conversion_to_impl = supports_tag_invoke<
256 value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
257 template< class Ctx, class T >
258 using has_nonthrowing_full_context_conversion_to_impl = supports_tag_invoke<
259 try_value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
260 template< class Ctx, class T, class Dir >
261 using has_user_conversion3 = mp11::mp_if<
262 std::is_same<Dir, value_from_conversion>,
263 mp11::mp_valid<has_full_context_conversion_from_impl, Ctx, T>,
264 mp11::mp_or<
265 mp11::mp_valid<has_full_context_conversion_to_impl, Ctx, T>,
266 mp11::mp_valid<
267 has_nonthrowing_full_context_conversion_to_impl, Ctx, T>>>;
268
269 template< class T >
270 using described_non_public_members = describe::describe_members<
271 T, describe::mod_private | describe::mod_protected>;
272 template< class T >
273 using described_bases = describe::describe_bases<
274 T, describe::mod_any_access>;
275
276 #if defined(BOOST_MSVC) && BOOST_MSVC < 1920
277
278 template< class T >
279 struct described_member_t_impl;
280
281 template< class T, class C >
282 struct described_member_t_impl<T C::*>
283 {
284 using type = T;
285 };
286
287 template< class T, class D >
288 using described_member_t = remove_cvref<
289 typename described_member_t_impl<
290 remove_cvref<decltype(D::pointer)> >::type>;
291
292 #else
293
294 template< class T, class D >
295 using described_member_t = remove_cvref<decltype(
296 std::declval<T&>().* D::pointer )>;
297
298 #endif
299
300 template< class T >
301 using described_members = describe::describe_members<
302 T, describe::mod_any_access | describe::mod_inherited>;
303
304 // user conversion (via tag_invoke)
305 template< class Ctx, class T, class Dir >
306 using user_conversion_category = mp11::mp_cond<
307 has_user_conversion3<Ctx, T, Dir>, full_context_conversion_tag,
308 has_user_conversion2<Ctx, T, Dir>, context_conversion_tag,
309 has_user_conversion1<T, Dir>, user_conversion_tag>;
310
311 // native conversions (constructors and member functions of value)
312 template< class T >
313 using native_conversion_category = mp11::mp_cond<
314 std::is_same<T, value>, value_conversion_tag,
315 std::is_same<T, array>, array_conversion_tag,
316 std::is_same<T, object>, object_conversion_tag,
317 std::is_same<T, string>, string_conversion_tag>;
318
319 // generic conversions
320 template< class T >
321 using generic_conversion_category = mp11::mp_cond<
322 std::is_same<T, bool>, bool_conversion_tag,
323 std::is_integral<T>, integral_conversion_tag,
324 std::is_floating_point<T>, floating_point_conversion_tag,
325 is_null_like<T>, null_like_conversion_tag,
326 is_string_like<T>, string_like_conversion_tag,
327 is_map_like<T>, map_like_conversion_tag,
328 is_sequence_like<T>, sequence_conversion_tag,
329 is_tuple_like<T>, tuple_conversion_tag,
330 is_described_class<T>, described_class_conversion_tag,
331 is_described_enum<T>, described_enum_conversion_tag,
332 is_variant_like<T>, variant_conversion_tag,
333 is_optional_like<T>, optional_conversion_tag,
334 is_path_like<T>, path_conversion_tag,
335 // failed to find a suitable implementation
336 mp11::mp_true, no_conversion_tag>;
337
338 template< class T >
339 using nested_type = typename T::type;
340 template< class T1, class T2 >
341 using conversion_category_impl_helper = mp11::mp_eval_if_not<
342 std::is_same<detail::no_conversion_tag, T1>,
343 T1,
344 mp11::mp_eval_or_q, T1, mp11::mp_quote<nested_type>, T2>;
345 template< class Ctx, class T, class Dir >
346 struct conversion_category_impl
347 {
348 using type = mp11::mp_fold<
349 mp11::mp_list<
350 mp11::mp_defer<user_conversion_category, Ctx, T, Dir>,
351 mp11::mp_defer<native_conversion_category, T>,
352 mp11::mp_defer<generic_conversion_category, T>>,
353 no_conversion_tag,
354 conversion_category_impl_helper>;
355 };
356 template< class Ctx, class T, class Dir >
357 using conversion_category =
358 typename conversion_category_impl< Ctx, T, Dir >::type;
359
360 template< class T >
361 using any_conversion_tag = mp11::mp_not<
362 std::is_same< T, no_conversion_tag > >;
363
364 template< class T, class Dir, class... Ctxs >
365 struct conversion_category_impl< std::tuple<Ctxs...>, T, Dir >
366 {
367 using ctxs = mp11::mp_list< remove_cvref<Ctxs>... >;
368 using cats = mp11::mp_list<
369 conversion_category<remove_cvref<Ctxs>, T, Dir>... >;
370
371 template< class I >
372 using exists = mp11::mp_less< I, mp11::mp_size<cats> >;
373
374 using context2 = mp11::mp_find< cats, full_context_conversion_tag >;
375 using context1 = mp11::mp_find< cats, context_conversion_tag >;
376 using context0 = mp11::mp_find< cats, user_conversion_tag >;
377 using index = mp11::mp_cond<
378 exists<context2>, context2,
379 exists<context1>, context1,
380 exists<context0>, context0,
381 mp11::mp_true, mp11::mp_find_if< cats, any_conversion_tag > >;
382 using type = mp11::mp_eval_or<
383 no_conversion_tag,
384 mp11::mp_at, cats, index >;
385 };
386
387 struct no_context
388 {};
389
390 struct allow_exceptions
391 {};
392
393 template <class T, class Dir>
394 using can_convert = mp11::mp_not<
395 std::is_same<
396 detail::conversion_category<no_context, T, Dir>,
397 detail::no_conversion_tag>>;
398
399 template<class Impl1, class Impl2>
400 using conversion_round_trips_helper = mp11::mp_or<
401 std::is_same<Impl1, Impl2>,
402 std::is_base_of<user_conversion_tag, Impl1>,
403 std::is_base_of<user_conversion_tag, Impl2>>;
404 template< class Ctx, class T, class Dir >
405 using conversion_round_trips = conversion_round_trips_helper<
406 conversion_category<Ctx, T, Dir>,
407 conversion_category<Ctx, T, mp11::mp_not<Dir>>>;
408
409 template< class T1, class T2 >
410 struct copy_cref_helper
411 {
412 using type = remove_cvref<T2>;
413 };
414 template< class T1, class T2 >
415 using copy_cref = typename copy_cref_helper< T1, T2 >::type;
416
417 template< class T1, class T2 >
418 struct copy_cref_helper<T1 const, T2>
419 {
420 using type = remove_cvref<T2> const;
421 };
422 template< class T1, class T2 >
423 struct copy_cref_helper<T1&, T2>
424 {
425 using type = copy_cref<T1, T2>&;
426 };
427 template< class T1, class T2 >
428 struct copy_cref_helper<T1&&, T2>
429 {
430 using type = copy_cref<T1, T2>&&;
431 };
432
433 template< class Rng, class Traits >
434 using forwarded_value_helper = mp11::mp_if<
435 std::is_convertible<
436 typename Traits::reference,
437 copy_cref<Rng, typename Traits::value_type> >,
438 copy_cref<Rng, typename Traits::value_type>,
439 typename Traits::value_type >;
440
441 template< class Rng >
442 using forwarded_value = forwarded_value_helper<
443 Rng, iterator_traits< Rng > >;
444
445 template< class Ctx, class T, class Dir >
446 struct supported_context
447 {
448 using type = Ctx;
449
450 static
451 type const&
452 60 get( Ctx const& ctx ) noexcept
453 {
454 60 return ctx;
455 }
456 };
457
458 template< class T, class Dir, class... Ctxs >
459 struct supported_context< std::tuple<Ctxs...>, T, Dir >
460 {
461 using Ctx = std::tuple<Ctxs...>;
462 using impl = conversion_category_impl<Ctx, T, Dir>;
463 using index = typename impl::index;
464 using next_supported = supported_context<
465 mp11::mp_at< typename impl::ctxs, index >, T, Dir >;
466 using type = typename next_supported::type;
467
468 static
469 type const&
470 19 get( Ctx const& ctx ) noexcept
471 {
472 19 return next_supported::get( std::get<index::value>( ctx ) );
473 }
474 };
475
476 template< class T >
477 using value_result_type = typename std::decay<
478 decltype( std::declval<T&>().value() )>::type;
479
480 template< class T >
481 using can_reset = decltype( std::declval<T&>().reset() );
482
483 template< class T >
484 using has_valueless_by_exception =
485 decltype( std::declval<T const&>().valueless_by_exception() );
486
487 } // namespace detail
488
489 template <class T>
490 struct result_for<T, value>
491 {
492 using type = system::result< detail::remove_cvref<T> >;
493 };
494
495 template<class T>
496 struct is_string_like
497 : std::is_convertible<T, string_view>
498 { };
499
500 template<class T>
501 struct is_path_like
502 : mp11::mp_all<
503 mp11::mp_valid_and_true<detail::is_its_own_value, T>,
504 mp11::mp_valid_and_true<detail::has_string_type, T>>
505 { };
506 template<class T>
507 struct is_sequence_like
508 : mp11::mp_all<
509 mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
510 mp11::mp_valid_and_true<detail::not_its_own_value, T>,
511 mp11::mp_valid<detail::begin_iterator_category, T>>
512 { };
513
514 template<class T>
515 struct is_map_like
516 : mp11::mp_all<
517 is_sequence_like<T>,
518 mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
519 is_string_like<detail::key_type<T>>,
520 mp11::mp_valid_and_true<detail::has_unique_keys, T>>
521 { };
522
523 template<class T>
524 struct is_tuple_like
525 : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
526 { };
527
528 template<>
529 struct is_null_like<std::nullptr_t>
530 : std::true_type
531 { };
532
533 #ifndef BOOST_NO_CXX17_HDR_VARIANT
534 template<>
535 struct is_null_like<std::monostate>
536 : std::true_type
537 { };
538 #endif // BOOST_NO_CXX17_HDR_VARIANT
539
540 template<class T>
541 struct is_described_class
542 : mp11::mp_and<
543 describe::has_describe_members<T>,
544 mp11::mp_not< std::is_union<T> >,
545 mp11::mp_empty<
546 mp11::mp_eval_or<
547 mp11::mp_list<>, detail::described_non_public_members, T>>,
548 mp11::mp_empty<
549 mp11::mp_eval_or<mp11::mp_list<>, detail::described_bases, T>>>
550 { };
551
552 template<class T>
553 struct is_described_enum
554 : describe::has_describe_enumerators<T>
555 { };
556
557 template<class T>
558 struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T>
559 { };
560
561 template<class T>
562 struct is_optional_like
563 : mp11::mp_and<
564 mp11::mp_not<std::is_void<
565 mp11::mp_eval_or<void, detail::value_result_type, T>>>,
566 mp11::mp_valid<detail::can_reset, T>>
567 { };
568
569 } // namespace json
570 } // namespace boost
571
572 #endif // BOOST_JSON_IMPL_CONVERSION_HPP
573