1  
//
1  
//
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
2  
// Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3  
// Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
3  
// Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
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_DETAIL_PARSE_INTO_HPP
11  
#ifndef BOOST_JSON_DETAIL_PARSE_INTO_HPP
12  
#define BOOST_JSON_DETAIL_PARSE_INTO_HPP
12  
#define BOOST_JSON_DETAIL_PARSE_INTO_HPP
13  

13  

14  
#include <boost/json/detail/config.hpp>
14  
#include <boost/json/detail/config.hpp>
15  

15  

16  
#include <boost/json/error.hpp>
16  
#include <boost/json/error.hpp>
17 -
#include <boost/json/value.hpp>
 
18  
#include <boost/json/conversion.hpp>
17  
#include <boost/json/conversion.hpp>
19  
#include <boost/describe/enum_from_string.hpp>
18  
#include <boost/describe/enum_from_string.hpp>
20  

19  

21  
#include <vector>
20  
#include <vector>
22  

21  

23  
/*
22  
/*
24  
 * This file contains the majority of parse_into functionality, specifically
23  
 * This file contains the majority of parse_into functionality, specifically
25  
 * the implementation of dedicated handlers for different generic categories of
24  
 * the implementation of dedicated handlers for different generic categories of
26  
 * types.
25  
 * types.
27  
 *
26  
 *
28  
 * At the core of parse_into is the specialisation basic_parser<
27  
 * At the core of parse_into is the specialisation basic_parser<
29  
 * detail::into_handler<T> >. detail::into_handler<T> is a handler for
28  
 * detail::into_handler<T> >. detail::into_handler<T> is a handler for
30  
 * basic_parser. It directly handles events on_comment_part and on_comment (by
29  
 * basic_parser. It directly handles events on_comment_part and on_comment (by
31  
 * ignoring them), on_document_begin (by enabling the nested dedicated
30  
 * ignoring them), on_document_begin (by enabling the nested dedicated
32  
 * handler), and on_document_end (by disabling the nested handler).
31  
 * handler), and on_document_end (by disabling the nested handler).
33  
 *
32  
 *
34  
 * Every other event is handled by the nested handler, which has the type
33  
 * Every other event is handled by the nested handler, which has the type
35 -
 * get_handler< T, into_handler<T> >. The second parameter is the parent
34 +
 * get_handler<T, P, Ctx>. The second parameter is the parent
36  
 * handler (in this case, it's the top handler, into_handler<T>). The type is
35  
 * handler (in this case, it's the top handler, into_handler<T>). The type is
37  
 * actually an alias to class template converting_handler, which has a separate
36  
 * actually an alias to class template converting_handler, which has a separate
38  
 * specialisation for every conversion category from the list of generic
37  
 * specialisation for every conversion category from the list of generic
39  
 * conversion categories (e.g. sequence_conversion_tag, tuple_conversion_tag,
38  
 * conversion categories (e.g. sequence_conversion_tag, tuple_conversion_tag,
40  
 * etc.) Instantiations of the template store a pointer to the parent handler
39  
 * etc.) Instantiations of the template store a pointer to the parent handler
41  
 * and a pointer to the value T.
40  
 * and a pointer to the value T.
42  
 *
41  
 *
43  
 * The nested handler handles specific parser events by setting error_code to
42  
 * The nested handler handles specific parser events by setting error_code to
44  
 * an appropriate value, if it receives an event it isn't supposed to handle
43  
 * an appropriate value, if it receives an event it isn't supposed to handle
45  
 * (e.g. a number handler getting an on_string event), and also updates the
44  
 * (e.g. a number handler getting an on_string event), and also updates the
46  
 * value when appropriate. Note that they never need to handle on_comment_part,
45  
 * value when appropriate. Note that they never need to handle on_comment_part,
47  
 * on_comment, on_document_begin, and on_document_end events, as those are
46  
 * on_comment, on_document_begin, and on_document_end events, as those are
48  
 * always handled by the top handler into_handler<T>.
47  
 * always handled by the top handler into_handler<T>.
49  
 *
48  
 *
50  
 * When the nested handler receives an event that completes the current value,
49  
 * When the nested handler receives an event that completes the current value,
51  
 * it is supposed to call its parent's signal_value member function. This is
50  
 * it is supposed to call its parent's signal_value member function. This is
52  
 * necessary for correct handling of composite types (e.g. sequences).
51  
 * necessary for correct handling of composite types (e.g. sequences).
53  
 *
52  
 *
54  
 * Finally, nested handlers should always call parent's signal_end member
53  
 * Finally, nested handlers should always call parent's signal_end member
55  
 * function if they don't handle on_array_end themselves. This is necessary
54  
 * function if they don't handle on_array_end themselves. This is necessary
56  
 * to correctly handle nested composites (e.g. sequences inside sequences).
55  
 * to correctly handle nested composites (e.g. sequences inside sequences).
57  
 * signal_end can return false and set error state when the containing parser
56  
 * signal_end can return false and set error state when the containing parser
58  
 * requires more elements.
57  
 * requires more elements.
59  
 *
58  
 *
60  
 * converting_handler instantiations for composite categories of types have
59  
 * converting_handler instantiations for composite categories of types have
61  
 * their own nested handlers, to which they themselves delegate events. For
60  
 * their own nested handlers, to which they themselves delegate events. For
62  
 * complex types you will get a tree of handlers with into_handler<T> as the
61  
 * complex types you will get a tree of handlers with into_handler<T> as the
63  
 * root and handlers for scalars as leaves.
62  
 * root and handlers for scalars as leaves.
64  
 *
63  
 *
65  
 * To reiterate, only into_handler has to handle on_comment_part, on_comment,
64  
 * To reiterate, only into_handler has to handle on_comment_part, on_comment,
66  
 * on_document_begin, and on_document_end; only handlers for composites and
65  
 * on_document_begin, and on_document_end; only handlers for composites and
67  
 * into_handler has to provide signal_value and signal_end; all handlers
66  
 * into_handler has to provide signal_value and signal_end; all handlers
68  
 * except for into_handler have to call their parent's signal_end from
67  
 * except for into_handler have to call their parent's signal_end from
69  
 * their on_array_begin, if they don't handle it themselves; once a handler
68  
 * their on_array_begin, if they don't handle it themselves; once a handler
70  
 * receives an event that finishes its current value, it should call its
69  
 * receives an event that finishes its current value, it should call its
71  
 * parent's signal_value.
70  
 * parent's signal_value.
72  
 */
71  
 */
73  

72  

74  
namespace boost {
73  
namespace boost {
75  
namespace json {
74  
namespace json {
76  
namespace detail {
75  
namespace detail {
77  

76  

78 -
template< class Impl, class T, class Parent >
77 +
template< class Impl, class T, class Parent, class Ctx >
79  
class converting_handler;
78  
class converting_handler;
80  

79  

81  
// get_handler
80  
// get_handler
82 -
template< class V, class P >
81 +
template<class V, class P, class Ctx>
83 -
using get_handler = converting_handler< generic_conversion_category<V>, V, P >;
82 +
using get_handler = converting_handler<
 
83 +
    generic_conversion_category<V, Ctx>, V, P, Ctx>;
84  

84  

85  
template<error E> class handler_error_base
85  
template<error E> class handler_error_base
86  
{
86  
{
87  
public:
87  
public:
88  

88  

89  
    handler_error_base() = default;
89  
    handler_error_base() = default;
90  

90  

91  
    handler_error_base( handler_error_base const& ) = delete;
91  
    handler_error_base( handler_error_base const& ) = delete;
92  
    handler_error_base& operator=( handler_error_base const& ) = delete;
92  
    handler_error_base& operator=( handler_error_base const& ) = delete;
93  

93  

94  
public:
94  
public:
95  

95  

96  
    bool on_object_begin( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
96  
    bool on_object_begin( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
97  
    bool on_array_begin( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
97  
    bool on_array_begin( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
98  
    bool on_array_end( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
98  
    bool on_array_end( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
99  
    bool on_string_part( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
99  
    bool on_string_part( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
100  
    bool on_string( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
100  
    bool on_string( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
101  
    bool on_number_part( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
101  
    bool on_number_part( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
102  
    bool on_int64( system::error_code& ec, std::int64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
102  
    bool on_int64( system::error_code& ec, std::int64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
103  
    bool on_uint64( system::error_code& ec, std::uint64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
103  
    bool on_uint64( system::error_code& ec, std::uint64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
104  
    bool on_double( system::error_code& ec, double ) { BOOST_JSON_FAIL( ec, E ); return false; }
104  
    bool on_double( system::error_code& ec, double ) { BOOST_JSON_FAIL( ec, E ); return false; }
105  
    bool on_bool( system::error_code& ec, bool ) { BOOST_JSON_FAIL( ec, E ); return false; }
105  
    bool on_bool( system::error_code& ec, bool ) { BOOST_JSON_FAIL( ec, E ); return false; }
106  
    bool on_null( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
106  
    bool on_null( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
107  

107  

108  
    // LCOV_EXCL_START
108  
    // LCOV_EXCL_START
109  
    // parses that can't handle this would fail at on_object_begin
109  
    // parses that can't handle this would fail at on_object_begin
110  
    bool on_object_end( system::error_code& ) { BOOST_ASSERT( false ); return false; }
110  
    bool on_object_end( system::error_code& ) { BOOST_ASSERT( false ); return false; }
111  
    bool on_key_part( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
111  
    bool on_key_part( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
112  
    bool on_key( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
112  
    bool on_key( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
113  
    // LCOV_EXCL_STOP
113  
    // LCOV_EXCL_STOP
114  
};
114  
};
115  

115  

116  
template< class P, error E >
116  
template< class P, error E >
117  
class scalar_handler
117  
class scalar_handler
118  
    : public handler_error_base<E>
118  
    : public handler_error_base<E>
119  
{
119  
{
120  
protected:
120  
protected:
121  
    P* parent_;
121  
    P* parent_;
122  

122  

123  
public:
123  
public:
124  
    scalar_handler(scalar_handler const&) = delete;
124  
    scalar_handler(scalar_handler const&) = delete;
125  
    scalar_handler& operator=(scalar_handler const&) = delete;
125  
    scalar_handler& operator=(scalar_handler const&) = delete;
126  

126  

127  
    scalar_handler(P* p): parent_( p )
127  
    scalar_handler(P* p): parent_( p )
128  
    {}
128  
    {}
129  

129  

130  
    bool on_array_end( system::error_code& ec )
130  
    bool on_array_end( system::error_code& ec )
131  
    {
131  
    {
132  
        return parent_->signal_end(ec);
132  
        return parent_->signal_end(ec);
133  
    }
133  
    }
134  
};
134  
};
135  

135  

136 -
template< class D, class V, class P, error E >
136 +
template<class D, class V, class P, class Ctx, error E>
137  
class composite_handler
137  
class composite_handler
138  
{
138  
{
139  
protected:
139  
protected:
140 -
    using inner_handler_type = get_handler<V, D>;
140 +
    using representation = conversion_representation<V, Ctx>;
 
141 +
    using inner_handler_type = get_handler<representation, D, Ctx>;
141  

142  

142  
    P* parent_;
143  
    P* parent_;
143  
#if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
144  
#if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
144  
# pragma GCC diagnostic push
145  
# pragma GCC diagnostic push
145  
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
146  
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
146  
#endif
147  
#endif
147 -
    V next_value_ = {};
148 +
    representation next_value_ = {};
148  
    inner_handler_type inner_;
149  
    inner_handler_type inner_;
149  
    bool inner_active_ = false;
150  
    bool inner_active_ = false;
150  

151  

151  
public:
152  
public:
152 -
    composite_handler( composite_handler const& ) = delete;
153 +
    composite_handler(composite_handler const&) = delete;
153 -
    composite_handler& operator=( composite_handler const& ) = delete;
154 +
    composite_handler& operator=(composite_handler const&) = delete;
154  

155  

155 -
    composite_handler( P* p )
156 +
    composite_handler(P* p)
156  
        : parent_(p), inner_( &next_value_, static_cast<D*>(this) )
157  
        : parent_(p), inner_( &next_value_, static_cast<D*>(this) )
157  
    {}
158  
    {}
158  
#if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
159  
#if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
159  
# pragma GCC diagnostic pop
160  
# pragma GCC diagnostic pop
160  
#endif
161  
#endif
161  

162  

162  
    bool signal_end(system::error_code& ec)
163  
    bool signal_end(system::error_code& ec)
163  
    {
164  
    {
164  
        inner_active_ = false;
165  
        inner_active_ = false;
165  
        return parent_->signal_value(ec);
166  
        return parent_->signal_value(ec);
166  
    }
167  
    }
167  

168  

168  
#define BOOST_JSON_INVOKE_INNER(f) \
169  
#define BOOST_JSON_INVOKE_INNER(f) \
169  
    if( !inner_active_ ) { \
170  
    if( !inner_active_ ) { \
170  
        BOOST_JSON_FAIL(ec, E); \
171  
        BOOST_JSON_FAIL(ec, E); \
171  
        return false; \
172  
        return false; \
172  
    } \
173  
    } \
173  
    else \
174  
    else \
174  
        return inner_.f
175  
        return inner_.f
175  

176  

176  
    bool on_object_begin( system::error_code& ec )
177  
    bool on_object_begin( system::error_code& ec )
177  
    {
178  
    {
178  
        BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
179  
        BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
179  
    }
180  
    }
180  

181  

181  
    bool on_object_end( system::error_code& ec )
182  
    bool on_object_end( system::error_code& ec )
182  
    {
183  
    {
183  
        BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
184  
        BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
184  
    }
185  
    }
185  

186  

186  
    bool on_array_begin( system::error_code& ec )
187  
    bool on_array_begin( system::error_code& ec )
187  
    {
188  
    {
188  
        BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
189  
        BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
189  
    }
190  
    }
190  

191  

191  
    bool on_array_end( system::error_code& ec )
192  
    bool on_array_end( system::error_code& ec )
192  
    {
193  
    {
193  
        BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
194  
        BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
194  
    }
195  
    }
195  

196  

196  
    bool on_key_part( system::error_code& ec, string_view sv )
197  
    bool on_key_part( system::error_code& ec, string_view sv )
197  
    {
198  
    {
198  
        BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
199  
        BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
199  
    }
200  
    }
200  

201  

201  
    bool on_key( system::error_code& ec, string_view sv )
202  
    bool on_key( system::error_code& ec, string_view sv )
202  
    {
203  
    {
203  
        BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
204  
        BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
204  
    }
205  
    }
205  

206  

206  
    bool on_string_part( system::error_code& ec, string_view sv )
207  
    bool on_string_part( system::error_code& ec, string_view sv )
207  
    {
208  
    {
208  
        BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
209  
        BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
209  
    }
210  
    }
210  

211  

211  
    bool on_string( system::error_code& ec, string_view sv )
212  
    bool on_string( system::error_code& ec, string_view sv )
212  
    {
213  
    {
213  
        BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
214  
        BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
214  
    }
215  
    }
215  

216  

216  
    bool on_number_part( system::error_code& ec )
217  
    bool on_number_part( system::error_code& ec )
217  
    {
218  
    {
218  
        BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
219  
        BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
219  
    }
220  
    }
220  

221  

221  
    bool on_int64( system::error_code& ec, std::int64_t v )
222  
    bool on_int64( system::error_code& ec, std::int64_t v )
222  
    {
223  
    {
223  
        BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
224  
        BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
224  
    }
225  
    }
225  

226  

226  
    bool on_uint64( system::error_code& ec, std::uint64_t v )
227  
    bool on_uint64( system::error_code& ec, std::uint64_t v )
227  
    {
228  
    {
228  
        BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
229  
        BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
229  
    }
230  
    }
230  

231  

231  
    bool on_double( system::error_code& ec, double v )
232  
    bool on_double( system::error_code& ec, double v )
232  
    {
233  
    {
233  
        BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
234  
        BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
234  
    }
235  
    }
235  

236  

236  
    bool on_bool( system::error_code& ec, bool v )
237  
    bool on_bool( system::error_code& ec, bool v )
237  
    {
238  
    {
238  
        BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
239  
        BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
239  
    }
240  
    }
240  

241  

241  
    bool on_null( system::error_code& ec )
242  
    bool on_null( system::error_code& ec )
242  
    {
243  
    {
243  
        BOOST_JSON_INVOKE_INNER( on_null(ec) );
244  
        BOOST_JSON_INVOKE_INNER( on_null(ec) );
244  
    }
245  
    }
245  

246  

246  
#undef BOOST_JSON_INVOKE_INNER
247  
#undef BOOST_JSON_INVOKE_INNER
247  
};
248  
};
248  

249  

249  
// integral handler
250  
// integral handler
250  
template<class V,
251  
template<class V,
251  
typename std::enable_if<std::is_signed<V>::value, int>::type = 0>
252  
typename std::enable_if<std::is_signed<V>::value, int>::type = 0>
252  
bool integral_in_range( std::int64_t v )
253  
bool integral_in_range( std::int64_t v )
253  
{
254  
{
254  
    return v >= (std::numeric_limits<V>::min)() && v <= (std::numeric_limits<V>::max)();
255  
    return v >= (std::numeric_limits<V>::min)() && v <= (std::numeric_limits<V>::max)();
255  
}
256  
}
256  

257  

257  
template<class V,
258  
template<class V,
258  
typename std::enable_if<!std::is_signed<V>::value, int>::type = 0>
259  
typename std::enable_if<!std::is_signed<V>::value, int>::type = 0>
259  
bool integral_in_range( std::int64_t v )
260  
bool integral_in_range( std::int64_t v )
260  
{
261  
{
261  
    return v >= 0 && static_cast<std::uint64_t>( v ) <= (std::numeric_limits<V>::max)();
262  
    return v >= 0 && static_cast<std::uint64_t>( v ) <= (std::numeric_limits<V>::max)();
262  
}
263  
}
263  

264  

264  
template<class V>
265  
template<class V>
265  
bool integral_in_range( std::uint64_t v )
266  
bool integral_in_range( std::uint64_t v )
266  
{
267  
{
267  
    return v <= static_cast<typename std::make_unsigned<V>::type>( (std::numeric_limits<V>::max)() );
268  
    return v <= static_cast<typename std::make_unsigned<V>::type>( (std::numeric_limits<V>::max)() );
268  
}
269  
}
269  

270  

270 -
template< class V, class P >
271 +
template< class V, class P, class Ctx >
271 -
class converting_handler<integral_conversion_tag, V, P>
272 +
class converting_handler<integral_conversion_tag, V, P, Ctx>
272  
    : public scalar_handler<P, error::not_integer>
273  
    : public scalar_handler<P, error::not_integer>
273  
{
274  
{
274  
private:
275  
private:
275  
    V* value_;
276  
    V* value_;
276  

277  

277  
public:
278  
public:
278  
    converting_handler( V* v, P* p )
279  
    converting_handler( V* v, P* p )
279  
        : converting_handler::scalar_handler(p)
280  
        : converting_handler::scalar_handler(p)
280  
        , value_(v)
281  
        , value_(v)
281  
    {}
282  
    {}
282  

283  

283  
    bool on_number_part( system::error_code& )
284  
    bool on_number_part( system::error_code& )
284  
    {
285  
    {
285  
        return true;
286  
        return true;
286  
    }
287  
    }
287  

288  

288  
    bool on_int64(system::error_code& ec, std::int64_t v)
289  
    bool on_int64(system::error_code& ec, std::int64_t v)
289  
    {
290  
    {
290  
        if( !integral_in_range<V>( v ) )
291  
        if( !integral_in_range<V>( v ) )
291  
        {
292  
        {
292  
            BOOST_JSON_FAIL( ec, error::not_exact );
293  
            BOOST_JSON_FAIL( ec, error::not_exact );
293  
            return false;
294  
            return false;
294  
        }
295  
        }
295  

296  

296  
        *value_ = static_cast<V>( v );
297  
        *value_ = static_cast<V>( v );
297  
        return this->parent_->signal_value(ec);
298  
        return this->parent_->signal_value(ec);
298  
    }
299  
    }
299  

300  

300  
    bool on_uint64(system::error_code& ec, std::uint64_t v)
301  
    bool on_uint64(system::error_code& ec, std::uint64_t v)
301  
    {
302  
    {
302  
        if( !integral_in_range<V>(v) )
303  
        if( !integral_in_range<V>(v) )
303  
        {
304  
        {
304  
            BOOST_JSON_FAIL( ec, error::not_exact );
305  
            BOOST_JSON_FAIL( ec, error::not_exact );
305  
            return false;
306  
            return false;
306  
        }
307  
        }
307  

308  

308  
        *value_ = static_cast<V>(v);
309  
        *value_ = static_cast<V>(v);
309  
        return this->parent_->signal_value(ec);
310  
        return this->parent_->signal_value(ec);
310  
    }
311  
    }
311  
};
312  
};
312  

313  

313  
// floating point handler
314  
// floating point handler
314 -
template< class V, class P>
315 +
template< class V, class P, class Ctx>
315 -
class converting_handler<floating_point_conversion_tag, V, P>
316 +
class converting_handler<floating_point_conversion_tag, V, P, Ctx>
316  
    : public scalar_handler<P, error::not_double>
317  
    : public scalar_handler<P, error::not_double>
317  
{
318  
{
318  
private:
319  
private:
319  
    V* value_;
320  
    V* value_;
320  

321  

321  
public:
322  
public:
322  
    converting_handler( V* v, P* p )
323  
    converting_handler( V* v, P* p )
323  
        : converting_handler::scalar_handler(p)
324  
        : converting_handler::scalar_handler(p)
324  
        , value_(v)
325  
        , value_(v)
325  
    {}
326  
    {}
326  

327  

327  
    bool on_number_part( system::error_code& )
328  
    bool on_number_part( system::error_code& )
328  
    {
329  
    {
329  
        return true;
330  
        return true;
330  
    }
331  
    }
331  

332  

332  
    bool on_int64(system::error_code& ec, std::int64_t v)
333  
    bool on_int64(system::error_code& ec, std::int64_t v)
333  
    {
334  
    {
334  
        *value_ = static_cast<V>(v);
335  
        *value_ = static_cast<V>(v);
335  
        return this->parent_->signal_value(ec);
336  
        return this->parent_->signal_value(ec);
336  
    }
337  
    }
337  

338  

338  
    bool on_uint64(system::error_code& ec, std::uint64_t v)
339  
    bool on_uint64(system::error_code& ec, std::uint64_t v)
339  
    {
340  
    {
340  
        *value_ = static_cast<V>(v);
341  
        *value_ = static_cast<V>(v);
341  
        return this->parent_->signal_value(ec);
342  
        return this->parent_->signal_value(ec);
342  
    }
343  
    }
343  

344  

344  
    bool on_double(system::error_code& ec, double v)
345  
    bool on_double(system::error_code& ec, double v)
345  
    {
346  
    {
346  
        *value_ = static_cast<V>(v);
347  
        *value_ = static_cast<V>(v);
347  
        return this->parent_->signal_value(ec);
348  
        return this->parent_->signal_value(ec);
348  
    }
349  
    }
349  
};
350  
};
350  

351  

351  
// string handler
352  
// string handler
352 -
template< class V, class P >
353 +
template<class V, class P, class Ctx>
353 -
class converting_handler<string_like_conversion_tag, V, P>
354 +
class converting_handler<string_like_conversion_tag, V, P, Ctx>
354  
    : public scalar_handler<P, error::not_string>
355  
    : public scalar_handler<P, error::not_string>
355  
{
356  
{
356  
private:
357  
private:
357  
    V* value_;
358  
    V* value_;
358  
    bool cleared_ = false;
359  
    bool cleared_ = false;
359  

360  

360  
public:
361  
public:
361  
    converting_handler( V* v, P* p )
362  
    converting_handler( V* v, P* p )
362  
        : converting_handler::scalar_handler(p)
363  
        : converting_handler::scalar_handler(p)
363  
        , value_(v)
364  
        , value_(v)
364  
    {}
365  
    {}
365  

366  

366  
    bool on_string_part( system::error_code&, string_view sv )
367  
    bool on_string_part( system::error_code&, string_view sv )
367  
    {
368  
    {
368  
        if( !cleared_ )
369  
        if( !cleared_ )
369  
        {
370  
        {
370  
            cleared_ = true;
371  
            cleared_ = true;
371  
            value_->clear();
372  
            value_->clear();
372  
        }
373  
        }
373  

374  

374  
        value_->append( sv.begin(), sv.end() );
375  
        value_->append( sv.begin(), sv.end() );
375  
        return true;
376  
        return true;
376  
    }
377  
    }
377  

378  

378  
    bool on_string(system::error_code& ec, string_view sv)
379  
    bool on_string(system::error_code& ec, string_view sv)
379  
    {
380  
    {
380  
        if( !cleared_ )
381  
        if( !cleared_ )
381  
            value_->clear();
382  
            value_->clear();
382  
        else
383  
        else
383  
            cleared_ = false;
384  
            cleared_ = false;
384  

385  

385  
        value_->append( sv.begin(), sv.end() );
386  
        value_->append( sv.begin(), sv.end() );
386  
        return this->parent_->signal_value(ec);
387  
        return this->parent_->signal_value(ec);
387  
    }
388  
    }
388  
};
389  
};
389  

390  

390  
// bool handler
391  
// bool handler
391 -
template< class V, class P >
392 +
template<class V, class P, class Ctx>
392 -
class converting_handler<bool_conversion_tag, V, P>
393 +
class converting_handler<bool_conversion_tag, V, P, Ctx>
393  
    : public scalar_handler<P, error::not_bool>
394  
    : public scalar_handler<P, error::not_bool>
394  
{
395  
{
395  
private:
396  
private:
396  
    V* value_;
397  
    V* value_;
397  

398  

398  
public:
399  
public:
399  
    converting_handler( V* v, P* p )
400  
    converting_handler( V* v, P* p )
400  
        : converting_handler::scalar_handler(p)
401  
        : converting_handler::scalar_handler(p)
401  
        , value_(v)
402  
        , value_(v)
402  
    {}
403  
    {}
403  

404  

404  
    bool on_bool(system::error_code& ec, bool v)
405  
    bool on_bool(system::error_code& ec, bool v)
405  
    {
406  
    {
406  
        *value_ = v;
407  
        *value_ = v;
407  
        return this->parent_->signal_value(ec);
408  
        return this->parent_->signal_value(ec);
408  
    }
409  
    }
409  
};
410  
};
410  

411  

411  
// null handler
412  
// null handler
412 -
template< class V, class P >
413 +
template<class V, class P, class Ctx>
413 -
class converting_handler<null_like_conversion_tag, V, P>
414 +
class converting_handler<null_like_conversion_tag, V, P, Ctx>
414  
    : public scalar_handler<P, error::not_null>
415  
    : public scalar_handler<P, error::not_null>
415  
{
416  
{
416  
private:
417  
private:
417  
    V* value_;
418  
    V* value_;
418  

419  

419  
public:
420  
public:
420  
    converting_handler( V* v, P* p )
421  
    converting_handler( V* v, P* p )
421  
        : converting_handler::scalar_handler(p)
422  
        : converting_handler::scalar_handler(p)
422  
        , value_(v)
423  
        , value_(v)
423  
    {}
424  
    {}
424  

425  

425  
    bool on_null(system::error_code& ec)
426  
    bool on_null(system::error_code& ec)
426  
    {
427  
    {
427  
        *value_ = {};
428  
        *value_ = {};
428  
        return this->parent_->signal_value(ec);
429  
        return this->parent_->signal_value(ec);
429  
    }
430  
    }
430  
};
431  
};
431  

432  

432  
// described enum handler
433  
// described enum handler
433 -
template< class V, class P >
434 +
template<class V, class P, class Ctx>
434 -
class converting_handler<described_enum_conversion_tag, V, P>
435 +
class converting_handler<described_enum_conversion_tag, V, P, Ctx>
435  
    : public scalar_handler<P, error::not_string>
436  
    : public scalar_handler<P, error::not_string>
436  
{
437  
{
437  
#ifndef BOOST_DESCRIBE_CXX14
438  
#ifndef BOOST_DESCRIBE_CXX14
438  

439  

439  
    static_assert(
440  
    static_assert(
440  
        sizeof(V) == 0, "Enum support for parse_into requires C++14" );
441  
        sizeof(V) == 0, "Enum support for parse_into requires C++14" );
441  

442  

442  
#else
443  
#else
443  

444  

444  
private:
445  
private:
445  
    V* value_;
446  
    V* value_;
446  
    std::string name_;
447  
    std::string name_;
447  

448  

448  
public:
449  
public:
449  
    converting_handler( V* v, P* p )
450  
    converting_handler( V* v, P* p )
450  
        : converting_handler::scalar_handler(p)
451  
        : converting_handler::scalar_handler(p)
451  
        , value_(v)
452  
        , value_(v)
452  
    {}
453  
    {}
453  

454  

454  
    bool on_string_part( system::error_code&, string_view sv )
455  
    bool on_string_part( system::error_code&, string_view sv )
455  
    {
456  
    {
456  
        name_.append( sv.begin(), sv.end() );
457  
        name_.append( sv.begin(), sv.end() );
457  
        return true;
458  
        return true;
458  
    }
459  
    }
459  

460  

460  
    bool on_string(system::error_code& ec, string_view sv)
461  
    bool on_string(system::error_code& ec, string_view sv)
461  
    {
462  
    {
462  
        string_view name = sv;
463  
        string_view name = sv;
463  
        if( !name_.empty() )
464  
        if( !name_.empty() )
464  
        {
465  
        {
465  
            name_.append( sv.begin(), sv.end() );
466  
            name_.append( sv.begin(), sv.end() );
466  
            name = name_;
467  
            name = name_;
467  
        }
468  
        }
468  

469  

469  
        if( !describe::enum_from_string(name, *value_) )
470  
        if( !describe::enum_from_string(name, *value_) )
470  
        {
471  
        {
471  
            BOOST_JSON_FAIL(ec, error::unknown_name);
472  
            BOOST_JSON_FAIL(ec, error::unknown_name);
472  
            return false;
473  
            return false;
473  
        }
474  
        }
474  

475  

475  
        return this->parent_->signal_value(ec);
476  
        return this->parent_->signal_value(ec);
476  
    }
477  
    }
477  

478  

478  
#endif // BOOST_DESCRIBE_CXX14
479  
#endif // BOOST_DESCRIBE_CXX14
479  
};
480  
};
480  

481  

481 -
template< class V, class P >
482 +
template<class V, class P, class Ctx>
482 -
class converting_handler<no_conversion_tag, V, P>
483 +
class converting_handler<no_conversion_tag, V, P, Ctx>
483  
{
484  
{
484  
    static_assert( sizeof(V) == 0, "This type is not supported" );
485  
    static_assert( sizeof(V) == 0, "This type is not supported" );
485  
};
486  
};
486  

487  

487  
// sequence handler
488  
// sequence handler
488  
template< class It >
489  
template< class It >
489  
bool cannot_insert(It i, It e)
490  
bool cannot_insert(It i, It e)
490  
{
491  
{
491  
    return i == e;
492  
    return i == e;
492  
}
493  
}
493  

494  

494  
template< class It1, class It2 >
495  
template< class It1, class It2 >
495  
std::false_type cannot_insert(It1, It2)
496  
std::false_type cannot_insert(It1, It2)
496  
{
497  
{
497  
    return {};
498  
    return {};
498  
}
499  
}
499  

500  

500  
template< class It >
501  
template< class It >
501  
bool needs_more_elements(It i, It e)
502  
bool needs_more_elements(It i, It e)
502  
{
503  
{
503  
    return i != e;
504  
    return i != e;
504  
}
505  
}
505  

506  

506  
template< class It1, class It2 >
507  
template< class It1, class It2 >
507  
std::false_type needs_more_elements(It1, It2)
508  
std::false_type needs_more_elements(It1, It2)
508  
{
509  
{
509  
    return {};
510  
    return {};
510  
}
511  
}
511  

512  

512  
template<class T>
513  
template<class T>
513  
void
514  
void
514  
clear_container(
515  
clear_container(
515  
    T&,
516  
    T&,
516  
    mp11::mp_int<2>)
517  
    mp11::mp_int<2>)
517  
{
518  
{
518  
}
519  
}
519  

520  

520  
template<class T>
521  
template<class T>
521  
void
522  
void
522  
clear_container(
523  
clear_container(
523  
    T& target,
524  
    T& target,
524  
    mp11::mp_int<1>)
525  
    mp11::mp_int<1>)
525  
{
526  
{
526  
    target.clear();
527  
    target.clear();
527  
}
528  
}
528  

529  

529  
template<class T>
530  
template<class T>
530  
void
531  
void
531  
clear_container(
532  
clear_container(
532  
    T& target,
533  
    T& target,
533  
    mp11::mp_int<0>)
534  
    mp11::mp_int<0>)
534  
{
535  
{
535  
    target.clear();
536  
    target.clear();
536  
}
537  
}
537  

538  

538 -
template< class V, class P >
539 +
template<class V, class P, class Ctx>
539 -
class converting_handler<sequence_conversion_tag, V, P>
540 +
class converting_handler<sequence_conversion_tag, V, P, Ctx>
540  
    : public composite_handler<
541  
    : public composite_handler<
541 -
        converting_handler<sequence_conversion_tag, V, P>,
542 +
        converting_handler<sequence_conversion_tag, V, P, Ctx>,
542  
        detail::value_type<V>,
543  
        detail::value_type<V>,
543  
        P,
544  
        P,
 
545 +
        Ctx,
544  
        error::not_array>
546  
        error::not_array>
545  
{
547  
{
546  
private:
548  
private:
547  
    V* value_;
549  
    V* value_;
548  

550  

549  
    using Inserter = decltype(
551  
    using Inserter = decltype(
550  
        detail::inserter(*value_, inserter_implementation<V>()) );
552  
        detail::inserter(*value_, inserter_implementation<V>()) );
551  
    Inserter inserter;
553  
    Inserter inserter;
552  

554  

553  
public:
555  
public:
554  
    converting_handler( V* v, P* p )
556  
    converting_handler( V* v, P* p )
555  
        : converting_handler::composite_handler(p)
557  
        : converting_handler::composite_handler(p)
556  
        , value_(v)
558  
        , value_(v)
557  
        , inserter( detail::inserter(*value_, inserter_implementation<V>()) )
559  
        , inserter( detail::inserter(*value_, inserter_implementation<V>()) )
558  
    {}
560  
    {}
559  

561  

560  
    bool signal_value(system::error_code& ec)
562  
    bool signal_value(system::error_code& ec)
561  
    {
563  
    {
562  
        if(cannot_insert( inserter, value_->end() ))
564  
        if(cannot_insert( inserter, value_->end() ))
563  
        {
565  
        {
564  
            BOOST_JSON_FAIL( ec, error::size_mismatch );
566  
            BOOST_JSON_FAIL( ec, error::size_mismatch );
565  
            return false;
567  
            return false;
566  
        }
568  
        }
567  

569  

568  
        *inserter++ = std::move(this->next_value_);
570  
        *inserter++ = std::move(this->next_value_);
569  
#if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
571  
#if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
570  
# pragma GCC diagnostic push
572  
# pragma GCC diagnostic push
571  
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
573  
# pragma GCC diagnostic ignored "-Wmissing-field-initializers"
572  
#endif
574  
#endif
573  
        this->next_value_ = {};
575  
        this->next_value_ = {};
574  
#if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
576  
#if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
575  
# pragma GCC diagnostic pop
577  
# pragma GCC diagnostic pop
576  
#endif
578  
#endif
577  
        return true;
579  
        return true;
578  
    }
580  
    }
579  

581  

580  
    bool signal_end(system::error_code& ec)
582  
    bool signal_end(system::error_code& ec)
581  
    {
583  
    {
582  
        if(needs_more_elements( inserter, value_->end() ))
584  
        if(needs_more_elements( inserter, value_->end() ))
583  
        {
585  
        {
584  
            BOOST_JSON_FAIL( ec, error::size_mismatch );
586  
            BOOST_JSON_FAIL( ec, error::size_mismatch );
585  
            return false;
587  
            return false;
586  
        }
588  
        }
587  

589  

588  
        inserter = detail::inserter(*value_, inserter_implementation<V>());
590  
        inserter = detail::inserter(*value_, inserter_implementation<V>());
589  

591  

590  
        return converting_handler::composite_handler::signal_end(ec);
592  
        return converting_handler::composite_handler::signal_end(ec);
591  
    }
593  
    }
592  

594  

593  
    bool on_array_begin( system::error_code& ec )
595  
    bool on_array_begin( system::error_code& ec )
594  
    {
596  
    {
595  
        if( this->inner_active_ )
597  
        if( this->inner_active_ )
596  
            return this->inner_.on_array_begin( ec );
598  
            return this->inner_.on_array_begin( ec );
597  

599  

598  
        this->inner_active_ = true;
600  
        this->inner_active_ = true;
599  
        clear_container( *value_, inserter_implementation<V>() );
601  
        clear_container( *value_, inserter_implementation<V>() );
600  
        return true;
602  
        return true;
601  
    }
603  
    }
602  

604  

603  
    bool on_array_end( system::error_code& ec )
605  
    bool on_array_end( system::error_code& ec )
604  
    {
606  
    {
605  
        if( this->inner_active_ )
607  
        if( this->inner_active_ )
606  
            return this->inner_.on_array_end( ec );
608  
            return this->inner_.on_array_end( ec );
607  

609  

608  
        return this->parent_->signal_end(ec);
610  
        return this->parent_->signal_end(ec);
609  
    }
611  
    }
610  
};
612  
};
611  

613  

612  
// map handler
614  
// map handler
613 -
template< class V, class P >
615 +
template<class V, class P, class Ctx>
614 -
class converting_handler<map_like_conversion_tag, V, P>
616 +
class converting_handler<map_like_conversion_tag, V, P, Ctx>
615  
    : public composite_handler<
617  
    : public composite_handler<
616 -
        converting_handler<map_like_conversion_tag, V, P>,
618 +
        converting_handler<map_like_conversion_tag, V, P, Ctx>,
617  
        detail::mapped_type<V>,
619  
        detail::mapped_type<V>,
618  
        P,
620  
        P,
 
621 +
        Ctx,
619  
        error::not_object>
622  
        error::not_object>
620  
{
623  
{
621  
private:
624  
private:
622  
    V* value_;
625  
    V* value_;
623  
    std::string key_;
626  
    std::string key_;
624  

627  

625  
public:
628  
public:
626  
    converting_handler( V* v, P* p )
629  
    converting_handler( V* v, P* p )
627  
        : converting_handler::composite_handler(p), value_(v)
630  
        : converting_handler::composite_handler(p), value_(v)
628  
    {}
631  
    {}
629  

632  

630  
    bool signal_value(system::error_code&)
633  
    bool signal_value(system::error_code&)
631  
    {
634  
    {
632 -
        value_->emplace( std::move(key_), std::move(this->next_value_) );
635 +
        using key_rep = conversion_representation<detail::key_type<V>, Ctx>;
633 -

636 +
        value_->emplace(
634 -
        key_ = {};
637 +
            static_cast<key_rep>( std::move(key_) ),
 
638 +
            std::move(this->next_value_) );
 
639 +
        key_.clear();
635  
        this->next_value_ = {};
640  
        this->next_value_ = {};
636  

641  

637  
        this->inner_active_ = false;
642  
        this->inner_active_ = false;
638  

643  

639  
        return true;
644  
        return true;
640  
    }
645  
    }
641  

646  

642  
    bool on_object_begin( system::error_code& ec )
647  
    bool on_object_begin( system::error_code& ec )
643  
    {
648  
    {
644  
        if( this->inner_active_ )
649  
        if( this->inner_active_ )
645  
            return this->inner_.on_object_begin(ec);
650  
            return this->inner_.on_object_begin(ec);
646  

651  

647  
        clear_container( *value_, inserter_implementation<V>() );
652  
        clear_container( *value_, inserter_implementation<V>() );
648  
        return true;
653  
        return true;
649  
    }
654  
    }
650  

655  

651  
    bool on_object_end(system::error_code& ec)
656  
    bool on_object_end(system::error_code& ec)
652  
    {
657  
    {
653  
        if( this->inner_active_ )
658  
        if( this->inner_active_ )
654  
            return this->inner_.on_object_end(ec);
659  
            return this->inner_.on_object_end(ec);
655  

660  

656  
        return this->parent_->signal_value(ec);
661  
        return this->parent_->signal_value(ec);
657  
    }
662  
    }
658  

663  

659  
    bool on_array_end( system::error_code& ec )
664  
    bool on_array_end( system::error_code& ec )
660  
    {
665  
    {
661  
        if( this->inner_active_ )
666  
        if( this->inner_active_ )
662  
            return this->inner_.on_array_end(ec);
667  
            return this->inner_.on_array_end(ec);
663  

668  

664  
        return this->parent_->signal_end(ec);
669  
        return this->parent_->signal_end(ec);
665  
    }
670  
    }
666  

671  

667  
    bool on_key_part( system::error_code& ec, string_view sv )
672  
    bool on_key_part( system::error_code& ec, string_view sv )
668  
    {
673  
    {
669  
        if( this->inner_active_ )
674  
        if( this->inner_active_ )
670  
            return this->inner_.on_key_part(ec, sv);
675  
            return this->inner_.on_key_part(ec, sv);
671  

676  

672  
        key_.append( sv.data(), sv.size() );
677  
        key_.append( sv.data(), sv.size() );
673  
        return true;
678  
        return true;
674  
    }
679  
    }
675  

680  

676  
    bool on_key( system::error_code& ec, string_view sv )
681  
    bool on_key( system::error_code& ec, string_view sv )
677  
    {
682  
    {
678  
        if( this->inner_active_ )
683  
        if( this->inner_active_ )
679  
            return this->inner_.on_key(ec, sv);
684  
            return this->inner_.on_key(ec, sv);
680  

685  

681  
        key_.append( sv.data(), sv.size() );
686  
        key_.append( sv.data(), sv.size() );
682  

687  

683  
        this->inner_active_ = true;
688  
        this->inner_active_ = true;
684  
        return true;
689  
        return true;
685  
    }
690  
    }
686  
};
691  
};
687  

692  

688  
// tuple handler
693  
// tuple handler
689 -
template<std::size_t I, class T>
694 +
template<
 
695 +
    std::size_t I,
 
696 +
    class P,
 
697 +
    class Ctx,
 
698 +
    class T,
 
699 +
    class Rep = conversion_representation<T, Ctx> >
690  
struct handler_tuple_element
700  
struct handler_tuple_element
691  
{
701  
{
692 -
    template< class... Args >
702 +
    using representation = conversion_representation<T, Ctx>;
693 -
    handler_tuple_element( Args&& ... args )
703 +
    using handler = get_handler<representation, P, Ctx>;
694 -
        : t_( static_cast<Args&&>(args)... )
704 +

 
705 +
    handler_tuple_element(T* t, P* p)
 
706 +
        : t_(std::addressof(rep_), p), tgt_(t)
695  
    {}
707  
    {}
696  

708  

697 -
    T t_;
709 +
    void
 
710 +
    finish()
 
711 +
    {
 
712 +
        *tgt_ = std::move(rep_);
 
713 +
    }
 
714 +

 
715 +
    Rep rep_;
 
716 +
    handler t_;
 
717 +
    T* tgt_;
698  
};
718  
};
699  

719  

700 -
template<std::size_t I, class T>
720 +
template<std::size_t I, class P, class Ctx, class T>
701 -
T&
721 +
struct handler_tuple_element<I, P, Ctx, T, T>
702 -
get( handler_tuple_element<I, T>& e )
722 +
{
 
723 +
    using handler = get_handler<T, P, Ctx>;
 
724 +

 
725 +
    handler_tuple_element(T* t, P* p)
 
726 +
        : t_(t, p)
 
727 +
    {}
 
728 +

 
729 +
    void
 
730 +
    finish() const noexcept
 
731 +
    {}
 
732 +

 
733 +
    handler t_;
 
734 +
};
 
735 +

 
736 +
template<std::size_t I, class P, class Ctx, class T>
 
737 +
typename handler_tuple_element<I, P, Ctx, T>::handler&
 
738 +
get( handler_tuple_element<I, P, Ctx, T>& e )
703  
{
739  
{
704  
    return e.t_;
740  
    return e.t_;
705  
}
741  
}
706  

742  

707  
template<
743  
template<
708  
    class P,
744  
    class P,
 
745 +
    class Ctx,
709  
    class LV,
746  
    class LV,
710  
    class S = mp11::make_index_sequence<mp11::mp_size<LV>::value> >
747  
    class S = mp11::make_index_sequence<mp11::mp_size<LV>::value> >
711  
struct handler_tuple;
748  
struct handler_tuple;
712  

749  

713 -
template< class P, template<class...> class L, class... V, std::size_t... I >
750 +
template< class P, class Ctx, template<class...> class L, class... V, std::size_t... I >
714 -
struct handler_tuple< P, L<V...>, mp11::index_sequence<I...> >
751 +
struct handler_tuple< P, Ctx, L<V...>, mp11::index_sequence<I...> >
715 -
    : handler_tuple_element<I, V>
752 +
    : handler_tuple_element<I, P, Ctx, V>
716  
    ...
753  
    ...
717  
{
754  
{
718  
    handler_tuple( handler_tuple const& ) = delete;
755  
    handler_tuple( handler_tuple const& ) = delete;
719  
    handler_tuple& operator=( handler_tuple const& ) = delete;
756  
    handler_tuple& operator=( handler_tuple const& ) = delete;
720  

757  

721  
    template< class Access, class T >
758  
    template< class Access, class T >
722  
    handler_tuple( Access access, T* pv, P* pp )
759  
    handler_tuple( Access access, T* pv, P* pp )
723 -
        : handler_tuple_element<I, V>(
760 +
        : handler_tuple_element<I, P, Ctx, V>(
724  
            access( pv, mp11::mp_size_t<I>() ),
761  
            access( pv, mp11::mp_size_t<I>() ),
725  
            pp )
762  
            pp )
726  
        ...
763  
        ...
727  
    {}
764  
    {}
 
765 +

 
766 +
    void
 
767 +
    finish()
 
768 +
    {
 
769 +
        auto _ = {
 
770 +
            (static_cast< handler_tuple_element<I, P, Ctx, V>* >(this)
 
771 +
                ->finish(), 0)
 
772 +
            ...};
 
773 +
        (void)_;
 
774 +
    }
728  
};
775  
};
729  

776  

730  
#if defined(BOOST_MSVC) && BOOST_MSVC < 1910
777  
#if defined(BOOST_MSVC) && BOOST_MSVC < 1910
731  

778  

732  
template< class T >
779  
template< class T >
733  
struct tuple_element_list_impl
780  
struct tuple_element_list_impl
734  
{
781  
{
735  
    template< class I >
782  
    template< class I >
736  
    using tuple_element_helper = tuple_element_t<I::value, T>;
783  
    using tuple_element_helper = tuple_element_t<I::value, T>;
737  

784  

738  
    using type = mp11::mp_transform<
785  
    using type = mp11::mp_transform<
739  
        tuple_element_helper,
786  
        tuple_element_helper,
740  
        mp11::mp_iota< std::tuple_size<T> > >;
787  
        mp11::mp_iota< std::tuple_size<T> > >;
741  
};
788  
};
742  
template< class T >
789  
template< class T >
743  
using tuple_element_list = typename tuple_element_list_impl<T>::type;
790  
using tuple_element_list = typename tuple_element_list_impl<T>::type;
744  

791  

745  
#else
792  
#else
746  

793  

747  
template< class I, class T >
794  
template< class I, class T >
748  
using tuple_element_helper = tuple_element_t<I::value, T>;
795  
using tuple_element_helper = tuple_element_t<I::value, T>;
749  
template< class T >
796  
template< class T >
750  
using tuple_element_list = mp11::mp_transform_q<
797  
using tuple_element_list = mp11::mp_transform_q<
751  
    mp11::mp_bind_back< tuple_element_helper, T>,
798  
    mp11::mp_bind_back< tuple_element_helper, T>,
752  
    mp11::mp_iota< std::tuple_size<T> > >;
799  
    mp11::mp_iota< std::tuple_size<T> > >;
753  

800  

754  
#endif
801  
#endif
755  

802  

756  
template< class Op, class... Args>
803  
template< class Op, class... Args>
757  
struct handler_op_invoker
804  
struct handler_op_invoker
758  
{
805  
{
759  
public:
806  
public:
760  
    std::tuple<Args&...> args;
807  
    std::tuple<Args&...> args;
761  

808  

762  
    template< class Handler >
809  
    template< class Handler >
763  
    bool
810  
    bool
764  
    operator()( Handler& handler ) const
811  
    operator()( Handler& handler ) const
765  
    {
812  
    {
766  
        return (*this)( handler, mp11::index_sequence_for<Args...>() );
813  
        return (*this)( handler, mp11::index_sequence_for<Args...>() );
767  
    }
814  
    }
768  

815  

769  
private:
816  
private:
770  
    template< class Handler, std::size_t... I >
817  
    template< class Handler, std::size_t... I >
771  
    bool
818  
    bool
772  
    operator()( Handler& handler, mp11::index_sequence<I...> ) const
819  
    operator()( Handler& handler, mp11::index_sequence<I...> ) const
773  
    {
820  
    {
774  
        return Op()( handler, std::get<I>(args)... );
821  
        return Op()( handler, std::get<I>(args)... );
775  
    }
822  
    }
776  
};
823  
};
777  

824  

778  
template< class Handlers, class F >
825  
template< class Handlers, class F >
779  
struct tuple_handler_op_invoker
826  
struct tuple_handler_op_invoker
780  
{
827  
{
781  
    Handlers& handlers;
828  
    Handlers& handlers;
782  
    F fn;
829  
    F fn;
783  

830  

784  
    template< class I >
831  
    template< class I >
785  
    bool
832  
    bool
786  
    operator()( I ) const
833  
    operator()( I ) const
787  
    {
834  
    {
788  
        return fn( get<I::value>(handlers) );
835  
        return fn( get<I::value>(handlers) );
789  
    }
836  
    }
790  
};
837  
};
791  

838  

792  
struct tuple_accessor
839  
struct tuple_accessor
793  
{
840  
{
794  
    template< class T, class I >
841  
    template< class T, class I >
795  
    auto operator()( T* t, I ) const -> tuple_element_t<I::value, T>*
842  
    auto operator()( T* t, I ) const -> tuple_element_t<I::value, T>*
796  
    {
843  
    {
797  
        using std::get;
844  
        using std::get;
798  
        return &get<I::value>(*t);
845  
        return &get<I::value>(*t);
799  
    }
846  
    }
800  
};
847  
};
801  

848  

802 -
template< class T, class P >
849 +
template<class T, class P, class Ctx>
803 -
class converting_handler<tuple_conversion_tag, T, P>
850 +
class converting_handler<tuple_conversion_tag, T, P, Ctx>
804  
{
851  
{
805  

852  

806  
private:
853  
private:
807  
    using ElementTypes = tuple_element_list<T>;
854  
    using ElementTypes = tuple_element_list<T>;
808 -

855 +
    using HandlerTuple = handler_tuple<converting_handler, Ctx, ElementTypes>;
809 -
    template<class V>
 
810 -
    using ElementHandler = get_handler<V, converting_handler>;
 
811 -
    using InnerHandlers = mp11::mp_transform<ElementHandler, ElementTypes>;
 
812 -
    using HandlerTuple = handler_tuple<converting_handler, InnerHandlers>;
 
813  

856  

814  
    T* value_;
857  
    T* value_;
815  
    P* parent_;
858  
    P* parent_;
816  

859  

817  
    HandlerTuple handlers_;
860  
    HandlerTuple handlers_;
818  
    int inner_active_ = -1;
861  
    int inner_active_ = -1;
819  

862  

820  
public:
863  
public:
821  
    converting_handler( converting_handler const& ) = delete;
864  
    converting_handler( converting_handler const& ) = delete;
822  
    converting_handler& operator=( converting_handler const& ) = delete;
865  
    converting_handler& operator=( converting_handler const& ) = delete;
823  

866  

824  
    converting_handler( T* v, P* p )
867  
    converting_handler( T* v, P* p )
825  
        : value_(v) , parent_(p) , handlers_(tuple_accessor(), v, this)
868  
        : value_(v) , parent_(p) , handlers_(tuple_accessor(), v, this)
826  
    {}
869  
    {}
827  

870  

828  
    bool signal_value(system::error_code&)
871  
    bool signal_value(system::error_code&)
829  
    {
872  
    {
830  
        ++inner_active_;
873  
        ++inner_active_;
831  
        return true;
874  
        return true;
832  
    }
875  
    }
833  

876  

834  
    bool signal_end(system::error_code& ec)
877  
    bool signal_end(system::error_code& ec)
835  
    {
878  
    {
836  
        constexpr int N = std::tuple_size<T>::value;
879  
        constexpr int N = std::tuple_size<T>::value;
837  
        if( inner_active_ < N )
880  
        if( inner_active_ < N )
838  
        {
881  
        {
839  
            BOOST_JSON_FAIL( ec, error::size_mismatch );
882  
            BOOST_JSON_FAIL( ec, error::size_mismatch );
840  
            return false;
883  
            return false;
841  
        }
884  
        }
842  

885  

 
886 +
        handlers_.finish();
843  
        inner_active_ = -1;
887  
        inner_active_ = -1;
844  
        return parent_->signal_value(ec);
888  
        return parent_->signal_value(ec);
845  
    }
889  
    }
846  

890  

847  
#define BOOST_JSON_HANDLE_EVENT(fn) \
891  
#define BOOST_JSON_HANDLE_EVENT(fn) \
848  
    struct do_ ## fn \
892  
    struct do_ ## fn \
849  
    { \
893  
    { \
850  
        template< class H, class... Args > \
894  
        template< class H, class... Args > \
851  
        bool operator()( H& h, Args& ... args ) const \
895  
        bool operator()( H& h, Args& ... args ) const \
852  
        { \
896  
        { \
853  
            return h. fn (args...); \
897  
            return h. fn (args...); \
854  
        } \
898  
        } \
855  
    }; \
899  
    }; \
856  
       \
900  
       \
857  
    template< class... Args > \
901  
    template< class... Args > \
858  
    bool fn( system::error_code& ec, Args&& ... args ) \
902  
    bool fn( system::error_code& ec, Args&& ... args ) \
859  
    { \
903  
    { \
860  
        if( inner_active_ < 0 ) \
904  
        if( inner_active_ < 0 ) \
861  
        { \
905  
        { \
862  
            BOOST_JSON_FAIL( ec, error::not_array ); \
906  
            BOOST_JSON_FAIL( ec, error::not_array ); \
863  
            return false; \
907  
            return false; \
864  
        } \
908  
        } \
865  
        constexpr int N = std::tuple_size<T>::value; \
909  
        constexpr int N = std::tuple_size<T>::value; \
866  
        if( inner_active_ >= N ) \
910  
        if( inner_active_ >= N ) \
867  
        { \
911  
        { \
868  
            BOOST_JSON_FAIL( ec, error::size_mismatch ); \
912  
            BOOST_JSON_FAIL( ec, error::size_mismatch ); \
869  
            return false; \
913  
            return false; \
870  
        } \
914  
        } \
871  
        using F = handler_op_invoker< do_ ## fn, system::error_code, Args...>; \
915  
        using F = handler_op_invoker< do_ ## fn, system::error_code, Args...>; \
872  
        using H = decltype(handlers_); \
916  
        using H = decltype(handlers_); \
873  
        return mp11::mp_with_index<N>( \
917  
        return mp11::mp_with_index<N>( \
874  
            inner_active_, \
918  
            inner_active_, \
875  
            tuple_handler_op_invoker<H, F>{ \
919  
            tuple_handler_op_invoker<H, F>{ \
876  
                handlers_, \
920  
                handlers_, \
877  
                F{ std::forward_as_tuple(ec, args...) } } ); \
921  
                F{ std::forward_as_tuple(ec, args...) } } ); \
878  
    }
922  
    }
879  

923  

880  
    BOOST_JSON_HANDLE_EVENT( on_object_begin )
924  
    BOOST_JSON_HANDLE_EVENT( on_object_begin )
881  
    BOOST_JSON_HANDLE_EVENT( on_object_end )
925  
    BOOST_JSON_HANDLE_EVENT( on_object_end )
882  

926  

883  
    struct do_on_array_begin
927  
    struct do_on_array_begin
884  
    {
928  
    {
885  
        HandlerTuple& handlers;
929  
        HandlerTuple& handlers;
886  
        system::error_code& ec;
930  
        system::error_code& ec;
887  

931  

888  
        template< class I >
932  
        template< class I >
889  
        bool operator()( I ) const
933  
        bool operator()( I ) const
890  
        {
934  
        {
891  
            return get<I::value>(handlers).on_array_begin(ec);
935  
            return get<I::value>(handlers).on_array_begin(ec);
892  
        }
936  
        }
893  
    };
937  
    };
894  
    bool on_array_begin( system::error_code& ec )
938  
    bool on_array_begin( system::error_code& ec )
895  
    {
939  
    {
896  
        if( inner_active_ < 0 )
940  
        if( inner_active_ < 0 )
897  
        {
941  
        {
898  
            inner_active_ = 0;
942  
            inner_active_ = 0;
899  
            return true;
943  
            return true;
900  
        }
944  
        }
901  

945  

902  
        constexpr int N = std::tuple_size<T>::value;
946  
        constexpr int N = std::tuple_size<T>::value;
903  

947  

904  
        if( inner_active_ >= N )
948  
        if( inner_active_ >= N )
905  
        {
949  
        {
906  
            BOOST_JSON_FAIL( ec, error::size_mismatch );
950  
            BOOST_JSON_FAIL( ec, error::size_mismatch );
907  
            return false;
951  
            return false;
908  
        }
952  
        }
909  

953  

910  
        return mp11::mp_with_index<N>(
954  
        return mp11::mp_with_index<N>(
911  
            inner_active_, do_on_array_begin{handlers_, ec} );
955  
            inner_active_, do_on_array_begin{handlers_, ec} );
912  
    }
956  
    }
913  

957  

914  
    struct do_on_array_end
958  
    struct do_on_array_end
915  
    {
959  
    {
916  
        HandlerTuple& handlers;
960  
        HandlerTuple& handlers;
917  
        system::error_code& ec;
961  
        system::error_code& ec;
918  

962  

919  
        template< class I >
963  
        template< class I >
920  
        bool operator()( I ) const
964  
        bool operator()( I ) const
921  
        {
965  
        {
922  
            return get<I::value>(handlers).on_array_end(ec);
966  
            return get<I::value>(handlers).on_array_end(ec);
923  
        }
967  
        }
924  
    };
968  
    };
925  
    bool on_array_end( system::error_code& ec )
969  
    bool on_array_end( system::error_code& ec )
926  
    {
970  
    {
927  
        if( inner_active_ < 0 )
971  
        if( inner_active_ < 0 )
928  
            return parent_->signal_end(ec);
972  
            return parent_->signal_end(ec);
929  

973  

930  
        constexpr int N = std::tuple_size<T>::value;
974  
        constexpr int N = std::tuple_size<T>::value;
931  

975  

932  
        if( inner_active_ >= N )
976  
        if( inner_active_ >= N )
933  
            return signal_end(ec);
977  
            return signal_end(ec);
934  

978  

935  
        return mp11::mp_with_index<N>(
979  
        return mp11::mp_with_index<N>(
936  
            inner_active_, do_on_array_end{handlers_, ec} );
980  
            inner_active_, do_on_array_end{handlers_, ec} );
937  
    }
981  
    }
938  

982  

939  
    BOOST_JSON_HANDLE_EVENT( on_key_part )
983  
    BOOST_JSON_HANDLE_EVENT( on_key_part )
940  
    BOOST_JSON_HANDLE_EVENT( on_key )
984  
    BOOST_JSON_HANDLE_EVENT( on_key )
941  
    BOOST_JSON_HANDLE_EVENT( on_string_part )
985  
    BOOST_JSON_HANDLE_EVENT( on_string_part )
942  
    BOOST_JSON_HANDLE_EVENT( on_string )
986  
    BOOST_JSON_HANDLE_EVENT( on_string )
943  
    BOOST_JSON_HANDLE_EVENT( on_number_part )
987  
    BOOST_JSON_HANDLE_EVENT( on_number_part )
944  
    BOOST_JSON_HANDLE_EVENT( on_int64 )
988  
    BOOST_JSON_HANDLE_EVENT( on_int64 )
945  
    BOOST_JSON_HANDLE_EVENT( on_uint64 )
989  
    BOOST_JSON_HANDLE_EVENT( on_uint64 )
946  
    BOOST_JSON_HANDLE_EVENT( on_double )
990  
    BOOST_JSON_HANDLE_EVENT( on_double )
947  
    BOOST_JSON_HANDLE_EVENT( on_bool )
991  
    BOOST_JSON_HANDLE_EVENT( on_bool )
948  
    BOOST_JSON_HANDLE_EVENT( on_null )
992  
    BOOST_JSON_HANDLE_EVENT( on_null )
949  

993  

950  
#undef BOOST_JSON_HANDLE_EVENT
994  
#undef BOOST_JSON_HANDLE_EVENT
951  
};
995  
};
952  

996  

953  
// described struct handler
997  
// described struct handler
954  
#if defined(BOOST_MSVC) && BOOST_MSVC < 1910
998  
#if defined(BOOST_MSVC) && BOOST_MSVC < 1910
955  

999  

956  
template< class T >
1000  
template< class T >
957  
struct struct_element_list_impl
1001  
struct struct_element_list_impl
958  
{
1002  
{
959  
    template< class D >
1003  
    template< class D >
960  
    using helper = described_member_t<T, D>;
1004  
    using helper = described_member_t<T, D>;
961  

1005  

962  
    using type = mp11::mp_transform< helper, described_members<T> >;
1006  
    using type = mp11::mp_transform< helper, described_members<T> >;
963  
};
1007  
};
964  
template< class T >
1008  
template< class T >
965  
using struct_element_list = typename struct_element_list_impl<T>::type;
1009  
using struct_element_list = typename struct_element_list_impl<T>::type;
966  

1010  

967  
#else
1011  
#else
968  

1012  

969  
template< class T >
1013  
template< class T >
970  
using struct_element_list = mp11::mp_transform_q<
1014  
using struct_element_list = mp11::mp_transform_q<
971  
    mp11::mp_bind_front< described_member_t, T >, described_members<T> >;
1015  
    mp11::mp_bind_front< described_member_t, T >, described_members<T> >;
972  

1016  

973  
#endif
1017  
#endif
974  

1018  

975  
struct struct_accessor
1019  
struct struct_accessor
976  
{
1020  
{
977  
    template< class T >
1021  
    template< class T >
978  
    auto operator()( T*, mp11::mp_size< described_members<T> > ) const
1022  
    auto operator()( T*, mp11::mp_size< described_members<T> > ) const
979  
        -> void*
1023  
        -> void*
980  
    {
1024  
    {
981  
        return nullptr;
1025  
        return nullptr;
982  
    }
1026  
    }
983  

1027  

984  
    template< class T, class I >
1028  
    template< class T, class I >
985  
    auto operator()( T* t, I ) const
1029  
    auto operator()( T* t, I ) const
986  
        -> described_member_t<T, mp11::mp_at< described_members<T>, I> >*
1030  
        -> described_member_t<T, mp11::mp_at< described_members<T>, I> >*
987  
    {
1031  
    {
988  
        using Ds = described_members<T>;
1032  
        using Ds = described_members<T>;
989  
        using D = mp11::mp_at<Ds, I>;
1033  
        using D = mp11::mp_at<Ds, I>;
990  
        return &(t->*D::pointer);
1034  
        return &(t->*D::pointer);
991  
    }
1035  
    }
992  
};
1036  
};
993  

1037  

994  
struct struct_key_searcher
1038  
struct struct_key_searcher
995  
{
1039  
{
996  
    string_view key;
1040  
    string_view key;
997  
    int& found;
1041  
    int& found;
998  
    int index = 0;
1042  
    int index = 0;
999  

1043  

1000  
    struct_key_searcher(string_view key, int& found) noexcept
1044  
    struct_key_searcher(string_view key, int& found) noexcept
1001  
        : key(key), found(found)
1045  
        : key(key), found(found)
1002  
    {}
1046  
    {}
1003  

1047  

1004  
    template< class D >
1048  
    template< class D >
1005  
    void
1049  
    void
1006  
    operator()( D )
1050  
    operator()( D )
1007  
    {
1051  
    {
1008  
        if( key == D::name )
1052  
        if( key == D::name )
1009  
            found = index;
1053  
            found = index;
1010  
        ++index;
1054  
        ++index;
1011  
    }
1055  
    }
1012  
};
1056  
};
1013  

1057  

1014  
template<class P>
1058  
template<class P>
1015  
struct ignoring_handler
1059  
struct ignoring_handler
1016  
{
1060  
{
1017  
    P* parent_;
1061  
    P* parent_;
1018  
    std::size_t array_depth_ = 0;
1062  
    std::size_t array_depth_ = 0;
1019  
    std::size_t object_depth_ = 0;
1063  
    std::size_t object_depth_ = 0;
1020  

1064  

1021  
    ignoring_handler(ignoring_handler const&) = delete;
1065  
    ignoring_handler(ignoring_handler const&) = delete;
1022  
    ignoring_handler& operator=(ignoring_handler const&) = delete;
1066  
    ignoring_handler& operator=(ignoring_handler const&) = delete;
1023  

1067  

1024 -
    ignoring_handler(void*, P* p) noexcept
1068 +
    ignoring_handler(P* p) noexcept
1025  
        : parent_(p)
1069  
        : parent_(p)
1026  
    {}
1070  
    {}
1027  

1071  

1028  
    bool on_object_begin(system::error_code&)
1072  
    bool on_object_begin(system::error_code&)
1029  
    {
1073  
    {
1030  
        ++object_depth_;
1074  
        ++object_depth_;
1031  
        return true;
1075  
        return true;
1032  
    }
1076  
    }
1033  

1077  

1034  
    bool on_object_end(system::error_code& ec)
1078  
    bool on_object_end(system::error_code& ec)
1035  
    {
1079  
    {
1036  
        BOOST_ASSERT( object_depth_ > 0 );
1080  
        BOOST_ASSERT( object_depth_ > 0 );
1037  
        --object_depth_;
1081  
        --object_depth_;
1038  

1082  

1039  
        if( (array_depth_ + object_depth_) == 0 )
1083  
        if( (array_depth_ + object_depth_) == 0 )
1040  
            return parent_->signal_value(ec);
1084  
            return parent_->signal_value(ec);
1041  
        return true;
1085  
        return true;
1042  
    }
1086  
    }
1043  

1087  

1044  
    bool on_array_begin(system::error_code&)
1088  
    bool on_array_begin(system::error_code&)
1045  
    {
1089  
    {
1046  
        ++array_depth_;
1090  
        ++array_depth_;
1047  
        return true;
1091  
        return true;
1048  
    }
1092  
    }
1049  

1093  

1050  
    bool on_array_end(system::error_code& ec)
1094  
    bool on_array_end(system::error_code& ec)
1051  
    {
1095  
    {
1052  
        BOOST_ASSERT( array_depth_ > 0 );
1096  
        BOOST_ASSERT( array_depth_ > 0 );
1053  
        --array_depth_;
1097  
        --array_depth_;
1054  

1098  

1055  
        if( (array_depth_ + object_depth_) == 0 )
1099  
        if( (array_depth_ + object_depth_) == 0 )
1056 -
            return parent_->signal_value(ec);
1100 +
            return parent_->signal_end(ec);
1057  
        return true;
1101  
        return true;
1058  
    }
1102  
    }
1059  

1103  

1060  
    bool on_key_part(system::error_code&, string_view)
1104  
    bool on_key_part(system::error_code&, string_view)
1061  
    {
1105  
    {
1062  
        return true;
1106  
        return true;
1063  
    }
1107  
    }
1064  

1108  

1065  
    bool on_key(system::error_code&, string_view)
1109  
    bool on_key(system::error_code&, string_view)
1066  
    {
1110  
    {
1067  
        return true;
1111  
        return true;
1068  
    }
1112  
    }
1069  

1113  

1070  
    bool on_string_part(system::error_code&, string_view)
1114  
    bool on_string_part(system::error_code&, string_view)
1071  
    {
1115  
    {
1072  
        return true;
1116  
        return true;
1073  
    }
1117  
    }
1074  

1118  

1075  
    bool on_string(system::error_code& ec, string_view)
1119  
    bool on_string(system::error_code& ec, string_view)
1076  
    {
1120  
    {
1077  
        if( (array_depth_ + object_depth_) == 0 )
1121  
        if( (array_depth_ + object_depth_) == 0 )
1078  
            return parent_->signal_value(ec);
1122  
            return parent_->signal_value(ec);
1079  
        return true;
1123  
        return true;
1080  
    }
1124  
    }
1081  

1125  

1082  
    bool on_number_part(system::error_code&)
1126  
    bool on_number_part(system::error_code&)
1083  
    {
1127  
    {
1084  
        return true;
1128  
        return true;
1085  
    }
1129  
    }
1086  

1130  

1087  
    bool on_int64(system::error_code& ec, std::int64_t)
1131  
    bool on_int64(system::error_code& ec, std::int64_t)
1088  
    {
1132  
    {
1089  
        if( (array_depth_ + object_depth_) == 0 )
1133  
        if( (array_depth_ + object_depth_) == 0 )
1090  
            return parent_->signal_value(ec);
1134  
            return parent_->signal_value(ec);
1091  
        return true;
1135  
        return true;
1092  
    }
1136  
    }
1093  

1137  

1094  
    bool on_uint64(system::error_code& ec, std::uint64_t)
1138  
    bool on_uint64(system::error_code& ec, std::uint64_t)
1095  
    {
1139  
    {
1096  
        if( (array_depth_ + object_depth_) == 0 )
1140  
        if( (array_depth_ + object_depth_) == 0 )
1097  
            return parent_->signal_value(ec);
1141  
            return parent_->signal_value(ec);
1098  
        return true;
1142  
        return true;
1099  
    }
1143  
    }
1100  

1144  

1101  
    bool on_double(system::error_code& ec, double)
1145  
    bool on_double(system::error_code& ec, double)
1102  
    {
1146  
    {
1103  
        if( (array_depth_ + object_depth_) == 0 )
1147  
        if( (array_depth_ + object_depth_) == 0 )
1104  
            return parent_->signal_value(ec);
1148  
            return parent_->signal_value(ec);
1105  
        return true;
1149  
        return true;
1106  
    }
1150  
    }
1107  

1151  

1108  
    bool on_bool(system::error_code& ec, bool)
1152  
    bool on_bool(system::error_code& ec, bool)
1109  
    {
1153  
    {
1110  
        if( (array_depth_ + object_depth_) == 0 )
1154  
        if( (array_depth_ + object_depth_) == 0 )
1111  
            return parent_->signal_value(ec);
1155  
            return parent_->signal_value(ec);
1112  
        return true;
1156  
        return true;
1113  
    }
1157  
    }
1114  

1158  

1115  
    bool on_null(system::error_code& ec)
1159  
    bool on_null(system::error_code& ec)
1116  
    {
1160  
    {
1117  
        if( (array_depth_ + object_depth_) == 0 )
1161  
        if( (array_depth_ + object_depth_) == 0 )
1118  
            return parent_->signal_value(ec);
1162  
            return parent_->signal_value(ec);
1119  
        return true;
1163  
        return true;
1120  
    }
1164  
    }
1121  
};
1165  
};
1122  

1166  

1123 -
template<class V, class P>
1167 +
template<class V, class P, class Ctx>
1124 -
class converting_handler<described_class_conversion_tag, V, P>
1168 +
class converting_handler<described_class_conversion_tag, V, P, Ctx>
1125  
{
1169  
{
1126  
#if !defined(BOOST_DESCRIBE_CXX14)
1170  
#if !defined(BOOST_DESCRIBE_CXX14)
1127  

1171  

1128  
    static_assert(
1172  
    static_assert(
1129  
        sizeof(V) == 0, "Struct support for parse_into requires C++14" );
1173  
        sizeof(V) == 0, "Struct support for parse_into requires C++14" );
1130  

1174  

1131  
#else
1175  
#else
1132  

1176  

1133 -
    static_assert(
 
1134 -
        uniquely_named_members<V>::value,
 
1135 -
        "The type has several described members with the same name.");
 
1136 -

 
1137  
private:
1177  
private:
1138  
    using Dm = described_members<V>;
1178  
    using Dm = described_members<V>;
1139  
    using Dt = struct_element_list<V>;
1179  
    using Dt = struct_element_list<V>;
 
1180 +
    using InnerCount = mp11::mp_size<Dt>;
1140  

1181  

1141 -
    template<class T>
1182 +
    handler_tuple<converting_handler, Ctx, Dt> handlers_;
1142 -
    using MemberHandler = get_handler<T, converting_handler>;
1183 +
    ignoring_handler<converting_handler> ignorer_;
1143 -
    using InnerHandlers = mp11::mp_push_back<
1184 +
    std::string key_;
1144 -
        mp11::mp_transform<MemberHandler, Dt>,
 
1145 -
        ignoring_handler<converting_handler> >;
 
1146 -
    using InnerCount = mp11::mp_size<InnerHandlers>;
 
1147 -

 
1148  
    V* value_;
1185  
    V* value_;
1149 -

 
1150 -
    std::string key_;
 
1151 -

 
1152 -
    handler_tuple<converting_handler, InnerHandlers> handlers_;
 
1153  
    P* parent_;
1186  
    P* parent_;
1154  
    int inner_active_ = -1;
1187  
    int inner_active_ = -1;
1155  
    std::size_t activated_ = 0;
1188  
    std::size_t activated_ = 0;
1156  

1189  

1157  
public:
1190  
public:
1158  
    converting_handler( converting_handler const& ) = delete;
1191  
    converting_handler( converting_handler const& ) = delete;
1159  
    converting_handler& operator=( converting_handler const& ) = delete;
1192  
    converting_handler& operator=( converting_handler const& ) = delete;
1160  

1193  

1161  
    converting_handler( V* v, P* p )
1194  
    converting_handler( V* v, P* p )
1162 -
        : value_(v), parent_(p), handlers_(struct_accessor(), v, this)
1195 +
        : handlers_(struct_accessor(), v, this)
 
1196 +
        , ignorer_(this)
 
1197 +
        , value_(v)
 
1198 +
        , parent_(p)
1163  
    {}
1199  
    {}
1164  

1200  

1165  
    struct is_required_checker
1201  
    struct is_required_checker
1166 -
        bool operator()( mp11::mp_size<Dt> ) const noexcept
 
1167 -
        {
 
1168 -
            return false;
 
1169 -
        }
 
1170 -

 
1171  
    {
1202  
    {
1172  
        template< class I >
1203  
        template< class I >
1173  
        auto operator()( I ) const noexcept
1204  
        auto operator()( I ) const noexcept
1174  
        {
1205  
        {
1175  
            using T = mp11::mp_at<Dt, I>;
1206  
            using T = mp11::mp_at<Dt, I>;
1176  
            return !is_optional_like<T>::value;
1207  
            return !is_optional_like<T>::value;
1177  
        }
1208  
        }
1178 -

 
1179  
    };
1209  
    };
1180  
    bool signal_value(system::error_code&)
1210  
    bool signal_value(system::error_code&)
1181  
    {
1211  
    {
1182  
        BOOST_ASSERT( inner_active_ >= 0 );
1212  
        BOOST_ASSERT( inner_active_ >= 0 );
1183 -
        bool required_member = mp11::mp_with_index<InnerCount>(
1213 +
        if( static_cast<std::size_t>(inner_active_) < InnerCount::value )
1184 -
            inner_active_,
1214 +
        {
1185 -
            is_required_checker{});
1215 +
            bool required_member = mp11::mp_with_index<InnerCount>(
1186 -
        if( required_member )
1216 +
                inner_active_,
1187 -
            ++activated_;
1217 +
                is_required_checker{});
 
1218 +
            if( required_member )
 
1219 +
                ++activated_;
 
1220 +
        }
1188  

1221  

1189  
        key_ = {};
1222  
        key_ = {};
1190  
        inner_active_ = -1;
1223  
        inner_active_ = -1;
1191  
        return true;
1224  
        return true;
1192  
    }
1225  
    }
1193  

1226  

1194  
    bool signal_end(system::error_code& ec)
1227  
    bool signal_end(system::error_code& ec)
1195  
    {
1228  
    {
1196  
        key_ = {};
1229  
        key_ = {};
1197  
        inner_active_ = -1;
1230  
        inner_active_ = -1;
1198  
        return parent_->signal_value(ec);
1231  
        return parent_->signal_value(ec);
1199  
    }
1232  
    }
1200  

1233  

1201  
#define BOOST_JSON_INVOKE_INNER(fn) \
1234  
#define BOOST_JSON_INVOKE_INNER(fn) \
1202  
    if( inner_active_ < 0 ) \
1235  
    if( inner_active_ < 0 ) \
1203  
    { \
1236  
    { \
1204  
        BOOST_JSON_FAIL( ec, error::not_object ); \
1237  
        BOOST_JSON_FAIL( ec, error::not_object ); \
1205  
        return false; \
1238  
        return false; \
1206  
    } \
1239  
    } \
 
1240 +
    if(inner_active_ == InnerCount::value) \
 
1241 +
        return ignorer_.fn; \
1207  
    auto f = [&](auto& handler) { return handler.fn ; }; \
1242  
    auto f = [&](auto& handler) { return handler.fn ; }; \
1208  
    using F = decltype(f); \
1243  
    using F = decltype(f); \
1209  
    using H = decltype(handlers_); \
1244  
    using H = decltype(handlers_); \
1210  
    return mp11::mp_with_index<InnerCount>( \
1245  
    return mp11::mp_with_index<InnerCount>( \
1211  
            inner_active_, \
1246  
            inner_active_, \
1212  
            tuple_handler_op_invoker<H, F>{handlers_, f} );
1247  
            tuple_handler_op_invoker<H, F>{handlers_, f} );
1213  

1248  

1214  
    bool on_object_begin( system::error_code& ec )
1249  
    bool on_object_begin( system::error_code& ec )
1215  
    {
1250  
    {
1216  
        if( inner_active_ < 0 )
1251  
        if( inner_active_ < 0 )
1217  
            return true;
1252  
            return true;
1218  

1253  

1219  
        BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1254  
        BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1220  
    }
1255  
    }
1221  

1256  

1222  
    bool on_object_end( system::error_code& ec )
1257  
    bool on_object_end( system::error_code& ec )
1223  
    {
1258  
    {
1224  
        if( inner_active_ < 0 )
1259  
        if( inner_active_ < 0 )
1225  
        {
1260  
        {
1226  
            using C = mp11::mp_count_if<Dt, is_optional_like>;
1261  
            using C = mp11::mp_count_if<Dt, is_optional_like>;
1227  
            constexpr int N = mp11::mp_size<Dt>::value - C::value;
1262  
            constexpr int N = mp11::mp_size<Dt>::value - C::value;
1228  
            if( activated_ < N )
1263  
            if( activated_ < N )
1229  
            {
1264  
            {
1230  
                BOOST_JSON_FAIL( ec, error::size_mismatch );
1265  
                BOOST_JSON_FAIL( ec, error::size_mismatch );
1231  
                return false;
1266  
                return false;
1232  
            }
1267  
            }
1233  

1268  

 
1269 +
            handlers_.finish();
1234  
            return parent_->signal_value(ec);
1270  
            return parent_->signal_value(ec);
1235  
        }
1271  
        }
1236  

1272  

1237  
        BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1273  
        BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1238  
    }
1274  
    }
1239  

1275  

1240  
    bool on_array_begin( system::error_code& ec )
1276  
    bool on_array_begin( system::error_code& ec )
1241  
    {
1277  
    {
1242  
        BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1278  
        BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1243  
    }
1279  
    }
1244  

1280  

1245  
    bool on_array_end( system::error_code& ec )
1281  
    bool on_array_end( system::error_code& ec )
1246  
    {
1282  
    {
1247  
        if( inner_active_ < 0 )
1283  
        if( inner_active_ < 0 )
1248  
            return parent_->signal_end(ec);
1284  
            return parent_->signal_end(ec);
1249  

1285  

1250  
        BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1286  
        BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1251  
    }
1287  
    }
1252  

1288  

1253  
    bool on_key_part( system::error_code& ec, string_view sv )
1289  
    bool on_key_part( system::error_code& ec, string_view sv )
1254  
    {
1290  
    {
1255  
        if( inner_active_ < 0 )
1291  
        if( inner_active_ < 0 )
1256  
        {
1292  
        {
1257  
            key_.append( sv.data(), sv.size() );
1293  
            key_.append( sv.data(), sv.size() );
1258  
            return true;
1294  
            return true;
1259  
        }
1295  
        }
1260  

1296  

1261  
        BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1297  
        BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1262  
    }
1298  
    }
1263  

1299  

1264  
    bool on_key( system::error_code& ec, string_view sv )
1300  
    bool on_key( system::error_code& ec, string_view sv )
1265  
    {
1301  
    {
1266  
        if( inner_active_ >= 0 )
1302  
        if( inner_active_ >= 0 )
1267  
        {
1303  
        {
1268  
            BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1304  
            BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1269  
        }
1305  
        }
1270  

1306  

1271  
        string_view key = sv;
1307  
        string_view key = sv;
1272  
        if( !key_.empty() )
1308  
        if( !key_.empty() )
1273  
        {
1309  
        {
1274  
            key_.append( sv.data(), sv.size() );
1310  
            key_.append( sv.data(), sv.size() );
1275  
            key = key_;
1311  
            key = key_;
1276  
        }
1312  
        }
1277  

1313  

1278 -
        inner_active_ = InnerCount::value - 1;
1314 +
        inner_active_ = InnerCount::value;
1279  
        mp11::mp_for_each<Dm>( struct_key_searcher(key, inner_active_) );
1315  
        mp11::mp_for_each<Dm>( struct_key_searcher(key, inner_active_) );
1280  
        return true;
1316  
        return true;
1281  
    }
1317  
    }
1282  

1318  

1283  
    bool on_string_part( system::error_code& ec, string_view sv )
1319  
    bool on_string_part( system::error_code& ec, string_view sv )
1284  
    {
1320  
    {
1285  
        BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1321  
        BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1286  
    }
1322  
    }
1287  

1323  

1288  
    bool on_string( system::error_code& ec, string_view sv )
1324  
    bool on_string( system::error_code& ec, string_view sv )
1289  
    {
1325  
    {
1290  
        BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1326  
        BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1291  
    }
1327  
    }
1292  

1328  

1293  
    bool on_number_part( system::error_code& ec )
1329  
    bool on_number_part( system::error_code& ec )
1294  
    {
1330  
    {
1295  
        BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1331  
        BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1296  
    }
1332  
    }
1297  

1333  

1298  
    bool on_int64( system::error_code& ec, std::int64_t v )
1334  
    bool on_int64( system::error_code& ec, std::int64_t v )
1299  
    {
1335  
    {
1300  
        BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1336  
        BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1301  
    }
1337  
    }
1302  

1338  

1303  
    bool on_uint64( system::error_code& ec, std::uint64_t v )
1339  
    bool on_uint64( system::error_code& ec, std::uint64_t v )
1304  
    {
1340  
    {
1305  
        BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1341  
        BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1306  
    }
1342  
    }
1307  

1343  

1308  
    bool on_double( system::error_code& ec, double v )
1344  
    bool on_double( system::error_code& ec, double v )
1309  
    {
1345  
    {
1310  
        BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1346  
        BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1311  
    }
1347  
    }
1312  

1348  

1313  
    bool on_bool( system::error_code& ec, bool v )
1349  
    bool on_bool( system::error_code& ec, bool v )
1314  
    {
1350  
    {
1315  
        BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1351  
        BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1316  
    }
1352  
    }
1317  

1353  

1318  
    bool on_null( system::error_code& ec )
1354  
    bool on_null( system::error_code& ec )
1319  
    {
1355  
    {
1320  
        BOOST_JSON_INVOKE_INNER( on_null(ec) );
1356  
        BOOST_JSON_INVOKE_INNER( on_null(ec) );
1321  
    }
1357  
    }
1322  

1358  

1323  
#undef BOOST_JSON_INVOKE_INNER
1359  
#undef BOOST_JSON_INVOKE_INNER
1324  

1360  

1325  
#endif
1361  
#endif
1326  
};
1362  
};
1327  

1363  

1328  
// variant handler
1364  
// variant handler
1329  
struct object_begin_handler_event
1365  
struct object_begin_handler_event
1330  
{ };
1366  
{ };
1331  

1367  

1332  
struct object_end_handler_event
1368  
struct object_end_handler_event
1333  
{ };
1369  
{ };
1334  

1370  

1335  
struct array_begin_handler_event
1371  
struct array_begin_handler_event
1336  
{ };
1372  
{ };
1337  

1373  

1338  
struct array_end_handler_event
1374  
struct array_end_handler_event
1339  
{ };
1375  
{ };
1340  

1376  

1341  
struct key_handler_event
1377  
struct key_handler_event
1342  
{
1378  
{
1343  
    std::string value;
1379  
    std::string value;
1344  
};
1380  
};
1345  

1381  

1346  
struct string_handler_event
1382  
struct string_handler_event
1347  
{
1383  
{
1348  
    std::string value;
1384  
    std::string value;
1349  
};
1385  
};
1350  

1386  

1351  
struct int64_handler_event
1387  
struct int64_handler_event
1352  
{
1388  
{
1353  
    std::int64_t value;
1389  
    std::int64_t value;
1354  
};
1390  
};
1355  

1391  

1356  
struct uint64_handler_event
1392  
struct uint64_handler_event
1357  
{
1393  
{
1358  
    std::uint64_t value;
1394  
    std::uint64_t value;
1359  
};
1395  
};
1360  

1396  

1361  
struct double_handler_event
1397  
struct double_handler_event
1362  
{
1398  
{
1363  
    double value;
1399  
    double value;
1364  
};
1400  
};
1365  

1401  

1366  
struct bool_handler_event
1402  
struct bool_handler_event
1367  
{
1403  
{
1368  
    bool value;
1404  
    bool value;
1369  
};
1405  
};
1370  

1406  

1371  
struct null_handler_event
1407  
struct null_handler_event
1372  
{ };
1408  
{ };
1373  

1409  

1374  
using parse_event = variant2::variant<
1410  
using parse_event = variant2::variant<
1375  
    object_begin_handler_event,
1411  
    object_begin_handler_event,
1376  
    object_end_handler_event,
1412  
    object_end_handler_event,
1377  
    array_begin_handler_event,
1413  
    array_begin_handler_event,
1378  
    array_end_handler_event,
1414  
    array_end_handler_event,
1379  
    key_handler_event,
1415  
    key_handler_event,
1380  
    string_handler_event,
1416  
    string_handler_event,
1381  
    int64_handler_event,
1417  
    int64_handler_event,
1382  
    uint64_handler_event,
1418  
    uint64_handler_event,
1383  
    double_handler_event,
1419  
    double_handler_event,
1384  
    bool_handler_event,
1420  
    bool_handler_event,
1385  
    null_handler_event>;
1421  
    null_handler_event>;
1386  

1422  

1387  
template< class H >
1423  
template< class H >
1388  
struct event_visitor
1424  
struct event_visitor
1389  
{
1425  
{
1390  
    H& handler;
1426  
    H& handler;
1391  
    system::error_code& ec;
1427  
    system::error_code& ec;
1392  

1428  

1393  
    bool
1429  
    bool
1394  
    operator()(object_begin_handler_event&) const
1430  
    operator()(object_begin_handler_event&) const
1395  
    {
1431  
    {
1396  
        return handler.on_object_begin(ec);
1432  
        return handler.on_object_begin(ec);
1397  
    }
1433  
    }
1398  

1434  

1399  
    bool
1435  
    bool
1400  
    operator()(object_end_handler_event&) const
1436  
    operator()(object_end_handler_event&) const
1401  
    {
1437  
    {
1402  
        return handler.on_object_end(ec);
1438  
        return handler.on_object_end(ec);
1403  
    }
1439  
    }
1404  

1440  

1405  
    bool
1441  
    bool
1406  
    operator()(array_begin_handler_event&) const
1442  
    operator()(array_begin_handler_event&) const
1407  
    {
1443  
    {
1408  
        return handler.on_array_begin(ec);
1444  
        return handler.on_array_begin(ec);
1409  
    }
1445  
    }
1410  

1446  

1411  
    bool
1447  
    bool
1412  
    operator()(array_end_handler_event&) const
1448  
    operator()(array_end_handler_event&) const
1413  
    {
1449  
    {
1414  
        return handler.on_array_end(ec);
1450  
        return handler.on_array_end(ec);
1415  
    }
1451  
    }
1416  

1452  

1417  
    bool
1453  
    bool
1418  
    operator()(key_handler_event& ev) const
1454  
    operator()(key_handler_event& ev) const
1419  
    {
1455  
    {
1420  
        return handler.on_key(ec, ev.value);
1456  
        return handler.on_key(ec, ev.value);
1421  
    }
1457  
    }
1422  

1458  

1423  
    bool
1459  
    bool
1424  
    operator()(string_handler_event& ev) const
1460  
    operator()(string_handler_event& ev) const
1425  
    {
1461  
    {
1426  
        return handler.on_string(ec, ev.value);
1462  
        return handler.on_string(ec, ev.value);
1427  
    }
1463  
    }
1428  

1464  

1429  
    bool
1465  
    bool
1430  
    operator()(int64_handler_event& ev) const
1466  
    operator()(int64_handler_event& ev) const
1431  
    {
1467  
    {
1432  
        return handler.on_int64(ec, ev.value);
1468  
        return handler.on_int64(ec, ev.value);
1433  
    }
1469  
    }
1434  

1470  

1435  
    bool
1471  
    bool
1436  
    operator()(uint64_handler_event& ev) const
1472  
    operator()(uint64_handler_event& ev) const
1437  
    {
1473  
    {
1438  
        return handler.on_uint64(ec, ev.value);
1474  
        return handler.on_uint64(ec, ev.value);
1439  
    }
1475  
    }
1440  

1476  

1441  
    bool
1477  
    bool
1442  
    operator()(double_handler_event& ev) const
1478  
    operator()(double_handler_event& ev) const
1443  
    {
1479  
    {
1444  
        return handler.on_double(ec, ev.value);
1480  
        return handler.on_double(ec, ev.value);
1445  
    }
1481  
    }
1446  

1482  

1447  
    bool
1483  
    bool
1448  
    operator()(bool_handler_event& ev) const
1484  
    operator()(bool_handler_event& ev) const
1449  
    {
1485  
    {
1450  
        return handler.on_bool(ec, ev.value);
1486  
        return handler.on_bool(ec, ev.value);
1451  
    }
1487  
    }
1452  

1488  

1453  
    bool
1489  
    bool
1454  
    operator()(null_handler_event&) const
1490  
    operator()(null_handler_event&) const
1455  
    {
1491  
    {
1456  
        return handler.on_null(ec);
1492  
        return handler.on_null(ec);
1457  
    }
1493  
    }
1458  
};
1494  
};
1459  

1495  

1460 -
// L<T...> -> variant< monostate, get_handler<T, P>... >
1496 +
// L<T...> -> variant< monostate, get_handler<T, P, Ctx>... >
1461 -
template< class P, class L >
1497 +
template<class P, class L, class Ctx>
1462  
using inner_handler_variant = mp11::mp_push_front<
1498  
using inner_handler_variant = mp11::mp_push_front<
1463  
    mp11::mp_transform_q<
1499  
    mp11::mp_transform_q<
1464 -
        mp11::mp_bind_back<get_handler, P>,
1500 +
        mp11::mp_bind_back<get_handler, P, Ctx>,
1465  
        mp11::mp_apply<variant2::variant, L>>,
1501  
        mp11::mp_apply<variant2::variant, L>>,
1466  
    variant2::monostate>;
1502  
    variant2::monostate>;
1467  

1503  

1468 -
template< class T, class P >
1504 +
template<class T, class P, class Ctx>
1469 -
class converting_handler<variant_conversion_tag, T, P>
1505 +
class converting_handler<variant_conversion_tag, T, P, Ctx>
1470  
{
1506  
{
1471  
private:
1507  
private:
1472  
    using variant_size = mp11::mp_size<T>;
1508  
    using variant_size = mp11::mp_size<T>;
1473  

1509  

1474  
    T* value_;
1510  
    T* value_;
1475  
    P* parent_;
1511  
    P* parent_;
1476  

1512  

1477  
    std::string string_;
1513  
    std::string string_;
1478  
    std::vector< parse_event > events_;
1514  
    std::vector< parse_event > events_;
1479 -
    inner_handler_variant<converting_handler, T> inner_;
1515 +
    inner_handler_variant<converting_handler, T, Ctx> inner_;
1480  
    int inner_active_ = -1;
1516  
    int inner_active_ = -1;
1481  

1517  

1482  
public:
1518  
public:
1483  
    converting_handler( converting_handler const& ) = delete;
1519  
    converting_handler( converting_handler const& ) = delete;
1484  
    converting_handler& operator=( converting_handler const& ) = delete;
1520  
    converting_handler& operator=( converting_handler const& ) = delete;
1485  

1521  

1486  
    converting_handler( T* v, P* p )
1522  
    converting_handler( T* v, P* p )
1487  
        : value_( v )
1523  
        : value_( v )
1488  
        , parent_( p )
1524  
        , parent_( p )
1489  
    {}
1525  
    {}
1490  

1526  

1491  
    bool signal_value(system::error_code& ec)
1527  
    bool signal_value(system::error_code& ec)
1492  
    {
1528  
    {
1493  
        inner_.template emplace<0>();
1529  
        inner_.template emplace<0>();
1494  
        inner_active_ = -1;
1530  
        inner_active_ = -1;
1495  
        events_.clear();
1531  
        events_.clear();
1496  
        return parent_->signal_value(ec);
1532  
        return parent_->signal_value(ec);
1497  
    }
1533  
    }
1498  

1534  

1499  
    bool signal_end(system::error_code& ec)
1535  
    bool signal_end(system::error_code& ec)
1500  
    {
1536  
    {
1501  
        return parent_->signal_end(ec);
1537  
        return parent_->signal_end(ec);
1502  
    }
1538  
    }
1503  

1539  

1504  
    struct alternative_selector
1540  
    struct alternative_selector
1505  
    {
1541  
    {
1506  
        converting_handler* self;
1542  
        converting_handler* self;
1507  

1543  

1508  
        template< class I >
1544  
        template< class I >
1509  
        void
1545  
        void
1510  
        operator()( I ) const
1546  
        operator()( I ) const
1511  
        {
1547  
        {
1512  
            using V = mp11::mp_at<T, I>;
1548  
            using V = mp11::mp_at<T, I>;
1513  
            auto& v = self->value_->template emplace<I::value>( V{} );
1549  
            auto& v = self->value_->template emplace<I::value>( V{} );
1514  
            self->inner_.template emplace<I::value + 1>(&v, self);
1550  
            self->inner_.template emplace<I::value + 1>(&v, self);
1515  
        }
1551  
        }
1516  
    };
1552  
    };
1517  
    void
1553  
    void
1518  
    next_alternative()
1554  
    next_alternative()
1519  
    {
1555  
    {
1520  
        if( ++inner_active_ >= static_cast<int>(variant_size::value) )
1556  
        if( ++inner_active_ >= static_cast<int>(variant_size::value) )
1521  
            return;
1557  
            return;
1522  

1558  

1523  
        mp11::mp_with_index< variant_size::value >(
1559  
        mp11::mp_with_index< variant_size::value >(
1524  
            inner_active_, alternative_selector{this} );
1560  
            inner_active_, alternative_selector{this} );
1525  
    }
1561  
    }
1526  

1562  

1527  
    struct event_processor
1563  
    struct event_processor
1528  
    {
1564  
    {
1529  
        converting_handler* self;
1565  
        converting_handler* self;
1530  
        system::error_code& ec;
1566  
        system::error_code& ec;
1531  
        parse_event& event;
1567  
        parse_event& event;
1532  

1568  

1533  
        template< class I >
1569  
        template< class I >
1534  
        bool operator()( I ) const
1570  
        bool operator()( I ) const
1535  
        {
1571  
        {
1536  
            auto& handler = variant2::get<I::value + 1>(self->inner_);
1572  
            auto& handler = variant2::get<I::value + 1>(self->inner_);
1537  
            using Handler = remove_cvref<decltype(handler)>;
1573  
            using Handler = remove_cvref<decltype(handler)>;
1538  
            return variant2::visit(
1574  
            return variant2::visit(
1539  
                event_visitor<Handler>{handler, ec}, event );
1575  
                event_visitor<Handler>{handler, ec}, event );
1540  
        }
1576  
        }
1541  
    };
1577  
    };
1542  
    bool process_events(system::error_code& ec)
1578  
    bool process_events(system::error_code& ec)
1543  
    {
1579  
    {
1544  
        constexpr std::size_t N = variant_size::value;
1580  
        constexpr std::size_t N = variant_size::value;
1545  

1581  

1546  
        // should be pointers not iterators, otherwise MSVC crashes
1582  
        // should be pointers not iterators, otherwise MSVC crashes
1547  
        auto const last = events_.data() + events_.size();
1583  
        auto const last = events_.data() + events_.size();
1548  
        auto first = last - 1;
1584  
        auto first = last - 1;
1549  
        bool ok = false;
1585  
        bool ok = false;
1550  

1586  

1551  
        if( inner_active_ < 0 )
1587  
        if( inner_active_ < 0 )
1552  
            next_alternative();
1588  
            next_alternative();
1553  
        do
1589  
        do
1554  
        {
1590  
        {
1555  
            if( static_cast<std::size_t>(inner_active_) >= N )
1591  
            if( static_cast<std::size_t>(inner_active_) >= N )
1556  
            {
1592  
            {
1557  
                BOOST_JSON_FAIL( ec, error::exhausted_variants );
1593  
                BOOST_JSON_FAIL( ec, error::exhausted_variants );
1558  
                return false;
1594  
                return false;
1559  
            }
1595  
            }
1560  

1596  

1561  
            for ( ; first != last; ++first )
1597  
            for ( ; first != last; ++first )
1562  
            {
1598  
            {
1563  
                ok = mp11::mp_with_index< N >(
1599  
                ok = mp11::mp_with_index< N >(
1564  
                    inner_active_, event_processor{this, ec, *first} );
1600  
                    inner_active_, event_processor{this, ec, *first} );
1565  
                if( !ok )
1601  
                if( !ok )
1566  
                {
1602  
                {
1567  
                    first = events_.data();
1603  
                    first = events_.data();
1568  
                    next_alternative();
1604  
                    next_alternative();
1569  
                    ec.clear();
1605  
                    ec.clear();
1570  
                    break;
1606  
                    break;
1571  
                }
1607  
                }
1572  
            }
1608  
            }
1573  
        }
1609  
        }
1574  
        while( !ok );
1610  
        while( !ok );
1575  

1611  

1576  
        return true;
1612  
        return true;
1577  
    }
1613  
    }
1578  

1614  

1579  
#define BOOST_JSON_INVOKE_INNER(ev, ec) \
1615  
#define BOOST_JSON_INVOKE_INNER(ev, ec) \
1580  
    events_.emplace_back( ev ); \
1616  
    events_.emplace_back( ev ); \
1581  
    return process_events(ec);
1617  
    return process_events(ec);
1582  

1618  

1583  
    bool on_object_begin( system::error_code& ec )
1619  
    bool on_object_begin( system::error_code& ec )
1584  
    {
1620  
    {
1585  
        BOOST_JSON_INVOKE_INNER( object_begin_handler_event{}, ec );
1621  
        BOOST_JSON_INVOKE_INNER( object_begin_handler_event{}, ec );
1586  
    }
1622  
    }
1587  

1623  

1588  
    bool on_object_end( system::error_code& ec )
1624  
    bool on_object_end( system::error_code& ec )
1589  
    {
1625  
    {
1590  
        BOOST_JSON_INVOKE_INNER( object_end_handler_event{}, ec );
1626  
        BOOST_JSON_INVOKE_INNER( object_end_handler_event{}, ec );
1591  
    }
1627  
    }
1592  

1628  

1593  
    bool on_array_begin( system::error_code& ec )
1629  
    bool on_array_begin( system::error_code& ec )
1594  
    {
1630  
    {
1595  
        BOOST_JSON_INVOKE_INNER( array_begin_handler_event{}, ec );
1631  
        BOOST_JSON_INVOKE_INNER( array_begin_handler_event{}, ec );
1596  
    }
1632  
    }
1597  

1633  

1598  
    bool on_array_end( system::error_code& ec )
1634  
    bool on_array_end( system::error_code& ec )
1599  
    {
1635  
    {
1600  
        if( !inner_active_ )
1636  
        if( !inner_active_ )
1601  
            return signal_end(ec);
1637  
            return signal_end(ec);
1602  

1638  

1603  
        BOOST_JSON_INVOKE_INNER( array_end_handler_event{}, ec );
1639  
        BOOST_JSON_INVOKE_INNER( array_end_handler_event{}, ec );
1604  
    }
1640  
    }
1605  

1641  

1606  
    bool on_key_part( system::error_code&, string_view sv )
1642  
    bool on_key_part( system::error_code&, string_view sv )
1607  
    {
1643  
    {
1608  
        string_.append(sv);
1644  
        string_.append(sv);
1609  
        return true;
1645  
        return true;
1610  
    }
1646  
    }
1611  

1647  

1612  
    bool on_key( system::error_code& ec, string_view sv )
1648  
    bool on_key( system::error_code& ec, string_view sv )
1613  
    {
1649  
    {
1614  
        string_.append(sv);
1650  
        string_.append(sv);
1615  
        BOOST_JSON_INVOKE_INNER( key_handler_event{ std::move(string_) }, ec );
1651  
        BOOST_JSON_INVOKE_INNER( key_handler_event{ std::move(string_) }, ec );
1616  
    }
1652  
    }
1617  

1653  

1618  
    bool on_string_part( system::error_code&, string_view sv )
1654  
    bool on_string_part( system::error_code&, string_view sv )
1619  
    {
1655  
    {
1620  
        string_.append(sv);
1656  
        string_.append(sv);
1621  
        return true;
1657  
        return true;
1622  
    }
1658  
    }
1623  

1659  

1624  
    bool on_string( system::error_code& ec, string_view sv )
1660  
    bool on_string( system::error_code& ec, string_view sv )
1625  
    {
1661  
    {
1626  
        string_.append(sv);
1662  
        string_.append(sv);
1627  
        BOOST_JSON_INVOKE_INNER(
1663  
        BOOST_JSON_INVOKE_INNER(
1628  
            string_handler_event{ std::move(string_) }, ec );
1664  
            string_handler_event{ std::move(string_) }, ec );
1629  
    }
1665  
    }
1630  

1666  

1631  
    bool on_number_part( system::error_code& )
1667  
    bool on_number_part( system::error_code& )
1632  
    {
1668  
    {
1633  
        return true;
1669  
        return true;
1634  
    }
1670  
    }
1635  

1671  

1636  
    bool on_int64( system::error_code& ec, std::int64_t v )
1672  
    bool on_int64( system::error_code& ec, std::int64_t v )
1637  
    {
1673  
    {
1638  
        BOOST_JSON_INVOKE_INNER( int64_handler_event{v}, ec );
1674  
        BOOST_JSON_INVOKE_INNER( int64_handler_event{v}, ec );
1639  
    }
1675  
    }
1640  

1676  

1641  
    bool on_uint64( system::error_code& ec, std::uint64_t v )
1677  
    bool on_uint64( system::error_code& ec, std::uint64_t v )
1642  
    {
1678  
    {
1643  
        BOOST_JSON_INVOKE_INNER( uint64_handler_event{v}, ec );
1679  
        BOOST_JSON_INVOKE_INNER( uint64_handler_event{v}, ec );
1644  
    }
1680  
    }
1645  

1681  

1646  
    bool on_double( system::error_code& ec, double v )
1682  
    bool on_double( system::error_code& ec, double v )
1647  
    {
1683  
    {
1648  
        BOOST_JSON_INVOKE_INNER( double_handler_event{v}, ec );
1684  
        BOOST_JSON_INVOKE_INNER( double_handler_event{v}, ec );
1649  
    }
1685  
    }
1650  

1686  

1651  
    bool on_bool( system::error_code& ec, bool v )
1687  
    bool on_bool( system::error_code& ec, bool v )
1652  
    {
1688  
    {
1653  
        BOOST_JSON_INVOKE_INNER( bool_handler_event{v}, ec );
1689  
        BOOST_JSON_INVOKE_INNER( bool_handler_event{v}, ec );
1654  
    }
1690  
    }
1655  

1691  

1656  
    bool on_null( system::error_code& ec )
1692  
    bool on_null( system::error_code& ec )
1657  
    {
1693  
    {
1658  
        BOOST_JSON_INVOKE_INNER( null_handler_event{}, ec );
1694  
        BOOST_JSON_INVOKE_INNER( null_handler_event{}, ec );
1659  
    }
1695  
    }
1660  

1696  

1661  
#undef BOOST_JSON_INVOKE_INNER
1697  
#undef BOOST_JSON_INVOKE_INNER
1662  
};
1698  
};
1663  

1699  

1664  
// optional handler
1700  
// optional handler
1665 -
template<class V, class P>
1701 +
template<class V, class P, class Ctx>
1666 -
class converting_handler<optional_conversion_tag, V, P>
1702 +
class converting_handler<optional_conversion_tag, V, P, Ctx>
1667  
{
1703  
{
1668  
private:
1704  
private:
1669  
    using inner_type = value_result_type<V>;
1705  
    using inner_type = value_result_type<V>;
1670 -
    using inner_handler_type = get_handler<inner_type, converting_handler>;
1706 +
    using inner_rep = conversion_representation<inner_type, Ctx>;
 
1707 +
    using inner_handler_type = get_handler<
 
1708 +
        inner_rep, converting_handler, Ctx>;
1671  

1709  

1672  
    V* value_;
1710  
    V* value_;
1673  
    P* parent_;
1711  
    P* parent_;
1674  

1712  

1675 -
    inner_type inner_value_ = {};
1713 +
    inner_rep inner_value_ = {};
1676  
    inner_handler_type inner_;
1714  
    inner_handler_type inner_;
1677  
    bool inner_active_ = false;
1715  
    bool inner_active_ = false;
1678  

1716  

1679  
public:
1717  
public:
1680  
    converting_handler( converting_handler const& ) = delete;
1718  
    converting_handler( converting_handler const& ) = delete;
1681  
    converting_handler& operator=( converting_handler const& ) = delete;
1719  
    converting_handler& operator=( converting_handler const& ) = delete;
1682  

1720  

1683  
    converting_handler( V* v, P* p )
1721  
    converting_handler( V* v, P* p )
1684  
        : value_(v), parent_(p), inner_(&inner_value_, this)
1722  
        : value_(v), parent_(p), inner_(&inner_value_, this)
1685  
    {}
1723  
    {}
1686  

1724  

1687  
    bool signal_value(system::error_code& ec)
1725  
    bool signal_value(system::error_code& ec)
1688  
    {
1726  
    {
1689  
        *value_ = std::move(inner_value_);
1727  
        *value_ = std::move(inner_value_);
1690  

1728  

1691  
        inner_active_ = false;
1729  
        inner_active_ = false;
1692  
        return parent_->signal_value(ec);
1730  
        return parent_->signal_value(ec);
1693  
    }
1731  
    }
1694  

1732  

1695  
    bool signal_end(system::error_code& ec)
1733  
    bool signal_end(system::error_code& ec)
1696  
    {
1734  
    {
1697  
        return parent_->signal_end(ec);
1735  
        return parent_->signal_end(ec);
1698  
    }
1736  
    }
1699  

1737  

1700  
#define BOOST_JSON_INVOKE_INNER(fn) \
1738  
#define BOOST_JSON_INVOKE_INNER(fn) \
1701  
    if( !inner_active_ ) \
1739  
    if( !inner_active_ ) \
1702  
        inner_active_ = true; \
1740  
        inner_active_ = true; \
1703  
    return inner_.fn;
1741  
    return inner_.fn;
1704  

1742  

1705  
    bool on_object_begin( system::error_code& ec )
1743  
    bool on_object_begin( system::error_code& ec )
1706  
    {
1744  
    {
1707  
        BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1745  
        BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1708  
    }
1746  
    }
1709  

1747  

1710  
    bool on_object_end( system::error_code& ec )
1748  
    bool on_object_end( system::error_code& ec )
1711  
    {
1749  
    {
1712  
        BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1750  
        BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1713  
    }
1751  
    }
1714  

1752  

1715  
    bool on_array_begin( system::error_code& ec )
1753  
    bool on_array_begin( system::error_code& ec )
1716  
    {
1754  
    {
1717  
        BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1755  
        BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1718  
    }
1756  
    }
1719  

1757  

1720  
    bool on_array_end( system::error_code& ec )
1758  
    bool on_array_end( system::error_code& ec )
1721  
    {
1759  
    {
1722  
        if( !inner_active_ )
1760  
        if( !inner_active_ )
1723  
            return signal_end(ec);
1761  
            return signal_end(ec);
1724  

1762  

1725  
        BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1763  
        BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1726  
    }
1764  
    }
1727  

1765  

1728  
    bool on_key_part( system::error_code& ec, string_view sv )
1766  
    bool on_key_part( system::error_code& ec, string_view sv )
1729  
    {
1767  
    {
1730  
        BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1768  
        BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1731  
    }
1769  
    }
1732  

1770  

1733  
    bool on_key( system::error_code& ec, string_view sv )
1771  
    bool on_key( system::error_code& ec, string_view sv )
1734  
    {
1772  
    {
1735  
        BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1773  
        BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1736  
    }
1774  
    }
1737  

1775  

1738  
    bool on_string_part( system::error_code& ec, string_view sv )
1776  
    bool on_string_part( system::error_code& ec, string_view sv )
1739  
    {
1777  
    {
1740  
        BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1778  
        BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1741  
    }
1779  
    }
1742  

1780  

1743  
    bool on_string( system::error_code& ec, string_view sv )
1781  
    bool on_string( system::error_code& ec, string_view sv )
1744  
    {
1782  
    {
1745  
        BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1783  
        BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1746  
    }
1784  
    }
1747  

1785  

1748  
    bool on_number_part( system::error_code& ec )
1786  
    bool on_number_part( system::error_code& ec )
1749  
    {
1787  
    {
1750  
        BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1788  
        BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1751  
    }
1789  
    }
1752  

1790  

1753  
    bool on_int64( system::error_code& ec, std::int64_t v )
1791  
    bool on_int64( system::error_code& ec, std::int64_t v )
1754  
    {
1792  
    {
1755  
        BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1793  
        BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1756  
    }
1794  
    }
1757  

1795  

1758  
    bool on_uint64( system::error_code& ec, std::uint64_t v )
1796  
    bool on_uint64( system::error_code& ec, std::uint64_t v )
1759  
    {
1797  
    {
1760  
        BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1798  
        BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1761  
    }
1799  
    }
1762  

1800  

1763  
    bool on_double( system::error_code& ec, double v )
1801  
    bool on_double( system::error_code& ec, double v )
1764  
    {
1802  
    {
1765  
        BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1803  
        BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1766  
    }
1804  
    }
1767  

1805  

1768  
    bool on_bool( system::error_code& ec, bool v )
1806  
    bool on_bool( system::error_code& ec, bool v )
1769  
    {
1807  
    {
1770  
        BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1808  
        BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1771  
    }
1809  
    }
1772  

1810  

1773  
    bool on_null(system::error_code& ec)
1811  
    bool on_null(system::error_code& ec)
1774  
    {
1812  
    {
1775  
        if( !inner_active_ )
1813  
        if( !inner_active_ )
1776  
        {
1814  
        {
1777  
            *value_ = {};
1815  
            *value_ = {};
1778  
            return this->parent_->signal_value(ec);
1816  
            return this->parent_->signal_value(ec);
1779  
        }
1817  
        }
1780  
        else
1818  
        else
1781  
        {
1819  
        {
1782  
            return inner_.on_null(ec);
1820  
            return inner_.on_null(ec);
1783  
        }
1821  
        }
1784  
    }
1822  
    }
1785  

1823  

1786  
#undef BOOST_JSON_INVOKE_INNER
1824  
#undef BOOST_JSON_INVOKE_INNER
1787  
};
1825  
};
1788  

1826  

1789  
// path handler
1827  
// path handler
1790 -
template< class V, class P >
1828 +
template<class V, class P, class Ctx>
1791 -
class converting_handler<path_conversion_tag, V, P>
1829 +
class converting_handler<path_conversion_tag, V, P, Ctx>
1792  
    : public scalar_handler<P, error::not_string>
1830  
    : public scalar_handler<P, error::not_string>
1793  
{
1831  
{
1794  
private:
1832  
private:
1795  
    V* value_;
1833  
    V* value_;
1796  
    bool cleared_ = false;
1834  
    bool cleared_ = false;
1797  

1835  

1798  
public:
1836  
public:
1799  
    converting_handler( V* v, P* p )
1837  
    converting_handler( V* v, P* p )
1800  
        : converting_handler::scalar_handler(p)
1838  
        : converting_handler::scalar_handler(p)
1801  
        , value_(v)
1839  
        , value_(v)
1802  
    {}
1840  
    {}
1803  

1841  

1804  
    bool on_string_part( system::error_code&, string_view sv )
1842  
    bool on_string_part( system::error_code&, string_view sv )
1805  
    {
1843  
    {
1806  
        if( !cleared_ )
1844  
        if( !cleared_ )
1807  
        {
1845  
        {
1808  
            cleared_ = true;
1846  
            cleared_ = true;
1809  
            value_->clear();
1847  
            value_->clear();
1810  
        }
1848  
        }
1811  

1849  

1812  
        value_->concat( sv.begin(), sv.end() );
1850  
        value_->concat( sv.begin(), sv.end() );
1813  
        return true;
1851  
        return true;
1814  
    }
1852  
    }
1815  

1853  

1816  
    bool on_string(system::error_code& ec, string_view sv)
1854  
    bool on_string(system::error_code& ec, string_view sv)
1817  
    {
1855  
    {
1818  
        if( !cleared_ )
1856  
        if( !cleared_ )
1819  
            value_->clear();
1857  
            value_->clear();
1820  
        else
1858  
        else
1821  
            cleared_ = false;
1859  
            cleared_ = false;
1822  

1860  

1823  
        value_->concat( sv.begin(), sv.end() );
1861  
        value_->concat( sv.begin(), sv.end() );
1824  

1862  

1825  
        return this->parent_->signal_value(ec);
1863  
        return this->parent_->signal_value(ec);
1826  
    }
1864  
    }
1827  
};
1865  
};
1828  

1866  

1829  
// into_handler
1867  
// into_handler
1830 -
template< class V >
1868 +
template<
1831 -
class into_handler
1869 +
    class T, class Ctx, class Rep = conversion_representation<T, Ctx> >
 
1870 +
class direct_target_holder
1832  
{
1871  
{
1833 -
private:
1872 +
    Rep rep_;
 
1873 +
    T* tgt_;
1834  

1874  

1835 -
    using inner_handler_type = get_handler<V, into_handler>;
1875 +
public:
 
1876 +
    using representation = Rep;
1836  

1877  

1837 -
    inner_handler_type inner_;
1878 +
    direct_target_holder(T* tgt)
1838 -
    bool inner_active_ = true;
1879 +
        : rep_(*tgt)
 
1880 +
        , tgt_(tgt)
 
1881 +
    {}
1839  

1882  

1840 -
public:
1883 +
    representation*
 
1884 +
    target_address(T*) noexcept
 
1885 +
    {
 
1886 +
        return std::addressof(rep_);
 
1887 +
    }
1841  

1888  

1842 -
    into_handler( into_handler const& ) = delete;
1889 +
    void
1843 -
    into_handler& operator=( into_handler const& ) = delete;
1890 +
    finish()
 
1891 +
    {
 
1892 +
        *tgt_ = std::move(rep_);
 
1893 +
    }
 
1894 +
};
1844  

1895  

 
1896 +
template<class T, class Ctx>
 
1897 +
class direct_target_holder<T, Ctx, T>
 
1898 +
{
1845  
public:
1899  
public:
 
1900 +
    using representation = T;
 
1901 +

 
1902 +
    direct_target_holder(T*) noexcept
 
1903 +
    {}
 
1904 +

 
1905 +
    representation*
 
1906 +
    target_address(T* tgt) const noexcept
 
1907 +
    {
 
1908 +
        return tgt;
 
1909 +
    }
 
1910 +

 
1911 +
    void
 
1912 +
    finish() const noexcept
 
1913 +
    {}
 
1914 +
};
 
1915 +

 
1916 +
template<class V, class Ctx>
 
1917 +
class into_handler
 
1918 +
    : direct_target_holder<V, Ctx>
 
1919 +
{
 
1920 +
private:
 
1921 +
    using inner_handler_type = get_handler<
 
1922 +
        typename into_handler::representation, into_handler, Ctx>;
 
1923 +

 
1924 +
    inner_handler_type inner_;
 
1925 +
    bool inner_active_ = true;
1846  

1926  

 
1927 +
public:
1847  
    static constexpr std::size_t max_object_size = object::max_size();
1928  
    static constexpr std::size_t max_object_size = object::max_size();
1848  
    static constexpr std::size_t max_array_size = array::max_size();
1929  
    static constexpr std::size_t max_array_size = array::max_size();
1849  
    static constexpr std::size_t max_key_size = string::max_size();
1930  
    static constexpr std::size_t max_key_size = string::max_size();
1850  
    static constexpr std::size_t max_string_size = string::max_size();
1931  
    static constexpr std::size_t max_string_size = string::max_size();
1851  

1932  

1852 -
public:
1933 +
    into_handler(into_handler const&) = delete;
 
1934 +
    into_handler& operator=(into_handler const&) = delete;
1853  

1935  

1854 -
    explicit into_handler( V* v ): inner_( v, this )
1936 +
    explicit
1855 -
    {
1937 +
    into_handler( V* v, Ctx const& = Ctx{} )
1856 -
    }
1938 +
        : into_handler::direct_target_holder(v)
 
1939 +
        , inner_(into_handler::target_address(v), this)
 
1940 +
    {}
1857  

1941  

1858  
    bool signal_value(system::error_code&)
1942  
    bool signal_value(system::error_code&)
1859  
    {
1943  
    {
 
1944 +
        into_handler::finish();
1860  
        return true;
1945  
        return true;
1861  
    }
1946  
    }
1862  

1947  

1863  
    bool signal_end(system::error_code&)
1948  
    bool signal_end(system::error_code&)
1864  
    {
1949  
    {
1865  
        return true;
1950  
        return true;
1866  
    }
1951  
    }
1867  

1952  

1868  
    bool on_document_begin( system::error_code& )
1953  
    bool on_document_begin( system::error_code& )
1869  
    {
1954  
    {
1870  
        return true;
1955  
        return true;
1871  
    }
1956  
    }
1872  

1957  

1873  
    bool on_document_end( system::error_code& )
1958  
    bool on_document_end( system::error_code& )
1874  
    {
1959  
    {
1875  
        inner_active_ = false;
1960  
        inner_active_ = false;
1876  
        return true;
1961  
        return true;
1877  
    }
1962  
    }
1878  

1963  

1879  
#define BOOST_JSON_INVOKE_INNER(f) \
1964  
#define BOOST_JSON_INVOKE_INNER(f) \
1880  
    if( !inner_active_ ) \
1965  
    if( !inner_active_ ) \
1881  
    { \
1966  
    { \
1882  
        BOOST_JSON_FAIL( ec, error::extra_data ); \
1967  
        BOOST_JSON_FAIL( ec, error::extra_data ); \
1883  
        return false; \
1968  
        return false; \
1884  
    } \
1969  
    } \
1885  
    else \
1970  
    else \
1886  
        return inner_.f
1971  
        return inner_.f
1887  

1972  

1888  
    bool on_object_begin( system::error_code& ec )
1973  
    bool on_object_begin( system::error_code& ec )
1889  
    {
1974  
    {
1890  
        BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1975  
        BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1891  
    }
1976  
    }
1892  

1977  

1893  
    bool on_object_end( std::size_t, system::error_code& ec )
1978  
    bool on_object_end( std::size_t, system::error_code& ec )
1894  
    {
1979  
    {
1895  
        BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1980  
        BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1896  
    }
1981  
    }
1897  

1982  

1898  
    bool on_array_begin( system::error_code& ec )
1983  
    bool on_array_begin( system::error_code& ec )
1899  
    {
1984  
    {
1900  
        BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1985  
        BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1901  
    }
1986  
    }
1902  

1987  

1903  
    bool on_array_end( std::size_t, system::error_code& ec )
1988  
    bool on_array_end( std::size_t, system::error_code& ec )
1904  
    {
1989  
    {
1905  
        BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1990  
        BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1906  
    }
1991  
    }
1907  

1992  

1908  
    bool on_key_part( string_view sv, std::size_t, system::error_code& ec )
1993  
    bool on_key_part( string_view sv, std::size_t, system::error_code& ec )
1909  
    {
1994  
    {
1910  
        BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1995  
        BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1911  
    }
1996  
    }
1912  

1997  

1913  
    bool on_key( string_view sv, std::size_t, system::error_code& ec )
1998  
    bool on_key( string_view sv, std::size_t, system::error_code& ec )
1914  
    {
1999  
    {
1915  
        BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
2000  
        BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1916  
    }
2001  
    }
1917  

2002  

1918  
    bool on_string_part( string_view sv, std::size_t, system::error_code& ec )
2003  
    bool on_string_part( string_view sv, std::size_t, system::error_code& ec )
1919  
    {
2004  
    {
1920  
        BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
2005  
        BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1921  
    }
2006  
    }
1922  

2007  

1923  
    bool on_string( string_view sv, std::size_t, system::error_code& ec )
2008  
    bool on_string( string_view sv, std::size_t, system::error_code& ec )
1924  
    {
2009  
    {
1925  
        BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
2010  
        BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1926  
    }
2011  
    }
1927  

2012  

1928  
    bool on_number_part( string_view, system::error_code& ec )
2013  
    bool on_number_part( string_view, system::error_code& ec )
1929  
    {
2014  
    {
1930  
        BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
2015  
        BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1931  
    }
2016  
    }
1932  

2017  

1933  
    bool on_int64( std::int64_t v, string_view, system::error_code& ec )
2018  
    bool on_int64( std::int64_t v, string_view, system::error_code& ec )
1934  
    {
2019  
    {
1935  
        BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
2020  
        BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1936  
    }
2021  
    }
1937  

2022  

1938  
    bool on_uint64( std::uint64_t v, string_view, system::error_code& ec )
2023  
    bool on_uint64( std::uint64_t v, string_view, system::error_code& ec )
1939  
    {
2024  
    {
1940  
        BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
2025  
        BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1941  
    }
2026  
    }
1942  

2027  

1943  
    bool on_double( double v, string_view, system::error_code& ec )
2028  
    bool on_double( double v, string_view, system::error_code& ec )
1944  
    {
2029  
    {
1945  
        BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
2030  
        BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1946  
    }
2031  
    }
1947  

2032  

1948  
    bool on_bool( bool v, system::error_code& ec )
2033  
    bool on_bool( bool v, system::error_code& ec )
1949  
    {
2034  
    {
1950  
        BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
2035  
        BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1951  
    }
2036  
    }
1952  

2037  

1953  
    bool on_null( system::error_code& ec )
2038  
    bool on_null( system::error_code& ec )
1954  
    {
2039  
    {
1955  
        BOOST_JSON_INVOKE_INNER( on_null(ec) );
2040  
        BOOST_JSON_INVOKE_INNER( on_null(ec) );
1956  
    }
2041  
    }
1957  

2042  

1958  
    bool on_comment_part(string_view, system::error_code&)
2043  
    bool on_comment_part(string_view, system::error_code&)
1959  
    {
2044  
    {
1960  
        return true;
2045  
        return true;
1961  
    }
2046  
    }
1962  

2047  

1963  
    bool on_comment(string_view, system::error_code&)
2048  
    bool on_comment(string_view, system::error_code&)
1964  
    {
2049  
    {
1965  
        return true;
2050  
        return true;
1966 -

 
1967  
    }
2051  
    }
1968  
#undef BOOST_JSON_INVOKE_INNER
2052  
#undef BOOST_JSON_INVOKE_INNER
1969  
};
2053  
};
1970  

2054  

1971  
} // namespace detail
2055  
} // namespace detail
1972  
} // namespace boost
2056  
} // namespace boost
1973  
} // namespace json
2057  
} // namespace json
1974  

2058  

1975  
#endif
2059  
#endif