Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-11-01 08:21:20

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& ec)
0162     {
0163         inner_active_ = false;
0164         return parent_->signal_value(ec);
0165     }
0166 
0167 #define BOOST_JSON_INVOKE_INNER(f) \
0168     if( !inner_active_ ) { \
0169         BOOST_JSON_FAIL(ec, E); \
0170         return false; \
0171     } \
0172     else \
0173         return inner_.f
0174 
0175     bool on_object_begin( system::error_code& ec )
0176     {
0177         BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
0178     }
0179 
0180     bool on_object_end( system::error_code& ec )
0181     {
0182         BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
0183     }
0184 
0185     bool on_array_begin( system::error_code& ec )
0186     {
0187         BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
0188     }
0189 
0190     bool on_array_end( system::error_code& ec )
0191     {
0192         BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
0193     }
0194 
0195     bool on_key_part( system::error_code& ec, string_view sv )
0196     {
0197         BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
0198     }
0199 
0200     bool on_key( system::error_code& ec, string_view sv )
0201     {
0202         BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
0203     }
0204 
0205     bool on_string_part( system::error_code& ec, string_view sv )
0206     {
0207         BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
0208     }
0209 
0210     bool on_string( system::error_code& ec, string_view sv )
0211     {
0212         BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
0213     }
0214 
0215     bool on_number_part( system::error_code& ec )
0216     {
0217         BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
0218     }
0219 
0220     bool on_int64( system::error_code& ec, std::int64_t v )
0221     {
0222         BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
0223     }
0224 
0225     bool on_uint64( system::error_code& ec, std::uint64_t v )
0226     {
0227         BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
0228     }
0229 
0230     bool on_double( system::error_code& ec, double v )
0231     {
0232         BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
0233     }
0234 
0235     bool on_bool( system::error_code& ec, bool v )
0236     {
0237         BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
0238     }
0239 
0240     bool on_null( system::error_code& ec )
0241     {
0242         BOOST_JSON_INVOKE_INNER( on_null(ec) );
0243     }
0244 
0245 #undef BOOST_JSON_INVOKE_INNER
0246 };
0247 
0248 // integral handler
0249 template<class V,
0250 typename std::enable_if<std::is_signed<V>::value, int>::type = 0>
0251 bool integral_in_range( std::int64_t v )
0252 {
0253     return v >= (std::numeric_limits<V>::min)() && v <= (std::numeric_limits<V>::max)();
0254 }
0255 
0256 template<class V,
0257 typename std::enable_if<!std::is_signed<V>::value, int>::type = 0>
0258 bool integral_in_range( std::int64_t v )
0259 {
0260     return v >= 0 && static_cast<std::uint64_t>( v ) <= (std::numeric_limits<V>::max)();
0261 }
0262 
0263 template<class V>
0264 bool integral_in_range( std::uint64_t v )
0265 {
0266     return v <= static_cast<typename std::make_unsigned<V>::type>( (std::numeric_limits<V>::max)() );
0267 }
0268 
0269 template< class V, class P >
0270 class converting_handler<integral_conversion_tag, V, P>
0271     : public scalar_handler<P, error::not_integer>
0272 {
0273 private:
0274     V* value_;
0275 
0276 public:
0277     converting_handler( V* v, P* p )
0278         : converting_handler::scalar_handler(p)
0279         , value_(v)
0280     {}
0281 
0282     bool on_number_part( system::error_code& )
0283     {
0284         return true;
0285     }
0286 
0287     bool on_int64(system::error_code& ec, std::int64_t v)
0288     {
0289         if( !integral_in_range<V>( v ) )
0290         {
0291             BOOST_JSON_FAIL( ec, error::not_exact );
0292             return false;
0293         }
0294 
0295         *value_ = static_cast<V>( v );
0296         return this->parent_->signal_value(ec);
0297     }
0298 
0299     bool on_uint64(system::error_code& ec, std::uint64_t v)
0300     {
0301         if( !integral_in_range<V>(v) )
0302         {
0303             BOOST_JSON_FAIL( ec, error::not_exact );
0304             return false;
0305         }
0306 
0307         *value_ = static_cast<V>(v);
0308         return this->parent_->signal_value(ec);
0309     }
0310 };
0311 
0312 // floating point handler
0313 template< class V, class P>
0314 class converting_handler<floating_point_conversion_tag, V, P>
0315     : public scalar_handler<P, error::not_double>
0316 {
0317 private:
0318     V* value_;
0319 
0320 public:
0321     converting_handler( V* v, P* p )
0322         : converting_handler::scalar_handler(p)
0323         , value_(v)
0324     {}
0325 
0326     bool on_number_part( system::error_code& )
0327     {
0328         return true;
0329     }
0330 
0331     bool on_int64(system::error_code& ec, std::int64_t v)
0332     {
0333         *value_ = static_cast<V>(v);
0334         return this->parent_->signal_value(ec);
0335     }
0336 
0337     bool on_uint64(system::error_code& ec, std::uint64_t v)
0338     {
0339         *value_ = static_cast<V>(v);
0340         return this->parent_->signal_value(ec);
0341     }
0342 
0343     bool on_double(system::error_code& ec, double v)
0344     {
0345         *value_ = static_cast<V>(v);
0346         return this->parent_->signal_value(ec);
0347     }
0348 };
0349 
0350 // string handler
0351 template< class V, class P >
0352 class converting_handler<string_like_conversion_tag, V, P>
0353     : public scalar_handler<P, error::not_string>
0354 {
0355 private:
0356     V* value_;
0357     bool cleared_ = false;
0358 
0359 public:
0360     converting_handler( V* v, P* p )
0361         : converting_handler::scalar_handler(p)
0362         , value_(v)
0363     {}
0364 
0365     bool on_string_part( system::error_code&, string_view sv )
0366     {
0367         if( !cleared_ )
0368         {
0369             cleared_ = true;
0370             value_->clear();
0371         }
0372 
0373         value_->append( sv.begin(), sv.end() );
0374         return true;
0375     }
0376 
0377     bool on_string(system::error_code& ec, string_view sv)
0378     {
0379         if( !cleared_ )
0380             value_->clear();
0381         else
0382             cleared_ = false;
0383 
0384         value_->append( sv.begin(), sv.end() );
0385         return this->parent_->signal_value(ec);
0386     }
0387 };
0388 
0389 // bool handler
0390 template< class V, class P >
0391 class converting_handler<bool_conversion_tag, V, P>
0392     : public scalar_handler<P, error::not_bool>
0393 {
0394 private:
0395     V* value_;
0396 
0397 public:
0398     converting_handler( V* v, P* p )
0399         : converting_handler::scalar_handler(p)
0400         , value_(v)
0401     {}
0402 
0403     bool on_bool(system::error_code& ec, bool v)
0404     {
0405         *value_ = v;
0406         return this->parent_->signal_value(ec);
0407     }
0408 };
0409 
0410 // null handler
0411 template< class V, class P >
0412 class converting_handler<null_like_conversion_tag, V, P>
0413     : public scalar_handler<P, error::not_null>
0414 {
0415 private:
0416     V* value_;
0417 
0418 public:
0419     converting_handler( V* v, P* p )
0420         : converting_handler::scalar_handler(p)
0421         , value_(v)
0422     {}
0423 
0424     bool on_null(system::error_code& ec)
0425     {
0426         *value_ = {};
0427         return this->parent_->signal_value(ec);
0428     }
0429 };
0430 
0431 // described enum handler
0432 template< class V, class P >
0433 class converting_handler<described_enum_conversion_tag, V, P>
0434     : public scalar_handler<P, error::not_string>
0435 {
0436 #ifndef BOOST_DESCRIBE_CXX14
0437 
0438     static_assert(
0439         sizeof(V) == 0, "Enum support for parse_into requires C++14" );
0440 
0441 #else
0442 
0443 private:
0444     V* value_;
0445     std::string name_;
0446 
0447 public:
0448     converting_handler( V* v, P* p )
0449         : converting_handler::scalar_handler(p)
0450         , value_(v)
0451     {}
0452 
0453     bool on_string_part( system::error_code&, string_view sv )
0454     {
0455         name_.append( sv.begin(), sv.end() );
0456         return true;
0457     }
0458 
0459     bool on_string(system::error_code& ec, string_view sv)
0460     {
0461         string_view name = sv;
0462         if( !name_.empty() )
0463         {
0464             name_.append( sv.begin(), sv.end() );
0465             name = name_;
0466         }
0467 
0468         if( !describe::enum_from_string(name, *value_) )
0469         {
0470             BOOST_JSON_FAIL(ec, error::unknown_name);
0471             return false;
0472         }
0473 
0474         return this->parent_->signal_value(ec);
0475     }
0476 
0477 #endif // BOOST_DESCRIBE_CXX14
0478 };
0479 
0480 template< class V, class P >
0481 class converting_handler<no_conversion_tag, V, P>
0482 {
0483     static_assert( sizeof(V) == 0, "This type is not supported" );
0484 };
0485 
0486 // sequence handler
0487 template< class It >
0488 bool cannot_insert(It i, It e)
0489 {
0490     return i == e;
0491 }
0492 
0493 template< class It1, class It2 >
0494 std::false_type cannot_insert(It1, It2)
0495 {
0496     return {};
0497 }
0498 
0499 template< class It >
0500 bool needs_more_elements(It i, It e)
0501 {
0502     return i != e;
0503 }
0504 
0505 template< class It1, class It2 >
0506 std::false_type needs_more_elements(It1, It2)
0507 {
0508     return {};
0509 }
0510 
0511 template<class T>
0512 void
0513 clear_container(
0514     T&,
0515     mp11::mp_int<2>)
0516 {
0517 }
0518 
0519 template<class T>
0520 void
0521 clear_container(
0522     T& target,
0523     mp11::mp_int<1>)
0524 {
0525     target.clear();
0526 }
0527 
0528 template<class T>
0529 void
0530 clear_container(
0531     T& target,
0532     mp11::mp_int<0>)
0533 {
0534     target.clear();
0535 }
0536 
0537 template< class V, class P >
0538 class converting_handler<sequence_conversion_tag, V, P>
0539     : public composite_handler<
0540         converting_handler<sequence_conversion_tag, V, P>,
0541         detail::value_type<V>,
0542         P,
0543         error::not_array>
0544 {
0545 private:
0546     V* value_;
0547 
0548     using Inserter = decltype(
0549         detail::inserter(*value_, inserter_implementation<V>()) );
0550     Inserter inserter;
0551 
0552 public:
0553     converting_handler( V* v, P* p )
0554         : converting_handler::composite_handler(p)
0555         , value_(v)
0556         , inserter( detail::inserter(*value_, inserter_implementation<V>()) )
0557     {}
0558 
0559     bool signal_value(system::error_code& ec)
0560     {
0561         if(cannot_insert( inserter, value_->end() ))
0562         {
0563             BOOST_JSON_FAIL( ec, error::size_mismatch );
0564             return false;
0565         }
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         return true;
0577     }
0578 
0579     bool signal_end(system::error_code& ec)
0580     {
0581         if(needs_more_elements( inserter, value_->end() ))
0582         {
0583             BOOST_JSON_FAIL( ec, error::size_mismatch );
0584             return false;
0585         }
0586 
0587         inserter = detail::inserter(*value_, inserter_implementation<V>());
0588 
0589         return converting_handler::composite_handler::signal_end(ec);
0590     }
0591 
0592     bool on_array_begin( system::error_code& ec )
0593     {
0594         if( this->inner_active_ )
0595             return this->inner_.on_array_begin( ec );
0596 
0597         this->inner_active_ = true;
0598         clear_container( *value_, inserter_implementation<V>() );
0599         return true;
0600     }
0601 
0602     bool on_array_end( system::error_code& ec )
0603     {
0604         if( this->inner_active_ )
0605             return this->inner_.on_array_end( ec );
0606 
0607         return this->parent_->signal_end(ec);
0608     }
0609 };
0610 
0611 // map handler
0612 template< class V, class P >
0613 class converting_handler<map_like_conversion_tag, V, P>
0614     : public composite_handler<
0615         converting_handler<map_like_conversion_tag, V, P>,
0616         detail::mapped_type<V>,
0617         P,
0618         error::not_object>
0619 {
0620 private:
0621     V* value_;
0622     std::string key_;
0623 
0624 public:
0625     converting_handler( V* v, P* p )
0626         : converting_handler::composite_handler(p), value_(v)
0627     {}
0628 
0629     bool signal_value(system::error_code&)
0630     {
0631         value_->emplace( std::move(key_), std::move(this->next_value_) );
0632 
0633         key_ = {};
0634         this->next_value_ = {};
0635 
0636         this->inner_active_ = false;
0637 
0638         return true;
0639     }
0640 
0641     bool on_object_begin( system::error_code& ec )
0642     {
0643         if( this->inner_active_ )
0644             return this->inner_.on_object_begin(ec);
0645 
0646         clear_container( *value_, inserter_implementation<V>() );
0647         return true;
0648     }
0649 
0650     bool on_object_end(system::error_code& ec)
0651     {
0652         if( this->inner_active_ )
0653             return this->inner_.on_object_end(ec);
0654 
0655         return this->parent_->signal_value(ec);
0656     }
0657 
0658     bool on_array_end( system::error_code& ec )
0659     {
0660         if( this->inner_active_ )
0661             return this->inner_.on_array_end(ec);
0662 
0663         return this->parent_->signal_end(ec);
0664     }
0665 
0666     bool on_key_part( system::error_code& ec, string_view sv )
0667     {
0668         if( this->inner_active_ )
0669             return this->inner_.on_key_part(ec, sv);
0670 
0671         key_.append( sv.data(), sv.size() );
0672         return true;
0673     }
0674 
0675     bool on_key( system::error_code& ec, string_view sv )
0676     {
0677         if( this->inner_active_ )
0678             return this->inner_.on_key(ec, sv);
0679 
0680         key_.append( sv.data(), sv.size() );
0681 
0682         this->inner_active_ = true;
0683         return true;
0684     }
0685 };
0686 
0687 // tuple handler
0688 template<std::size_t I, class T>
0689 struct handler_tuple_element
0690 {
0691     template< class... Args >
0692     handler_tuple_element( Args&& ... args )
0693         : t_( static_cast<Args&&>(args)... )
0694     {}
0695 
0696     T t_;
0697 };
0698 
0699 template<std::size_t I, class T>
0700 T&
0701 get( handler_tuple_element<I, T>& e )
0702 {
0703     return e.t_;
0704 }
0705 
0706 template<
0707     class P,
0708     class LV,
0709     class S = mp11::make_index_sequence<mp11::mp_size<LV>::value> >
0710 struct handler_tuple;
0711 
0712 template< class P, template<class...> class L, class... V, std::size_t... I >
0713 struct handler_tuple< P, L<V...>, mp11::index_sequence<I...> >
0714     : handler_tuple_element<I, V>
0715     ...
0716 {
0717     handler_tuple( handler_tuple const& ) = delete;
0718     handler_tuple& operator=( handler_tuple const& ) = delete;
0719 
0720     template< class Access, class T >
0721     handler_tuple( Access access, T* pv, P* pp )
0722         : handler_tuple_element<I, V>(
0723             access( pv, mp11::mp_size_t<I>() ),
0724             pp )
0725         ...
0726     {}
0727 };
0728 
0729 #if defined(BOOST_MSVC) && BOOST_MSVC < 1910
0730 
0731 template< class T >
0732 struct tuple_element_list_impl
0733 {
0734     template< class I >
0735     using tuple_element_helper = tuple_element_t<I::value, T>;
0736 
0737     using type = mp11::mp_transform<
0738         tuple_element_helper,
0739         mp11::mp_iota< std::tuple_size<T> > >;
0740 };
0741 template< class T >
0742 using tuple_element_list = typename tuple_element_list_impl<T>::type;
0743 
0744 #else
0745 
0746 template< class I, class T >
0747 using tuple_element_helper = tuple_element_t<I::value, T>;
0748 template< class T >
0749 using tuple_element_list = mp11::mp_transform_q<
0750     mp11::mp_bind_back< tuple_element_helper, T>,
0751     mp11::mp_iota< std::tuple_size<T> > >;
0752 
0753 #endif
0754 
0755 template< class Op, class... Args>
0756 struct handler_op_invoker
0757 {
0758 public:
0759     std::tuple<Args&...> args;
0760 
0761     template< class Handler >
0762     bool
0763     operator()( Handler& handler ) const
0764     {
0765         return (*this)( handler, mp11::index_sequence_for<Args...>() );
0766     }
0767 
0768 private:
0769     template< class Handler, std::size_t... I >
0770     bool
0771     operator()( Handler& handler, mp11::index_sequence<I...> ) const
0772     {
0773         return Op()( handler, std::get<I>(args)... );
0774     }
0775 };
0776 
0777 template< class Handlers, class F >
0778 struct tuple_handler_op_invoker
0779 {
0780     Handlers& handlers;
0781     F fn;
0782 
0783     template< class I >
0784     bool
0785     operator()( I ) const
0786     {
0787         return fn( get<I::value>(handlers) );
0788     }
0789 };
0790 
0791 struct tuple_accessor
0792 {
0793     template< class T, class I >
0794     auto operator()( T* t, I ) const -> tuple_element_t<I::value, T>*
0795     {
0796         using std::get;
0797         return &get<I::value>(*t);
0798     }
0799 };
0800 
0801 template< class T, class P >
0802 class converting_handler<tuple_conversion_tag, T, P>
0803 {
0804 
0805 private:
0806     using ElementTypes = tuple_element_list<T>;
0807 
0808     template<class V>
0809     using ElementHandler = get_handler<V, converting_handler>;
0810     using InnerHandlers = mp11::mp_transform<ElementHandler, ElementTypes>;
0811     using HandlerTuple = handler_tuple<converting_handler, InnerHandlers>;
0812 
0813     T* value_;
0814     P* parent_;
0815 
0816     HandlerTuple handlers_;
0817     int inner_active_ = -1;
0818 
0819 public:
0820     converting_handler( converting_handler const& ) = delete;
0821     converting_handler& operator=( converting_handler const& ) = delete;
0822 
0823     converting_handler( T* v, P* p )
0824         : value_(v) , parent_(p) , handlers_(tuple_accessor(), v, this)
0825     {}
0826 
0827     bool signal_value(system::error_code&)
0828     {
0829         ++inner_active_;
0830         return true;
0831     }
0832 
0833     bool signal_end(system::error_code& ec)
0834     {
0835         constexpr int N = std::tuple_size<T>::value;
0836         if( inner_active_ < N )
0837         {
0838             BOOST_JSON_FAIL( ec, error::size_mismatch );
0839             return false;
0840         }
0841 
0842         inner_active_ = -1;
0843         return parent_->signal_value(ec);
0844     }
0845 
0846 #define BOOST_JSON_HANDLE_EVENT(fn) \
0847     struct do_ ## fn \
0848     { \
0849         template< class H, class... Args > \
0850         bool operator()( H& h, Args& ... args ) const \
0851         { \
0852             return h. fn (args...); \
0853         } \
0854     }; \
0855        \
0856     template< class... Args > \
0857     bool fn( system::error_code& ec, Args&& ... args ) \
0858     { \
0859         if( inner_active_ < 0 ) \
0860         { \
0861             BOOST_JSON_FAIL( ec, error::not_array ); \
0862             return false; \
0863         } \
0864         constexpr int N = std::tuple_size<T>::value; \
0865         if( inner_active_ >= N ) \
0866         { \
0867             BOOST_JSON_FAIL( ec, error::size_mismatch ); \
0868             return false; \
0869         } \
0870         using F = handler_op_invoker< do_ ## fn, system::error_code, Args...>; \
0871         using H = decltype(handlers_); \
0872         return mp11::mp_with_index<N>( \
0873             inner_active_, \
0874             tuple_handler_op_invoker<H, F>{ \
0875                 handlers_, \
0876                 F{ std::forward_as_tuple(ec, args...) } } ); \
0877     }
0878 
0879     BOOST_JSON_HANDLE_EVENT( on_object_begin )
0880     BOOST_JSON_HANDLE_EVENT( on_object_end )
0881 
0882     struct do_on_array_begin
0883     {
0884         HandlerTuple& handlers;
0885         system::error_code& ec;
0886 
0887         template< class I >
0888         bool operator()( I ) const
0889         {
0890             return get<I::value>(handlers).on_array_begin(ec);
0891         }
0892     };
0893     bool on_array_begin( system::error_code& ec )
0894     {
0895         if( inner_active_ < 0 )
0896         {
0897             inner_active_ = 0;
0898             return true;
0899         }
0900 
0901         constexpr int N = std::tuple_size<T>::value;
0902 
0903         if( inner_active_ >= N )
0904         {
0905             BOOST_JSON_FAIL( ec, error::size_mismatch );
0906             return false;
0907         }
0908 
0909         return mp11::mp_with_index<N>(
0910             inner_active_, do_on_array_begin{handlers_, ec} );
0911     }
0912 
0913     struct do_on_array_end
0914     {
0915         HandlerTuple& handlers;
0916         system::error_code& ec;
0917 
0918         template< class I >
0919         bool operator()( I ) const
0920         {
0921             return get<I::value>(handlers).on_array_end(ec);
0922         }
0923     };
0924     bool on_array_end( system::error_code& ec )
0925     {
0926         if( inner_active_ < 0 )
0927             return parent_->signal_end(ec);
0928 
0929         constexpr int N = std::tuple_size<T>::value;
0930 
0931         if( inner_active_ >= N )
0932             return signal_end(ec);
0933 
0934         return mp11::mp_with_index<N>(
0935             inner_active_, do_on_array_end{handlers_, ec} );
0936     }
0937 
0938     BOOST_JSON_HANDLE_EVENT( on_key_part )
0939     BOOST_JSON_HANDLE_EVENT( on_key )
0940     BOOST_JSON_HANDLE_EVENT( on_string_part )
0941     BOOST_JSON_HANDLE_EVENT( on_string )
0942     BOOST_JSON_HANDLE_EVENT( on_number_part )
0943     BOOST_JSON_HANDLE_EVENT( on_int64 )
0944     BOOST_JSON_HANDLE_EVENT( on_uint64 )
0945     BOOST_JSON_HANDLE_EVENT( on_double )
0946     BOOST_JSON_HANDLE_EVENT( on_bool )
0947     BOOST_JSON_HANDLE_EVENT( on_null )
0948 
0949 #undef BOOST_JSON_HANDLE_EVENT
0950 };
0951 
0952 // described struct handler
0953 #if defined(BOOST_MSVC) && BOOST_MSVC < 1910
0954 
0955 template< class T >
0956 struct struct_element_list_impl
0957 {
0958     template< class D >
0959     using helper = described_member_t<T, D>;
0960 
0961     using type = mp11::mp_transform< helper, described_members<T> >;
0962 };
0963 template< class T >
0964 using struct_element_list = typename struct_element_list_impl<T>::type;
0965 
0966 #else
0967 
0968 template< class T >
0969 using struct_element_list = mp11::mp_transform_q<
0970     mp11::mp_bind_front< described_member_t, T >, described_members<T> >;
0971 
0972 #endif
0973 
0974 struct struct_accessor
0975 {
0976     template< class T >
0977     auto operator()( T*, mp11::mp_size< described_members<T> > ) const
0978         -> void*
0979     {
0980         return nullptr;
0981     }
0982 
0983     template< class T, class I >
0984     auto operator()( T* t, I ) const
0985         -> described_member_t<T, mp11::mp_at< described_members<T>, I> >*
0986     {
0987         using Ds = described_members<T>;
0988         using D = mp11::mp_at<Ds, I>;
0989         return &(t->*D::pointer);
0990     }
0991 };
0992 
0993 struct struct_key_searcher
0994 {
0995     string_view key;
0996     int& found;
0997     int index = 0;
0998 
0999     struct_key_searcher(string_view key, int& found) noexcept
1000         : key(key), found(found)
1001     {}
1002 
1003     template< class D >
1004     void
1005     operator()( D )
1006     {
1007         if( key == D::name )
1008             found = index;
1009         ++index;
1010     }
1011 };
1012 
1013 template<class P>
1014 struct ignoring_handler
1015 {
1016     P* parent_;
1017     std::size_t array_depth_ = 0;
1018     std::size_t object_depth_ = 0;
1019 
1020     ignoring_handler(ignoring_handler const&) = delete;
1021     ignoring_handler& operator=(ignoring_handler const&) = delete;
1022 
1023     ignoring_handler(void*, P* p) noexcept
1024         : parent_(p)
1025     {}
1026 
1027     bool on_object_begin(system::error_code&)
1028     {
1029         ++object_depth_;
1030         return true;
1031     }
1032 
1033     bool on_object_end(system::error_code& ec)
1034     {
1035         BOOST_ASSERT( object_depth_ > 0 );
1036         --object_depth_;
1037 
1038         if( (array_depth_ + object_depth_) == 0 )
1039             return parent_->signal_value(ec);
1040         return true;
1041     }
1042 
1043     bool on_array_begin(system::error_code&)
1044     {
1045         ++array_depth_;
1046         return true;
1047     }
1048 
1049     bool on_array_end(system::error_code& ec)
1050     {
1051         BOOST_ASSERT( array_depth_ > 0 );
1052         --array_depth_;
1053 
1054         if( (array_depth_ + object_depth_) == 0 )
1055             return parent_->signal_end(ec);
1056         return true;
1057     }
1058 
1059     bool on_key_part(system::error_code&, string_view)
1060     {
1061         return true;
1062     }
1063 
1064     bool on_key(system::error_code&, string_view)
1065     {
1066         return true;
1067     }
1068 
1069     bool on_string_part(system::error_code&, string_view)
1070     {
1071         return true;
1072     }
1073 
1074     bool on_string(system::error_code& ec, string_view)
1075     {
1076         if( (array_depth_ + object_depth_) == 0 )
1077             return parent_->signal_value(ec);
1078         return true;
1079     }
1080 
1081     bool on_number_part(system::error_code&)
1082     {
1083         return true;
1084     }
1085 
1086     bool on_int64(system::error_code& ec, std::int64_t)
1087     {
1088         if( (array_depth_ + object_depth_) == 0 )
1089             return parent_->signal_value(ec);
1090         return true;
1091     }
1092 
1093     bool on_uint64(system::error_code& ec, std::uint64_t)
1094     {
1095         if( (array_depth_ + object_depth_) == 0 )
1096             return parent_->signal_value(ec);
1097         return true;
1098     }
1099 
1100     bool on_double(system::error_code& ec, double)
1101     {
1102         if( (array_depth_ + object_depth_) == 0 )
1103             return parent_->signal_value(ec);
1104         return true;
1105     }
1106 
1107     bool on_bool(system::error_code& ec, bool)
1108     {
1109         if( (array_depth_ + object_depth_) == 0 )
1110             return parent_->signal_value(ec);
1111         return true;
1112     }
1113 
1114     bool on_null(system::error_code& ec)
1115     {
1116         if( (array_depth_ + object_depth_) == 0 )
1117             return parent_->signal_value(ec);
1118         return true;
1119     }
1120 };
1121 
1122 template<class V, class P>
1123 class converting_handler<described_class_conversion_tag, V, P>
1124 {
1125 #if !defined(BOOST_DESCRIBE_CXX14)
1126 
1127     static_assert(
1128         sizeof(V) == 0, "Struct support for parse_into requires C++14" );
1129 
1130 #else
1131 
1132 private:
1133     using Dm = described_members<V>;
1134     using Dt = struct_element_list<V>;
1135 
1136     template<class T>
1137     using MemberHandler = get_handler<T, converting_handler>;
1138     using InnerHandlers = mp11::mp_push_back<
1139         mp11::mp_transform<MemberHandler, Dt>,
1140         ignoring_handler<converting_handler> >;
1141     using InnerCount = mp11::mp_size<InnerHandlers>;
1142 
1143     V* value_;
1144     P* parent_;
1145 
1146     std::string key_;
1147 
1148     handler_tuple<converting_handler, InnerHandlers> handlers_;
1149     int inner_active_ = -1;
1150     std::size_t activated_ = 0;
1151 
1152 public:
1153     converting_handler( converting_handler const& ) = delete;
1154     converting_handler& operator=( converting_handler const& ) = delete;
1155 
1156     converting_handler( V* v, P* p )
1157         : value_(v), parent_(p), handlers_(struct_accessor(), v, this)
1158     {}
1159 
1160     struct is_required_checker
1161     {
1162         bool operator()( mp11::mp_size<Dt> ) const noexcept
1163         {
1164             return false;
1165         }
1166 
1167         template< class I >
1168         auto operator()( I ) const noexcept
1169         {
1170             using T = mp11::mp_at<Dt, I>;
1171             return !is_optional_like<T>::value;
1172         }
1173     };
1174 
1175     bool signal_value(system::error_code&)
1176     {
1177         BOOST_ASSERT( inner_active_ >= 0 );
1178         bool required_member = mp11::mp_with_index<InnerCount>(
1179             inner_active_,
1180             is_required_checker{});
1181         if( required_member )
1182             ++activated_;
1183 
1184         key_ = {};
1185         inner_active_ = -1;
1186         return true;
1187     }
1188 
1189     bool signal_end(system::error_code& ec)
1190     {
1191         key_ = {};
1192         inner_active_ = -1;
1193         return parent_->signal_value(ec);
1194     }
1195 
1196 #define BOOST_JSON_INVOKE_INNER(fn) \
1197     if( inner_active_ < 0 ) \
1198     { \
1199         BOOST_JSON_FAIL( ec, error::not_object ); \
1200         return false; \
1201     } \
1202     auto f = [&](auto& handler) { return handler.fn ; }; \
1203     using F = decltype(f); \
1204     using H = decltype(handlers_); \
1205     return mp11::mp_with_index<InnerCount>( \
1206             inner_active_, \
1207             tuple_handler_op_invoker<H, F>{handlers_, f} );
1208 
1209     bool on_object_begin( system::error_code& ec )
1210     {
1211         if( inner_active_ < 0 )
1212             return true;
1213 
1214         BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1215     }
1216 
1217     bool on_object_end( system::error_code& ec )
1218     {
1219         if( inner_active_ < 0 )
1220         {
1221             using C = mp11::mp_count_if<Dt, is_optional_like>;
1222             constexpr int N = mp11::mp_size<Dt>::value - C::value;
1223             if( activated_ < N )
1224             {
1225                 BOOST_JSON_FAIL( ec, error::size_mismatch );
1226                 return false;
1227             }
1228 
1229             return parent_->signal_value(ec);
1230         }
1231 
1232         BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1233     }
1234 
1235     bool on_array_begin( system::error_code& ec )
1236     {
1237         BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1238     }
1239 
1240     bool on_array_end( system::error_code& ec )
1241     {
1242         if( inner_active_ < 0 )
1243             return parent_->signal_end(ec);
1244 
1245         BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1246     }
1247 
1248     bool on_key_part( system::error_code& ec, string_view sv )
1249     {
1250         if( inner_active_ < 0 )
1251         {
1252             key_.append( sv.data(), sv.size() );
1253             return true;
1254         }
1255 
1256         BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1257     }
1258 
1259     bool on_key( system::error_code& ec, string_view sv )
1260     {
1261         if( inner_active_ >= 0 )
1262         {
1263             BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1264         }
1265 
1266         string_view key = sv;
1267         if( !key_.empty() )
1268         {
1269             key_.append( sv.data(), sv.size() );
1270             key = key_;
1271         }
1272 
1273         inner_active_ = InnerCount::value - 1;
1274         mp11::mp_for_each<Dm>( struct_key_searcher(key, inner_active_) );
1275         return true;
1276     }
1277 
1278     bool on_string_part( system::error_code& ec, string_view sv )
1279     {
1280         BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1281     }
1282 
1283     bool on_string( system::error_code& ec, string_view sv )
1284     {
1285         BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1286     }
1287 
1288     bool on_number_part( system::error_code& ec )
1289     {
1290         BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1291     }
1292 
1293     bool on_int64( system::error_code& ec, std::int64_t v )
1294     {
1295         BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1296     }
1297 
1298     bool on_uint64( system::error_code& ec, std::uint64_t v )
1299     {
1300         BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1301     }
1302 
1303     bool on_double( system::error_code& ec, double v )
1304     {
1305         BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1306     }
1307 
1308     bool on_bool( system::error_code& ec, bool v )
1309     {
1310         BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1311     }
1312 
1313     bool on_null( system::error_code& ec )
1314     {
1315         BOOST_JSON_INVOKE_INNER( on_null(ec) );
1316     }
1317 
1318 #undef BOOST_JSON_INVOKE_INNER
1319 
1320 #endif
1321 };
1322 
1323 // variant handler
1324 struct object_begin_handler_event
1325 { };
1326 
1327 struct object_end_handler_event
1328 { };
1329 
1330 struct array_begin_handler_event
1331 { };
1332 
1333 struct array_end_handler_event
1334 { };
1335 
1336 struct key_handler_event
1337 {
1338     std::string value;
1339 };
1340 
1341 struct string_handler_event
1342 {
1343     std::string value;
1344 };
1345 
1346 struct int64_handler_event
1347 {
1348     std::int64_t value;
1349 };
1350 
1351 struct uint64_handler_event
1352 {
1353     std::uint64_t value;
1354 };
1355 
1356 struct double_handler_event
1357 {
1358     double value;
1359 };
1360 
1361 struct bool_handler_event
1362 {
1363     bool value;
1364 };
1365 
1366 struct null_handler_event
1367 { };
1368 
1369 using parse_event = variant2::variant<
1370     object_begin_handler_event,
1371     object_end_handler_event,
1372     array_begin_handler_event,
1373     array_end_handler_event,
1374     key_handler_event,
1375     string_handler_event,
1376     int64_handler_event,
1377     uint64_handler_event,
1378     double_handler_event,
1379     bool_handler_event,
1380     null_handler_event>;
1381 
1382 template< class H >
1383 struct event_visitor
1384 {
1385     H& handler;
1386     system::error_code& ec;
1387 
1388     bool
1389     operator()(object_begin_handler_event&) const
1390     {
1391         return handler.on_object_begin(ec);
1392     }
1393 
1394     bool
1395     operator()(object_end_handler_event&) const
1396     {
1397         return handler.on_object_end(ec);
1398     }
1399 
1400     bool
1401     operator()(array_begin_handler_event&) const
1402     {
1403         return handler.on_array_begin(ec);
1404     }
1405 
1406     bool
1407     operator()(array_end_handler_event&) const
1408     {
1409         return handler.on_array_end(ec);
1410     }
1411 
1412     bool
1413     operator()(key_handler_event& ev) const
1414     {
1415         return handler.on_key(ec, ev.value);
1416     }
1417 
1418     bool
1419     operator()(string_handler_event& ev) const
1420     {
1421         return handler.on_string(ec, ev.value);
1422     }
1423 
1424     bool
1425     operator()(int64_handler_event& ev) const
1426     {
1427         return handler.on_int64(ec, ev.value);
1428     }
1429 
1430     bool
1431     operator()(uint64_handler_event& ev) const
1432     {
1433         return handler.on_uint64(ec, ev.value);
1434     }
1435 
1436     bool
1437     operator()(double_handler_event& ev) const
1438     {
1439         return handler.on_double(ec, ev.value);
1440     }
1441 
1442     bool
1443     operator()(bool_handler_event& ev) const
1444     {
1445         return handler.on_bool(ec, ev.value);
1446     }
1447 
1448     bool
1449     operator()(null_handler_event&) const
1450     {
1451         return handler.on_null(ec);
1452     }
1453 };
1454 
1455 // L<T...> -> variant< monostate, get_handler<T, P>... >
1456 template< class P, class L >
1457 using inner_handler_variant = mp11::mp_push_front<
1458     mp11::mp_transform_q<
1459         mp11::mp_bind_back<get_handler, P>,
1460         mp11::mp_apply<variant2::variant, L>>,
1461     variant2::monostate>;
1462 
1463 template< class T, class P >
1464 class converting_handler<variant_conversion_tag, T, P>
1465 {
1466 private:
1467     using variant_size = mp11::mp_size<T>;
1468 
1469     T* value_;
1470     P* parent_;
1471 
1472     std::string string_;
1473     std::vector< parse_event > events_;
1474     inner_handler_variant<converting_handler, T> inner_;
1475     int inner_active_ = -1;
1476 
1477 public:
1478     converting_handler( converting_handler const& ) = delete;
1479     converting_handler& operator=( converting_handler const& ) = delete;
1480 
1481     converting_handler( T* v, P* p )
1482         : value_( v )
1483         , parent_( p )
1484     {}
1485 
1486     bool signal_value(system::error_code& ec)
1487     {
1488         inner_.template emplace<0>();
1489         inner_active_ = -1;
1490         events_.clear();
1491         return parent_->signal_value(ec);
1492     }
1493 
1494     bool signal_end(system::error_code& ec)
1495     {
1496         return parent_->signal_end(ec);
1497     }
1498 
1499     struct alternative_selector
1500     {
1501         converting_handler* self;
1502 
1503         template< class I >
1504         void
1505         operator()( I ) const
1506         {
1507             using V = mp11::mp_at<T, I>;
1508             auto& v = self->value_->template emplace<I::value>( V{} );
1509             self->inner_.template emplace<I::value + 1>(&v, self);
1510         }
1511     };
1512     void
1513     next_alternative()
1514     {
1515         if( ++inner_active_ >= static_cast<int>(variant_size::value) )
1516             return;
1517 
1518         mp11::mp_with_index< variant_size::value >(
1519             inner_active_, alternative_selector{this} );
1520     }
1521 
1522     struct event_processor
1523     {
1524         converting_handler* self;
1525         system::error_code& ec;
1526         parse_event& event;
1527 
1528         template< class I >
1529         bool operator()( I ) const
1530         {
1531             auto& handler = variant2::get<I::value + 1>(self->inner_);
1532             using Handler = remove_cvref<decltype(handler)>;
1533             return variant2::visit(
1534                 event_visitor<Handler>{handler, ec}, event );
1535         }
1536     };
1537     bool process_events(system::error_code& ec)
1538     {
1539         constexpr std::size_t N = variant_size::value;
1540 
1541         // should be pointers not iterators, otherwise MSVC crashes
1542         auto const last = events_.data() + events_.size();
1543         auto first = last - 1;
1544         bool ok = false;
1545 
1546         if( inner_active_ < 0 )
1547             next_alternative();
1548         do
1549         {
1550             if( static_cast<std::size_t>(inner_active_) >= N )
1551             {
1552                 BOOST_JSON_FAIL( ec, error::exhausted_variants );
1553                 return false;
1554             }
1555 
1556             for ( ; first != last; ++first )
1557             {
1558                 ok = mp11::mp_with_index< N >(
1559                     inner_active_, event_processor{this, ec, *first} );
1560                 if( !ok )
1561                 {
1562                     first = events_.data();
1563                     next_alternative();
1564                     ec.clear();
1565                     break;
1566                 }
1567             }
1568         }
1569         while( !ok );
1570 
1571         return true;
1572     }
1573 
1574 #define BOOST_JSON_INVOKE_INNER(ev, ec) \
1575     events_.emplace_back( ev ); \
1576     return process_events(ec);
1577 
1578     bool on_object_begin( system::error_code& ec )
1579     {
1580         BOOST_JSON_INVOKE_INNER( object_begin_handler_event{}, ec );
1581     }
1582 
1583     bool on_object_end( system::error_code& ec )
1584     {
1585         BOOST_JSON_INVOKE_INNER( object_end_handler_event{}, ec );
1586     }
1587 
1588     bool on_array_begin( system::error_code& ec )
1589     {
1590         BOOST_JSON_INVOKE_INNER( array_begin_handler_event{}, ec );
1591     }
1592 
1593     bool on_array_end( system::error_code& ec )
1594     {
1595         if( !inner_active_ )
1596             return signal_end(ec);
1597 
1598         BOOST_JSON_INVOKE_INNER( array_end_handler_event{}, ec );
1599     }
1600 
1601     bool on_key_part( system::error_code&, string_view sv )
1602     {
1603         string_.append(sv);
1604         return true;
1605     }
1606 
1607     bool on_key( system::error_code& ec, string_view sv )
1608     {
1609         string_.append(sv);
1610         BOOST_JSON_INVOKE_INNER( key_handler_event{ std::move(string_) }, ec );
1611     }
1612 
1613     bool on_string_part( system::error_code&, string_view sv )
1614     {
1615         string_.append(sv);
1616         return true;
1617     }
1618 
1619     bool on_string( system::error_code& ec, string_view sv )
1620     {
1621         string_.append(sv);
1622         BOOST_JSON_INVOKE_INNER(
1623             string_handler_event{ std::move(string_) }, ec );
1624     }
1625 
1626     bool on_number_part( system::error_code& )
1627     {
1628         return true;
1629     }
1630 
1631     bool on_int64( system::error_code& ec, std::int64_t v )
1632     {
1633         BOOST_JSON_INVOKE_INNER( int64_handler_event{v}, ec );
1634     }
1635 
1636     bool on_uint64( system::error_code& ec, std::uint64_t v )
1637     {
1638         BOOST_JSON_INVOKE_INNER( uint64_handler_event{v}, ec );
1639     }
1640 
1641     bool on_double( system::error_code& ec, double v )
1642     {
1643         BOOST_JSON_INVOKE_INNER( double_handler_event{v}, ec );
1644     }
1645 
1646     bool on_bool( system::error_code& ec, bool v )
1647     {
1648         BOOST_JSON_INVOKE_INNER( bool_handler_event{v}, ec );
1649     }
1650 
1651     bool on_null( system::error_code& ec )
1652     {
1653         BOOST_JSON_INVOKE_INNER( null_handler_event{}, ec );
1654     }
1655 
1656 #undef BOOST_JSON_INVOKE_INNER
1657 };
1658 
1659 // optional handler
1660 template<class V, class P>
1661 class converting_handler<optional_conversion_tag, V, P>
1662 {
1663 private:
1664     using inner_type = value_result_type<V>;
1665     using inner_handler_type = get_handler<inner_type, converting_handler>;
1666 
1667     V* value_;
1668     P* parent_;
1669 
1670     inner_type inner_value_ = {};
1671     inner_handler_type inner_;
1672     bool inner_active_ = false;
1673 
1674 public:
1675     converting_handler( converting_handler const& ) = delete;
1676     converting_handler& operator=( converting_handler const& ) = delete;
1677 
1678     converting_handler( V* v, P* p )
1679         : value_(v), parent_(p), inner_(&inner_value_, this)
1680     {}
1681 
1682     bool signal_value(system::error_code& ec)
1683     {
1684         *value_ = std::move(inner_value_);
1685 
1686         inner_active_ = false;
1687         return parent_->signal_value(ec);
1688     }
1689 
1690     bool signal_end(system::error_code& ec)
1691     {
1692         return parent_->signal_end(ec);
1693     }
1694 
1695 #define BOOST_JSON_INVOKE_INNER(fn) \
1696     if( !inner_active_ ) \
1697         inner_active_ = true; \
1698     return inner_.fn;
1699 
1700     bool on_object_begin( system::error_code& ec )
1701     {
1702         BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1703     }
1704 
1705     bool on_object_end( system::error_code& ec )
1706     {
1707         BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1708     }
1709 
1710     bool on_array_begin( system::error_code& ec )
1711     {
1712         BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1713     }
1714 
1715     bool on_array_end( system::error_code& ec )
1716     {
1717         if( !inner_active_ )
1718             return signal_end(ec);
1719 
1720         BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1721     }
1722 
1723     bool on_key_part( system::error_code& ec, string_view sv )
1724     {
1725         BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1726     }
1727 
1728     bool on_key( system::error_code& ec, string_view sv )
1729     {
1730         BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1731     }
1732 
1733     bool on_string_part( system::error_code& ec, string_view sv )
1734     {
1735         BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1736     }
1737 
1738     bool on_string( system::error_code& ec, string_view sv )
1739     {
1740         BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1741     }
1742 
1743     bool on_number_part( system::error_code& ec )
1744     {
1745         BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1746     }
1747 
1748     bool on_int64( system::error_code& ec, std::int64_t v )
1749     {
1750         BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1751     }
1752 
1753     bool on_uint64( system::error_code& ec, std::uint64_t v )
1754     {
1755         BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1756     }
1757 
1758     bool on_double( system::error_code& ec, double v )
1759     {
1760         BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1761     }
1762 
1763     bool on_bool( system::error_code& ec, bool v )
1764     {
1765         BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1766     }
1767 
1768     bool on_null(system::error_code& ec)
1769     {
1770         if( !inner_active_ )
1771         {
1772             *value_ = {};
1773             return this->parent_->signal_value(ec);
1774         }
1775         else
1776         {
1777             return inner_.on_null(ec);
1778         }
1779     }
1780 
1781 #undef BOOST_JSON_INVOKE_INNER
1782 };
1783 
1784 // path handler
1785 template< class V, class P >
1786 class converting_handler<path_conversion_tag, V, P>
1787     : public scalar_handler<P, error::not_string>
1788 {
1789 private:
1790     V* value_;
1791     bool cleared_ = false;
1792 
1793 public:
1794     converting_handler( V* v, P* p )
1795         : converting_handler::scalar_handler(p)
1796         , value_(v)
1797     {}
1798 
1799     bool on_string_part( system::error_code&, string_view sv )
1800     {
1801         if( !cleared_ )
1802         {
1803             cleared_ = true;
1804             value_->clear();
1805         }
1806 
1807         value_->concat( sv.begin(), sv.end() );
1808         return true;
1809     }
1810 
1811     bool on_string(system::error_code& ec, string_view sv)
1812     {
1813         if( !cleared_ )
1814             value_->clear();
1815         else
1816             cleared_ = false;
1817 
1818         value_->concat( sv.begin(), sv.end() );
1819 
1820         return this->parent_->signal_value(ec);
1821     }
1822 };
1823 
1824 // into_handler
1825 template< class V >
1826 class into_handler
1827 {
1828 private:
1829 
1830     using inner_handler_type = get_handler<V, into_handler>;
1831 
1832     inner_handler_type inner_;
1833     bool inner_active_ = true;
1834 
1835 public:
1836 
1837     into_handler( into_handler const& ) = delete;
1838     into_handler& operator=( into_handler const& ) = delete;
1839 
1840 public:
1841 
1842     static constexpr std::size_t max_object_size = object::max_size();
1843     static constexpr std::size_t max_array_size = array::max_size();
1844     static constexpr std::size_t max_key_size = string::max_size();
1845     static constexpr std::size_t max_string_size = string::max_size();
1846 
1847 public:
1848 
1849     explicit into_handler( V* v ): inner_( v, this )
1850     {
1851     }
1852 
1853     bool signal_value(system::error_code&)
1854     {
1855         return true;
1856     }
1857 
1858     bool signal_end(system::error_code&)
1859     {
1860         return true;
1861     }
1862 
1863     bool on_document_begin( system::error_code& )
1864     {
1865         return true;
1866     }
1867 
1868     bool on_document_end( system::error_code& )
1869     {
1870         inner_active_ = false;
1871         return true;
1872     }
1873 
1874 #define BOOST_JSON_INVOKE_INNER(f) \
1875     if( !inner_active_ ) \
1876     { \
1877         BOOST_JSON_FAIL( ec, error::extra_data ); \
1878         return false; \
1879     } \
1880     else \
1881         return inner_.f
1882 
1883     bool on_object_begin( system::error_code& ec )
1884     {
1885         BOOST_JSON_INVOKE_INNER( on_object_begin(ec) );
1886     }
1887 
1888     bool on_object_end( std::size_t, system::error_code& ec )
1889     {
1890         BOOST_JSON_INVOKE_INNER( on_object_end(ec) );
1891     }
1892 
1893     bool on_array_begin( system::error_code& ec )
1894     {
1895         BOOST_JSON_INVOKE_INNER( on_array_begin(ec) );
1896     }
1897 
1898     bool on_array_end( std::size_t, system::error_code& ec )
1899     {
1900         BOOST_JSON_INVOKE_INNER( on_array_end(ec) );
1901     }
1902 
1903     bool on_key_part( string_view sv, std::size_t, system::error_code& ec )
1904     {
1905         BOOST_JSON_INVOKE_INNER( on_key_part(ec, sv) );
1906     }
1907 
1908     bool on_key( string_view sv, std::size_t, system::error_code& ec )
1909     {
1910         BOOST_JSON_INVOKE_INNER( on_key(ec, sv) );
1911     }
1912 
1913     bool on_string_part( string_view sv, std::size_t, system::error_code& ec )
1914     {
1915         BOOST_JSON_INVOKE_INNER( on_string_part(ec, sv) );
1916     }
1917 
1918     bool on_string( string_view sv, std::size_t, system::error_code& ec )
1919     {
1920         BOOST_JSON_INVOKE_INNER( on_string(ec, sv) );
1921     }
1922 
1923     bool on_number_part( string_view, system::error_code& ec )
1924     {
1925         BOOST_JSON_INVOKE_INNER( on_number_part(ec) );
1926     }
1927 
1928     bool on_int64( std::int64_t v, string_view, system::error_code& ec )
1929     {
1930         BOOST_JSON_INVOKE_INNER( on_int64(ec, v) );
1931     }
1932 
1933     bool on_uint64( std::uint64_t v, string_view, system::error_code& ec )
1934     {
1935         BOOST_JSON_INVOKE_INNER( on_uint64(ec, v) );
1936     }
1937 
1938     bool on_double( double v, string_view, system::error_code& ec )
1939     {
1940         BOOST_JSON_INVOKE_INNER( on_double(ec, v) );
1941     }
1942 
1943     bool on_bool( bool v, system::error_code& ec )
1944     {
1945         BOOST_JSON_INVOKE_INNER( on_bool(ec, v) );
1946     }
1947 
1948     bool on_null( system::error_code& ec )
1949     {
1950         BOOST_JSON_INVOKE_INNER( on_null(ec) );
1951     }
1952 
1953     bool on_comment_part(string_view, system::error_code&)
1954     {
1955         return true;
1956     }
1957 
1958     bool on_comment(string_view, system::error_code&)
1959     {
1960         return true;
1961     }
1962 
1963 #undef BOOST_JSON_INVOKE_INNER
1964 };
1965 
1966 } // namespace detail
1967 } // namespace boost
1968 } // namespace json
1969 
1970 #endif