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/value.hpp>
 
15  
#include <boost/json/fwd.hpp>
14  
#include <boost/json/fwd.hpp>
16  
#include <boost/json/string_view.hpp>
15  
#include <boost/json/string_view.hpp>
17  
#include <boost/describe/enumerators.hpp>
16  
#include <boost/describe/enumerators.hpp>
18  
#include <boost/describe/members.hpp>
17  
#include <boost/describe/members.hpp>
19  
#include <boost/describe/bases.hpp>
18  
#include <boost/describe/bases.hpp>
20  
#include <boost/mp11/algorithm.hpp>
19  
#include <boost/mp11/algorithm.hpp>
21  
#include <boost/mp11/utility.hpp>
20  
#include <boost/mp11/utility.hpp>
22  
#include <boost/system/result.hpp>
21  
#include <boost/system/result.hpp>
23  

22  

24  
#include <iterator>
23  
#include <iterator>
25  
#include <tuple>
24  
#include <tuple>
26  
#include <utility>
25  
#include <utility>
27  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
26  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
28  
# include <variant>
27  
# include <variant>
29  
#endif // BOOST_NO_CXX17_HDR_VARIANT
28  
#endif // BOOST_NO_CXX17_HDR_VARIANT
30  

29  

31  
namespace boost {
30  
namespace boost {
32  
namespace json {
31  
namespace json {
 
32 +

 
33 +
class value_ref;
 
34 +

33  
namespace detail {
35  
namespace detail {
34  

36  

35  
#ifdef __cpp_lib_nonmember_container_access
37  
#ifdef __cpp_lib_nonmember_container_access
36  
using std::size;
38  
using std::size;
37  
#endif
39  
#endif
38  

40  

39  
template<std::size_t I, class T>
41  
template<std::size_t I, class T>
40  
using tuple_element_t = typename std::tuple_element<I, T>::type;
42  
using tuple_element_t = typename std::tuple_element<I, T>::type;
41  

43  

42  
template<class T>
44  
template<class T>
43  
using iterator_type = decltype(std::begin(std::declval<T&>()));
45  
using iterator_type = decltype(std::begin(std::declval<T&>()));
44  
template<class T>
46  
template<class T>
45  
using iterator_traits = std::iterator_traits< iterator_type<T> >;
47  
using iterator_traits = std::iterator_traits< iterator_type<T> >;
46  

48  

47  
template<class T>
49  
template<class T>
48  
using value_type = typename iterator_traits<T>::value_type;
50  
using value_type = typename iterator_traits<T>::value_type;
49  
template<class T>
51  
template<class T>
50  
using mapped_type = tuple_element_t< 1, value_type<T> >;
52  
using mapped_type = tuple_element_t< 1, value_type<T> >;
51  

53  

52  
// had to make the metafunction always succeeding in order to make it work
54  
// had to make the metafunction always succeeding in order to make it work
53  
// with msvc 14.0
55  
// with msvc 14.0
54  
template<class T>
56  
template<class T>
55  
using key_type_helper = tuple_element_t< 0, value_type<T> >;
57  
using key_type_helper = tuple_element_t< 0, value_type<T> >;
56  
template<class T>
58  
template<class T>
57  
using key_type = mp11::mp_eval_or<
59  
using key_type = mp11::mp_eval_or<
58  
    void,
60  
    void,
59  
    key_type_helper,
61  
    key_type_helper,
60  
    T>;
62  
    T>;
61  

63  

62  
template<class T>
64  
template<class T>
63  
using are_begin_and_end_same = std::is_same<
65  
using are_begin_and_end_same = std::is_same<
64  
    iterator_type<T>,
66  
    iterator_type<T>,
65  
    decltype(std::end(std::declval<T&>()))>;
67  
    decltype(std::end(std::declval<T&>()))>;
66  

68  

67  
// msvc 14.0 gets confused when std::is_same is used directly
69  
// msvc 14.0 gets confused when std::is_same is used directly
68  
template<class A, class B>
70  
template<class A, class B>
69  
using is_same_msvc_140 = std::is_same<A, B>;
71  
using is_same_msvc_140 = std::is_same<A, B>;
70  
template<class T>
72  
template<class T>
71  
using is_its_own_value = is_same_msvc_140<value_type<T>, T>;
73  
using is_its_own_value = is_same_msvc_140<value_type<T>, T>;
72  

74  

73  
template<class T>
75  
template<class T>
74  
using not_its_own_value = mp11::mp_not< is_its_own_value<T> >;
76  
using not_its_own_value = mp11::mp_not< is_its_own_value<T> >;
75  

77  

76  
template<class T>
78  
template<class T>
77  
using begin_iterator_category = typename std::iterator_traits<
79  
using begin_iterator_category = typename std::iterator_traits<
78  
    iterator_type<T>>::iterator_category;
80  
    iterator_type<T>>::iterator_category;
79  

81  

80  
template<class T>
82  
template<class T>
81  
using has_positive_tuple_size = mp11::mp_bool<
83  
using has_positive_tuple_size = mp11::mp_bool<
82  
    (std::tuple_size<T>::value > 0) >;
84  
    (std::tuple_size<T>::value > 0) >;
83  

85  

84  
template<class T>
86  
template<class T>
85  
using has_unique_keys = has_positive_tuple_size<decltype(
87  
using has_unique_keys = has_positive_tuple_size<decltype(
86  
    std::declval<T&>().emplace(
88  
    std::declval<T&>().emplace(
87  
        std::declval<value_type<T>>()))>;
89  
        std::declval<value_type<T>>()))>;
88  

90  

89  
template<class T>
91  
template<class T>
90  
using has_string_type = std::is_same<
92  
using has_string_type = std::is_same<
91  
    typename T::string_type, std::basic_string<typename T::value_type> >;
93  
    typename T::string_type, std::basic_string<typename T::value_type> >;
92  

94  

93  
template<class T>
95  
template<class T>
94  
struct is_value_type_pair_helper : std::false_type
96  
struct is_value_type_pair_helper : std::false_type
95  
{ };
97  
{ };
96  
template<class T1, class T2>
98  
template<class T1, class T2>
97  
struct is_value_type_pair_helper<std::pair<T1, T2>> : std::true_type
99  
struct is_value_type_pair_helper<std::pair<T1, T2>> : std::true_type
98  
{ };
100  
{ };
99  
template<class T>
101  
template<class T>
100  
using is_value_type_pair = is_value_type_pair_helper<value_type<T>>;
102  
using is_value_type_pair = is_value_type_pair_helper<value_type<T>>;
101  

103  

102  
template<class T>
104  
template<class T>
103  
using has_size_member_helper
105  
using has_size_member_helper
104  
    = std::is_convertible<decltype(std::declval<T&>().size()), std::size_t>;
106  
    = std::is_convertible<decltype(std::declval<T&>().size()), std::size_t>;
105  
template<class T>
107  
template<class T>
106  
using has_size_member = mp11::mp_valid_and_true<has_size_member_helper, T>;
108  
using has_size_member = mp11::mp_valid_and_true<has_size_member_helper, T>;
107  
template<class T>
109  
template<class T>
108  
using has_free_size_helper
110  
using has_free_size_helper
109  
    = std::is_convertible<
111  
    = std::is_convertible<
110  
        decltype(size(std::declval<T const&>())),
112  
        decltype(size(std::declval<T const&>())),
111  
        std::size_t>;
113  
        std::size_t>;
112  
template<class T>
114  
template<class T>
113  
using has_free_size = mp11::mp_valid_and_true<has_free_size_helper, T>;
115  
using has_free_size = mp11::mp_valid_and_true<has_free_size_helper, T>;
114  
template<class T>
116  
template<class T>
115  
using size_implementation = mp11::mp_cond<
117  
using size_implementation = mp11::mp_cond<
116  
    has_size_member<T>, mp11::mp_int<3>,
118  
    has_size_member<T>, mp11::mp_int<3>,
117  
    has_free_size<T>,   mp11::mp_int<2>,
119  
    has_free_size<T>,   mp11::mp_int<2>,
118  
    std::is_array<T>,   mp11::mp_int<1>,
120  
    std::is_array<T>,   mp11::mp_int<1>,
119  
    mp11::mp_true,      mp11::mp_int<0>>;
121  
    mp11::mp_true,      mp11::mp_int<0>>;
120  

122  

121  
template<class T>
123  
template<class T>
122  
std::size_t
124  
std::size_t
123  
try_size(T&& cont, mp11::mp_int<3>)
125  
try_size(T&& cont, mp11::mp_int<3>)
124  
{
126  
{
125  
    return cont.size();
127  
    return cont.size();
126  
}
128  
}
127  

129  

128  
template<class T>
130  
template<class T>
129  
std::size_t
131  
std::size_t
130  
try_size(T& cont, mp11::mp_int<2>)
132  
try_size(T& cont, mp11::mp_int<2>)
131  
{
133  
{
132  
    return size(cont);
134  
    return size(cont);
133  
}
135  
}
134  

136  

135  
template<class T, std::size_t N>
137  
template<class T, std::size_t N>
136  
std::size_t
138  
std::size_t
137  
try_size(T(&)[N], mp11::mp_int<1>)
139  
try_size(T(&)[N], mp11::mp_int<1>)
138  
{
140  
{
139  
    return N;
141  
    return N;
140  
}
142  
}
141  

143  

142  
template<class T>
144  
template<class T>
143  
std::size_t
145  
std::size_t
144  
try_size(T&, mp11::mp_int<0>)
146  
try_size(T&, mp11::mp_int<0>)
145  
{
147  
{
146  
    return 0;
148  
    return 0;
147  
}
149  
}
148  

150  

149  
template<class T>
151  
template<class T>
150  
using has_push_back_helper
152  
using has_push_back_helper
151  
    = decltype(std::declval<T&>().push_back(std::declval<value_type<T>>()));
153  
    = decltype(std::declval<T&>().push_back(std::declval<value_type<T>>()));
152  
template<class T>
154  
template<class T>
153  
using has_push_back = mp11::mp_valid<has_push_back_helper, T>;
155  
using has_push_back = mp11::mp_valid<has_push_back_helper, T>;
154  
template<class T>
156  
template<class T>
155  
using inserter_implementation = mp11::mp_cond<
157  
using inserter_implementation = mp11::mp_cond<
156  
    is_tuple_like<T>, mp11::mp_int<2>,
158  
    is_tuple_like<T>, mp11::mp_int<2>,
157  
    has_push_back<T>, mp11::mp_int<1>,
159  
    has_push_back<T>, mp11::mp_int<1>,
158  
    mp11::mp_true,    mp11::mp_int<0>>;
160  
    mp11::mp_true,    mp11::mp_int<0>>;
159  

161  

160  
template<class T>
162  
template<class T>
161  
iterator_type<T>
163  
iterator_type<T>
162  
inserter(
164  
inserter(
163  
    T& target,
165  
    T& target,
164  
    mp11::mp_int<2>)
166  
    mp11::mp_int<2>)
165  
{
167  
{
166  
    return target.begin();
168  
    return target.begin();
167  
}
169  
}
168  

170  

169  
template<class T>
171  
template<class T>
170  
std::back_insert_iterator<T>
172  
std::back_insert_iterator<T>
171  
inserter(
173  
inserter(
172  
    T& target,
174  
    T& target,
173  
    mp11::mp_int<1>)
175  
    mp11::mp_int<1>)
174  
{
176  
{
175  
    return std::back_inserter(target);
177  
    return std::back_inserter(target);
176  
}
178  
}
177  

179  

178  
template<class T>
180  
template<class T>
179  
std::insert_iterator<T>
181  
std::insert_iterator<T>
180  
inserter(
182  
inserter(
181  
    T& target,
183  
    T& target,
182  
    mp11::mp_int<0>)
184  
    mp11::mp_int<0>)
183  
{
185  
{
184  
    return std::inserter( target, target.end() );
186  
    return std::inserter( target, target.end() );
185  
}
187  
}
186  

188  

187  
using value_from_conversion = mp11::mp_true;
189  
using value_from_conversion = mp11::mp_true;
188  
using value_to_conversion = mp11::mp_false;
190  
using value_to_conversion = mp11::mp_false;
189  

191  

190  
struct user_conversion_tag { };
192  
struct user_conversion_tag { };
191  
struct context_conversion_tag : user_conversion_tag { };
193  
struct context_conversion_tag : user_conversion_tag { };
192  
struct full_context_conversion_tag : context_conversion_tag { };
194  
struct full_context_conversion_tag : context_conversion_tag { };
193  
struct native_conversion_tag { };
195  
struct native_conversion_tag { };
194  
struct value_conversion_tag : native_conversion_tag { };
196  
struct value_conversion_tag : native_conversion_tag { };
195  
struct object_conversion_tag : native_conversion_tag { };
197  
struct object_conversion_tag : native_conversion_tag { };
196  
struct array_conversion_tag : native_conversion_tag { };
198  
struct array_conversion_tag : native_conversion_tag { };
197  
struct string_conversion_tag : native_conversion_tag { };
199  
struct string_conversion_tag : native_conversion_tag { };
198  
struct bool_conversion_tag : native_conversion_tag { };
200  
struct bool_conversion_tag : native_conversion_tag { };
 
201 +
struct value_ref_tag : native_conversion_tag { };
199  
struct number_conversion_tag : native_conversion_tag { };
202  
struct number_conversion_tag : native_conversion_tag { };
200  
struct integral_conversion_tag : number_conversion_tag { };
203  
struct integral_conversion_tag : number_conversion_tag { };
201  
struct floating_point_conversion_tag : number_conversion_tag { };
204  
struct floating_point_conversion_tag : number_conversion_tag { };
202  
struct null_like_conversion_tag { };
205  
struct null_like_conversion_tag { };
203  
struct string_like_conversion_tag { };
206  
struct string_like_conversion_tag { };
204  
struct map_like_conversion_tag { };
207  
struct map_like_conversion_tag { };
205  
struct path_conversion_tag { };
208  
struct path_conversion_tag { };
206  
struct sequence_conversion_tag { };
209  
struct sequence_conversion_tag { };
207  
struct tuple_conversion_tag { };
210  
struct tuple_conversion_tag { };
208  
struct described_class_conversion_tag { };
211  
struct described_class_conversion_tag { };
209  
struct described_enum_conversion_tag { };
212  
struct described_enum_conversion_tag { };
210  
struct variant_conversion_tag { };
213  
struct variant_conversion_tag { };
211  
struct optional_conversion_tag { };
214  
struct optional_conversion_tag { };
212  
struct no_conversion_tag { };
215  
struct no_conversion_tag { };
213  

216  

214  
template<class... Args>
217  
template<class... Args>
215  
using supports_tag_invoke = decltype(tag_invoke( std::declval<Args>()... ));
218  
using supports_tag_invoke = decltype(tag_invoke( std::declval<Args>()... ));
216  

219  

217  
template<class T>
220  
template<class T>
218  
using has_user_conversion_from_impl = supports_tag_invoke<
221  
using has_user_conversion_from_impl = supports_tag_invoke<
219  
    value_from_tag, value&, T&& >;
222  
    value_from_tag, value&, T&& >;
220  
template<class T>
223  
template<class T>
221  
using has_user_conversion_to_impl = supports_tag_invoke<
224  
using has_user_conversion_to_impl = supports_tag_invoke<
222  
    value_to_tag<T>, value const& >;
225  
    value_to_tag<T>, value const& >;
223  
template<class T>
226  
template<class T>
224  
using has_nonthrowing_user_conversion_to_impl = supports_tag_invoke<
227  
using has_nonthrowing_user_conversion_to_impl = supports_tag_invoke<
225  
    try_value_to_tag<T>, value const& >;
228  
    try_value_to_tag<T>, value const& >;
226  
template< class T, class Dir >
229  
template< class T, class Dir >
227  
using has_user_conversion1 = mp11::mp_if<
230  
using has_user_conversion1 = mp11::mp_if<
228  
    std::is_same<Dir, value_from_conversion>,
231  
    std::is_same<Dir, value_from_conversion>,
229  
    mp11::mp_valid<has_user_conversion_from_impl, T>,
232  
    mp11::mp_valid<has_user_conversion_from_impl, T>,
230  
    mp11::mp_or<
233  
    mp11::mp_or<
231  
        mp11::mp_valid<has_user_conversion_to_impl, T>,
234  
        mp11::mp_valid<has_user_conversion_to_impl, T>,
232  
        mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>>>;
235  
        mp11::mp_valid<has_nonthrowing_user_conversion_to_impl, T>>>;
233  

236  

234  
template< class Ctx, class T >
237  
template< class Ctx, class T >
235  
using has_context_conversion_from_impl = supports_tag_invoke<
238  
using has_context_conversion_from_impl = supports_tag_invoke<
236  
    value_from_tag, value&, T&&, Ctx const& >;
239  
    value_from_tag, value&, T&&, Ctx const& >;
237  
template< class Ctx, class T >
240  
template< class Ctx, class T >
238  
using has_context_conversion_to_impl = supports_tag_invoke<
241  
using has_context_conversion_to_impl = supports_tag_invoke<
239  
    value_to_tag<T>, value const&, Ctx const& >;
242  
    value_to_tag<T>, value const&, Ctx const& >;
240  
template< class Ctx, class T >
243  
template< class Ctx, class T >
241  
using has_nonthrowing_context_conversion_to_impl = supports_tag_invoke<
244  
using has_nonthrowing_context_conversion_to_impl = supports_tag_invoke<
242  
    try_value_to_tag<T>, value const&, Ctx const& >;
245  
    try_value_to_tag<T>, value const&, Ctx const& >;
243  
template< class Ctx, class T, class Dir >
246  
template< class Ctx, class T, class Dir >
244  
using has_user_conversion2 = mp11::mp_if<
247  
using has_user_conversion2 = mp11::mp_if<
245  
    std::is_same<Dir, value_from_conversion>,
248  
    std::is_same<Dir, value_from_conversion>,
246  
    mp11::mp_valid<has_context_conversion_from_impl, Ctx, T>,
249  
    mp11::mp_valid<has_context_conversion_from_impl, Ctx, T>,
247  
    mp11::mp_or<
250  
    mp11::mp_or<
248  
        mp11::mp_valid<has_context_conversion_to_impl, Ctx, T>,
251  
        mp11::mp_valid<has_context_conversion_to_impl, Ctx, T>,
249  
        mp11::mp_valid<has_nonthrowing_context_conversion_to_impl, Ctx, T>>>;
252  
        mp11::mp_valid<has_nonthrowing_context_conversion_to_impl, Ctx, T>>>;
250  

253  

251  
template< class Ctx, class T >
254  
template< class Ctx, class T >
252  
using has_full_context_conversion_from_impl = supports_tag_invoke<
255  
using has_full_context_conversion_from_impl = supports_tag_invoke<
253  
    value_from_tag, value&, T&&, Ctx const&, Ctx const& >;
256  
    value_from_tag, value&, T&&, Ctx const&, Ctx const& >;
254  
template< class Ctx, class T >
257  
template< class Ctx, class T >
255  
using has_full_context_conversion_to_impl = supports_tag_invoke<
258  
using has_full_context_conversion_to_impl = supports_tag_invoke<
256  
    value_to_tag<T>, value const&, Ctx const&,  Ctx const& >;
259  
    value_to_tag<T>, value const&, Ctx const&,  Ctx const& >;
257  
template< class Ctx, class T >
260  
template< class Ctx, class T >
258  
using has_nonthrowing_full_context_conversion_to_impl = supports_tag_invoke<
261  
using has_nonthrowing_full_context_conversion_to_impl = supports_tag_invoke<
259  
    try_value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
262  
    try_value_to_tag<T>, value const&, Ctx const&, Ctx const& >;
260  
template< class Ctx, class T, class Dir >
263  
template< class Ctx, class T, class Dir >
261  
using has_user_conversion3 = mp11::mp_if<
264  
using has_user_conversion3 = mp11::mp_if<
262  
    std::is_same<Dir, value_from_conversion>,
265  
    std::is_same<Dir, value_from_conversion>,
263  
    mp11::mp_valid<has_full_context_conversion_from_impl, Ctx, T>,
266  
    mp11::mp_valid<has_full_context_conversion_from_impl, Ctx, T>,
264  
    mp11::mp_or<
267  
    mp11::mp_or<
265  
        mp11::mp_valid<has_full_context_conversion_to_impl, Ctx, T>,
268  
        mp11::mp_valid<has_full_context_conversion_to_impl, Ctx, T>,
266  
        mp11::mp_valid<
269  
        mp11::mp_valid<
267  
            has_nonthrowing_full_context_conversion_to_impl, Ctx, T>>>;
270  
            has_nonthrowing_full_context_conversion_to_impl, Ctx, T>>>;
268  

271  

269  
template< class T >
272  
template< class T >
270  
using described_non_public_members = describe::describe_members<
273  
using described_non_public_members = describe::describe_members<
271  
    T,
274  
    T,
272  
    describe::mod_private
275  
    describe::mod_private
273  
        | describe::mod_protected
276  
        | describe::mod_protected
274  
        | boost::describe::mod_inherited>;
277  
        | boost::describe::mod_inherited>;
275  

278  

276  
#if defined(BOOST_MSVC) && BOOST_MSVC < 1920
279  
#if defined(BOOST_MSVC) && BOOST_MSVC < 1920
277  

280  

278  
template< class T >
281  
template< class T >
279  
struct described_member_t_impl;
282  
struct described_member_t_impl;
280  

283  

281  
template< class T, class C >
284  
template< class T, class C >
282  
struct described_member_t_impl<T C::*>
285  
struct described_member_t_impl<T C::*>
283  
{
286  
{
284  
    using type = T;
287  
    using type = T;
285  
};
288  
};
286  

289  

287  
template< class T, class D >
290  
template< class T, class D >
288  
using described_member_t = remove_cvref<
291  
using described_member_t = remove_cvref<
289  
    typename described_member_t_impl<
292  
    typename described_member_t_impl<
290  
        remove_cvref<decltype(D::pointer)> >::type>;
293  
        remove_cvref<decltype(D::pointer)> >::type>;
291  

294  

292  
#else
295  
#else
293  

296  

294  
template< class T, class D >
297  
template< class T, class D >
295  
using described_member_t = remove_cvref<decltype(
298  
using described_member_t = remove_cvref<decltype(
296  
    std::declval<T&>().* D::pointer )>;
299  
    std::declval<T&>().* D::pointer )>;
297  

300  

298  
#endif
301  
#endif
299  

302  

300  
template< class T >
303  
template< class T >
301  
using described_members = describe::describe_members<
304  
using described_members = describe::describe_members<
302  
    T, describe::mod_any_access | describe::mod_inherited>;
305  
    T, describe::mod_any_access | describe::mod_inherited>;
303  

306  

304  
#ifdef BOOST_DESCRIBE_CXX14
307  
#ifdef BOOST_DESCRIBE_CXX14
305  

308  

306  
constexpr
309  
constexpr
307  
bool
310  
bool
308  
compare_strings(char const* l, char const* r)
311  
compare_strings(char const* l, char const* r)
309  
{
312  
{
310  
#if defined(_MSC_VER) && (_MSC_VER <= 1900) && !defined(__clang__)
313  
#if defined(_MSC_VER) && (_MSC_VER <= 1900) && !defined(__clang__)
311  
    return *l == *r && ( (*l == 0) | compare_strings(l + 1, r + 1) );
314  
    return *l == *r && ( (*l == 0) | compare_strings(l + 1, r + 1) );
312  
#else
315  
#else
313  
    do
316  
    do
314  
    {
317  
    {
315  
        if( *l != *r )
318  
        if( *l != *r )
316  
            return false;
319  
            return false;
317  
        if( *l == 0 )
320  
        if( *l == 0 )
318  
            return true;
321  
            return true;
319  
        ++l;
322  
        ++l;
320  
        ++r;
323  
        ++r;
321  
    } while(true);
324  
    } while(true);
322  
#endif
325  
#endif
323  
}
326  
}
324  

327  

325  
template< class L, class R >
328  
template< class L, class R >
326  
struct equal_member_names
329  
struct equal_member_names
327  
    : mp11::mp_bool< compare_strings(L::name, R::name) >
330  
    : mp11::mp_bool< compare_strings(L::name, R::name) >
328  
{};
331  
{};
329  

332  

330  
template< class T >
333  
template< class T >
331  
using uniquely_named_members = mp11::mp_same<
334  
using uniquely_named_members = mp11::mp_same<
332  
    mp11::mp_unique_if< described_members<T>, equal_member_names >,
335  
    mp11::mp_unique_if< described_members<T>, equal_member_names >,
333  
    described_members<T> >;
336  
    described_members<T> >;
334  

337  

335  
#else
338  
#else
336  

339  

337  
// we only check this in C++14, but the template should exist nevertheless
340  
// we only check this in C++14, but the template should exist nevertheless
338  
template< class T >
341  
template< class T >
339  
using uniquely_named_members = std::true_type;
342  
using uniquely_named_members = std::true_type;
340  

343  

341  
#endif // BOOST_DESCRIBE_CXX14
344  
#endif // BOOST_DESCRIBE_CXX14
342  

345  

343  
// user conversion (via tag_invoke)
346  
// user conversion (via tag_invoke)
344  
template< class Ctx, class T, class Dir >
347  
template< class Ctx, class T, class Dir >
345  
using user_conversion_category = mp11::mp_cond<
348  
using user_conversion_category = mp11::mp_cond<
346  
    has_user_conversion3<Ctx, T, Dir>, full_context_conversion_tag,
349  
    has_user_conversion3<Ctx, T, Dir>, full_context_conversion_tag,
347  
    has_user_conversion2<Ctx, T, Dir>, context_conversion_tag,
350  
    has_user_conversion2<Ctx, T, Dir>, context_conversion_tag,
348  
    has_user_conversion1<T, Dir>,      user_conversion_tag>;
351  
    has_user_conversion1<T, Dir>,      user_conversion_tag>;
349  

352  

350  
// native conversions (constructors and member functions of value)
353  
// native conversions (constructors and member functions of value)
351  
template< class T >
354  
template< class T >
352  
using native_conversion_category = mp11::mp_cond<
355  
using native_conversion_category = mp11::mp_cond<
353  
    std::is_same<T, value>,  value_conversion_tag,
356  
    std::is_same<T, value>,  value_conversion_tag,
354  
    std::is_same<T, array>,  array_conversion_tag,
357  
    std::is_same<T, array>,  array_conversion_tag,
355  
    std::is_same<T, object>, object_conversion_tag,
358  
    std::is_same<T, object>, object_conversion_tag,
356  
    std::is_same<T, string>, string_conversion_tag>;
359  
    std::is_same<T, string>, string_conversion_tag>;
357  

360  

358  
// generic conversions
361  
// generic conversions
359  
template< class T >
362  
template< class T >
360  
using generic_conversion_category = mp11::mp_cond<
363  
using generic_conversion_category = mp11::mp_cond<
 
364 +
    // std::is_same<T,std::initializer_list<value_ref>>, init_list_tag,
 
365 +
    std::is_same<T, value_ref>, value_ref_tag,
 
366 +

361  
    std::is_same<T, bool>,     bool_conversion_tag,
367  
    std::is_same<T, bool>,     bool_conversion_tag,
362  
    std::is_integral<T>,       integral_conversion_tag,
368  
    std::is_integral<T>,       integral_conversion_tag,
363  
    std::is_floating_point<T>, floating_point_conversion_tag,
369  
    std::is_floating_point<T>, floating_point_conversion_tag,
364  
    is_null_like<T>,           null_like_conversion_tag,
370  
    is_null_like<T>,           null_like_conversion_tag,
365  
    is_string_like<T>,         string_like_conversion_tag,
371  
    is_string_like<T>,         string_like_conversion_tag,
366  
    is_variant_like<T>,        variant_conversion_tag,
372  
    is_variant_like<T>,        variant_conversion_tag,
367  
    is_optional_like<T>,       optional_conversion_tag,
373  
    is_optional_like<T>,       optional_conversion_tag,
368  
    is_map_like<T>,            map_like_conversion_tag,
374  
    is_map_like<T>,            map_like_conversion_tag,
369  
    is_sequence_like<T>,       sequence_conversion_tag,
375  
    is_sequence_like<T>,       sequence_conversion_tag,
370  
    is_tuple_like<T>,          tuple_conversion_tag,
376  
    is_tuple_like<T>,          tuple_conversion_tag,
371  
    is_described_class<T>,     described_class_conversion_tag,
377  
    is_described_class<T>,     described_class_conversion_tag,
372  
    is_described_enum<T>,      described_enum_conversion_tag,
378  
    is_described_enum<T>,      described_enum_conversion_tag,
373  
    is_path_like<T>,           path_conversion_tag,
379  
    is_path_like<T>,           path_conversion_tag,
374  
    // failed to find a suitable implementation
380  
    // failed to find a suitable implementation
375  
    mp11::mp_true,             no_conversion_tag>;
381  
    mp11::mp_true,             no_conversion_tag>;
376  

382  

377  
template< class T >
383  
template< class T >
378  
using nested_type = typename T::type;
384  
using nested_type = typename T::type;
379  
template< class T1, class T2 >
385  
template< class T1, class T2 >
380  
using conversion_category_impl_helper = mp11::mp_eval_if_not<
386  
using conversion_category_impl_helper = mp11::mp_eval_if_not<
381  
    std::is_same<detail::no_conversion_tag, T1>,
387  
    std::is_same<detail::no_conversion_tag, T1>,
382  
    T1,
388  
    T1,
383  
    mp11::mp_eval_or_q, T1, mp11::mp_quote<nested_type>, T2>;
389  
    mp11::mp_eval_or_q, T1, mp11::mp_quote<nested_type>, T2>;
384  
template< class Ctx, class T, class Dir >
390  
template< class Ctx, class T, class Dir >
385  
struct conversion_category_impl
391  
struct conversion_category_impl
386  
{
392  
{
387  
    using type = mp11::mp_fold<
393  
    using type = mp11::mp_fold<
388  
        mp11::mp_list<
394  
        mp11::mp_list<
389  
            mp11::mp_defer<user_conversion_category, Ctx, T, Dir>,
395  
            mp11::mp_defer<user_conversion_category, Ctx, T, Dir>,
390  
            mp11::mp_defer<native_conversion_category, T>,
396  
            mp11::mp_defer<native_conversion_category, T>,
391  
            mp11::mp_defer<generic_conversion_category, T>>,
397  
            mp11::mp_defer<generic_conversion_category, T>>,
392  
        no_conversion_tag,
398  
        no_conversion_tag,
393  
        conversion_category_impl_helper>;
399  
        conversion_category_impl_helper>;
394  
};
400  
};
395  
template< class Ctx, class T, class Dir >
401  
template< class Ctx, class T, class Dir >
396  
using conversion_category =
402  
using conversion_category =
397  
    typename conversion_category_impl< Ctx, T, Dir >::type;
403  
    typename conversion_category_impl< Ctx, T, Dir >::type;
398  

404  

399  
template< class T >
405  
template< class T >
400  
using any_conversion_tag = mp11::mp_not<
406  
using any_conversion_tag = mp11::mp_not<
401  
    std::is_same< T, no_conversion_tag > >;
407  
    std::is_same< T, no_conversion_tag > >;
402  

408  

403  
template< class T, class Dir, class... Ctxs >
409  
template< class T, class Dir, class... Ctxs >
404  
struct conversion_category_impl< std::tuple<Ctxs...>, T, Dir >
410  
struct conversion_category_impl< std::tuple<Ctxs...>, T, Dir >
405  
{
411  
{
406  
    using ctxs = mp11::mp_list< remove_cvref<Ctxs>... >;
412  
    using ctxs = mp11::mp_list< remove_cvref<Ctxs>... >;
407  
    using cats = mp11::mp_list<
413  
    using cats = mp11::mp_list<
408  
        conversion_category<remove_cvref<Ctxs>, T, Dir>... >;
414  
        conversion_category<remove_cvref<Ctxs>, T, Dir>... >;
409  

415  

410  
    template< class I >
416  
    template< class I >
411  
    using exists = mp11::mp_less< I, mp11::mp_size<cats> >;
417  
    using exists = mp11::mp_less< I, mp11::mp_size<cats> >;
412  

418  

413  
    using context2 = mp11::mp_find< cats, full_context_conversion_tag >;
419  
    using context2 = mp11::mp_find< cats, full_context_conversion_tag >;
414  
    using context1 = mp11::mp_find< cats, context_conversion_tag >;
420  
    using context1 = mp11::mp_find< cats, context_conversion_tag >;
415  
    using context0 = mp11::mp_find< cats, user_conversion_tag >;
421  
    using context0 = mp11::mp_find< cats, user_conversion_tag >;
416  
    using index = mp11::mp_cond<
422  
    using index = mp11::mp_cond<
417  
        exists<context2>, context2,
423  
        exists<context2>, context2,
418  
        exists<context1>, context1,
424  
        exists<context1>, context1,
419  
        exists<context0>, context0,
425  
        exists<context0>, context0,
420  
        mp11::mp_true, mp11::mp_find_if< cats, any_conversion_tag > >;
426  
        mp11::mp_true, mp11::mp_find_if< cats, any_conversion_tag > >;
421  
    using type = mp11::mp_eval_or<
427  
    using type = mp11::mp_eval_or<
422  
        no_conversion_tag,
428  
        no_conversion_tag,
423  
        mp11::mp_at, cats, index >;
429  
        mp11::mp_at, cats, index >;
424  
};
430  
};
425  

431  

426  
struct no_context
432  
struct no_context
427  
{};
433  
{};
428  

434  

429  
template <class T, class Dir>
435  
template <class T, class Dir>
430  
using can_convert = mp11::mp_not<
436  
using can_convert = mp11::mp_not<
431  
    std::is_same<
437  
    std::is_same<
432  
        detail::conversion_category<no_context, T, Dir>,
438  
        detail::conversion_category<no_context, T, Dir>,
433  
        detail::no_conversion_tag>>;
439  
        detail::no_conversion_tag>>;
434  

440  

435  
template<class Impl1, class Impl2>
441  
template<class Impl1, class Impl2>
436  
using conversion_round_trips_helper = mp11::mp_or<
442  
using conversion_round_trips_helper = mp11::mp_or<
437  
    std::is_same<Impl1, Impl2>,
443  
    std::is_same<Impl1, Impl2>,
438  
    std::is_base_of<user_conversion_tag, Impl1>,
444  
    std::is_base_of<user_conversion_tag, Impl1>,
439  
    std::is_base_of<user_conversion_tag, Impl2>>;
445  
    std::is_base_of<user_conversion_tag, Impl2>>;
440  
template< class Ctx, class T, class Dir >
446  
template< class Ctx, class T, class Dir >
441  
using conversion_round_trips  = conversion_round_trips_helper<
447  
using conversion_round_trips  = conversion_round_trips_helper<
442  
    conversion_category<Ctx, T, Dir>,
448  
    conversion_category<Ctx, T, Dir>,
443  
    conversion_category<Ctx, T, mp11::mp_not<Dir>>>;
449  
    conversion_category<Ctx, T, mp11::mp_not<Dir>>>;
444  

450  

445  
template< class T1, class T2 >
451  
template< class T1, class T2 >
446  
struct copy_cref_helper
452  
struct copy_cref_helper
447  
{
453  
{
448  
    using type = remove_cvref<T2>;
454  
    using type = remove_cvref<T2>;
449  
};
455  
};
450  
template< class T1, class T2 >
456  
template< class T1, class T2 >
451  
using copy_cref = typename copy_cref_helper< T1, T2 >::type;
457  
using copy_cref = typename copy_cref_helper< T1, T2 >::type;
452  

458  

453  
template< class T1, class T2 >
459  
template< class T1, class T2 >
454  
struct copy_cref_helper<T1 const, T2>
460  
struct copy_cref_helper<T1 const, T2>
455  
{
461  
{
456  
    using type = remove_cvref<T2> const;
462  
    using type = remove_cvref<T2> const;
457  
};
463  
};
458  
template< class T1, class T2 >
464  
template< class T1, class T2 >
459  
struct copy_cref_helper<T1&, T2>
465  
struct copy_cref_helper<T1&, T2>
460  
{
466  
{
461  
    using type = copy_cref<T1, T2>&;
467  
    using type = copy_cref<T1, T2>&;
462  
};
468  
};
463  
template< class T1, class T2 >
469  
template< class T1, class T2 >
464  
struct copy_cref_helper<T1&&, T2>
470  
struct copy_cref_helper<T1&&, T2>
465  
{
471  
{
466  
    using type = copy_cref<T1, T2>&&;
472  
    using type = copy_cref<T1, T2>&&;
467  
};
473  
};
468  

474  

469  
template< class Rng, class Traits >
475  
template< class Rng, class Traits >
470  
using forwarded_value_helper = mp11::mp_if<
476  
using forwarded_value_helper = mp11::mp_if<
471  
    std::is_convertible<
477  
    std::is_convertible<
472  
        typename Traits::reference,
478  
        typename Traits::reference,
473  
        copy_cref<Rng, typename Traits::value_type> >,
479  
        copy_cref<Rng, typename Traits::value_type> >,
474  
    copy_cref<Rng, typename Traits::value_type>,
480  
    copy_cref<Rng, typename Traits::value_type>,
475  
    typename Traits::value_type >;
481  
    typename Traits::value_type >;
476  

482  

477  
template< class Rng >
483  
template< class Rng >
478  
using forwarded_value = forwarded_value_helper<
484  
using forwarded_value = forwarded_value_helper<
479  
    Rng, iterator_traits< Rng > >;
485  
    Rng, iterator_traits< Rng > >;
480  

486  

481  
template< class Ctx, class T, class Dir >
487  
template< class Ctx, class T, class Dir >
482  
struct supported_context
488  
struct supported_context
483  
{
489  
{
484  
    using type = Ctx;
490  
    using type = Ctx;
485  

491  

486  
    static
492  
    static
487  
    type const&
493  
    type const&
488  
    get( Ctx const& ctx ) noexcept
494  
    get( Ctx const& ctx ) noexcept
489  
    {
495  
    {
490  
        return ctx;
496  
        return ctx;
491  
    }
497  
    }
492  
};
498  
};
493  

499  

494  
template< class T, class Dir, class... Ctxs >
500  
template< class T, class Dir, class... Ctxs >
495  
struct supported_context< std::tuple<Ctxs...>, T, Dir >
501  
struct supported_context< std::tuple<Ctxs...>, T, Dir >
496  
{
502  
{
497  
    using Ctx = std::tuple<Ctxs...>;
503  
    using Ctx = std::tuple<Ctxs...>;
498  
    using impl = conversion_category_impl<Ctx, T, Dir>;
504  
    using impl = conversion_category_impl<Ctx, T, Dir>;
499  
    using index = typename impl::index;
505  
    using index = typename impl::index;
500  
    using next_supported = supported_context<
506  
    using next_supported = supported_context<
501  
        mp11::mp_at< typename impl::ctxs, index >, T, Dir >;
507  
        mp11::mp_at< typename impl::ctxs, index >, T, Dir >;
502  
    using type = typename next_supported::type;
508  
    using type = typename next_supported::type;
503  

509  

504  
    static
510  
    static
505  
    type const&
511  
    type const&
506  
    get( Ctx const& ctx ) noexcept
512  
    get( Ctx const& ctx ) noexcept
507  
    {
513  
    {
508  
        return next_supported::get( std::get<index::value>( ctx ) );
514  
        return next_supported::get( std::get<index::value>( ctx ) );
509  
    }
515  
    }
510  
};
516  
};
511  

517  

512  
template< class T >
518  
template< class T >
513  
using value_result_type = typename std::decay<
519  
using value_result_type = typename std::decay<
514  
    decltype( std::declval<T&>().value() )>::type;
520  
    decltype( std::declval<T&>().value() )>::type;
515  

521  

516  
template< class T >
522  
template< class T >
517  
using can_reset = decltype( std::declval<T&>().reset() );
523  
using can_reset = decltype( std::declval<T&>().reset() );
518  

524  

519  
template< class T >
525  
template< class T >
520  
using has_valueless_by_exception =
526  
using has_valueless_by_exception =
521  
    decltype( std::declval<T const&>().valueless_by_exception() );
527  
    decltype( std::declval<T const&>().valueless_by_exception() );
522  

528  

523  
} // namespace detail
529  
} // namespace detail
524  

530  

525  
template <class T>
531  
template <class T>
526  
struct result_for<T, value>
532  
struct result_for<T, value>
527  
{
533  
{
528  
    using type = system::result< detail::remove_cvref<T> >;
534  
    using type = system::result< detail::remove_cvref<T> >;
529  
};
535  
};
530  

536  

531  
template<class T>
537  
template<class T>
532  
struct is_string_like
538  
struct is_string_like
533  
    : std::is_convertible<T, string_view>
539  
    : std::is_convertible<T, string_view>
534  
{ };
540  
{ };
535  

541  

536  
template<class T>
542  
template<class T>
537  
struct is_path_like
543  
struct is_path_like
538  
    : mp11::mp_all<
544  
    : mp11::mp_all<
539  
        mp11::mp_valid_and_true<detail::is_its_own_value, T>,
545  
        mp11::mp_valid_and_true<detail::is_its_own_value, T>,
540  
        mp11::mp_valid_and_true<detail::has_string_type, T>>
546  
        mp11::mp_valid_and_true<detail::has_string_type, T>>
541  
{ };
547  
{ };
542  
template<class T>
548  
template<class T>
543  
struct is_sequence_like
549  
struct is_sequence_like
544  
    : mp11::mp_all<
550  
    : mp11::mp_all<
545  
        mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
551  
        mp11::mp_valid_and_true<detail::are_begin_and_end_same, T>,
546  
        mp11::mp_valid_and_true<detail::not_its_own_value, T>,
552  
        mp11::mp_valid_and_true<detail::not_its_own_value, T>,
547  
        mp11::mp_valid<detail::begin_iterator_category, T>>
553  
        mp11::mp_valid<detail::begin_iterator_category, T>>
548  
{ };
554  
{ };
549  

555  

550  
template<class T>
556  
template<class T>
551  
struct is_map_like
557  
struct is_map_like
552  
    : mp11::mp_all<
558  
    : mp11::mp_all<
553  
        is_sequence_like<T>,
559  
        is_sequence_like<T>,
554  
        mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
560  
        mp11::mp_valid_and_true<detail::is_value_type_pair, T>,
555  
        is_string_like<detail::key_type<T>>,
561  
        is_string_like<detail::key_type<T>>,
556  
        mp11::mp_valid_and_true<detail::has_unique_keys, T>>
562  
        mp11::mp_valid_and_true<detail::has_unique_keys, T>>
557  
{ };
563  
{ };
558  

564  

559  
template<class T>
565  
template<class T>
560  
struct is_tuple_like
566  
struct is_tuple_like
561  
    : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
567  
    : mp11::mp_valid_and_true<detail::has_positive_tuple_size, T>
562  
{ };
568  
{ };
563  

569  

564  
template<>
570  
template<>
565  
struct is_null_like<std::nullptr_t>
571  
struct is_null_like<std::nullptr_t>
566  
    : std::true_type
572  
    : std::true_type
567  
{ };
573  
{ };
568  

574  

569  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
575  
#ifndef BOOST_NO_CXX17_HDR_VARIANT
570  
template<>
576  
template<>
571  
struct is_null_like<std::monostate>
577  
struct is_null_like<std::monostate>
572  
    : std::true_type
578  
    : std::true_type
573  
{ };
579  
{ };
574  
#endif // BOOST_NO_CXX17_HDR_VARIANT
580  
#endif // BOOST_NO_CXX17_HDR_VARIANT
575  

581  

576  
template<class T>
582  
template<class T>
577  
struct is_described_class
583  
struct is_described_class
578  
    : mp11::mp_and<
584  
    : mp11::mp_and<
579  
        describe::has_describe_members<T>,
585  
        describe::has_describe_members<T>,
580  
        mp11::mp_not< std::is_union<T> >,
586  
        mp11::mp_not< std::is_union<T> >,
581  
        mp11::mp_empty<
587  
        mp11::mp_empty<
582  
            mp11::mp_eval_or<
588  
            mp11::mp_eval_or<
583  
                mp11::mp_list<>, detail::described_non_public_members, T>>>
589  
                mp11::mp_list<>, detail::described_non_public_members, T>>>
584  
{ };
590  
{ };
585  

591  

586  
template<class T>
592  
template<class T>
587  
struct is_described_enum
593  
struct is_described_enum
588  
    : describe::has_describe_enumerators<T>
594  
    : describe::has_describe_enumerators<T>
589  
{ };
595  
{ };
590  

596  

591  
template<class T>
597  
template<class T>
592  
struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T>
598  
struct is_variant_like : mp11::mp_valid<detail::has_valueless_by_exception, T>
593  
{ };
599  
{ };
594  

600  

595  
template<class T>
601  
template<class T>
596  
struct is_optional_like
602  
struct is_optional_like
597  
    : mp11::mp_and<
603  
    : mp11::mp_and<
598  
        mp11::mp_not<std::is_void<
604  
        mp11::mp_not<std::is_void<
599  
            mp11::mp_eval_or<void, detail::value_result_type, T>>>,
605  
            mp11::mp_eval_or<void, detail::value_result_type, T>>>,
600  
        mp11::mp_valid<detail::can_reset, T>>
606  
        mp11::mp_valid<detail::can_reset, T>>
601  
{ };
607  
{ };
602  

608  

603  
} // namespace json
609  
} // namespace json
604  
} // namespace boost
610  
} // namespace boost
605  

611  

606  
#endif // BOOST_JSON_IMPL_CONVERSION_HPP
612  
#endif // BOOST_JSON_IMPL_CONVERSION_HPP