TLA Line data Source code
1 : //
2 : // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
3 : // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
4 : //
5 : // Distributed under the Boost Software License, Version 1.0. (See accompanying
6 : // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
7 : //
8 : // Official repository: https://github.com/boostorg/json
9 : //
10 :
11 : #ifndef BOOST_JSON_DETAIL_PARSE_INTO_HPP
12 : #define BOOST_JSON_DETAIL_PARSE_INTO_HPP
13 :
14 : #include <boost/json/detail/config.hpp>
15 :
16 : #include <boost/json/error.hpp>
17 : #include <boost/json/conversion.hpp>
18 : #include <boost/json/value.hpp>
19 : #include <boost/describe/enum_from_string.hpp>
20 :
21 : #include <bitset>
22 : #include <vector>
23 :
24 : /*
25 : * This file contains the majority of parse_into functionality, specifically
26 : * the implementation of dedicated handlers for different generic categories of
27 : * types.
28 : *
29 : * At the core of parse_into is the specialisation basic_parser<
30 : * detail::into_handler<T> >. detail::into_handler<T> is a handler for
31 : * basic_parser. It directly handles events on_comment_part and on_comment (by
32 : * ignoring them), on_document_begin (by enabling the nested dedicated
33 : * handler), and on_document_end (by disabling the nested handler).
34 : *
35 : * Every other event is handled by the nested handler, which has the type
36 : * get_handler< T, into_handler<T> >. The second parameter is the parent
37 : * handler (in this case, it's the top handler, into_handler<T>). The type is
38 : * actually an alias to class template converting_handler, which has a separate
39 : * specialisation for every conversion category from the list of generic
40 : * conversion categories (e.g. sequence_category, tuple_category, etc.)
41 : * Instantiations of the template store a pointer to the parent handler and a
42 : * pointer to the value T.
43 : *
44 : * The nested handler handles specific parser events by setting error_code to
45 : * an appropriate value, if it receives an event it isn't supposed to handle
46 : * (e.g. a number handler getting an on_string event), and also updates the
47 : * value when appropriate. Note that they never need to handle on_comment_part,
48 : * on_comment, on_document_begin, and on_document_end events, as those are
49 : * always handled by the top handler into_handler<T>.
50 : *
51 : * When the nested handler receives an event that completes the current value,
52 : * it is supposed to call its parent's signal_value member function. This is
53 : * necessary for correct handling of composite types (e.g. sequences).
54 : *
55 : * Finally, nested handlers should always call parent's signal_end member
56 : * function if they don't handle on_array_end themselves. This is necessary
57 : * to correctly handle nested composites (e.g. sequences inside sequences).
58 : * signal_end can return false and set error state when the containing parser
59 : * requires more elements.
60 : *
61 : * converting_handler instantiations for composite categories of types have
62 : * their own nested handlers, to which they themselves delegate events. For
63 : * complex types you will get a tree of handlers with into_handler<T> as the
64 : * root and handlers for scalars as leaves.
65 : *
66 : * To reiterate, only into_handler has to handle on_comment_part, on_comment,
67 : * on_document_begin, and on_document_end; only handlers for composites and
68 : * into_handler has to provide signal_value and signal_end; all handlers
69 : * except for into_handler have to call their parent's signal_end from
70 : * their on_array_begin, if they don't handle it themselves; once a handler
71 : * receives an event that finishes its current value, it should call its
72 : * parent's signal_value.
73 : */
74 :
75 : namespace boost {
76 : namespace json {
77 : namespace detail {
78 :
79 : template< conversion_category C, class T, class Parent >
80 : class converting_handler;
81 :
82 : // get_handler
83 : template< class V, class P >
84 : using get_handler = converting_handler<
85 : get_conversion_category<
86 : V, void, direct_custom_checks, direct_fallback_checks>::value,
87 : V,
88 : P>;
89 :
90 : template<error E> class handler_error_base
91 : {
92 : public:
93 :
94 : handler_error_base() = default;
95 :
96 : handler_error_base( handler_error_base const& ) = delete;
97 : handler_error_base& operator=( handler_error_base const& ) = delete;
98 :
99 : public:
100 :
101 HIT 2 : bool on_object_begin( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
102 7 : bool on_array_begin( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
103 : bool on_array_end( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
104 1 : bool on_string_part( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
105 60 : bool on_string( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
106 2 : bool on_number_part( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
107 8 : bool on_int64( system::error_code& ec, std::int64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
108 8 : bool on_uint64( system::error_code& ec, std::uint64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
109 7 : bool on_double( system::error_code& ec, double ) { BOOST_JSON_FAIL( ec, E ); return false; }
110 2 : bool on_bool( system::error_code& ec, bool ) { BOOST_JSON_FAIL( ec, E ); return false; }
111 4 : bool on_null( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
112 :
113 : // LCOV_EXCL_START
114 : // parses that can't handle this would fail at on_object_begin
115 : bool on_object_end( system::error_code& ) { BOOST_ASSERT( false ); return false; }
116 : bool on_key_part( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
117 : bool on_key( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
118 : // LCOV_EXCL_STOP
119 : };
120 :
121 : template< class P, error E >
122 : class scalar_handler
123 : : public handler_error_base<E>
124 : {
125 : protected:
126 : P* parent_;
127 :
128 : public:
129 : scalar_handler(scalar_handler const&) = delete;
130 : scalar_handler& operator=(scalar_handler const&) = delete;
131 :
132 861 : scalar_handler(P* p): parent_( p )
133 861 : {}
134 :
135 225 : bool on_array_end( system::error_code& ec )
136 : {
137 225 : return parent_->signal_end(ec);
138 : }
139 : };
140 :
141 : template< class D, class V, class P, error E >
142 : class composite_handler
143 : {
144 : protected:
145 : using inner_handler_type = get_handler<V, D>;
146 :
147 : P* parent_;
148 : #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
149 : # pragma GCC diagnostic push
150 : # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
151 : #endif
152 : V next_value_ = {};
153 : inner_handler_type inner_;
154 : bool inner_active_ = false;
155 :
156 : public:
157 : composite_handler( composite_handler const& ) = delete;
158 : composite_handler& operator=( composite_handler const& ) = delete;
159 :
160 472 : composite_handler( P* p )
161 472 : : parent_(p), inner_( &next_value_, static_cast<D*>(this) )
162 472 : {}
163 : #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
164 : # pragma GCC diagnostic pop
165 : #endif
166 :
167 336 : bool signal_end(system::error_code& ec)
168 : {
169 336 : inner_active_ = false;
170 336 : return parent_->signal_value(ec);
171 : }
172 :
173 : #define BOOST_JSON_INVOKE_INNER(f) \
174 : if( !inner_active_ ) { \
175 : BOOST_JSON_FAIL(ec, E); \
176 : return false; \
177 : } \
178 : else \
179 : return inner_.f
180 :
181 21 : bool on_object_begin( system::error_code& ec )
182 : {
183 21 : BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
184 : }
185 :
186 21 : bool on_object_end( system::error_code& ec )
187 : {
188 21 : BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
189 : }
190 :
191 66 : bool on_array_begin( system::error_code& ec )
192 : {
193 66 : BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
194 : }
195 :
196 : bool on_array_end( system::error_code& ec )
197 : {
198 : BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
199 : }
200 :
201 3 : bool on_key_part( system::error_code& ec, string_view sv )
202 : {
203 3 : BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
204 : }
205 :
206 21 : bool on_key( system::error_code& ec, string_view sv )
207 : {
208 21 : BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
209 : }
210 :
211 24 : bool on_string_part( system::error_code& ec, string_view sv )
212 : {
213 24 : BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
214 : }
215 :
216 50 : bool on_string( system::error_code& ec, string_view sv )
217 : {
218 50 : BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
219 : }
220 :
221 252 : bool on_number_part( system::error_code& ec )
222 : {
223 252 : BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
224 : }
225 :
226 1055 : bool on_int64( system::error_code& ec, std::int64_t v )
227 : {
228 1055 : BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
229 : }
230 :
231 7 : bool on_uint64( system::error_code& ec, std::uint64_t v )
232 : {
233 7 : BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
234 : }
235 :
236 42 : bool on_double( system::error_code& ec, double v )
237 : {
238 42 : BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
239 : }
240 :
241 21 : bool on_bool( system::error_code& ec, bool v )
242 : {
243 21 : BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
244 : }
245 :
246 14 : bool on_null( system::error_code& ec )
247 : {
248 14 : BOOST_JSON_INVOKE_INNER( on_null(ec) );
249 : }
250 :
251 : #undef BOOST_JSON_INVOKE_INNER
252 : };
253 :
254 : // integral handler
255 : template<class V,
256 : typename std::enable_if<std::is_signed<V>::value, int>::type = 0>
257 764 : bool integral_in_range( std::int64_t v )
258 : {
259 764 : return v >= (std::numeric_limits<V>::min)() && v <= (std::numeric_limits<V>::max)();
260 : }
261 :
262 : template<class V,
263 : typename std::enable_if<!std::is_signed<V>::value, int>::type = 0>
264 35 : bool integral_in_range( std::int64_t v )
265 : {
266 35 : return v >= 0 && static_cast<std::uint64_t>( v ) <= (std::numeric_limits<V>::max)();
267 : }
268 :
269 : template<class V>
270 37 : bool integral_in_range( std::uint64_t v )
271 : {
272 37 : return v <= static_cast<typename std::make_unsigned<V>::type>( (std::numeric_limits<V>::max)() );
273 : }
274 :
275 : template< class V, class P >
276 : class converting_handler<conversion_category::integer, V, P>
277 : : public scalar_handler<P, error::not_integer>
278 : {
279 : private:
280 : V* value_;
281 :
282 : public:
283 598 : converting_handler( V* v, P* p )
284 : : converting_handler::scalar_handler(p)
285 598 : , value_(v)
286 598 : {}
287 :
288 325 : bool on_number_part( system::error_code& )
289 : {
290 325 : return true;
291 : }
292 :
293 799 : bool on_int64(system::error_code& ec, std::int64_t v)
294 : {
295 799 : if( !integral_in_range<V>( v ) )
296 : {
297 2 : BOOST_JSON_FAIL( ec, error::not_exact );
298 2 : return false;
299 : }
300 :
301 797 : *value_ = static_cast<V>( v );
302 797 : return this->parent_->signal_value(ec);
303 : }
304 :
305 37 : bool on_uint64(system::error_code& ec, std::uint64_t v)
306 : {
307 37 : if( !integral_in_range<V>(v) )
308 : {
309 2 : BOOST_JSON_FAIL( ec, error::not_exact );
310 2 : return false;
311 : }
312 :
313 35 : *value_ = static_cast<V>(v);
314 35 : return this->parent_->signal_value(ec);
315 : }
316 : };
317 :
318 : // floating point handler
319 : template< class V, class P>
320 : class converting_handler<conversion_category::floating_point, V, P>
321 : : public scalar_handler<P, error::not_double>
322 : {
323 : private:
324 : V* value_;
325 :
326 : public:
327 53 : converting_handler( V* v, P* p )
328 : : converting_handler::scalar_handler(p)
329 53 : , value_(v)
330 53 : {}
331 :
332 112 : bool on_number_part( system::error_code& )
333 : {
334 112 : return true;
335 : }
336 :
337 1 : bool on_int64(system::error_code& ec, std::int64_t v)
338 : {
339 1 : *value_ = static_cast<V>(v);
340 1 : return this->parent_->signal_value(ec);
341 : }
342 :
343 1 : bool on_uint64(system::error_code& ec, std::uint64_t v)
344 : {
345 1 : *value_ = static_cast<V>(v);
346 1 : return this->parent_->signal_value(ec);
347 : }
348 :
349 63 : bool on_double(system::error_code& ec, double v)
350 : {
351 63 : *value_ = static_cast<V>(v);
352 63 : return this->parent_->signal_value(ec);
353 : }
354 : };
355 :
356 : // string handler
357 : template< class V, class P >
358 : class converting_handler<string_category::value, V, P>
359 : : public scalar_handler<P, error::not_string>
360 : {
361 : private:
362 : V* value_;
363 : bool cleared_ = false;
364 :
365 : public:
366 95 : converting_handler( V* v, P* p )
367 : : converting_handler::scalar_handler(p)
368 95 : , value_(v)
369 95 : {}
370 :
371 21 : bool on_string_part( system::error_code&, string_view sv )
372 : {
373 21 : if( !cleared_ )
374 : {
375 5 : cleared_ = true;
376 5 : value_->clear();
377 : }
378 :
379 21 : value_->append( sv.begin(), sv.end() );
380 21 : return true;
381 : }
382 :
383 100 : bool on_string(system::error_code& ec, string_view sv)
384 : {
385 100 : if( !cleared_ )
386 95 : value_->clear();
387 : else
388 5 : cleared_ = false;
389 :
390 100 : value_->append( sv.begin(), sv.end() );
391 100 : return this->parent_->signal_value(ec);
392 : }
393 : };
394 :
395 : // bool handler
396 : template< class V, class P >
397 : class converting_handler<conversion_category::boolean, V, P>
398 : : public scalar_handler<P, error::not_bool>
399 : {
400 : private:
401 : V* value_;
402 :
403 : public:
404 60 : converting_handler( V* v, P* p )
405 : : converting_handler::scalar_handler(p)
406 60 : , value_(v)
407 60 : {}
408 :
409 42 : bool on_bool(system::error_code& ec, bool v)
410 : {
411 42 : *value_ = v;
412 42 : return this->parent_->signal_value(ec);
413 : }
414 : };
415 :
416 : // null handler
417 : template< class V, class P >
418 : class converting_handler<null_category::value, V, P>
419 : : public scalar_handler<P, error::not_null>
420 : {
421 : private:
422 : V* value_;
423 :
424 : public:
425 55 : converting_handler( V* v, P* p )
426 : : converting_handler::scalar_handler(p)
427 55 : , value_(v)
428 55 : {}
429 :
430 35 : bool on_null(system::error_code& ec)
431 : {
432 35 : *value_ = {};
433 35 : return this->parent_->signal_value(ec);
434 : }
435 : };
436 :
437 : // described enum handler
438 : template< class V, class P >
439 : class converting_handler<described_enum_category::value, V, P>
440 : : public scalar_handler<P, error::not_string>
441 : {
442 : #ifndef BOOST_DESCRIBE_CXX14
443 :
444 : static_assert(
445 : sizeof(V) == 0, "Enum support for parse_into requires C++14" );
446 :
447 : #else
448 :
449 : private:
450 : V* value_;
451 : std::string name_;
452 :
453 : public:
454 : converting_handler( V* v, P* p )
455 : : converting_handler::scalar_handler(p)
456 : , value_(v)
457 : {}
458 :
459 : bool on_string_part( system::error_code&, string_view sv )
460 : {
461 : name_.append( sv.begin(), sv.end() );
462 : return true;
463 : }
464 :
465 : bool on_string(system::error_code& ec, string_view sv)
466 : {
467 : string_view name = sv;
468 : if( !name_.empty() )
469 : {
470 : name_.append( sv.begin(), sv.end() );
471 : name = name_;
472 : }
473 :
474 : if( !describe::enum_from_string(name, *value_) )
475 : {
476 : BOOST_JSON_FAIL(ec, error::unknown_name);
477 : return false;
478 : }
479 :
480 : name_.clear();
481 : return this->parent_->signal_value(ec);
482 : }
483 :
484 : #endif // BOOST_DESCRIBE_CXX14
485 : };
486 :
487 : template< class V, class P >
488 : class converting_handler<unknown_category::value, V, P>
489 : {
490 : static_assert( sizeof(V) == 0, "This type is not supported" );
491 : };
492 :
493 : // sequence handler
494 : template< class It >
495 132 : bool cannot_insert(It i, It e)
496 : {
497 132 : return i == e;
498 : }
499 :
500 : template< class It1, class It2 >
501 615 : std::false_type cannot_insert(It1, It2)
502 : {
503 615 : return {};
504 : }
505 :
506 : template< class It >
507 32 : bool needs_more_elements(It i, It e)
508 : {
509 32 : return i != e;
510 : }
511 :
512 : template< class It1, class It2 >
513 308 : std::false_type needs_more_elements(It1, It2)
514 : {
515 308 : return {};
516 : }
517 :
518 : template<class T>
519 : void
520 34 : clear_container(
521 : T&,
522 : mp11::mp_int<2>)
523 : {
524 34 : }
525 :
526 : template<class T>
527 : void
528 288 : clear_container(
529 : T& target,
530 : mp11::mp_int<1>)
531 : {
532 288 : target.clear();
533 288 : }
534 :
535 : template<class T>
536 : void
537 185 : clear_container(
538 : T& target,
539 : mp11::mp_int<0>)
540 : {
541 185 : target.clear();
542 185 : }
543 :
544 : template< class V, class P >
545 : class converting_handler<sequence_category::value, V, P>
546 : : public composite_handler<
547 : converting_handler<sequence_category::value, V, P>,
548 : detail::value_type<V>,
549 : P,
550 : error::not_array>
551 : {
552 : private:
553 : V* value_;
554 :
555 : using Inserter = decltype(
556 : detail::inserter(*value_, inserter_implementation<V>()) );
557 : Inserter inserter;
558 :
559 : public:
560 328 : converting_handler( V* v, P* p )
561 : : converting_handler::composite_handler(p)
562 328 : , value_(v)
563 328 : , inserter( detail::inserter(*value_, inserter_implementation<V>()) )
564 328 : {}
565 :
566 747 : bool signal_value(system::error_code& ec)
567 : {
568 747 : if(cannot_insert( inserter, value_->end() ))
569 : {
570 2 : BOOST_JSON_FAIL( ec, error::size_mismatch );
571 2 : return false;
572 : }
573 :
574 745 : *inserter++ = std::move(this->next_value_);
575 : #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
576 : # pragma GCC diagnostic push
577 : # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
578 : #endif
579 745 : this->next_value_ = {};
580 : #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
581 : # pragma GCC diagnostic pop
582 : #endif
583 745 : return true;
584 : }
585 :
586 340 : bool signal_end(system::error_code& ec)
587 : {
588 340 : if(needs_more_elements( inserter, value_->end() ))
589 : {
590 4 : BOOST_JSON_FAIL( ec, error::size_mismatch );
591 4 : return false;
592 : }
593 :
594 336 : return converting_handler::composite_handler::signal_end(ec);
595 : }
596 :
597 568 : bool on_array_begin( system::error_code& ec )
598 : {
599 568 : if( this->inner_active_ )
600 217 : return this->inner_.on_array_begin( ec );
601 :
602 351 : this->inner_active_ = true;
603 351 : clear_container( *value_, inserter_implementation<V>() );
604 351 : inserter = detail::inserter(*value_, inserter_implementation<V>());
605 351 : return true;
606 : }
607 :
608 606 : bool on_array_end( system::error_code& ec )
609 : {
610 606 : if( this->inner_active_ )
611 557 : return this->inner_.on_array_end( ec );
612 :
613 49 : return this->parent_->signal_end(ec);
614 : }
615 : };
616 :
617 : // map handler
618 : template< class V, class P >
619 : class converting_handler<map_category::value, V, P>
620 : : public composite_handler<
621 : converting_handler<map_category::value, V, P>,
622 : detail::mapped_type<V>,
623 : P,
624 : error::not_object>
625 : {
626 : private:
627 : V* value_;
628 : std::string key_;
629 :
630 : public:
631 144 : converting_handler( V* v, P* p )
632 144 : : converting_handler::composite_handler(p), value_(v)
633 144 : {}
634 :
635 149 : bool signal_value(system::error_code&)
636 : {
637 149 : value_->emplace( std::move(key_), std::move(this->next_value_) );
638 :
639 149 : key_ = {};
640 149 : this->next_value_ = {};
641 :
642 149 : this->inner_active_ = false;
643 :
644 149 : return true;
645 : }
646 :
647 172 : bool on_object_begin( system::error_code& ec )
648 : {
649 172 : if( this->inner_active_ )
650 16 : return this->inner_.on_object_begin(ec);
651 :
652 156 : clear_container( *value_, inserter_implementation<V>() );
653 156 : return true;
654 : }
655 :
656 161 : bool on_object_end(system::error_code& ec)
657 : {
658 161 : if( this->inner_active_ )
659 16 : return this->inner_.on_object_end(ec);
660 :
661 145 : return this->parent_->signal_value(ec);
662 : }
663 :
664 67 : bool on_array_end( system::error_code& ec )
665 : {
666 67 : if( this->inner_active_ )
667 60 : return this->inner_.on_array_end(ec);
668 :
669 7 : return this->parent_->signal_end(ec);
670 : }
671 :
672 47 : bool on_key_part( system::error_code& ec, string_view sv )
673 : {
674 47 : if( this->inner_active_ )
675 2 : return this->inner_.on_key_part(ec, sv);
676 :
677 45 : key_.append( sv.data(), sv.size() );
678 45 : return true;
679 : }
680 :
681 174 : bool on_key( system::error_code& ec, string_view sv )
682 : {
683 174 : if( this->inner_active_ )
684 14 : return this->inner_.on_key(ec, sv);
685 :
686 160 : key_.append( sv.data(), sv.size() );
687 :
688 160 : this->inner_active_ = true;
689 160 : return true;
690 : }
691 : };
692 :
693 : // tuple handler
694 : template<std::size_t I, class T>
695 : struct handler_tuple_element
696 : {
697 : template< class... Args >
698 286 : handler_tuple_element( Args&& ... args )
699 286 : : t_( static_cast<Args&&>(args)... )
700 286 : {}
701 :
702 : T t_;
703 : };
704 :
705 : template<std::size_t I, class T>
706 : T&
707 520 : get( handler_tuple_element<I, T>& e )
708 : {
709 520 : return e.t_;
710 : }
711 :
712 : template<
713 : class P,
714 : class LV,
715 : class S = mp11::make_index_sequence<mp11::mp_size<LV>::value> >
716 : struct handler_tuple;
717 :
718 : template< class P, template<class...> class L, class... V, std::size_t... I >
719 : struct handler_tuple< P, L<V...>, mp11::index_sequence<I...> >
720 : : handler_tuple_element<I, V>
721 : ...
722 : {
723 : handler_tuple( handler_tuple const& ) = delete;
724 : handler_tuple& operator=( handler_tuple const& ) = delete;
725 :
726 : template< class Access, class T >
727 129 : handler_tuple( Access access, T* pv, P* pp )
728 : : handler_tuple_element<I, V>(
729 6 : access( pv, mp11::mp_size_t<I>() ),
730 : pp )
731 129 : ...
732 129 : {}
733 : };
734 :
735 : #if defined(BOOST_MSVC) && BOOST_MSVC < 1910
736 :
737 : template< class T >
738 : struct tuple_element_list_impl
739 : {
740 : template< class I >
741 : using tuple_element_helper = tuple_element_t<I::value, T>;
742 :
743 : using type = mp11::mp_transform<
744 : tuple_element_helper,
745 : mp11::mp_iota< std::tuple_size<T> > >;
746 : };
747 : template< class T >
748 : using tuple_element_list = typename tuple_element_list_impl<T>::type;
749 :
750 : #else
751 :
752 : template< class I, class T >
753 : using tuple_element_helper = tuple_element_t<I::value, T>;
754 : template< class T >
755 : using tuple_element_list = mp11::mp_transform_q<
756 : mp11::mp_bind_back< tuple_element_helper, T>,
757 : mp11::mp_iota< std::tuple_size<T> > >;
758 :
759 : #endif
760 :
761 : template< class Op, class... Args>
762 : struct handler_op_invoker
763 : {
764 : public:
765 : std::tuple<Args&...> args;
766 :
767 : template< class Handler >
768 : bool
769 470 : operator()( Handler& handler ) const
770 : {
771 470 : return (*this)( handler, mp11::index_sequence_for<Args...>() );
772 : }
773 :
774 : private:
775 : template< class Handler, std::size_t... I >
776 : bool
777 470 : operator()( Handler& handler, mp11::index_sequence<I...> ) const
778 : {
779 470 : return Op()( handler, std::get<I>(args)... );
780 : }
781 : };
782 :
783 : template< class Handlers, class F >
784 : struct tuple_handler_op_invoker
785 : {
786 : Handlers& handlers;
787 : F fn;
788 :
789 : template< class I >
790 : bool
791 470 : operator()( I ) const
792 : {
793 470 : return fn( get<I::value>(handlers) );
794 : }
795 : };
796 :
797 : struct tuple_accessor
798 : {
799 : template< class T, class I >
800 286 : auto operator()( T* t, I ) const -> tuple_element_t<I::value, T>*
801 : {
802 : using std::get;
803 286 : return &get<I::value>(*t);
804 : }
805 : };
806 :
807 : template< class T, class P >
808 : class converting_handler<tuple_category::value, T, P>
809 : {
810 :
811 : private:
812 : using ElementTypes = tuple_element_list<T>;
813 :
814 : template<class V>
815 : using ElementHandler = get_handler<V, converting_handler>;
816 : using InnerHandlers = mp11::mp_transform<ElementHandler, ElementTypes>;
817 : using HandlerTuple = handler_tuple<converting_handler, InnerHandlers>;
818 :
819 : T* value_;
820 : P* parent_;
821 :
822 : HandlerTuple handlers_;
823 : int inner_active_ = -1;
824 :
825 : public:
826 : converting_handler( converting_handler const& ) = delete;
827 : converting_handler& operator=( converting_handler const& ) = delete;
828 :
829 129 : converting_handler( T* v, P* p )
830 129 : : value_(v) , parent_(p) , handlers_(tuple_accessor(), v, this)
831 129 : {}
832 :
833 283 : bool signal_value(system::error_code&)
834 : {
835 283 : ++inner_active_;
836 283 : return true;
837 : }
838 :
839 123 : bool signal_end(system::error_code& ec)
840 : {
841 123 : constexpr int N = std::tuple_size<T>::value;
842 123 : if( inner_active_ < N )
843 : {
844 4 : BOOST_JSON_FAIL( ec, error::size_mismatch );
845 4 : return false;
846 : }
847 :
848 119 : inner_active_ = -1;
849 119 : return parent_->signal_value(ec);
850 : }
851 :
852 : #define BOOST_JSON_HANDLE_EVENT(fn) \
853 : struct do_ ## fn \
854 : { \
855 : template< class H, class... Args > \
856 : bool operator()( H& h, Args& ... args ) const \
857 : { \
858 : return h. fn (args...); \
859 : } \
860 : }; \
861 : \
862 : template< class... Args > \
863 : bool fn( system::error_code& ec, Args&& ... args ) \
864 : { \
865 : if( inner_active_ < 0 ) \
866 : { \
867 : BOOST_JSON_FAIL( ec, error::not_array ); \
868 : return false; \
869 : } \
870 : constexpr int N = std::tuple_size<T>::value; \
871 : if( inner_active_ >= N ) \
872 : { \
873 : BOOST_JSON_FAIL( ec, error::size_mismatch ); \
874 : return false; \
875 : } \
876 : using F = handler_op_invoker< do_ ## fn, system::error_code, Args...>; \
877 : using H = decltype(handlers_); \
878 : return mp11::mp_with_index<N>( \
879 : inner_active_, \
880 : tuple_handler_op_invoker<H, F>{ \
881 : handlers_, \
882 : F{ std::forward_as_tuple(ec, args...) } } ); \
883 : }
884 :
885 56 : BOOST_JSON_HANDLE_EVENT( on_object_begin )
886 42 : BOOST_JSON_HANDLE_EVENT( on_object_end )
887 :
888 : struct do_on_array_begin
889 : {
890 : HandlerTuple& handlers;
891 : system::error_code& ec;
892 :
893 : template< class I >
894 23 : bool operator()( I ) const
895 : {
896 23 : return get<I::value>(handlers).on_array_begin(ec);
897 : }
898 : };
899 159 : bool on_array_begin( system::error_code& ec )
900 : {
901 159 : if( inner_active_ < 0 )
902 : {
903 134 : inner_active_ = 0;
904 134 : return true;
905 : }
906 :
907 25 : constexpr int N = std::tuple_size<T>::value;
908 :
909 25 : if( inner_active_ >= N )
910 : {
911 2 : BOOST_JSON_FAIL( ec, error::size_mismatch );
912 2 : return false;
913 : }
914 :
915 23 : return mp11::mp_with_index<N>(
916 23 : inner_active_, do_on_array_begin{handlers_, ec} );
917 : }
918 :
919 : struct do_on_array_end
920 : {
921 : HandlerTuple& handlers;
922 : system::error_code& ec;
923 :
924 : template< class I >
925 27 : bool operator()( I ) const
926 : {
927 27 : return get<I::value>(handlers).on_array_end(ec);
928 : }
929 : };
930 195 : bool on_array_end( system::error_code& ec )
931 : {
932 195 : if( inner_active_ < 0 )
933 49 : return parent_->signal_end(ec);
934 :
935 146 : constexpr int N = std::tuple_size<T>::value;
936 :
937 146 : if( inner_active_ >= N )
938 119 : return signal_end(ec);
939 :
940 27 : return mp11::mp_with_index<N>(
941 27 : inner_active_, do_on_array_end{handlers_, ec} );
942 : }
943 :
944 6 : BOOST_JSON_HANDLE_EVENT( on_key_part )
945 56 : BOOST_JSON_HANDLE_EVENT( on_key )
946 10 : BOOST_JSON_HANDLE_EVENT( on_string_part )
947 56 : BOOST_JSON_HANDLE_EVENT( on_string )
948 160 : BOOST_JSON_HANDLE_EVENT( on_number_part )
949 432 : BOOST_JSON_HANDLE_EVENT( on_int64 )
950 14 : BOOST_JSON_HANDLE_EVENT( on_uint64 )
951 70 : BOOST_JSON_HANDLE_EVENT( on_double )
952 28 : BOOST_JSON_HANDLE_EVENT( on_bool )
953 14 : BOOST_JSON_HANDLE_EVENT( on_null )
954 :
955 : #undef BOOST_JSON_HANDLE_EVENT
956 : };
957 :
958 : // described struct handler
959 : #if defined(BOOST_MSVC) && BOOST_MSVC < 1910
960 :
961 : template< class T >
962 : struct struct_element_list_impl
963 : {
964 : template< class D >
965 : using helper = described_member_t<T, D>;
966 :
967 : using type = mp11::mp_transform< helper, described_members<T> >;
968 : };
969 : template< class T >
970 : using struct_element_list = typename struct_element_list_impl<T>::type;
971 :
972 : #else
973 :
974 : template< class T >
975 : using struct_element_list = mp11::mp_transform_q<
976 : mp11::mp_bind_front< described_member_t, T >, described_members<T> >;
977 :
978 : #endif
979 :
980 : struct struct_accessor
981 : {
982 : template< class T >
983 : auto operator()( T*, mp11::mp_size< described_members<T> > ) const
984 : -> void*
985 : {
986 : return nullptr;
987 : }
988 :
989 : template< class T, class I >
990 : auto operator()( T* t, I ) const
991 : -> described_member_t<T, mp11::mp_at< described_members<T>, I> >*
992 : {
993 : using Ds = described_members<T>;
994 : using D = mp11::mp_at<Ds, I>;
995 : return &(t->*D::pointer);
996 : }
997 : };
998 :
999 : struct struct_key_searcher
1000 : {
1001 : string_view key;
1002 : int& found;
1003 : int index = 0;
1004 :
1005 : struct_key_searcher(string_view key, int& found) noexcept
1006 : : key(key), found(found)
1007 : {}
1008 :
1009 : template< class D >
1010 : void
1011 : operator()( D )
1012 : {
1013 : if( key == D::name )
1014 : found = index;
1015 : ++index;
1016 : }
1017 : };
1018 :
1019 : template<class P>
1020 : struct ignoring_handler
1021 : {
1022 : P* parent_;
1023 : std::size_t array_depth_ = 0;
1024 : std::size_t object_depth_ = 0;
1025 :
1026 : ignoring_handler(ignoring_handler const&) = delete;
1027 : ignoring_handler& operator=(ignoring_handler const&) = delete;
1028 :
1029 : ignoring_handler(void*, P* p) noexcept
1030 : : parent_(p)
1031 : {}
1032 :
1033 : bool on_object_begin(system::error_code&)
1034 : {
1035 : ++object_depth_;
1036 : return true;
1037 : }
1038 :
1039 : bool on_object_end(system::error_code& ec)
1040 : {
1041 : BOOST_ASSERT( object_depth_ > 0 );
1042 : --object_depth_;
1043 :
1044 : if( (array_depth_ + object_depth_) == 0 )
1045 : return parent_->signal_value(ec);
1046 : return true;
1047 : }
1048 :
1049 : bool on_array_begin(system::error_code&)
1050 : {
1051 : ++array_depth_;
1052 : return true;
1053 : }
1054 :
1055 : bool on_array_end(system::error_code& ec)
1056 : {
1057 : BOOST_ASSERT( array_depth_ > 0 );
1058 : --array_depth_;
1059 :
1060 : if( (array_depth_ + object_depth_) == 0 )
1061 : return parent_->signal_value(ec);
1062 : return true;
1063 : }
1064 :
1065 : bool on_key_part(system::error_code&, string_view)
1066 : {
1067 : return true;
1068 : }
1069 :
1070 : bool on_key(system::error_code&, string_view)
1071 : {
1072 : return true;
1073 : }
1074 :
1075 : bool on_string_part(system::error_code&, string_view)
1076 : {
1077 : return true;
1078 : }
1079 :
1080 : bool on_string(system::error_code& ec, string_view)
1081 : {
1082 : if( (array_depth_ + object_depth_) == 0 )
1083 : return parent_->signal_value(ec);
1084 : return true;
1085 : }
1086 :
1087 : bool on_number_part(system::error_code&)
1088 : {
1089 : return true;
1090 : }
1091 :
1092 : bool on_int64(system::error_code& ec, std::int64_t)
1093 : {
1094 : if( (array_depth_ + object_depth_) == 0 )
1095 : return parent_->signal_value(ec);
1096 : return true;
1097 : }
1098 :
1099 : bool on_uint64(system::error_code& ec, std::uint64_t)
1100 : {
1101 : if( (array_depth_ + object_depth_) == 0 )
1102 : return parent_->signal_value(ec);
1103 : return true;
1104 : }
1105 :
1106 : bool on_double(system::error_code& ec, double)
1107 : {
1108 : if( (array_depth_ + object_depth_) == 0 )
1109 : return parent_->signal_value(ec);
1110 : return true;
1111 : }
1112 :
1113 : bool on_bool(system::error_code& ec, bool)
1114 : {
1115 : if( (array_depth_ + object_depth_) == 0 )
1116 : return parent_->signal_value(ec);
1117 : return true;
1118 : }
1119 :
1120 : bool on_null(system::error_code& ec)
1121 : {
1122 : if( (array_depth_ + object_depth_) == 0 )
1123 : return parent_->signal_value(ec);
1124 : return true;
1125 : }
1126 : };
1127 :
1128 : template<class V, class P>
1129 : class converting_handler<described_class_category::value, V, P>
1130 : {
1131 : #if !defined(BOOST_DESCRIBE_CXX14)
1132 :
1133 : static_assert(
1134 : sizeof(V) == 0, "Struct support for parse_into requires C++14" );
1135 :
1136 : #else
1137 :
1138 : private:
1139 : static_assert(
1140 : uniquely_named_members<V>::value,
1141 : "The type has several described members with the same name.");
1142 :
1143 : using Dm = described_members<V>;
1144 : using Dt = struct_element_list<V>;
1145 :
1146 : template<class T>
1147 : using MemberHandler = get_handler<T, converting_handler>;
1148 : using InnerHandlers = mp11::mp_push_back<
1149 : mp11::mp_transform<MemberHandler, Dt>,
1150 : ignoring_handler<converting_handler> >;
1151 : using InnerCount = mp11::mp_size<InnerHandlers>;
1152 :
1153 : V* value_;
1154 : P* parent_;
1155 :
1156 : std::string key_;
1157 :
1158 : handler_tuple<converting_handler, InnerHandlers> handlers_;
1159 : int inner_active_ = -1;
1160 : std::bitset<mp11::mp_size<Dt>::value> seen_;
1161 :
1162 : public:
1163 : converting_handler( converting_handler const& ) = delete;
1164 : converting_handler& operator=( converting_handler const& ) = delete;
1165 :
1166 : converting_handler( V* v, P* p )
1167 : : value_(v), parent_(p), handlers_(struct_accessor(), v, this)
1168 : {}
1169 :
1170 : struct is_required_checker
1171 : {
1172 : bool operator()( mp11::mp_size<Dt> ) const noexcept
1173 : {
1174 : return false;
1175 : }
1176 :
1177 : template< class I >
1178 : auto operator()( I ) const noexcept
1179 : {
1180 : using T = mp11::mp_at<Dt, I>;
1181 : return !is_optional_like<T>::value;
1182 : }
1183 : };
1184 :
1185 : bool signal_value(system::error_code&)
1186 : {
1187 : BOOST_ASSERT( inner_active_ >= 0 );
1188 : bool required_member = mp11::mp_with_index<InnerCount>(
1189 : inner_active_,
1190 : is_required_checker{});
1191 : if( required_member )
1192 : seen_[inner_active_] = true;
1193 :
1194 : key_ = {};
1195 : inner_active_ = -1;
1196 : return true;
1197 : }
1198 :
1199 : bool signal_end(system::error_code& ec)
1200 : {
1201 : key_ = {};
1202 : inner_active_ = -1;
1203 : return parent_->signal_value(ec);
1204 : }
1205 :
1206 : #define BOOST_JSON_INVOKE_INNER(fn) \
1207 : if( inner_active_ < 0 ) \
1208 : { \
1209 : BOOST_JSON_FAIL( ec, error::not_object ); \
1210 : return false; \
1211 : } \
1212 : auto f = [&](auto& handler) { return handler.fn ; }; \
1213 : using F = decltype(f); \
1214 : using H = decltype(handlers_); \
1215 : return mp11::mp_with_index<InnerCount>( \
1216 : inner_active_, \
1217 : tuple_handler_op_invoker<H, F>{handlers_, f} );
1218 :
1219 : bool on_object_begin( system::error_code& ec )
1220 : {
1221 : if( inner_active_ < 0 )
1222 : {
1223 : seen_.reset();
1224 : return true;
1225 : }
1226 :
1227 : BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1228 : }
1229 :
1230 : bool on_object_end( system::error_code& ec )
1231 : {
1232 : if( inner_active_ < 0 )
1233 : {
1234 : using C = mp11::mp_count_if<Dt, is_optional_like>;
1235 : constexpr int N = mp11::mp_size<Dt>::value - C::value;
1236 : if( seen_.count() < N )
1237 : {
1238 : BOOST_JSON_FAIL( ec, error::size_mismatch );
1239 : return false;
1240 : }
1241 :
1242 : return parent_->signal_value(ec);
1243 : }
1244 :
1245 : BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1246 : }
1247 :
1248 : bool on_array_begin( system::error_code& ec )
1249 : {
1250 : BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1251 : }
1252 :
1253 : bool on_array_end( system::error_code& ec )
1254 : {
1255 : if( inner_active_ < 0 )
1256 : return parent_->signal_end(ec);
1257 :
1258 : BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1259 : }
1260 :
1261 : bool on_key_part( system::error_code& ec, string_view sv )
1262 : {
1263 : if( inner_active_ < 0 )
1264 : {
1265 : key_.append( sv.data(), sv.size() );
1266 : return true;
1267 : }
1268 :
1269 : BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1270 : }
1271 :
1272 : bool on_key( system::error_code& ec, string_view sv )
1273 : {
1274 : if( inner_active_ >= 0 )
1275 : {
1276 : BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1277 : }
1278 :
1279 : string_view key = sv;
1280 : if( !key_.empty() )
1281 : {
1282 : key_.append( sv.data(), sv.size() );
1283 : key = key_;
1284 : }
1285 :
1286 : inner_active_ = InnerCount::value - 1;
1287 : mp11::mp_for_each<Dm>( struct_key_searcher(key, inner_active_) );
1288 : return true;
1289 : }
1290 :
1291 : bool on_string_part( system::error_code& ec, string_view sv )
1292 : {
1293 : BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1294 : }
1295 :
1296 : bool on_string( system::error_code& ec, string_view sv )
1297 : {
1298 : BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1299 : }
1300 :
1301 : bool on_number_part( system::error_code& ec )
1302 : {
1303 : BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1304 : }
1305 :
1306 : bool on_int64( system::error_code& ec, std::int64_t v )
1307 : {
1308 : BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1309 : }
1310 :
1311 : bool on_uint64( system::error_code& ec, std::uint64_t v )
1312 : {
1313 : BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1314 : }
1315 :
1316 : bool on_double( system::error_code& ec, double v )
1317 : {
1318 : BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1319 : }
1320 :
1321 : bool on_bool( system::error_code& ec, bool v )
1322 : {
1323 : BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1324 : }
1325 :
1326 : bool on_null( system::error_code& ec )
1327 : {
1328 : BOOST_JSON_INVOKE_INNER( on_null(ec) );
1329 : }
1330 :
1331 : #undef BOOST_JSON_INVOKE_INNER
1332 :
1333 : #endif
1334 : };
1335 :
1336 : // variant handler
1337 : struct object_begin_handler_event
1338 : { };
1339 :
1340 : struct object_end_handler_event
1341 : { };
1342 :
1343 : struct array_begin_handler_event
1344 : { };
1345 :
1346 : struct array_end_handler_event
1347 : { };
1348 :
1349 : struct key_handler_event
1350 : {
1351 : std::string value;
1352 : };
1353 :
1354 : struct string_handler_event
1355 : {
1356 : std::string value;
1357 : };
1358 :
1359 : struct int64_handler_event
1360 : {
1361 : std::int64_t value;
1362 : };
1363 :
1364 : struct uint64_handler_event
1365 : {
1366 : std::uint64_t value;
1367 : };
1368 :
1369 : struct double_handler_event
1370 : {
1371 : double value;
1372 : };
1373 :
1374 : struct bool_handler_event
1375 : {
1376 : bool value;
1377 : };
1378 :
1379 : struct null_handler_event
1380 : { };
1381 :
1382 : using parse_event = variant2::variant<
1383 : object_begin_handler_event,
1384 : object_end_handler_event,
1385 : array_begin_handler_event,
1386 : array_end_handler_event,
1387 : key_handler_event,
1388 : string_handler_event,
1389 : int64_handler_event,
1390 : uint64_handler_event,
1391 : double_handler_event,
1392 : bool_handler_event,
1393 : null_handler_event>;
1394 :
1395 : template< class H >
1396 : struct event_visitor
1397 : {
1398 : H& handler;
1399 : system::error_code& ec;
1400 :
1401 : bool
1402 14 : operator()(object_begin_handler_event&) const
1403 : {
1404 14 : return handler.on_object_begin(ec);
1405 : }
1406 :
1407 : bool
1408 7 : operator()(object_end_handler_event&) const
1409 : {
1410 7 : return handler.on_object_end(ec);
1411 : }
1412 :
1413 : bool
1414 58 : operator()(array_begin_handler_event&) const
1415 : {
1416 58 : return handler.on_array_begin(ec);
1417 : }
1418 :
1419 : bool
1420 37 : operator()(array_end_handler_event&) const
1421 : {
1422 37 : return handler.on_array_end(ec);
1423 : }
1424 :
1425 : bool
1426 21 : operator()(key_handler_event& ev) const
1427 : {
1428 21 : return handler.on_key(ec, ev.value);
1429 : }
1430 :
1431 : bool
1432 108 : operator()(string_handler_event& ev) const
1433 : {
1434 108 : return handler.on_string(ec, ev.value);
1435 : }
1436 :
1437 : bool
1438 207 : operator()(int64_handler_event& ev) const
1439 : {
1440 207 : return handler.on_int64(ec, ev.value);
1441 : }
1442 :
1443 : bool
1444 14 : operator()(uint64_handler_event& ev) const
1445 : {
1446 14 : return handler.on_uint64(ec, ev.value);
1447 : }
1448 :
1449 : bool
1450 21 : operator()(double_handler_event& ev) const
1451 : {
1452 21 : return handler.on_double(ec, ev.value);
1453 : }
1454 :
1455 : bool
1456 7 : operator()(bool_handler_event& ev) const
1457 : {
1458 7 : return handler.on_bool(ec, ev.value);
1459 : }
1460 :
1461 : bool
1462 7 : operator()(null_handler_event&) const
1463 : {
1464 7 : return handler.on_null(ec);
1465 : }
1466 : };
1467 :
1468 : // L<T...> -> variant< monostate, get_handler<T, P>... >
1469 : template< class P, class L >
1470 : using inner_handler_variant = mp11::mp_push_front<
1471 : mp11::mp_transform_q<
1472 : mp11::mp_bind_back<get_handler, P>,
1473 : mp11::mp_apply<variant2::variant, L>>,
1474 : variant2::monostate>;
1475 :
1476 : template< class T, class P >
1477 : class converting_handler<variant_category::value, T, P>
1478 : {
1479 : private:
1480 : using variant_size = mp11::mp_size<T>;
1481 :
1482 : T* value_;
1483 : P* parent_;
1484 :
1485 : std::string string_;
1486 : std::vector< parse_event > events_;
1487 : inner_handler_variant<converting_handler, T> inner_;
1488 : int inner_active_ = -1;
1489 :
1490 : public:
1491 : converting_handler( converting_handler const& ) = delete;
1492 : converting_handler& operator=( converting_handler const& ) = delete;
1493 :
1494 106 : converting_handler( T* v, P* p )
1495 106 : : value_( v )
1496 106 : , parent_( p )
1497 106 : {}
1498 :
1499 161 : bool signal_value(system::error_code& ec)
1500 : {
1501 161 : inner_.template emplace<0>();
1502 161 : inner_active_ = -1;
1503 161 : events_.clear();
1504 161 : return parent_->signal_value(ec);
1505 : }
1506 :
1507 14 : bool signal_end(system::error_code& ec)
1508 : {
1509 14 : return parent_->signal_end(ec);
1510 : }
1511 :
1512 : struct alternative_selector
1513 : {
1514 : converting_handler* self;
1515 :
1516 : template< class I >
1517 : void
1518 264 : operator()( I ) const
1519 : {
1520 : using V = mp11::mp_at<T, I>;
1521 264 : auto& v = self->value_->template emplace<I::value>( V{} );
1522 264 : self->inner_.template emplace<I::value + 1>(&v, self);
1523 264 : }
1524 : };
1525 : void
1526 272 : next_alternative()
1527 : {
1528 272 : if( ++inner_active_ >= static_cast<int>(variant_size::value) )
1529 8 : return;
1530 :
1531 264 : mp11::mp_with_index< variant_size::value >(
1532 264 : inner_active_, alternative_selector{this} );
1533 : }
1534 :
1535 : struct event_processor
1536 : {
1537 : converting_handler* self;
1538 : system::error_code& ec;
1539 : parse_event& event;
1540 :
1541 : template< class I >
1542 501 : bool operator()( I ) const
1543 : {
1544 501 : auto& handler = variant2::get<I::value + 1>(self->inner_);
1545 : using Handler = remove_cvref<decltype(handler)>;
1546 501 : return variant2::visit(
1547 1002 : event_visitor<Handler>{handler, ec}, event );
1548 : }
1549 : };
1550 357 : bool process_events(system::error_code& ec)
1551 : {
1552 357 : constexpr std::size_t N = variant_size::value;
1553 :
1554 : // should be pointers not iterators, otherwise MSVC crashes
1555 357 : auto const last = events_.data() + events_.size();
1556 357 : auto first = last - 1;
1557 357 : bool ok = false;
1558 :
1559 357 : if( inner_active_ < 0 )
1560 169 : next_alternative();
1561 : do
1562 : {
1563 460 : if( static_cast<std::size_t>(inner_active_) >= N )
1564 : {
1565 8 : BOOST_JSON_FAIL( ec, error::exhausted_variants );
1566 8 : return false;
1567 : }
1568 :
1569 850 : for ( ; first != last; ++first )
1570 : {
1571 1002 : ok = mp11::mp_with_index< N >(
1572 501 : inner_active_, event_processor{this, ec, *first} );
1573 501 : if( !ok )
1574 : {
1575 103 : first = events_.data();
1576 103 : next_alternative();
1577 103 : ec.clear();
1578 103 : break;
1579 : }
1580 : }
1581 : }
1582 452 : while( !ok );
1583 :
1584 349 : return true;
1585 : }
1586 :
1587 : #define BOOST_JSON_INVOKE_INNER(ev, ec) \
1588 : events_.emplace_back( ev ); \
1589 : return process_events(ec);
1590 :
1591 7 : bool on_object_begin( system::error_code& ec )
1592 : {
1593 7 : BOOST_JSON_INVOKE_INNER( object_begin_handler_event{}, ec );
1594 : }
1595 :
1596 7 : bool on_object_end( system::error_code& ec )
1597 : {
1598 7 : BOOST_JSON_INVOKE_INNER( object_end_handler_event{}, ec );
1599 : }
1600 :
1601 37 : bool on_array_begin( system::error_code& ec )
1602 : {
1603 37 : BOOST_JSON_INVOKE_INNER( array_begin_handler_event{}, ec );
1604 : }
1605 :
1606 51 : bool on_array_end( system::error_code& ec )
1607 : {
1608 51 : if( inner_active_ < 0 )
1609 14 : return signal_end(ec);
1610 :
1611 37 : BOOST_JSON_INVOKE_INNER( array_end_handler_event{}, ec );
1612 : }
1613 :
1614 5 : bool on_key_part( system::error_code&, string_view sv )
1615 : {
1616 5 : string_.append(sv);
1617 5 : return true;
1618 : }
1619 :
1620 14 : bool on_key( system::error_code& ec, string_view sv )
1621 : {
1622 14 : string_.append(sv);
1623 28 : BOOST_JSON_INVOKE_INNER( key_handler_event{ std::move(string_) }, ec );
1624 14 : }
1625 :
1626 31 : bool on_string_part( system::error_code&, string_view sv )
1627 : {
1628 31 : string_.append(sv);
1629 31 : return true;
1630 : }
1631 :
1632 48 : bool on_string( system::error_code& ec, string_view sv )
1633 : {
1634 48 : string_.append(sv);
1635 96 : BOOST_JSON_INVOKE_INNER(
1636 : string_handler_event{ std::move(string_) }, ec );
1637 48 : }
1638 :
1639 71 : bool on_number_part( system::error_code& )
1640 : {
1641 71 : return true;
1642 : }
1643 :
1644 172 : bool on_int64( system::error_code& ec, std::int64_t v )
1645 : {
1646 172 : BOOST_JSON_INVOKE_INNER( int64_handler_event{v}, ec );
1647 : }
1648 :
1649 7 : bool on_uint64( system::error_code& ec, std::uint64_t v )
1650 : {
1651 7 : BOOST_JSON_INVOKE_INNER( uint64_handler_event{v}, ec );
1652 : }
1653 :
1654 14 : bool on_double( system::error_code& ec, double v )
1655 : {
1656 14 : BOOST_JSON_INVOKE_INNER( double_handler_event{v}, ec );
1657 : }
1658 :
1659 7 : bool on_bool( system::error_code& ec, bool v )
1660 : {
1661 7 : BOOST_JSON_INVOKE_INNER( bool_handler_event{v}, ec );
1662 : }
1663 :
1664 7 : bool on_null( system::error_code& ec )
1665 : {
1666 7 : BOOST_JSON_INVOKE_INNER( null_handler_event{}, ec );
1667 : }
1668 :
1669 : #undef BOOST_JSON_INVOKE_INNER
1670 : };
1671 :
1672 : // optional handler
1673 : template<class V, class P>
1674 : class converting_handler<optional_category::value, V, P>
1675 : {
1676 : private:
1677 : using inner_type = value_result_type<V>;
1678 : using inner_handler_type = get_handler<inner_type, converting_handler>;
1679 :
1680 : V* value_;
1681 : P* parent_;
1682 :
1683 : inner_type inner_value_ = {};
1684 : inner_handler_type inner_;
1685 : bool inner_active_ = false;
1686 :
1687 : public:
1688 : converting_handler( converting_handler const& ) = delete;
1689 : converting_handler& operator=( converting_handler const& ) = delete;
1690 :
1691 : converting_handler( V* v, P* p )
1692 : : value_(v), parent_(p), inner_(&inner_value_, this)
1693 : {}
1694 :
1695 : bool signal_value(system::error_code& ec)
1696 : {
1697 : *value_ = std::move(inner_value_);
1698 :
1699 : inner_active_ = false;
1700 : return parent_->signal_value(ec);
1701 : }
1702 :
1703 : bool signal_end(system::error_code& ec)
1704 : {
1705 : return parent_->signal_end(ec);
1706 : }
1707 :
1708 : #define BOOST_JSON_INVOKE_INNER(fn) \
1709 : if( !inner_active_ ) \
1710 : inner_active_ = true; \
1711 : return inner_.fn;
1712 :
1713 : bool on_object_begin( system::error_code& ec )
1714 : {
1715 : BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1716 : }
1717 :
1718 : bool on_object_end( system::error_code& ec )
1719 : {
1720 : BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1721 : }
1722 :
1723 : bool on_array_begin( system::error_code& ec )
1724 : {
1725 : BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1726 : }
1727 :
1728 : bool on_array_end( system::error_code& ec )
1729 : {
1730 : if( !inner_active_ )
1731 : return signal_end(ec);
1732 :
1733 : BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1734 : }
1735 :
1736 : bool on_key_part( system::error_code& ec, string_view sv )
1737 : {
1738 : BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1739 : }
1740 :
1741 : bool on_key( system::error_code& ec, string_view sv )
1742 : {
1743 : BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1744 : }
1745 :
1746 : bool on_string_part( system::error_code& ec, string_view sv )
1747 : {
1748 : BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1749 : }
1750 :
1751 : bool on_string( system::error_code& ec, string_view sv )
1752 : {
1753 : BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1754 : }
1755 :
1756 : bool on_number_part( system::error_code& ec )
1757 : {
1758 : BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1759 : }
1760 :
1761 : bool on_int64( system::error_code& ec, std::int64_t v )
1762 : {
1763 : BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1764 : }
1765 :
1766 : bool on_uint64( system::error_code& ec, std::uint64_t v )
1767 : {
1768 : BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1769 : }
1770 :
1771 : bool on_double( system::error_code& ec, double v )
1772 : {
1773 : BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1774 : }
1775 :
1776 : bool on_bool( system::error_code& ec, bool v )
1777 : {
1778 : BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1779 : }
1780 :
1781 : bool on_null(system::error_code& ec)
1782 : {
1783 : if( !inner_active_ )
1784 : {
1785 : *value_ = {};
1786 : return this->parent_->signal_value(ec);
1787 : }
1788 : else
1789 : {
1790 : return inner_.on_null(ec);
1791 : }
1792 : }
1793 :
1794 : #undef BOOST_JSON_INVOKE_INNER
1795 : };
1796 :
1797 : // path handler
1798 : template< class V, class P >
1799 : class converting_handler<path_category::value, V, P>
1800 : : public scalar_handler<P, error::not_string>
1801 : {
1802 : private:
1803 : V* value_;
1804 : bool cleared_ = false;
1805 :
1806 : public:
1807 : converting_handler( V* v, P* p )
1808 : : converting_handler::scalar_handler(p)
1809 : , value_(v)
1810 : {}
1811 :
1812 : bool on_string_part( system::error_code&, string_view sv )
1813 : {
1814 : if( !cleared_ )
1815 : {
1816 : cleared_ = true;
1817 : value_->clear();
1818 : }
1819 :
1820 : value_->concat( sv.begin(), sv.end() );
1821 : return true;
1822 : }
1823 :
1824 : bool on_string(system::error_code& ec, string_view sv)
1825 : {
1826 : if( !cleared_ )
1827 : value_->clear();
1828 : else
1829 : cleared_ = false;
1830 :
1831 : value_->concat( sv.begin(), sv.end() );
1832 :
1833 : return this->parent_->signal_value(ec);
1834 : }
1835 : };
1836 :
1837 : // into_handler
1838 : template< class V >
1839 : class into_handler
1840 : {
1841 : private:
1842 :
1843 : using inner_handler_type = get_handler<V, into_handler>;
1844 :
1845 : inner_handler_type inner_;
1846 : bool inner_active_ = true;
1847 :
1848 : public:
1849 :
1850 : into_handler( into_handler const& ) = delete;
1851 : into_handler& operator=( into_handler const& ) = delete;
1852 :
1853 : public:
1854 :
1855 : static constexpr std::size_t max_object_size = object::max_size();
1856 : static constexpr std::size_t max_array_size = array::max_size();
1857 : static constexpr std::size_t max_key_size = string::max_size();
1858 : static constexpr std::size_t max_string_size = string::max_size();
1859 :
1860 : public:
1861 :
1862 546 : explicit into_handler( V* v ): inner_( v, this )
1863 : {
1864 546 : }
1865 :
1866 495 : bool signal_value(system::error_code&)
1867 : {
1868 495 : return true;
1869 : }
1870 :
1871 : // LCOV_EXCL_START
1872 : // the top handler is never inside an array, so nested handlers have no
1873 : // array end to forward to it
1874 : bool signal_end(system::error_code&)
1875 : {
1876 : return true;
1877 : }
1878 : // LCOV_EXCL_STOP
1879 :
1880 545 : bool on_document_begin( system::error_code& )
1881 : {
1882 545 : return true;
1883 : }
1884 :
1885 495 : bool on_document_end( system::error_code& )
1886 : {
1887 495 : inner_active_ = false;
1888 495 : return true;
1889 : }
1890 :
1891 : #define BOOST_JSON_INVOKE_INNER(f) \
1892 : if( !inner_active_ ) \
1893 : { \
1894 : BOOST_JSON_FAIL( ec, error::extra_data ); \
1895 : return false; \
1896 : } \
1897 : else \
1898 : return inner_.f
1899 :
1900 151 : bool on_object_begin( system::error_code& ec )
1901 : {
1902 151 : BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1903 : }
1904 :
1905 145 : bool on_object_end( std::size_t, system::error_code& ec )
1906 : {
1907 145 : BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1908 : }
1909 :
1910 477 : bool on_array_begin( system::error_code& ec )
1911 : {
1912 477 : BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1913 : }
1914 :
1915 463 : bool on_array_end( std::size_t, system::error_code& ec )
1916 : {
1917 463 : BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1918 : }
1919 :
1920 50 : bool on_key_part( string_view sv, std::size_t, system::error_code& ec )
1921 : {
1922 50 : BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1923 : }
1924 :
1925 153 : bool on_key( string_view sv, std::size_t, system::error_code& ec )
1926 : {
1927 153 : BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1928 : }
1929 :
1930 54 : bool on_string_part( string_view sv, std::size_t, system::error_code& ec )
1931 : {
1932 54 : BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1933 : }
1934 :
1935 101 : bool on_string( string_view sv, std::size_t, system::error_code& ec )
1936 : {
1937 101 : BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1938 : }
1939 :
1940 514 : bool on_number_part( string_view, system::error_code& ec )
1941 : {
1942 514 : BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1943 : }
1944 :
1945 791 : bool on_int64( std::int64_t v, string_view, system::error_code& ec )
1946 : {
1947 791 : BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1948 : }
1949 :
1950 39 : bool on_uint64( std::uint64_t v, string_view, system::error_code& ec )
1951 : {
1952 39 : BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1953 : }
1954 :
1955 63 : bool on_double( double v, string_view, system::error_code& ec )
1956 : {
1957 63 : BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1958 : }
1959 :
1960 44 : bool on_bool( bool v, system::error_code& ec )
1961 : {
1962 44 : BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1963 : }
1964 :
1965 39 : bool on_null( system::error_code& ec )
1966 : {
1967 39 : BOOST_JSON_INVOKE_INNER( on_null(ec) );
1968 : }
1969 :
1970 1311 : bool on_comment_part(string_view, system::error_code&)
1971 : {
1972 1311 : return true;
1973 : }
1974 :
1975 69 : bool on_comment(string_view, system::error_code&)
1976 : {
1977 69 : return true;
1978 : }
1979 :
1980 : #undef BOOST_JSON_INVOKE_INNER
1981 : };
1982 :
1983 : } // namespace detail
1984 : } // namespace boost
1985 : } // namespace json
1986 :
1987 : #endif
|