100.00% Lines (18/18) 100.00% Functions (9/9)
TLA Baseline Branch
Line Hits Code Line Hits Code
1   // 1   //
2   // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com) 2   // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
3   // Copyright (c) 2022 Dmitry Arkhipov (grisumbras@yandex.ru) 3   // Copyright (c) 2022 Dmitry Arkhipov (grisumbras@yandex.ru)
4   // 4   //
5   // Distributed under the Boost Software License, Version 1.0. (See accompanying 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) 6   // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7   // 7   //
8   // Official repository: https://github.com/boostorg/json 8   // Official repository: https://github.com/boostorg/json
9   // 9   //
10   10  
11   #ifndef BOOST_JSON_IMPL_CONVERSION_HPP 11   #ifndef BOOST_JSON_IMPL_CONVERSION_HPP
12   #define BOOST_JSON_IMPL_CONVERSION_HPP 12   #define BOOST_JSON_IMPL_CONVERSION_HPP
13   13  
14   #include <boost/json/fwd.hpp> 14   #include <boost/json/fwd.hpp>
15   #include <boost/json/string_view.hpp> 15   #include <boost/json/string_view.hpp>
16   #include <boost/describe/enumerators.hpp> 16   #include <boost/describe/enumerators.hpp>
17   #include <boost/describe/members.hpp> 17   #include <boost/describe/members.hpp>
18   #include <boost/describe/bases.hpp> 18   #include <boost/describe/bases.hpp>
19   #include <boost/mp11/algorithm.hpp> 19   #include <boost/mp11/algorithm.hpp>
20   #include <boost/mp11/utility.hpp> 20   #include <boost/mp11/utility.hpp>
21   #include <boost/system/result.hpp> 21   #include <boost/system/result.hpp>
22   22  
23   #include <iterator> 23   #include <iterator>
24   #include <tuple> 24   #include <tuple>
25   #include <utility> 25   #include <utility>
  26 +
26   #ifndef BOOST_NO_CXX17_HDR_VARIANT 27   #ifndef BOOST_NO_CXX17_HDR_VARIANT
27   # include <variant> 28   # include <variant>
28   #endif // BOOST_NO_CXX17_HDR_VARIANT 29   #endif // BOOST_NO_CXX17_HDR_VARIANT
29   30  
  31 + #ifdef BOOST_JSON_HAS_REFLECTION
  32 + # include <meta>
  33 + #endif // BOOST_JSON_HAS_REFLECTION
  34 +
30   namespace boost { 35   namespace boost {
31   namespace json { 36   namespace json {
32   37  
33   class value_ref; 38   class value_ref;
34   39  
35   namespace detail { 40   namespace detail {
36   41  
37   #ifdef __cpp_lib_nonmember_container_access 42   #ifdef __cpp_lib_nonmember_container_access
38   using std::size; 43   using std::size;
39   #endif 44   #endif
40   45  
41   template<std::size_t I, class T> 46   template<std::size_t I, class T>
42   using tuple_element_t = typename std::tuple_element<I, T>::type; 47   using tuple_element_t = typename std::tuple_element<I, T>::type;
43   48  
44   template<class T> 49   template<class T>
45   using iterator_type = decltype(std::begin(std::declval<T&>())); 50   using iterator_type = decltype(std::begin(std::declval<T&>()));
46   template<class T> 51   template<class T>
47   using iterator_traits = std::iterator_traits< iterator_type<T> >; 52   using iterator_traits = std::iterator_traits< iterator_type<T> >;
48   53  
49   template<class T> 54   template<class T>
50   using value_type = typename iterator_traits<T>::value_type; 55   using value_type = typename iterator_traits<T>::value_type;
51   template<class T> 56   template<class T>
52   using mapped_type = tuple_element_t< 1, value_type<T> >; 57   using mapped_type = tuple_element_t< 1, value_type<T> >;
53   58  
54   // had to make the metafunction always succeeding in order to make it work 59   // had to make the metafunction always succeeding in order to make it work
55   // with msvc 14.0 60   // with msvc 14.0
56   template<class T> 61   template<class T>
57   using key_type_helper = tuple_element_t< 0, value_type<T> >; 62   using key_type_helper = tuple_element_t< 0, value_type<T> >;
58   template<class T> 63   template<class T>
59   using key_type = mp11::mp_eval_or< 64   using key_type = mp11::mp_eval_or<
60   void, 65   void,
61   key_type_helper, 66   key_type_helper,
62   T>; 67   T>;
63   68  
64   template<class T> 69   template<class T>
65   using are_begin_and_end_same = std::is_same< 70   using are_begin_and_end_same = std::is_same<
66   iterator_type<T>, 71   iterator_type<T>,
67   decltype(std::end(std::declval<T&>()))>; 72   decltype(std::end(std::declval<T&>()))>;
68   73  
69   // msvc 14.0 gets confused when std::is_same is used directly 74   // msvc 14.0 gets confused when std::is_same is used directly
70   template<class A, class B> 75   template<class A, class B>
71   using is_same_msvc_140 = std::is_same<A, B>; 76   using is_same_msvc_140 = std::is_same<A, B>;
72   template<class T> 77   template<class T>
73   using is_its_own_value = is_same_msvc_140<value_type<T>, T>; 78   using is_its_own_value = is_same_msvc_140<value_type<T>, T>;
74   79  
75   template<class T> 80   template<class T>
76   using not_its_own_value = mp11::mp_not< is_its_own_value<T> >; 81   using not_its_own_value = mp11::mp_not< is_its_own_value<T> >;
77   82  
78   template<class T> 83   template<class T>
79   using begin_iterator_category = typename std::iterator_traits< 84   using begin_iterator_category = typename std::iterator_traits<
80   iterator_type<T>>::iterator_category; 85   iterator_type<T>>::iterator_category;
81   86  
82   template<class T> 87   template<class T>
83   using has_positive_tuple_size = mp11::mp_bool< 88   using has_positive_tuple_size = mp11::mp_bool<
84   (std::tuple_size<T>::value > 0) >; 89   (std::tuple_size<T>::value > 0) >;
85   90  
86   template<class T> 91   template<class T>
87   using has_unique_keys = has_positive_tuple_size<decltype( 92   using has_unique_keys = has_positive_tuple_size<decltype(
88   std::declval<T&>().emplace( 93   std::declval<T&>().emplace(
89   std::declval<value_type<T>>()))>; 94   std::declval<value_type<T>>()))>;
90   95  
91   template<class T> 96   template<class T>
92   using has_string_type = std::is_same< 97   using has_string_type = std::is_same<
93   typename T::string_type, std::basic_string<typename T::value_type> >; 98   typename T::string_type, std::basic_string<typename T::value_type> >;
94   99  
95   template<class T> 100   template<class T>
96   struct is_value_type_pair_helper : std::false_type 101   struct is_value_type_pair_helper : std::false_type
97   { }; 102   { };
98   template<class T1, class T2> 103   template<class T1, class T2>
99   struct is_value_type_pair_helper<std::pair<T1, T2>> : std::true_type 104   struct is_value_type_pair_helper<std::pair<T1, T2>> : std::true_type
100   { }; 105   { };
101   template<class T> 106   template<class T>
102   using is_value_type_pair = is_value_type_pair_helper<value_type<T>>; 107   using is_value_type_pair = is_value_type_pair_helper<value_type<T>>;
103   108  
104   template<class T> 109   template<class T>
105   using has_size_member_helper 110   using has_size_member_helper
106   = std::is_convertible<decltype(std::declval<T&>().size()), std::size_t>; 111   = std::is_convertible<decltype(std::declval<T&>().size()), std::size_t>;
107   template<class T> 112   template<class T>
108   using has_size_member = mp11::mp_valid_and_true<has_size_member_helper, T>; 113   using has_size_member = mp11::mp_valid_and_true<has_size_member_helper, T>;
109   template<class T> 114   template<class T>
110   using has_free_size_helper 115   using has_free_size_helper
111   = std::is_convertible< 116   = std::is_convertible<
112   decltype(size(std::declval<T const&>())), 117   decltype(size(std::declval<T const&>())),
113   std::size_t>; 118   std::size_t>;
114   template<class T> 119   template<class T>
115   using has_free_size = mp11::mp_valid_and_true<has_free_size_helper, T>; 120   using has_free_size = mp11::mp_valid_and_true<has_free_size_helper, T>;
116   template<class T> 121   template<class T>
117   using size_implementation = mp11::mp_cond< 122   using size_implementation = mp11::mp_cond<
118   has_size_member<T>, mp11::mp_int<3>, 123   has_size_member<T>, mp11::mp_int<3>,
119   has_free_size<T>, mp11::mp_int<2>, 124   has_free_size<T>, mp11::mp_int<2>,
120   std::is_array<T>, mp11::mp_int<1>, 125   std::is_array<T>, mp11::mp_int<1>,
121   mp11::mp_true, mp11::mp_int<0>>; 126   mp11::mp_true, mp11::mp_int<0>>;
122   127  
123   template<class T> 128   template<class T>
124   std::size_t 129   std::size_t
HITCBC 125   141 try_size(T&& cont, mp11::mp_int<3>) 130   141 try_size(T&& cont, mp11::mp_int<3>)
126   { 131   {
HITCBC 127   141 return cont.size(); 132   141 return cont.size();
128   } 133   }
129   134  
130   template<class T> 135   template<class T>
131   std::size_t 136   std::size_t
HITCBC 132   1 try_size(T& cont, mp11::mp_int<2>) 137   1 try_size(T& cont, mp11::mp_int<2>)
133   { 138   {
HITCBC 134   1 return size(cont); 139   1 return size(cont);
135   } 140   }
136   141  
137   template<class T, std::size_t N> 142   template<class T, std::size_t N>
138   std::size_t 143   std::size_t
HITCBC 139   1 try_size(T(&)[N], mp11::mp_int<1>) 144   1 try_size(T(&)[N], mp11::mp_int<1>)
140   { 145   {
HITCBC 141   1 return N; 146   1 return N;
142   } 147   }
143   148  
144   template<class T> 149   template<class T>
145   std::size_t 150   std::size_t
HITCBC 146   7 try_size(T&, mp11::mp_int<0>) 151   7 try_size(T&, mp11::mp_int<0>)
147   { 152   {
HITCBC 148   7 return 0; 153   7 return 0;
149   } 154   }
150   155  
151   template<class T> 156   template<class T>
152   using has_push_back_helper 157   using has_push_back_helper
153   = decltype(std::declval<T&>().push_back(std::declval<value_type<T>>())); 158   = decltype(std::declval<T&>().push_back(std::declval<value_type<T>>()));
154   template<class T> 159   template<class T>
155   using has_push_back = mp11::mp_valid<has_push_back_helper, T>; 160   using has_push_back = mp11::mp_valid<has_push_back_helper, T>;
156   template<class T> 161   template<class T>
157   using inserter_implementation = mp11::mp_cond< 162   using inserter_implementation = mp11::mp_cond<
158   is_tuple_like<T>, mp11::mp_int<2>, 163   is_tuple_like<T>, mp11::mp_int<2>,
159   has_push_back<T>, mp11::mp_int<1>, 164   has_push_back<T>, mp11::mp_int<1>,
160   mp11::mp_true, mp11::mp_int<0>>; 165   mp11::mp_true, mp11::mp_int<0>>;
161   166  
162   template<class T> 167   template<class T>
163   iterator_type<T> 168   iterator_type<T>
HITCBC 164   56 inserter( 169   56 inserter(
165   T& target, 170   T& target,
166   mp11::mp_int<2>) 171   mp11::mp_int<2>)
167   { 172   {
HITCBC 168   56 return target.begin(); 173   56 return target.begin();
169   } 174   }
170   175  
171   template<class T> 176   template<class T>
172   std::back_insert_iterator<T> 177   std::back_insert_iterator<T>
HITCBC 173   569 inserter( 178   569 inserter(
174   T& target, 179   T& target,
175   mp11::mp_int<1>) 180   mp11::mp_int<1>)
176   { 181   {
HITCBC 177   569 return std::back_inserter(target); 182   569 return std::back_inserter(target);
178   } 183   }
179   184  
180   template<class T> 185   template<class T>
181   std::insert_iterator<T> 186   std::insert_iterator<T>
HITCBC 182   62 inserter( 187   62 inserter(
183   T& target, 188   T& target,
184   mp11::mp_int<0>) 189   mp11::mp_int<0>)
185   { 190   {
HITCBC 186   62 return std::inserter( target, target.end() ); 191   62 return std::inserter( target, target.end() );
187   } 192   }
188   193  
  194 + using boolean_category = std::integral_constant<
  195 + conversion_category, conversion_category::boolean>;
  196 +
  197 + using integer_category = std::integral_constant<
  198 + conversion_category, conversion_category::integer>;
  199 +
  200 + using floating_point_category = std::integral_constant<
  201 + conversion_category, conversion_category::floating_point>;
  202 +
  203 + } // namespace detail
  204 +
  205 +
  206 + template<class T, class Ctx, class Enable>
  207 + struct use_category : unknown_category {};
  208 +
  209 + namespace detail {
  210 +
189   using value_from_conversion = mp11::mp_true; 211   using value_from_conversion = mp11::mp_true;
190   using value_to_conversion = mp11::mp_false; 212   using value_to_conversion = mp11::mp_false;
191   213  
192 - struct user_conversion_tag { }; 214 + using user_category = std::integral_constant<
193 - struct context_conversion_tag : user_conversion_tag { }; 215 + conversion_category, conversion_category::user>;
194 - struct full_context_conversion_tag : context_conversion_tag { }; 216 + using user_context_category = std::integral_constant<
195 - struct native_conversion_tag { }; 217 + conversion_category, conversion_category::user_context>;
196 - struct value_conversion_tag : native_conversion_tag { }; 218 + using user_full_context_category = std::integral_constant<
197 - struct object_conversion_tag : native_conversion_tag { }; 219 + conversion_category, conversion_category::user_full_context>;
198 - struct array_conversion_tag : native_conversion_tag { }; 220 + using json_value_category = std::integral_constant<
199 - struct string_conversion_tag : native_conversion_tag { }; 221 + conversion_category, conversion_category::json_value>;
200 - struct bool_conversion_tag : native_conversion_tag { }; 222 + using json_object_category = std::integral_constant<
201 - struct value_ref_tag : native_conversion_tag { }; 223 + conversion_category, conversion_category::json_object>;
202 - struct number_conversion_tag : native_conversion_tag { }; 224 + using json_array_category = std::integral_constant<
203 - struct integral_conversion_tag : number_conversion_tag { }; 225 + conversion_category, conversion_category::json_array>;
204 - struct floating_point_conversion_tag : number_conversion_tag { }; 226 + using json_string_category = std::integral_constant<
205 - struct null_like_conversion_tag { }; 227 + conversion_category, conversion_category::json_string>;
206 - struct string_like_conversion_tag { }; 228 + using json_value_ref_category = std::integral_constant<
207 - struct map_like_conversion_tag { }; 229 + conversion_category, conversion_category::json_value_ref>;
208 - struct path_conversion_tag { }; 230 +
209 - struct sequence_conversion_tag { }; 231 + template< class Cat >
210 - struct tuple_conversion_tag { }; 232 + using is_user_conversion = mp11::mp_bool<
211 - struct described_class_conversion_tag { }; 233 + Cat::value == conversion_category::user
212 - struct described_enum_conversion_tag { }; 234 + || Cat::value == conversion_category::user_context
213 - struct variant_conversion_tag { }; 235 + || Cat::value == conversion_category::user_full_context>;
214 - struct optional_conversion_tag { }; 236 +
215 - struct no_conversion_tag { }; 237 + template< class Cat >
  238 + using is_native_conversion = mp11::mp_bool<
  239 + Cat::value == conversion_category::user
  240 + || Cat::value == conversion_category::json_value
  241 + || Cat::value == conversion_category::json_object
  242 + || Cat::value == conversion_category::json_array
  243 + || Cat::value == conversion_category::json_string
  244 + || Cat::value == conversion_category::json_value_ref
  245 + || Cat::value == conversion_category::boolean
  246 + || Cat::value == conversion_category::integer
  247 + || Cat::value == conversion_category::floating_point>;
216   248  
217   template<class... Args> 249   template<class... Args>
218   using supports_tag_invoke = decltype(tag_invoke( std::declval<Args>()... )); 250   using supports_tag_invoke = decltype(tag_invoke( std::declval<Args>()... ));
219   251  
220   template<class T> 252   template<class T>
221   using has_user_conversion_from_impl = supports_tag_invoke< 253   using has_user_conversion_from_impl = supports_tag_invoke<
222   value_from_tag, value&, T&& >; 254   value_from_tag, value&, T&& >;
223   template<class T> 255   template<class T>
224   using has_user_conversion_to_impl = supports_tag_invoke< 256   using has_user_conversion_to_impl = supports_tag_invoke<
225   value_to_tag<T>, value const& >; 257   value_to_tag<T>, value const& >;
226   template<class T> 258   template<class T>
227   using has_nonthrowing_user_conversion_to_impl = supports_tag_invoke< 259   using has_nonthrowing_user_conversion_to_impl = supports_tag_invoke<
228   try_value_to_tag<T>, value const& >; 260   try_value_to_tag<T>, value const& >;
229   template< class T, class Dir > 261   template< class T, class Dir >
230   using has_user_conversion1 = mp11::mp_if< 262   using has_user_conversion1 = mp11::mp_if<
231   std::is_same<Dir, value_from_conversion>, 263   std::is_same<Dir, value_from_conversion>,
232   mp11::mp_valid<has_user_conversion_from_impl, T>, 264   mp11::mp_valid<has_user_conversion_from_impl, T>,
233   mp11::mp_or< 265   mp11::mp_or<
234   mp11::mp_valid<has_user_conversion_to_impl, T>, 266   mp11::mp_valid<has_user_conversion_to_impl, T>,
235   mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>>>; 267   mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>>>;
236   268  
237   template< class Ctx, class T > 269   template< class Ctx, class T >
238   using has_context_conversion_from_impl = supports_tag_invoke< 270   using has_context_conversion_from_impl = supports_tag_invoke<
239   value_from_tag, value&, T&&, Ctx const& >; 271   value_from_tag, value&, T&&, Ctx const& >;
240   template< class Ctx, class T > 272   template< class Ctx, class T >
241   using has_context_conversion_to_impl = supports_tag_invoke< 273   using has_context_conversion_to_impl = supports_tag_invoke<
242   value_to_tag<T>, value const&, Ctx const& >; 274   value_to_tag<T>, value const&, Ctx const& >;
243   template< class Ctx, class T > 275   template< class Ctx, class T >
244   using has_nonthrowing_context_conversion_to_impl = supports_tag_invoke< 276   using has_nonthrowing_context_conversion_to_impl = supports_tag_invoke<
245   try_value_to_tag<T>, value const&, Ctx const& >; 277   try_value_to_tag<T>, value const&, Ctx const& >;
246   template< class Ctx, class T, class Dir > 278   template< class Ctx, class T, class Dir >
247   using has_user_conversion2 = mp11::mp_if< 279   using has_user_conversion2 = mp11::mp_if<
248   std::is_same<Dir, value_from_conversion>, 280   std::is_same<Dir, value_from_conversion>,
249   mp11::mp_valid<has_context_conversion_from_impl, Ctx, T>, 281   mp11::mp_valid<has_context_conversion_from_impl, Ctx, T>,
250   mp11::mp_or< 282   mp11::mp_or<
251   mp11::mp_valid<has_context_conversion_to_impl, Ctx, T>, 283   mp11::mp_valid<has_context_conversion_to_impl, Ctx, T>,
252   mp11::mp_valid<has_nonthrowing_context_conversion_to_impl, Ctx, T>>>; 284   mp11::mp_valid<has_nonthrowing_context_conversion_to_impl, Ctx, T>>>;
253   285  
254   template< class Ctx, class T > 286   template< class Ctx, class T >
255   using has_full_context_conversion_from_impl = supports_tag_invoke< 287   using has_full_context_conversion_from_impl = supports_tag_invoke<
256   value_from_tag, value&, T&&, Ctx const&, Ctx const& >; 288   value_from_tag, value&, T&&, Ctx const&, Ctx const& >;
257   template< class Ctx, class T > 289   template< class Ctx, class T >
258   using has_full_context_conversion_to_impl = supports_tag_invoke< 290   using has_full_context_conversion_to_impl = supports_tag_invoke<
259   value_to_tag<T>, value const&, Ctx const&, Ctx const& >; 291   value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
260   template< class Ctx, class T > 292   template< class Ctx, class T >
261   using has_nonthrowing_full_context_conversion_to_impl = supports_tag_invoke< 293   using has_nonthrowing_full_context_conversion_to_impl = supports_tag_invoke<
262   try_value_to_tag<T>, value const&, Ctx const&, Ctx const& >; 294   try_value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
263   template< class Ctx, class T, class Dir > 295   template< class Ctx, class T, class Dir >
264   using has_user_conversion3 = mp11::mp_if< 296   using has_user_conversion3 = mp11::mp_if<
265   std::is_same<Dir, value_from_conversion>, 297   std::is_same<Dir, value_from_conversion>,
266   mp11::mp_valid<has_full_context_conversion_from_impl, Ctx, T>, 298   mp11::mp_valid<has_full_context_conversion_from_impl, Ctx, T>,
267   mp11::mp_or< 299   mp11::mp_or<
268   mp11::mp_valid<has_full_context_conversion_to_impl, Ctx, T>, 300   mp11::mp_valid<has_full_context_conversion_to_impl, Ctx, T>,
269   mp11::mp_valid< 301   mp11::mp_valid<
270   has_nonthrowing_full_context_conversion_to_impl, Ctx, T>>>; 302   has_nonthrowing_full_context_conversion_to_impl, Ctx, T>>>;
271   303  
272   template< class T > 304   template< class T >
273   using described_non_public_members = describe::describe_members< 305   using described_non_public_members = describe::describe_members<
274   T, 306   T,
275   describe::mod_private 307   describe::mod_private
276   | describe::mod_protected 308   | describe::mod_protected
277   | boost::describe::mod_inherited>; 309   | boost::describe::mod_inherited>;
278   310  
279   #if defined(BOOST_MSVC) && BOOST_MSVC < 1920 311   #if defined(BOOST_MSVC) && BOOST_MSVC < 1920
280   312  
281   template< class T > 313   template< class T >
282   struct described_member_t_impl; 314   struct described_member_t_impl;
283   315  
284   template< class T, class C > 316   template< class T, class C >
285   struct described_member_t_impl<T C::*> 317   struct described_member_t_impl<T C::*>
286   { 318   {
287   using type = T; 319   using type = T;
288   }; 320   };
289   321  
290   template< class T, class D > 322   template< class T, class D >
291   using described_member_t = remove_cvref< 323   using described_member_t = remove_cvref<
292   typename described_member_t_impl< 324   typename described_member_t_impl<
293   remove_cvref<decltype(D::pointer)> >::type>; 325   remove_cvref<decltype(D::pointer)> >::type>;
294   326  
295   #else 327   #else
296   328  
297   template< class T, class D > 329   template< class T, class D >
298   using described_member_t = remove_cvref<decltype( 330   using described_member_t = remove_cvref<decltype(
299   std::declval<T&>().* D::pointer )>; 331   std::declval<T&>().* D::pointer )>;
300   332  
301   #endif 333   #endif
302   334  
303   template< class T > 335   template< class T >
304   using described_members = describe::describe_members< 336   using described_members = describe::describe_members<
305   T, describe::mod_any_access | describe::mod_inherited>; 337   T, describe::mod_any_access | describe::mod_inherited>;
306   338  
307   #ifdef BOOST_DESCRIBE_CXX14 339   #ifdef BOOST_DESCRIBE_CXX14
308   340  
309   constexpr 341   constexpr
310   bool 342   bool
311   compare_strings(char const* l, char const* r) 343   compare_strings(char const* l, char const* r)
312   { 344   {
313   #if defined(_MSC_VER) && (_MSC_VER <= 1900) && !defined(__clang__) 345   #if defined(_MSC_VER) && (_MSC_VER <= 1900) && !defined(__clang__)
314   return *l == *r && ( (*l == 0) | compare_strings(l + 1, r + 1) ); 346   return *l == *r && ( (*l == 0) | compare_strings(l + 1, r + 1) );
315   #else 347   #else
316   do 348   do
317   { 349   {
318   if( *l != *r ) 350   if( *l != *r )
319   return false; 351   return false;
320   if( *l == 0 ) 352   if( *l == 0 )
321   return true; 353   return true;
322   ++l; 354   ++l;
323   ++r; 355   ++r;
324   } while(true); 356   } while(true);
325   #endif 357   #endif
326   } 358   }
327   359  
328   template< class L, class R > 360   template< class L, class R >
329   struct equal_member_names 361   struct equal_member_names
330   : mp11::mp_bool< compare_strings(L::name, R::name) > 362   : mp11::mp_bool< compare_strings(L::name, R::name) >
331   {}; 363   {};
332   364  
333   template< class T > 365   template< class T >
334   using uniquely_named_members = mp11::mp_same< 366   using uniquely_named_members = mp11::mp_same<
335   mp11::mp_unique_if< described_members<T>, equal_member_names >, 367   mp11::mp_unique_if< described_members<T>, equal_member_names >,
336   described_members<T> >; 368   described_members<T> >;
337   369  
338   #else 370   #else
339   371  
340   // we only check this in C++14, but the template should exist nevertheless 372   // we only check this in C++14, but the template should exist nevertheless
341   template< class T > 373   template< class T >
342   using uniquely_named_members = std::true_type; 374   using uniquely_named_members = std::true_type;
343   375  
344   #endif // BOOST_DESCRIBE_CXX14 376   #endif // BOOST_DESCRIBE_CXX14
345   377  
346   // user conversion (via tag_invoke) 378   // user conversion (via tag_invoke)
347 - template< class Ctx, class T, class Dir > 379 + template< class T, class Ctx, class Dir >
348 - using user_conversion_category = mp11::mp_cond< 380 + using tag_invoke_with_context_category = mp11::mp_cond<
349 - has_user_conversion3<Ctx, T, Dir>, full_context_conversion_tag, 381 + has_user_conversion3<Ctx, T, Dir>, user_full_context_category,
350 - has_user_conversion2<Ctx, T, Dir>, context_conversion_tag, 382 + has_user_conversion2<Ctx, T, Dir>, user_context_category>;
351 - has_user_conversion1<T, Dir>, user_conversion_tag>; 383 +
  384 + template< class T, class Dir >
  385 + using tag_invoke_category = mp11::mp_cond<
  386 + has_user_conversion1<T, Dir>, user_category>;
352   387  
353   // native conversions (constructors and member functions of value) 388   // native conversions (constructors and member functions of value)
354   template< class T > 389   template< class T >
355   using native_conversion_category = mp11::mp_cond< 390   using native_conversion_category = mp11::mp_cond<
356 - std::is_same<T, value>, value_conversion_tag, 391 + std::is_same<T, value_ref>, json_value_ref_category,
357 - std::is_same<T, array>, array_conversion_tag, 392 + std::is_same<T, value>, json_value_category,
358 - std::is_same<T, object>, object_conversion_tag, 393 + std::is_same<T, array>, json_array_category,
359 - std::is_same<T, string>, string_conversion_tag>; 394 + std::is_same<T, object>, json_object_category,
  395 + std::is_same<T, string>, json_string_category>;
360   396  
361 - // generic conversions 397 + template<class T>
362 - template< class T > 398 + struct deduced_category
363 - using generic_conversion_category = mp11::mp_cond< 399 + : mp11::mp_cond<
364 - // std::is_same<T,std::initializer_list<value_ref>>, init_list_tag, 400 + std::is_same<T, bool>, detail::boolean_category,
365 - std::is_same<T, value_ref>, value_ref_tag, 401 + std::is_integral<T>, detail::integer_category,
  402 + std::is_floating_point<T>, detail::floating_point_category,
  403 + is_null_like<T>, null_category,
  404 + is_string_like<T>, string_category,
  405 + is_variant_like<T>, variant_category,
  406 + is_optional_like<T>, optional_category,
  407 + is_map_like<T>, map_category,
  408 + is_sequence_like<T>, sequence_category,
  409 + is_tuple_like<T>, tuple_category,
  410 + is_described_class<T>, described_class_category,
  411 + is_described_enum<T>, described_enum_category,
  412 + is_path_like<T>, path_category,
  413 + // failed to find a suitable implementation
  414 + mp11::mp_true, unknown_category>
  415 + { };
366   416  
367 - std::is_same<T, bool>, bool_conversion_tag, 417 + struct no_context {};
368 - std::is_integral<T>, integral_conversion_tag, 418 +
369 - std::is_floating_point<T>, floating_point_conversion_tag, 419 + #ifdef BOOST_JSON_HAS_REFLECTION
370 - is_null_like<T>, null_like_conversion_tag, 420 +
371 - is_string_like<T>, string_like_conversion_tag, 421 + template<class T, class Ctx>
372 - is_variant_like<T>, variant_conversion_tag, 422 + constexpr
373 - is_optional_like<T>, optional_conversion_tag, 423 + conversion_category
374 - is_map_like<T>, map_like_conversion_tag, 424 + annotated_category_impl()
375 - is_sequence_like<T>, sequence_conversion_tag, 425 + {
376 - is_tuple_like<T>, tuple_conversion_tag, 426 + static constexpr auto annotations = std::define_static_array(
377 - is_described_class<T>, described_class_conversion_tag, 427 + std::meta::annotations_of_with_type(^^T, ^^conversion_category));
378 - is_described_enum<T>, described_enum_conversion_tag, 428 + template for (constexpr std::meta::info a_info: annotations)
379 - is_path_like<T>, path_conversion_tag, 429 + {
380 - // failed to find a suitable implementation 430 + return std::meta::extract<conversion_category>(a_info);
381 - mp11::mp_true, no_conversion_tag>; 431 + }
  432 + return conversion_category::unknown;
  433 + }
  434 +
  435 + template <class T, class Ctx>
  436 + using annotated_category = std::integral_constant<
  437 + conversion_category, annotated_category_impl<T, Ctx>()>;
  438 +
  439 + #endif // BOOST_JSON_HAS_REFLECTION
  440 + //
  441 + template <class T, class Ctx>
  442 + using use_category_helper = use_category<
  443 + T, mp11::mp_if<std::is_same<Ctx, no_context>, void, Ctx>>;
  444 +
  445 + template< class Dir >
  446 + struct all_custom_checks
  447 + {
  448 + template <class T, class Ctx>
  449 + using fn = mp11::mp_list<
  450 + mp11::mp_defer<use_category_helper, T, Ctx>,
  451 + #ifdef BOOST_JSON_HAS_REFLECTION
  452 + mp11::mp_defer<annotated_category, T, Ctx>,
  453 + #endif // BOOST_JSON_HAS_REFLECTION
  454 + mp11::mp_defer<tag_invoke_with_context_category, T, Ctx, Dir>>;
  455 + };
  456 +
  457 + template< class Dir >
  458 + struct all_fallback_checks
  459 + {
  460 + template <class T>
  461 + using fn = mp11::mp_list<
  462 + mp11::mp_defer<use_category_helper, T, void>,
  463 + #ifdef BOOST_JSON_HAS_REFLECTION
  464 + mp11::mp_defer<annotated_category, T, void>,
  465 + #endif // BOOST_JSON_HAS_REFLECTION
  466 + mp11::mp_defer<tag_invoke_category, T, Dir>,
  467 + mp11::mp_defer<native_conversion_category, T>,
  468 + mp11::mp_defer<deduced_category, T>>;
  469 + };
  470 +
  471 + template <class T, class Ctx>
  472 + using direct_custom_checks = mp11::mp_list<
  473 + mp11::mp_defer<use_category_helper, T, Ctx>
  474 + #ifdef BOOST_JSON_HAS_REFLECTION
  475 + ,
  476 + mp11::mp_defer<annotated_category, T, Ctx>
  477 + #endif // BOOST_JSON_HAS_REFLECTION
  478 + >;
  479 +
  480 + template <class T>
  481 + using direct_fallback_checks = mp11::mp_list<
  482 + mp11::mp_defer<use_category_helper, T, void>,
  483 + #ifdef BOOST_JSON_HAS_REFLECTION
  484 + mp11::mp_defer<annotated_category, T, void>,
  485 + #endif // BOOST_JSON_HAS_REFLECTION
  486 + mp11::mp_defer<deduced_category, T>>;
  487 +
  488 + template <class>
  489 + using no_checks = mp11::mp_list<>;
382   490  
383   template< class T > 491   template< class T >
384   using nested_type = typename T::type; 492   using nested_type = typename T::type;
  493 +
385   template< class T1, class T2 > 494   template< class T1, class T2 >
386 - using conversion_category_impl_helper = mp11::mp_eval_if_not< 495 + using get_conversion_category_helper = mp11::mp_eval_if_c<
387 - std::is_same<detail::no_conversion_tag, T1>, 496 + conversion_category::unknown != T1::value,
388   T1, 497   T1,
389   mp11::mp_eval_or_q, T1, mp11::mp_quote<nested_type>, T2>; 498   mp11::mp_eval_or_q, T1, mp11::mp_quote<nested_type>, T2>;
390 - template< class Ctx, class T, class Dir > 499 +
391 - struct conversion_category_impl 500 + template<
  501 + class T,
  502 + class Ctx,
  503 + template<class, class> class CustomChecks,
  504 + template<class> class FallbackChecks>
  505 + struct get_conversion_category_impl
392   { 506   {
393 - using type = mp11::mp_fold< 507 + using type = std::integral_constant<
394 - mp11::mp_list< 508 + conversion_category,
395 - mp11::mp_defer<user_conversion_category, Ctx, T, Dir>, 509 + mp11::mp_fold<
396 - mp11::mp_defer<native_conversion_category, T>, 510 + mp11::mp_append< CustomChecks<T, Ctx>, FallbackChecks<T> >,
397 - mp11::mp_defer<generic_conversion_category, T>>, 511 + unknown_category,
398 - no_conversion_tag, 512 + get_conversion_category_helper>::value>;
399 - conversion_category_impl_helper>;  
400   }; 513   };
401 - template< class Ctx, class T, class Dir > 514 +
402 - using conversion_category = 515 + template<
403 - typename conversion_category_impl< Ctx, T, Dir >::type; 516 + class T,
  517 + class Ctx,
  518 + template<class, class> class CustomChecks,
  519 + template<class> class FallbackChecks>
  520 + using get_conversion_category = typename get_conversion_category_impl<
  521 + T, Ctx, CustomChecks, FallbackChecks>::type;
404   522  
405   template< class T > 523   template< class T >
406 - using any_conversion_tag = mp11::mp_not< 524 + using any_conversion_tag = mp11::mp_not< std::is_same<T, unknown_category> >;
407 - std::is_same< T, no_conversion_tag > >;  
408   525  
409 - template< class T, class Dir, class... Ctxs > 526 + template<
410 - struct conversion_category_impl< std::tuple<Ctxs...>, T, Dir > 527 + class T,
  528 + template<class, class> class CustomChecks,
  529 + template<class> class FallbackChecks,
  530 + class... Ctxs >
  531 + struct get_conversion_category_impl<
  532 + T, std::tuple<Ctxs...>, CustomChecks, FallbackChecks>
411   { 533   {
412   using ctxs = mp11::mp_list< remove_cvref<Ctxs>... >; 534   using ctxs = mp11::mp_list< remove_cvref<Ctxs>... >;
413   using cats = mp11::mp_list< 535   using cats = mp11::mp_list<
414 - conversion_category<remove_cvref<Ctxs>, T, Dir>... >; 536 + get_conversion_category<
  537 + T, remove_cvref<Ctxs>, CustomChecks, no_checks>... >;
415   538  
416 - template< class I > 539 + using custom_index = mp11::mp_find_if< cats, any_conversion_tag >;
417 - using exists = mp11::mp_less< I, mp11::mp_size<cats> >; 540 + using is_custom = mp11::mp_less< custom_index, mp11::mp_size<cats> >;
418   541  
419 - using context2 = mp11::mp_find< cats, full_context_conversion_tag >; 542 + using index = mp11::mp_if< is_custom, custom_index, mp11::mp_size_t<0> >;
420 - using context1 = mp11::mp_find< cats, context_conversion_tag >;  
421 - using context0 = mp11::mp_find< cats, user_conversion_tag >;  
422 - using index = mp11::mp_cond<  
423 - exists<context2>, context2,  
424 - exists<context1>, context1,  
425 - exists<context0>, context0,  
426 - mp11::mp_true, mp11::mp_find_if< cats, any_conversion_tag > >;  
427 - using type = mp11::mp_eval_or<  
428 - no_conversion_tag,  
429 - mp11::mp_at, cats, index >;  
430 - };  
431   543  
432 - struct no_context 544 + using fallback_cat = mp11::mp_fold<
433 - {}; 545 + FallbackChecks<T>, unknown_category, get_conversion_category_helper>;
  546 +
  547 + using type = std::integral_constant<
  548 + conversion_category,
  549 + mp11::mp_eval_if_not<
  550 + is_custom, fallback_cat, mp11::mp_at, cats, index>::value>;
  551 + };
434   552  
435   template <class T, class Dir> 553   template <class T, class Dir>
436   using can_convert = mp11::mp_not< 554   using can_convert = mp11::mp_not<
437   std::is_same< 555   std::is_same<
438 - detail::conversion_category<no_context, T, Dir>, 556 + get_conversion_category<
439 - detail::no_conversion_tag>>; 557 + T,
440 - 558 + no_context,
441 - template<class Impl1, class Impl2> 559 + all_custom_checks<Dir>::template fn,
442 - using conversion_round_trips_helper = mp11::mp_or< 560 + all_fallback_checks<Dir>::template fn>,
443 - std::is_same<Impl1, Impl2>, 561 + unknown_category>>;
444 - std::is_base_of<user_conversion_tag, Impl1>,  
445 - std::is_base_of<user_conversion_tag, Impl2>>;  
446 - template< class Ctx, class T, class Dir >  
447 - using conversion_round_trips = conversion_round_trips_helper<  
448 - conversion_category<Ctx, T, Dir>,  
449 - conversion_category<Ctx, T, mp11::mp_not<Dir>>>;  
450   562  
451   template< class T1, class T2 > 563   template< class T1, class T2 >
452   struct copy_cref_helper 564   struct copy_cref_helper
453   { 565   {
454   using type = remove_cvref<T2>; 566   using type = remove_cvref<T2>;
455   }; 567   };
456   template< class T1, class T2 > 568   template< class T1, class T2 >
457   using copy_cref = typename copy_cref_helper< T1, T2 >::type; 569   using copy_cref = typename copy_cref_helper< T1, T2 >::type;
458   570  
459   template< class T1, class T2 > 571   template< class T1, class T2 >
460   struct copy_cref_helper<T1 const, T2> 572   struct copy_cref_helper<T1 const, T2>
461   { 573   {
462   using type = remove_cvref<T2> const; 574   using type = remove_cvref<T2> const;
463   }; 575   };
464   template< class T1, class T2 > 576   template< class T1, class T2 >
465   struct copy_cref_helper<T1&, T2> 577   struct copy_cref_helper<T1&, T2>
466   { 578   {
467   using type = copy_cref<T1, T2>&; 579   using type = copy_cref<T1, T2>&;
468   }; 580   };
469   template< class T1, class T2 > 581   template< class T1, class T2 >
470   struct copy_cref_helper<T1&&, T2> 582   struct copy_cref_helper<T1&&, T2>
471   { 583   {
472   using type = copy_cref<T1, T2>&&; 584   using type = copy_cref<T1, T2>&&;
473   }; 585   };
474   586  
475   template< class Rng, class Traits > 587   template< class Rng, class Traits >
476   using forwarded_value_helper = mp11::mp_if< 588   using forwarded_value_helper = mp11::mp_if<
477   std::is_convertible< 589   std::is_convertible<
478   typename Traits::reference, 590   typename Traits::reference,
479   copy_cref<Rng, typename Traits::value_type> >, 591   copy_cref<Rng, typename Traits::value_type> >,
480   copy_cref<Rng, typename Traits::value_type>, 592   copy_cref<Rng, typename Traits::value_type>,
481   typename Traits::value_type >; 593   typename Traits::value_type >;
482   594  
483   template< class Rng > 595   template< class Rng >
484   using forwarded_value = forwarded_value_helper< 596   using forwarded_value = forwarded_value_helper<
485   Rng, iterator_traits< Rng > >; 597   Rng, iterator_traits< Rng > >;
486   598  
487   template< class Ctx, class T, class Dir > 599   template< class Ctx, class T, class Dir >
488   struct supported_context 600   struct supported_context
489   { 601   {
490   using type = Ctx; 602   using type = Ctx;
491   603  
492   static 604   static
493   type const& 605   type const&
HITCBC 494   32 get( Ctx const& ctx ) noexcept 606   32 get( Ctx const& ctx ) noexcept
495   { 607   {
HITCBC 496   32 return ctx; 608   32 return ctx;
497   } 609   }
498   }; 610   };
499   611  
500   template< class T, class Dir, class... Ctxs > 612   template< class T, class Dir, class... Ctxs >
501   struct supported_context< std::tuple<Ctxs...>, T, Dir > 613   struct supported_context< std::tuple<Ctxs...>, T, Dir >
502   { 614   {
503   using Ctx = std::tuple<Ctxs...>; 615   using Ctx = std::tuple<Ctxs...>;
504 - using impl = conversion_category_impl<Ctx, T, Dir>; 616 + using impl = get_conversion_category_impl<
  617 + T,
  618 + Ctx,
  619 + all_custom_checks<Dir>::template fn,
  620 + all_fallback_checks<Dir>::template fn>;
505   using index = typename impl::index; 621   using index = typename impl::index;
506   using next_supported = supported_context< 622   using next_supported = supported_context<
507   mp11::mp_at< typename impl::ctxs, index >, T, Dir >; 623   mp11::mp_at< typename impl::ctxs, index >, T, Dir >;
508   using type = typename next_supported::type; 624   using type = typename next_supported::type;
509   625  
510   static 626   static
511   type const& 627   type const&
HITCBC 512   19 get( Ctx const& ctx ) noexcept 628   19 get( Ctx const& ctx ) noexcept
513   { 629   {
HITCBC 514   19 return next_supported::get( std::get<index::value>( ctx ) ); 630   19 return next_supported::get( std::get<index::value>( ctx ) );
515   } 631   }
516   }; 632   };
517   633  
518   template< class T > 634   template< class T >
519   using value_result_type = typename std::decay< 635   using value_result_type = typename std::decay<
520   decltype( std::declval<T&>().value() )>::type; 636   decltype( std::declval<T&>().value() )>::type;
521   637  
522   template< class T > 638   template< class T >
523   using can_reset = decltype( std::declval<T&>().reset() ); 639   using can_reset = decltype( std::declval<T&>().reset() );
524   640  
525   template< class T > 641   template< class T >
526   using has_valueless_by_exception = 642   using has_valueless_by_exception =
527   decltype( std::declval<T const&>().valueless_by_exception() ); 643   decltype( std::declval<T const&>().valueless_by_exception() );
528   644  
529   } // namespace detail 645   } // namespace detail
530   646  
531   template <class T> 647   template <class T>
532   struct result_for<T, value> 648   struct result_for<T, value>
533   { 649   {
534   using type = system::result< detail::remove_cvref<T> >; 650   using type = system::result< detail::remove_cvref<T> >;
535   }; 651   };
536   652  
537   template<class T> 653   template<class T>
538   struct is_string_like 654   struct is_string_like
539   : std::is_convertible<T, string_view> 655   : std::is_convertible<T, string_view>
540   { }; 656   { };
541   657  
542   template<class T> 658   template<class T>
543   struct is_path_like 659   struct is_path_like
544   : mp11::mp_all< 660   : mp11::mp_all<
545   mp11::mp_valid_and_true<detail::is_its_own_value, T>, 661   mp11::mp_valid_and_true<detail::is_its_own_value, T>,
546   mp11::mp_valid_and_true<detail::has_string_type, T>> 662   mp11::mp_valid_and_true<detail::has_string_type, T>>
547   { }; 663   { };
548   template<class T> 664   template<class T>
549   struct is_sequence_like 665   struct is_sequence_like
550   : mp11::mp_all< 666   : mp11::mp_all<
551   mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>, 667   mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
552   mp11::mp_valid_and_true<detail::not_its_own_value, T>, 668   mp11::mp_valid_and_true<detail::not_its_own_value, T>,
553   mp11::mp_valid<detail::begin_iterator_category, T>> 669   mp11::mp_valid<detail::begin_iterator_category, T>>
554   { }; 670   { };
555   671  
556   template<class T> 672   template<class T>
557   struct is_map_like 673   struct is_map_like
558   : mp11::mp_all< 674   : mp11::mp_all<
559   is_sequence_like<T>, 675   is_sequence_like<T>,
560   mp11::mp_valid_and_true<detail::is_value_type_pair, T>, 676   mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
561   is_string_like<detail::key_type<T>>, 677   is_string_like<detail::key_type<T>>,
562   mp11::mp_valid_and_true<detail::has_unique_keys, T>> 678   mp11::mp_valid_and_true<detail::has_unique_keys, T>>
563   { }; 679   { };
564   680  
565   template<class T> 681   template<class T>
566   struct is_tuple_like 682   struct is_tuple_like
567   : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T> 683   : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
568   { }; 684   { };
569   685  
570   template<> 686   template<>
571   struct is_null_like<std::nullptr_t> 687   struct is_null_like<std::nullptr_t>
572   : std::true_type 688   : std::true_type
573   { }; 689   { };
574   690  
575   #ifndef BOOST_NO_CXX17_HDR_VARIANT 691   #ifndef BOOST_NO_CXX17_HDR_VARIANT
576   template<> 692   template<>
577   struct is_null_like<std::monostate> 693   struct is_null_like<std::monostate>
578   : std::true_type 694   : std::true_type
579   { }; 695   { };
580   #endif // BOOST_NO_CXX17_HDR_VARIANT 696   #endif // BOOST_NO_CXX17_HDR_VARIANT
581   697  
582   template<class T> 698   template<class T>
583   struct is_described_class 699   struct is_described_class
584   : mp11::mp_and< 700   : mp11::mp_and<
585   describe::has_describe_members<T>, 701   describe::has_describe_members<T>,
586   mp11::mp_not< std::is_union<T> >, 702   mp11::mp_not< std::is_union<T> >,
587   mp11::mp_empty< 703   mp11::mp_empty<
588   mp11::mp_eval_or< 704   mp11::mp_eval_or<
589   mp11::mp_list<>, detail::described_non_public_members, T>>> 705   mp11::mp_list<>, detail::described_non_public_members, T>>>
590   { }; 706   { };
591   707  
592   template<class T> 708   template<class T>
593   struct is_described_enum 709   struct is_described_enum
594   : describe::has_describe_enumerators<T> 710   : describe::has_describe_enumerators<T>
595   { }; 711   { };
596   712  
597   template<class T> 713   template<class T>
598   struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T> 714   struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T>
599   { }; 715   { };
600   716  
601   template<class T> 717   template<class T>
602   struct is_optional_like 718   struct is_optional_like
603   : mp11::mp_and< 719   : mp11::mp_and<
604   mp11::mp_not<std::is_void< 720   mp11::mp_not<std::is_void<
605   mp11::mp_eval_or<void, detail::value_result_type, T>>>, 721   mp11::mp_eval_or<void, detail::value_result_type, T>>>,
606   mp11::mp_valid<detail::can_reset, T>> 722   mp11::mp_valid<detail::can_reset, T>>
607   { }; 723   { };
608   724  
609   } // namespace json 725   } // namespace json
610   } // namespace boost 726   } // namespace boost
611   727  
612   #endif // BOOST_JSON_IMPL_CONVERSION_HPP 728   #endif // BOOST_JSON_IMPL_CONVERSION_HPP