Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-06-30 08:17:44

0001 //
0002 // Copyright (c) 2019 Vinnie Falco (vinnie.falco@gmail.com)
0003 // Copyright (c) 2020 Krystian Stasiowski (sdkrystian@gmail.com)
0004 //
0005 // Distributed under the Boost Software License, Version 1.0. (See accompanying
0006 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
0007 //
0008 // Official repository: https://github.com/boostorg/json
0009 //
0010 
0011 #ifndef BOOST_JSON_DETAIL_PARSE_INTO_HPP
0012 #define BOOST_JSON_DETAIL_PARSE_INTO_HPP
0013 
0014 #include <boost/json/detail/config.hpp>
0015 
0016 #include <boost/json/error.hpp>
0017 #include <boost/json/conversion.hpp>
0018 #include <boost/describe/enum_from_string.hpp>
0019 
0020 #include <vector>
0021 
0022 /*
0023  * This file contains the majority of parse_into functionality, specifically
0024  * the implementation of dedicated handlers for different generic categories of
0025  * types.
0026  *
0027  * At the core of parse_into is the specialisation basic_parser<
0028  * detail::into_handler<T> >. detail::into_handler<T> is a handler for
0029  * basic_parser. It directly handles events on_comment_part and on_comment (by
0030  * ignoring them), on_document_begin (by enabling the nested dedicated
0031  * handler), and on_document_end (by disabling the nested handler).
0032  *
0033  * Every other event is handled by the nested handler, which has the type
0034  * get_handler< T, into_handler<T> >. The second parameter is the parent
0035  * handler (in this case, it's the top handler, into_handler<T>). The type is
0036  * actually an alias to class template converting_handler, which has a separate
0037  * specialisation for every conversion category from the list of generic
0038  * conversion categories (e.g. sequence_conversion_tag, tuple_conversion_tag,
0039  * etc.) Instantiations of the template store a pointer to the parent handler
0040  * and a pointer to the value T.
0041  *
0042  * The nested handler handles specific parser events by setting error_code to
0043  * an appropriate value, if it receives an event it isn't supposed to handle
0044  * (e.g. a number handler getting an on_string event), and also updates the
0045  * value when appropriate. Note that they never need to handle on_comment_part,
0046  * on_comment, on_document_begin, and on_document_end events, as those are
0047  * always handled by the top handler into_handler<T>.
0048  *
0049  * When the nested handler receives an event that completes the current value,
0050  * it is supposed to call its parent's signal_value member function. This is
0051  * necessary for correct handling of composite types (e.g. sequences).
0052  *
0053  * Finally, nested handlers should always call parent's signal_end member
0054  * function if they don't handle on_array_end themselves. This is necessary
0055  * to correctly handle nested composites (e.g. sequences inside sequences).
0056  * signal_end can return false and set error state when the containing parser
0057  * requires more elements.
0058  *
0059  * converting_handler instantiations for composite categories of types have
0060  * their own nested handlers, to which they themselves delegate events. For
0061  * complex types you will get a tree of handlers with into_handler<T> as the
0062  * root and handlers for scalars as leaves.
0063  *
0064  * To reiterate, only into_handler has to handle on_comment_part, on_comment,
0065  * on_document_begin, and on_document_end; only handlers for composites and
0066  * into_handler has to provide signal_value and signal_end; all handlers
0067  * except for into_handler have to call their parent's signal_end from
0068  * their on_array_begin, if they don't handle it themselves; once a handler
0069  * receives an event that finishes its current value, it should call its
0070  * parent's signal_value.
0071  */
0072 
0073 namespace boost {
0074 namespace json {
0075 namespace detail {
0076 
0077 template< class Impl, class T, class Parent >
0078 class converting_handler;
0079 
0080 // get_handler
0081 template< class V, class P >
0082 using get_handler = converting_handler< generic_conversion_category<V>, V, P >;
0083 
0084 template<error E> class handler_error_base
0085 {
0086 public:
0087 
0088     handler_error_base() = default;
0089 
0090     handler_error_base( handler_error_base const& ) = delete;
0091     handler_error_base& operator=( handler_error_base const& ) = delete;
0092 
0093 public:
0094 
0095     bool on_object_begin( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
0096     bool on_array_begin( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
0097     bool on_array_end( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
0098     bool on_string_part( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
0099     bool on_string( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
0100     bool on_number_part( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
0101     bool on_int64( system::error_code& ec, std::int64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
0102     bool on_uint64( system::error_code& ec, std::uint64_t ) { BOOST_JSON_FAIL( ec, E ); return false; }
0103     bool on_double( system::error_code& ec, double ) { BOOST_JSON_FAIL( ec, E ); return false; }
0104     bool on_bool( system::error_code& ec, bool ) { BOOST_JSON_FAIL( ec, E ); return false; }
0105     bool on_null( system::error_code& ec ) { BOOST_JSON_FAIL( ec, E ); return false; }
0106 
0107     // LCOV_EXCL_START
0108     // parses that can't handle this would fail at on_object_begin
0109     bool on_object_end( system::error_code& ) { BOOST_ASSERT( false ); return false; }
0110     bool on_key_part( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
0111     bool on_key( system::error_code& ec, string_view ) { BOOST_JSON_FAIL( ec, E ); return false; }
0112     // LCOV_EXCL_STOP
0113 };
0114 
0115 template< class P, error E >
0116 class scalar_handler
0117     : public handler_error_base<E>
0118 {
0119 protected:
0120     P* parent_;
0121 
0122 public:
0123     scalar_handler(scalar_handler const&) = delete;
0124     scalar_handler& operator=(scalar_handler const&) = delete;
0125 
0126     scalar_handler(P* p): parent_( p )
0127     {}
0128 
0129     bool on_array_end( system::error_code& ec )
0130     {
0131         return parent_->signal_end(ec);
0132     }
0133 };
0134 
0135 template< class D, class V, class P, error E >
0136 class composite_handler
0137 {
0138 protected:
0139     using inner_handler_type = get_handler<V, D>;
0140 
0141     P* parent_;
0142 #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
0143 # pragma GCC diagnostic push
0144 # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
0145 #endif
0146     V next_value_ = {};
0147     inner_handler_type inner_;
0148     bool inner_active_ = false;
0149 
0150 public:
0151     composite_handler( composite_handler const& ) = delete;
0152     composite_handler& operator=( composite_handler const& ) = delete;
0153 
0154     composite_handler( P* p )
0155         : parent_(p), inner_( &next_value_, static_cast<D*>(this) )
0156     {}
0157 #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
0158 # pragma GCC diagnostic pop
0159 #endif
0160 
0161     bool signal_end(system::error_code&)
0162     {
0163         inner_active_ = false;
0164         parent_->signal_value();
0165         return true;
0166     }
0167 
0168 #define BOOST_JSON_INVOKE_INNER(f) \
0169     if( !inner_active_ ) { \
0170         BOOST_JSON_FAIL(ec, E); \
0171         return false; \
0172     } \
0173     else \
0174         return inner_.f
0175 
0176     bool on_object_begin( system::error_code& ec )
0177     {
0178         BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
0179     }
0180 
0181     bool on_object_end( system::error_code& ec )
0182     {
0183         BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
0184     }
0185 
0186     bool on_array_begin( system::error_code& ec )
0187     {
0188         BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
0189     }
0190 
0191     bool on_array_end( system::error_code& ec )
0192     {
0193         BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
0194     }
0195 
0196     bool on_key_part( system::error_code& ec, string_view sv )
0197     {
0198         BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
0199     }
0200 
0201     bool on_key( system::error_code& ec, string_view sv )
0202     {
0203         BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
0204     }
0205 
0206     bool on_string_part( system::error_code& ec, string_view sv )
0207     {
0208         BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
0209     }
0210 
0211     bool on_string( system::error_code& ec, string_view sv )
0212     {
0213         BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
0214     }
0215 
0216     bool on_number_part( system::error_code& ec )
0217     {
0218         BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
0219     }
0220 
0221     bool on_int64( system::error_code& ec, std::int64_t v )
0222     {
0223         BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
0224     }
0225 
0226     bool on_uint64( system::error_code& ec, std::uint64_t v )
0227     {
0228         BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
0229     }
0230 
0231     bool on_double( system::error_code& ec, double v )
0232     {
0233         BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
0234     }
0235 
0236     bool on_bool( system::error_code& ec, bool v )
0237     {
0238         BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
0239     }
0240 
0241     bool on_null( system::error_code& ec )
0242     {
0243         BOOST_JSON_INVOKE_INNER( on_null(ec) );
0244     }
0245 
0246 #undef BOOST_JSON_INVOKE_INNER
0247 };
0248 
0249 // integral handler
0250 template<class V,
0251 typename std::enable_if<std::is_signed<V>::value, int>::type = 0>
0252 bool integral_in_range( std::int64_t v )
0253 {
0254     return v >= (std::numeric_limits<V>::min)() && v <= (std::numeric_limits<V>::max)();
0255 }
0256 
0257 template<class V,
0258 typename std::enable_if<!std::is_signed<V>::value, int>::type = 0>
0259 bool integral_in_range( std::int64_t v )
0260 {
0261     return v >= 0 && static_cast<std::uint64_t>( v ) <= (std::numeric_limits<V>::max)();
0262 }
0263 
0264 template<class V>
0265 bool integral_in_range( std::uint64_t v )
0266 {
0267     return v <= static_cast<typename std::make_unsigned<V>::type>( (std::numeric_limits<V>::max)() );
0268 }
0269 
0270 template< class V, class P >
0271 class converting_handler<integral_conversion_tag, V, P>
0272     : public scalar_handler<P, error::not_integer>
0273 {
0274 private:
0275     V* value_;
0276 
0277 public:
0278     converting_handler( V* v, P* p )
0279         : converting_handler::scalar_handler(p)
0280         , value_(v)
0281     {}
0282 
0283     bool on_number_part( system::error_code& )
0284     {
0285         return true;
0286     }
0287 
0288     bool on_int64( system::error_code& ec, std::int64_t v )
0289     {
0290         if( !integral_in_range<V>( v ) )
0291         {
0292             BOOST_JSON_FAIL( ec, error::not_exact );
0293             return false;
0294         }
0295 
0296         *value_ = static_cast<V>( v );
0297 
0298         this->parent_->signal_value();
0299         return true;
0300     }
0301 
0302     bool on_uint64( system::error_code& ec, std::uint64_t v )
0303     {
0304         if( !integral_in_range<V>( v ) )
0305         {
0306             BOOST_JSON_FAIL( ec, error::not_exact );
0307             return false;
0308         }
0309 
0310         *value_ = static_cast<V>( v );
0311 
0312         this->parent_->signal_value();
0313         return true;
0314     }
0315 };
0316 
0317 // floating point handler
0318 template< class V, class P>
0319 class converting_handler<floating_point_conversion_tag, V, P>
0320     : public scalar_handler<P, error::not_double>
0321 {
0322 private:
0323     V* value_;
0324 
0325 public:
0326     converting_handler( V* v, P* p )
0327         : converting_handler::scalar_handler(p)
0328         , value_(v)
0329     {}
0330 
0331     bool on_number_part( system::error_code& )
0332     {
0333         return true;
0334     }
0335 
0336     bool on_int64( system::error_code&, std::int64_t v )
0337     {
0338         *value_ = static_cast<V>( v );
0339 
0340         this->parent_->signal_value();
0341         return true;
0342     }
0343 
0344     bool on_uint64( system::error_code&, std::uint64_t v )
0345     {
0346         *value_ = static_cast<V>( v );
0347 
0348         this->parent_->signal_value();
0349         return true;
0350     }
0351 
0352     bool on_double( system::error_code&, double v )
0353     {
0354         *value_ = static_cast<V>( v );
0355 
0356         this->parent_->signal_value();
0357         return true;
0358     }
0359 };
0360 
0361 // string handler
0362 template< class V, class P >
0363 class converting_handler<string_like_conversion_tag, V, P>
0364     : public scalar_handler<P, error::not_string>
0365 {
0366 private:
0367     V* value_;
0368     bool cleared_ = false;
0369 
0370 public:
0371     converting_handler( V* v, P* p )
0372         : converting_handler::scalar_handler(p)
0373         , value_(v)
0374     {}
0375 
0376     bool on_string_part( system::error_code&, string_view sv )
0377     {
0378         if( !cleared_ )
0379         {
0380             cleared_ = true;
0381             value_->clear();
0382         }
0383 
0384         value_->append( sv.begin(), sv.end() );
0385         return true;
0386     }
0387 
0388     bool on_string( system::error_code&, string_view sv )
0389     {
0390         if( !cleared_ )
0391             value_->clear();
0392         else
0393             cleared_ = false;
0394 
0395         value_->append( sv.begin(), sv.end() );
0396 
0397         this->parent_->signal_value();
0398         return true;
0399     }
0400 };
0401 
0402 // bool handler
0403 template< class V, class P >
0404 class converting_handler<bool_conversion_tag, V, P>
0405     : public scalar_handler<P, error::not_bool>
0406 {
0407 private:
0408     V* value_;
0409 
0410 public:
0411     converting_handler( V* v, P* p )
0412         : converting_handler::scalar_handler(p)
0413         , value_(v)
0414     {}
0415 
0416     bool on_bool( system::error_code&, bool v )
0417     {
0418         *value_ = v;
0419 
0420         this->parent_->signal_value();
0421         return true;
0422     }
0423 };
0424 
0425 // null handler
0426 template< class V, class P >
0427 class converting_handler<null_like_conversion_tag, V, P>
0428     : public scalar_handler<P, error::not_null>
0429 {
0430 private:
0431     V* value_;
0432 
0433 public:
0434     converting_handler( V* v, P* p )
0435         : converting_handler::scalar_handler(p)
0436         , value_(v)
0437     {}
0438 
0439     bool on_null( system::error_code& )
0440     {
0441         *value_ = {};
0442 
0443         this->parent_->signal_value();
0444         return true;
0445     }
0446 };
0447 
0448 // described enum handler
0449 template< class V, class P >
0450 class converting_handler<described_enum_conversion_tag, V, P>
0451     : public scalar_handler<P, error::not_string>
0452 {
0453 #ifndef BOOST_DESCRIBE_CXX14
0454 
0455     static_assert(
0456         sizeof(V) == 0, "Enum support for parse_into requires C++14" );
0457 
0458 #else
0459 
0460 private:
0461     V* value_;
0462     std::string name_;
0463 
0464 public:
0465     converting_handler( V* v, P* p )
0466         : converting_handler::scalar_handler(p)
0467         , value_(v)
0468     {}
0469 
0470     bool on_string_part( system::error_code&, string_view sv )
0471     {
0472         name_.append( sv.begin(), sv.end() );
0473         return true;
0474     }
0475 
0476     bool on_string( system::error_code& ec, string_view sv )
0477     {
0478         string_view name = sv;
0479         if( !name_.empty() )
0480         {
0481             name_.append( sv.begin(), sv.end() );
0482             name = name_;
0483         }
0484 
0485         if( !describe::enum_from_string(name, *value_) )
0486         {
0487             BOOST_JSON_FAIL(ec, error::unknown_name);
0488             return false;
0489         }
0490 
0491         this->parent_->signal_value();
0492         return true;
0493     }
0494 
0495 #endif // BOOST_DESCRIBE_CXX14
0496 };
0497 
0498 template< class V, class P >
0499 class converting_handler<no_conversion_tag, V, P>
0500 {
0501     static_assert( sizeof(V) == 0, "This type is not supported" );
0502 };
0503 
0504 // sequence handler
0505 template< class It >
0506 bool check_inserter( It l, It r )
0507 {
0508     return l == r;
0509 }
0510 
0511 template< class It1, class It2 >
0512 std::true_type check_inserter( It1, It2 )
0513 {
0514     return {};
0515 }
0516 
0517 template<class T>
0518 void
0519 clear_container(
0520     T&,
0521     mp11::mp_int<2>)
0522 {
0523 }
0524 
0525 template<class T>
0526 void
0527 clear_container(
0528     T& target,
0529     mp11::mp_int<1>)
0530 {
0531     target.clear();
0532 }
0533 
0534 template<class T>
0535 void
0536 clear_container(
0537     T& target,
0538     mp11::mp_int<0>)
0539 {
0540     target.clear();
0541 }
0542 
0543 template< class V, class P >
0544 class converting_handler<sequence_conversion_tag, V, P>
0545     : public composite_handler<
0546         converting_handler<sequence_conversion_tag, V, P>,
0547         detail::value_type<V>,
0548         P,
0549         error::not_array>
0550 {
0551 private:
0552     V* value_;
0553 
0554     using Inserter = decltype(
0555         detail::inserter(*value_, inserter_implementation<V>()) );
0556     Inserter inserter;
0557 
0558 public:
0559     converting_handler( V* v, P* p )
0560         : converting_handler::composite_handler(p)
0561         , value_(v)
0562         , inserter( detail::inserter(*value_, inserter_implementation<V>()) )
0563     {}
0564 
0565     void signal_value()
0566     {
0567         *inserter++ = std::move(this->next_value_);
0568 #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
0569 # pragma GCC diagnostic push
0570 # pragma GCC diagnostic ignored "-Wmissing-field-initializers"
0571 #endif
0572         this->next_value_ = {};
0573 #if defined(__GNUC__) && __GNUC__ < 5 && !defined(__clang__)
0574 # pragma GCC diagnostic pop
0575 #endif
0576     }
0577 
0578     bool signal_end(system::error_code& ec)
0579     {
0580         if( !check_inserter( inserter, value_->end() ) )
0581         {
0582             BOOST_JSON_FAIL( ec, error::size_mismatch );
0583             return false;
0584         }
0585 
0586         inserter = detail::inserter(*value_, inserter_implementation<V>());
0587 
0588         return converting_handler::composite_handler::signal_end(ec);
0589     }
0590 
0591     bool on_array_begin( system::error_code& ec )
0592     {
0593         if( this->inner_active_ )
0594             return this->inner_.on_array_begin( ec );
0595 
0596         this->inner_active_ = true;
0597         clear_container( *value_, inserter_implementation<V>() );
0598         return true;
0599     }
0600 
0601     bool on_array_end( system::error_code& ec )
0602     {
0603         if( this->inner_active_ )
0604             return this->inner_.on_array_end( ec );
0605 
0606         return this->parent_->signal_end(ec);
0607     }
0608 };
0609 
0610 // map handler
0611 template< class V, class P >
0612 class converting_handler<map_like_conversion_tag, V, P>
0613     : public composite_handler<
0614         converting_handler<map_like_conversion_tag, V, P>,
0615         detail::mapped_type<V>,
0616         P,
0617         error::not_object>
0618 {
0619 private:
0620     V* value_;
0621     std::string key_;
0622 
0623 public:
0624     converting_handler( V* v, P* p )
0625         : converting_handler::composite_handler(p), value_(v)
0626     {}
0627 
0628     void signal_value()
0629     {
0630         value_->emplace( std::move(key_), std::move(this->next_value_) );
0631 
0632         key_ = {};
0633         this->next_value_ = {};
0634 
0635         this->inner_active_ = false;
0636     }
0637 
0638     bool on_object_begin( system::error_code& ec )
0639     {
0640         if( this->inner_active_ )
0641             return this->inner_.on_object_begin(ec);
0642 
0643         clear_container( *value_, inserter_implementation<V>() );
0644         return true;
0645     }
0646 
0647     bool on_object_end( system::error_code& ec )
0648     {
0649         if( this->inner_active_ )
0650             return this->inner_.on_object_end(ec);
0651 
0652         this->parent_->signal_value();
0653         return true;
0654     }
0655 
0656     bool on_array_end( system::error_code& ec )
0657     {
0658         if( this->inner_active_ )
0659             return this->inner_.on_array_end(ec);
0660 
0661         return this->parent_->signal_end(ec);
0662     }
0663 
0664     bool on_key_part( system::error_code& ec, string_view sv )
0665     {
0666         if( this->inner_active_ )
0667             return this->inner_.on_key_part(ec, sv);
0668 
0669         key_.append( sv.data(), sv.size() );
0670         return true;
0671     }
0672 
0673     bool on_key( system::error_code& ec, string_view sv )
0674     {
0675         if( this->inner_active_ )
0676             return this->inner_.on_key(ec, sv);
0677 
0678         key_.append( sv.data(), sv.size() );
0679 
0680         this->inner_active_ = true;
0681         return true;
0682     }
0683 };
0684 
0685 // tuple handler
0686 template<std::size_t I, class T>
0687 struct handler_tuple_element
0688 {
0689     template< class... Args >
0690     handler_tuple_element( Args&& ... args )
0691         : t_( static_cast<Args&&>(args)... )
0692     {}
0693 
0694     T t_;
0695 };
0696 
0697 template<std::size_t I, class T>
0698 T&
0699 get( handler_tuple_element<I, T>& e )
0700 {
0701     return e.t_;
0702 }
0703 
0704 template<
0705     class P,
0706     class LV,
0707     class S = mp11::make_index_sequence<mp11::mp_size<LV>::value> >
0708 struct handler_tuple;
0709 
0710 template< class P, template<class...> class L, class... V, std::size_t... I >
0711 struct handler_tuple< P, L<V...>, mp11::index_sequence<I...> >
0712     : handler_tuple_element< I, get_handler<V, P> >
0713     ...
0714 {
0715     handler_tuple( handler_tuple const& ) = delete;
0716     handler_tuple& operator=( handler_tuple const& ) = delete;
0717 
0718     template< class Access, class T >
0719     handler_tuple( Access access, T* pv, P* pp )
0720         : handler_tuple_element< I, get_handler<V, P> >(
0721             access( pv, mp11::mp_int<I>() ),
0722             pp )
0723         ...
0724     { }
0725 };
0726 
0727 #if defined(BOOST_MSVC) && BOOST_MSVC < 1910
0728 
0729 template< class T >
0730 struct tuple_element_list_impl
0731 {
0732     template< class I >
0733     using tuple_element_helper = tuple_element_t<I::value, T>;
0734 
0735     using type = mp11::mp_transform<
0736         tuple_element_helper,
0737         mp11::mp_iota< std::tuple_size<T> > >;
0738 };
0739 template< class T >
0740 using tuple_element_list = typename tuple_element_list_impl<T>::type;
0741 
0742 #else
0743 
0744 template< class I, class T >
0745 using tuple_element_helper = tuple_element_t<I::value, T>;
0746 template< class T >
0747 using tuple_element_list = mp11::mp_transform_q<
0748     mp11::mp_bind_back< tuple_element_helper, T>,
0749     mp11::mp_iota< std::tuple_size<T> > >;
0750 
0751 #endif
0752 
0753 template< class Op, class... Args>
0754 struct handler_op_invoker
0755 {
0756 public:
0757     std::tuple<Args&...> args;
0758 
0759     template< class Handler >
0760     bool
0761     operator()( Handler& handler ) const
0762     {
0763         return (*this)( handler, mp11::index_sequence_for<Args...>() );
0764     }
0765 
0766 private:
0767     template< class Handler, std::size_t... I >
0768     bool
0769     operator()( Handler& handler, mp11::index_sequence<I...> ) const
0770     {
0771         return Op()( handler, std::get<I>(args)... );
0772     }
0773 };
0774 
0775 template< class Handlers, class F >
0776 struct tuple_handler_op_invoker
0777 {
0778     Handlers& handlers;
0779     F fn;
0780 
0781     template< class I >
0782     bool
0783     operator()( I ) const
0784     {
0785         return fn( get<I::value>(handlers) );
0786     }
0787 };
0788 
0789 struct tuple_accessor
0790 {
0791     template< class T, class I >
0792     auto operator()( T* t, I ) const -> tuple_element_t<I::value, T>*
0793     {
0794         using std::get;
0795         return &get<I::value>(*t);
0796     }
0797 };
0798 
0799 template< class T, class P >
0800 class converting_handler<tuple_conversion_tag, T, P>
0801 {
0802 
0803 private:
0804     T* value_;
0805     P* parent_;
0806 
0807     handler_tuple< converting_handler, tuple_element_list<T> > handlers_;
0808     int inner_active_ = -1;
0809 
0810 public:
0811     converting_handler( converting_handler const& ) = delete;
0812     converting_handler& operator=( converting_handler const& ) = delete;
0813 
0814     converting_handler( T* v, P* p )
0815         : value_(v) , parent_(p) , handlers_(tuple_accessor(), v, this)
0816     {}
0817 
0818     void signal_value()
0819     {
0820         ++inner_active_;
0821     }
0822 
0823     bool signal_end(system::error_code& ec)
0824     {
0825         constexpr int N = std::tuple_size<T>::value;
0826         if( inner_active_ < N )
0827         {
0828             BOOST_JSON_FAIL( ec, error::size_mismatch );
0829             return true;
0830         }
0831 
0832         inner_active_ = -1;
0833         parent_->signal_value();
0834         return true;
0835     }
0836 
0837 #define BOOST_JSON_HANDLE_EVENT(fn) \
0838     struct do_ ## fn \
0839     { \
0840         template< class H, class... Args > \
0841         bool operator()( H& h, Args& ... args ) const \
0842         { \
0843             return h. fn (args...); \
0844         } \
0845     }; \
0846        \
0847     template< class... Args > \
0848     bool fn( system::error_code& ec, Args&& ... args ) \
0849     { \
0850         if( inner_active_ < 0 ) \
0851         { \
0852             BOOST_JSON_FAIL( ec, error::not_array ); \
0853             return false; \
0854         } \
0855         constexpr int N = std::tuple_size<T>::value; \
0856         if( inner_active_ >= N ) \
0857         { \
0858             BOOST_JSON_FAIL( ec, error::size_mismatch ); \
0859             return false; \
0860         } \
0861         using F = handler_op_invoker< do_ ## fn, system::error_code, Args...>; \
0862         using H = decltype(handlers_); \
0863         return mp11::mp_with_index<N>( \
0864             inner_active_, \
0865             tuple_handler_op_invoker<H, F>{ \
0866                 handlers_, \
0867                 F{ std::forward_as_tuple(ec, args...) } } ); \
0868     }
0869 
0870     BOOST_JSON_HANDLE_EVENT( on_object_begin )
0871     BOOST_JSON_HANDLE_EVENT( on_object_end )
0872 
0873     struct do_on_array_begin
0874     {
0875         handler_tuple< converting_handler, tuple_element_list<T> >& handlers;
0876         system::error_code& ec;
0877 
0878         template< class I >
0879         bool operator()( I ) const
0880         {
0881             return get<I::value>(handlers).on_array_begin(ec);
0882         }
0883     };
0884     bool on_array_begin( system::error_code& ec )
0885     {
0886         if( inner_active_ < 0 )
0887         {
0888             inner_active_ = 0;
0889             return true;
0890         }
0891 
0892         constexpr int N = std::tuple_size<T>::value;
0893 
0894         if( inner_active_ >= N )
0895         {
0896             BOOST_JSON_FAIL( ec, error::size_mismatch );
0897             return false;
0898         }
0899 
0900         return mp11::mp_with_index<N>(
0901             inner_active_, do_on_array_begin{handlers_, ec} );
0902     }
0903 
0904     struct do_on_array_end
0905     {
0906         handler_tuple< converting_handler, tuple_element_list<T> >& handlers;
0907         system::error_code& ec;
0908 
0909         template< class I >
0910         bool operator()( I ) const
0911         {
0912             return get<I::value>(handlers).on_array_end(ec);
0913         }
0914     };
0915     bool on_array_end( system::error_code& ec )
0916     {
0917         if( inner_active_ < 0 )
0918             return parent_->signal_end(ec);
0919 
0920         constexpr int N = std::tuple_size<T>::value;
0921 
0922         if( inner_active_ >= N )
0923             return signal_end(ec);
0924 
0925         return mp11::mp_with_index<N>(
0926             inner_active_, do_on_array_end{handlers_, ec} );
0927     }
0928 
0929     BOOST_JSON_HANDLE_EVENT( on_key_part )
0930     BOOST_JSON_HANDLE_EVENT( on_key )
0931     BOOST_JSON_HANDLE_EVENT( on_string_part )
0932     BOOST_JSON_HANDLE_EVENT( on_string )
0933     BOOST_JSON_HANDLE_EVENT( on_number_part )
0934     BOOST_JSON_HANDLE_EVENT( on_int64 )
0935     BOOST_JSON_HANDLE_EVENT( on_uint64 )
0936     BOOST_JSON_HANDLE_EVENT( on_double )
0937     BOOST_JSON_HANDLE_EVENT( on_bool )
0938     BOOST_JSON_HANDLE_EVENT( on_null )
0939 
0940 #undef BOOST_JSON_HANDLE_EVENT
0941 };
0942 
0943 // described struct handler
0944 #if defined(BOOST_MSVC) && BOOST_MSVC < 1910
0945 
0946 template< class T >
0947 struct struct_element_list_impl
0948 {
0949     template< class D >
0950     using helper = described_member_t<T, D>;
0951 
0952     using type = mp11::mp_transform< helper, described_members<T> >;
0953 };
0954 template< class T >
0955 using struct_element_list = typename struct_element_list_impl<T>::type;
0956 
0957 #else
0958 
0959 template< class T >
0960 using struct_element_list = mp11::mp_transform_q<
0961     mp11::mp_bind_front< described_member_t, T >, described_members<T> >;
0962 
0963 #endif
0964 
0965 struct struct_accessor
0966 {
0967     template< class T, class I >
0968     auto operator()( T* t, I ) const
0969         -> described_member_t<T, mp11::mp_at< described_members<T>, I> >*
0970     {
0971         using Ds = described_members<T>;
0972         using D = mp11::mp_at<Ds, I>;
0973         return &(t->*D::pointer);
0974     }
0975 };
0976 
0977 template< class F >
0978 struct struct_key_searcher
0979 {
0980     F fn;
0981 
0982     template< class D >
0983     void
0984     operator()( D ) const
0985     {
0986         fn( D::name ) ;
0987     }
0988 };
0989 
0990 template<class V, class P>
0991 class converting_handler<described_class_conversion_tag, V, P>
0992 {
0993 #if !defined(BOOST_DESCRIBE_CXX14)
0994 
0995     static_assert(
0996         sizeof(V) == 0, "Struct support for parse_into requires C++14" );
0997 
0998 #else
0999 
1000 private:
1001     V* value_;
1002     P* parent_;
1003 
1004     std::string key_;
1005 
1006     using Dm = described_members<V>;
1007 
1008     handler_tuple< converting_handler, struct_element_list<V> > handlers_;
1009     int inner_active_ = -1;
1010     std::size_t activated_ = 0;
1011 
1012 public:
1013     converting_handler( converting_handler const& ) = delete;
1014     converting_handler& operator=( converting_handler const& ) = delete;
1015 
1016     converting_handler( V* v, P* p )
1017         : value_(v), parent_(p), handlers_(struct_accessor(), v, this)
1018     {}
1019 
1020     struct is_optional_checker
1021     {
1022         template< class I >
1023         bool operator()( I ) const noexcept
1024         {
1025             using L = struct_element_list<V>;
1026             using T = mp11::mp_at<L, I>;
1027             return !is_optional_like<T>::value;
1028         }
1029     };
1030     void signal_value()
1031     {
1032         BOOST_ASSERT( inner_active_ >= 0 );
1033         bool required_member = mp11::mp_with_index< mp11::mp_size<Dm> >(
1034             inner_active_,
1035             is_optional_checker{});
1036         if( required_member )
1037             ++activated_;
1038 
1039         key_ = {};
1040         inner_active_ = -1;
1041     }
1042 
1043     bool signal_end(system::error_code&)
1044     {
1045         key_ = {};
1046         inner_active_ = -1;
1047         parent_->signal_value();
1048         return true;
1049     }
1050 
1051 #define BOOST_JSON_INVOKE_INNER(fn) \
1052     if( inner_active_ < 0 ) \
1053     { \
1054         BOOST_JSON_FAIL( ec, error::not_object ); \
1055         return false; \
1056     } \
1057     auto f = [&](auto& handler) { return handler.fn ; }; \
1058     using F = decltype(f); \
1059     using H = decltype(handlers_); \
1060     return mp11::mp_with_index< mp11::mp_size<Dm> >( \
1061             inner_active_, \
1062             tuple_handler_op_invoker<H, F>{handlers_, f} );
1063 
1064     bool on_object_begin( system::error_code& ec )
1065     {
1066         if( inner_active_ < 0 )
1067             return true;
1068 
1069         BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1070     }
1071 
1072     bool on_object_end( system::error_code& ec )
1073     {
1074         if( inner_active_ < 0 )
1075         {
1076             using L = struct_element_list<V>;
1077             using C = mp11::mp_count_if<L, is_optional_like>;
1078             constexpr int N = mp11::mp_size<L>::value - C::value;
1079             if( activated_ < N )
1080             {
1081                 BOOST_JSON_FAIL( ec, error::size_mismatch );
1082                 return false;
1083             }
1084 
1085             parent_->signal_value();
1086             return true;
1087         }
1088 
1089         BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1090     }
1091 
1092     bool on_array_begin( system::error_code& ec )
1093     {
1094         BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1095     }
1096 
1097     bool on_array_end( system::error_code& ec )
1098     {
1099         if( inner_active_ < 0 )
1100             return parent_->signal_end(ec);
1101 
1102         BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1103     }
1104 
1105     bool on_key_part( system::error_code& ec, string_view sv )
1106     {
1107         if( inner_active_ < 0 )
1108         {
1109             key_.append( sv.data(), sv.size() );
1110             return true;
1111         }
1112 
1113         BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1114     }
1115 
1116     bool on_key( system::error_code& ec, string_view sv )
1117     {
1118         if( inner_active_ >= 0 )
1119         {
1120             BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1121         }
1122 
1123         string_view key = sv;
1124         if( !key_.empty() )
1125         {
1126             key_.append( sv.data(), sv.size() );
1127             key = key_;
1128         }
1129 
1130         int i = 0;
1131 
1132         auto f = [&](char const* name)
1133         {
1134             if( key == name )
1135                 inner_active_ = i;
1136             ++i;
1137         };
1138 
1139         mp11::mp_for_each<Dm>(
1140             struct_key_searcher<decltype(f)>{f} );
1141 
1142         if( inner_active_ < 0 )
1143         {
1144             BOOST_JSON_FAIL(ec, error::unknown_name);
1145             return false;
1146         }
1147 
1148         return true;
1149     }
1150 
1151     bool on_string_part( system::error_code& ec, string_view sv )
1152     {
1153         BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1154     }
1155 
1156     bool on_string( system::error_code& ec, string_view sv )
1157     {
1158         BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1159     }
1160 
1161     bool on_number_part( system::error_code& ec )
1162     {
1163         BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1164     }
1165 
1166     bool on_int64( system::error_code& ec, std::int64_t v )
1167     {
1168         BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1169     }
1170 
1171     bool on_uint64( system::error_code& ec, std::uint64_t v )
1172     {
1173         BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1174     }
1175 
1176     bool on_double( system::error_code& ec, double v )
1177     {
1178         BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1179     }
1180 
1181     bool on_bool( system::error_code& ec, bool v )
1182     {
1183         BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1184     }
1185 
1186     bool on_null( system::error_code& ec )
1187     {
1188         BOOST_JSON_INVOKE_INNER( on_null(ec) );
1189     }
1190 
1191 #undef BOOST_JSON_INVOKE_INNER
1192 
1193 #endif
1194 };
1195 
1196 // variant handler
1197 struct object_begin_handler_event
1198 { };
1199 
1200 struct object_end_handler_event
1201 { };
1202 
1203 struct array_begin_handler_event
1204 { };
1205 
1206 struct array_end_handler_event
1207 { };
1208 
1209 struct key_handler_event
1210 {
1211     std::string value;
1212 };
1213 
1214 struct string_handler_event
1215 {
1216     std::string value;
1217 };
1218 
1219 struct int64_handler_event
1220 {
1221     std::int64_t value;
1222 };
1223 
1224 struct uint64_handler_event
1225 {
1226     std::uint64_t value;
1227 };
1228 
1229 struct double_handler_event
1230 {
1231     double value;
1232 };
1233 
1234 struct bool_handler_event
1235 {
1236     bool value;
1237 };
1238 
1239 struct null_handler_event
1240 { };
1241 
1242 using parse_event = variant2::variant<
1243     object_begin_handler_event,
1244     object_end_handler_event,
1245     array_begin_handler_event,
1246     array_end_handler_event,
1247     key_handler_event,
1248     string_handler_event,
1249     int64_handler_event,
1250     uint64_handler_event,
1251     double_handler_event,
1252     bool_handler_event,
1253     null_handler_event>;
1254 
1255 template< class H >
1256 struct event_visitor
1257 {
1258     H& handler;
1259     system::error_code& ec;
1260 
1261     bool
1262     operator()(object_begin_handler_event&) const
1263     {
1264         return handler.on_object_begin(ec);
1265     }
1266 
1267     bool
1268     operator()(object_end_handler_event&) const
1269     {
1270         return handler.on_object_end(ec);
1271     }
1272 
1273     bool
1274     operator()(array_begin_handler_event&) const
1275     {
1276         return handler.on_array_begin(ec);
1277     }
1278 
1279     bool
1280     operator()(array_end_handler_event&) const
1281     {
1282         return handler.on_array_end(ec);
1283     }
1284 
1285     bool
1286     operator()(key_handler_event& ev) const
1287     {
1288         return handler.on_key(ec, ev.value);
1289     }
1290 
1291     bool
1292     operator()(string_handler_event& ev) const
1293     {
1294         return handler.on_string(ec, ev.value);
1295     }
1296 
1297     bool
1298     operator()(int64_handler_event& ev) const
1299     {
1300         return handler.on_int64(ec, ev.value);
1301     }
1302 
1303     bool
1304     operator()(uint64_handler_event& ev) const
1305     {
1306         return handler.on_uint64(ec, ev.value);
1307     }
1308 
1309     bool
1310     operator()(double_handler_event& ev) const
1311     {
1312         return handler.on_double(ec, ev.value);
1313     }
1314 
1315     bool
1316     operator()(bool_handler_event& ev) const
1317     {
1318         return handler.on_bool(ec, ev.value);
1319     }
1320 
1321     bool
1322     operator()(null_handler_event&) const
1323     {
1324         return handler.on_null(ec);
1325     }
1326 };
1327 
1328 // L<T...> -> variant< monostate, get_handler<T, P>... >
1329 template< class P, class L >
1330 using inner_handler_variant = mp11::mp_push_front<
1331     mp11::mp_transform_q<
1332         mp11::mp_bind_back<get_handler, P>,
1333         mp11::mp_apply<variant2::variant, L>>,
1334     variant2::monostate>;
1335 
1336 template< class T, class P >
1337 class converting_handler<variant_conversion_tag, T, P>
1338 {
1339 private:
1340     using variant_size = mp11::mp_size<T>;
1341 
1342     T* value_;
1343     P* parent_;
1344 
1345     std::string string_;
1346     std::vector< parse_event > events_;
1347     inner_handler_variant<converting_handler, T> inner_;
1348     int inner_active_ = -1;
1349 
1350 public:
1351     converting_handler( converting_handler const& ) = delete;
1352     converting_handler& operator=( converting_handler const& ) = delete;
1353 
1354     converting_handler( T* v, P* p )
1355         : value_( v )
1356         , parent_( p )
1357     {}
1358 
1359     void signal_value()
1360     {
1361         inner_.template emplace<0>();
1362         inner_active_ = -1;
1363         events_.clear();
1364         parent_->signal_value();
1365     }
1366 
1367     bool signal_end(system::error_code& ec)
1368     {
1369         return parent_->signal_end(ec);
1370     }
1371 
1372     struct alternative_selector
1373     {
1374         converting_handler* self;
1375 
1376         template< class I >
1377         void
1378         operator()( I ) const
1379         {
1380             using V = mp11::mp_at<T, I>;
1381             auto& v = self->value_->template emplace<I::value>( V{} );
1382             self->inner_.template emplace<I::value + 1>(&v, self);
1383         }
1384     };
1385     void
1386     next_alternative()
1387     {
1388         if( ++inner_active_ >= static_cast<int>(variant_size::value) )
1389             return;
1390 
1391         mp11::mp_with_index< variant_size::value >(
1392             inner_active_, alternative_selector{this} );
1393     }
1394 
1395     struct event_processor
1396     {
1397         converting_handler* self;
1398         system::error_code& ec;
1399         parse_event& event;
1400 
1401         template< class I >
1402         bool operator()( I ) const
1403         {
1404             auto& handler = variant2::get<I::value + 1>(self->inner_);
1405             using Handler = remove_cvref<decltype(handler)>;
1406             return variant2::visit(
1407                 event_visitor<Handler>{handler, ec}, event );
1408         }
1409     };
1410     bool process_events(system::error_code& ec)
1411     {
1412         constexpr std::size_t N = variant_size::value;
1413 
1414         // should be pointers not iterators, otherwise MSVC crashes
1415         auto const last = events_.data() + events_.size();
1416         auto first = last - 1;
1417         bool ok = false;
1418 
1419         if( inner_active_ < 0 )
1420             next_alternative();
1421         do
1422         {
1423             if( static_cast<std::size_t>(inner_active_) >= N )
1424             {
1425                 BOOST_JSON_FAIL( ec, error::exhausted_variants );
1426                 return false;
1427             }
1428 
1429             for ( ; first != last; ++first )
1430             {
1431                 ok = mp11::mp_with_index< N >(
1432                     inner_active_, event_processor{this, ec, *first} );
1433                 if( !ok )
1434                 {
1435                     first = events_.data();
1436                     next_alternative();
1437                     ec.clear();
1438                     break;
1439                 }
1440             }
1441         }
1442         while( !ok );
1443 
1444         return true;
1445     }
1446 
1447 #define BOOST_JSON_INVOKE_INNER(ev, ec) \
1448     events_.emplace_back( ev ); \
1449     return process_events(ec);
1450 
1451     bool on_object_begin( system::error_code& ec )
1452     {
1453         BOOST_JSON_INVOKE_INNER( object_begin_handler_event{}, ec );
1454     }
1455 
1456     bool on_object_end( system::error_code& ec )
1457     {
1458         BOOST_JSON_INVOKE_INNER( object_end_handler_event{}, ec );
1459     }
1460 
1461     bool on_array_begin( system::error_code& ec )
1462     {
1463         BOOST_JSON_INVOKE_INNER( array_begin_handler_event{}, ec );
1464     }
1465 
1466     bool on_array_end( system::error_code& ec )
1467     {
1468         if( !inner_active_ )
1469             return signal_end(ec);
1470 
1471         BOOST_JSON_INVOKE_INNER( array_end_handler_event{}, ec );
1472     }
1473 
1474     bool on_key_part( system::error_code&, string_view sv )
1475     {
1476         string_.append(sv);
1477         return true;
1478     }
1479 
1480     bool on_key( system::error_code& ec, string_view sv )
1481     {
1482         string_.append(sv);
1483         BOOST_JSON_INVOKE_INNER( key_handler_event{ std::move(string_) }, ec );
1484     }
1485 
1486     bool on_string_part( system::error_code&, string_view sv )
1487     {
1488         string_.append(sv);
1489         return true;
1490     }
1491 
1492     bool on_string( system::error_code& ec, string_view sv )
1493     {
1494         string_.append(sv);
1495         BOOST_JSON_INVOKE_INNER(
1496             string_handler_event{ std::move(string_) }, ec );
1497     }
1498 
1499     bool on_number_part( system::error_code& )
1500     {
1501         return true;
1502     }
1503 
1504     bool on_int64( system::error_code& ec, std::int64_t v )
1505     {
1506         BOOST_JSON_INVOKE_INNER( int64_handler_event{v}, ec );
1507     }
1508 
1509     bool on_uint64( system::error_code& ec, std::uint64_t v )
1510     {
1511         BOOST_JSON_INVOKE_INNER( uint64_handler_event{v}, ec );
1512     }
1513 
1514     bool on_double( system::error_code& ec, double v )
1515     {
1516         BOOST_JSON_INVOKE_INNER( double_handler_event{v}, ec );
1517     }
1518 
1519     bool on_bool( system::error_code& ec, bool v )
1520     {
1521         BOOST_JSON_INVOKE_INNER( bool_handler_event{v}, ec );
1522     }
1523 
1524     bool on_null( system::error_code& ec )
1525     {
1526         BOOST_JSON_INVOKE_INNER( null_handler_event{}, ec );
1527     }
1528 
1529 #undef BOOST_JSON_INVOKE_INNER
1530 };
1531 
1532 // optional handler
1533 template<class V, class P>
1534 class converting_handler<optional_conversion_tag, V, P>
1535 {
1536 private:
1537     using inner_type = value_result_type<V>;
1538     using inner_handler_type = get_handler<inner_type, converting_handler>;
1539 
1540     V* value_;
1541     P* parent_;
1542 
1543     inner_type inner_value_ = {};
1544     inner_handler_type inner_;
1545     bool inner_active_ = false;
1546 
1547 public:
1548     converting_handler( converting_handler const& ) = delete;
1549     converting_handler& operator=( converting_handler const& ) = delete;
1550 
1551     converting_handler( V* v, P* p )
1552         : value_(v), parent_(p), inner_(&inner_value_, this)
1553     {}
1554 
1555     void signal_value()
1556     {
1557         *value_ = std::move(inner_value_);
1558 
1559         inner_active_ = false;
1560         parent_->signal_value();
1561     }
1562 
1563     bool signal_end(system::error_code& ec)
1564     {
1565         return parent_->signal_end(ec);
1566     }
1567 
1568 #define BOOST_JSON_INVOKE_INNER(fn) \
1569     if( !inner_active_ ) \
1570         inner_active_ = true; \
1571     return inner_.fn;
1572 
1573     bool on_object_begin( system::error_code& ec )
1574     {
1575         BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1576     }
1577 
1578     bool on_object_end( system::error_code& ec )
1579     {
1580         BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1581     }
1582 
1583     bool on_array_begin( system::error_code& ec )
1584     {
1585         BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1586     }
1587 
1588     bool on_array_end( system::error_code& ec )
1589     {
1590         if( !inner_active_ )
1591             return signal_end(ec);
1592 
1593         BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1594     }
1595 
1596     bool on_key_part( system::error_code& ec, string_view sv )
1597     {
1598         BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1599     }
1600 
1601     bool on_key( system::error_code& ec, string_view sv )
1602     {
1603         BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1604     }
1605 
1606     bool on_string_part( system::error_code& ec, string_view sv )
1607     {
1608         BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1609     }
1610 
1611     bool on_string( system::error_code& ec, string_view sv )
1612     {
1613         BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1614     }
1615 
1616     bool on_number_part( system::error_code& ec )
1617     {
1618         BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1619     }
1620 
1621     bool on_int64( system::error_code& ec, std::int64_t v )
1622     {
1623         BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1624     }
1625 
1626     bool on_uint64( system::error_code& ec, std::uint64_t v )
1627     {
1628         BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1629     }
1630 
1631     bool on_double( system::error_code& ec, double v )
1632     {
1633         BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1634     }
1635 
1636     bool on_bool( system::error_code& ec, bool v )
1637     {
1638         BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1639     }
1640 
1641     bool on_null( system::error_code& ec )
1642     {
1643         if( !inner_active_ )
1644         {
1645             *value_ = {};
1646 
1647             this->parent_->signal_value();
1648             return true;
1649         }
1650         else
1651         {
1652             return inner_.on_null(ec);
1653         }
1654     }
1655 
1656 #undef BOOST_JSON_INVOKE_INNER
1657 };
1658 
1659 // path handler
1660 template< class V, class P >
1661 class converting_handler<path_conversion_tag, V, P>
1662     : public scalar_handler<P, error::not_string>
1663 {
1664 private:
1665     V* value_;
1666     bool cleared_ = false;
1667 
1668 public:
1669     converting_handler( V* v, P* p )
1670         : converting_handler::scalar_handler(p)
1671         , value_(v)
1672     {}
1673 
1674     bool on_string_part( system::error_code&, string_view sv )
1675     {
1676         if( !cleared_ )
1677         {
1678             cleared_ = true;
1679             value_->clear();
1680         }
1681 
1682         value_->concat( sv.begin(), sv.end() );
1683         return true;
1684     }
1685 
1686     bool on_string( system::error_code&, string_view sv )
1687     {
1688         if( !cleared_ )
1689             value_->clear();
1690         else
1691             cleared_ = false;
1692 
1693         value_->concat( sv.begin(), sv.end() );
1694 
1695         this->parent_->signal_value();
1696         return true;
1697     }
1698 };
1699 
1700 // into_handler
1701 template< class V >
1702 class into_handler
1703 {
1704 private:
1705 
1706     using inner_handler_type = get_handler<V, into_handler>;
1707 
1708     inner_handler_type inner_;
1709     bool inner_active_ = true;
1710 
1711 public:
1712 
1713     into_handler( into_handler const& ) = delete;
1714     into_handler& operator=( into_handler const& ) = delete;
1715 
1716 public:
1717 
1718     static constexpr std::size_t max_object_size = object::max_size();
1719     static constexpr std::size_t max_array_size = array::max_size();
1720     static constexpr std::size_t max_key_size = string::max_size();
1721     static constexpr std::size_t max_string_size = string::max_size();
1722 
1723 public:
1724 
1725     explicit into_handler( V* v ): inner_( v, this )
1726     {
1727     }
1728 
1729     void signal_value()
1730     {
1731     }
1732 
1733     bool signal_end(system::error_code&)
1734     {
1735         return true;
1736     }
1737 
1738     bool on_document_begin( system::error_code& )
1739     {
1740         return true;
1741     }
1742 
1743     bool on_document_end( system::error_code& )
1744     {
1745         inner_active_ = false;
1746         return true;
1747     }
1748 
1749 #define BOOST_JSON_INVOKE_INNER(f) \
1750     if( !inner_active_ ) \
1751     { \
1752         BOOST_JSON_FAIL( ec, error::extra_data ); \
1753         return false; \
1754     } \
1755     else \
1756         return inner_.f
1757 
1758     bool on_object_begin( system::error_code& ec )
1759     {
1760         BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1761     }
1762 
1763     bool on_object_end( std::size_t, system::error_code& ec )
1764     {
1765         BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1766     }
1767 
1768     bool on_array_begin( system::error_code& ec )
1769     {
1770         BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1771     }
1772 
1773     bool on_array_end( std::size_t, system::error_code& ec )
1774     {
1775         BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1776     }
1777 
1778     bool on_key_part( string_view sv, std::size_t, system::error_code& ec )
1779     {
1780         BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1781     }
1782 
1783     bool on_key( string_view sv, std::size_t, system::error_code& ec )
1784     {
1785         BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1786     }
1787 
1788     bool on_string_part( string_view sv, std::size_t, system::error_code& ec )
1789     {
1790         BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1791     }
1792 
1793     bool on_string( string_view sv, std::size_t, system::error_code& ec )
1794     {
1795         BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1796     }
1797 
1798     bool on_number_part( string_view, system::error_code& ec )
1799     {
1800         BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1801     }
1802 
1803     bool on_int64( std::int64_t v, string_view, system::error_code& ec )
1804     {
1805         BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1806     }
1807 
1808     bool on_uint64( std::uint64_t v, string_view, system::error_code& ec )
1809     {
1810         BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1811     }
1812 
1813     bool on_double( double v, string_view, system::error_code& ec )
1814     {
1815         BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1816     }
1817 
1818     bool on_bool( bool v, system::error_code& ec )
1819     {
1820         BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1821     }
1822 
1823     bool on_null( system::error_code& ec )
1824     {
1825         BOOST_JSON_INVOKE_INNER( on_null(ec) );
1826     }
1827 
1828     bool on_comment_part(string_view, system::error_code&)
1829     {
1830         return true;
1831     }
1832 
1833     bool on_comment(string_view, system::error_code&)
1834     {
1835         return true;
1836     }
1837 
1838 #undef BOOST_JSON_INVOKE_INNER
1839 };
1840 
1841 } // namespace detail
1842 } // namespace boost
1843 } // namespace json
1844 
1845 #endif