Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
grisumbras committed Aug 21, 2023
1 parent 9fc43fa commit f73d03a
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
37 changes: 31 additions & 6 deletions include/boost/json/detail/parse_into.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <boost/describe/enum_from_string.hpp>

#include <vector>
#include <iostream>

/*
* This file contains the majority of parse_into functionality, specifically
Expand Down Expand Up @@ -1244,7 +1245,7 @@ class converting_handler<variant_conversion_tag, T, P>
converting_handler( T* v, P* p )
: value_( v )
, parent_( p )
{}
{ }

void signal_value()
{
Expand All @@ -1267,69 +1268,93 @@ class converting_handler<variant_conversion_tag, T, P>
void
operator()( I ) const
{
std::cerr << "alternative_selector " << I::value << " 1\n";
using V = mp11::mp_at<T, I>;
auto& v = self->value_->template emplace<I::value>( V{} );
std::cerr << "alternative_selector " << I::value << " 2\n";
self->inner_.template emplace<I::value + 1>(&v, self);
std::cerr << "alternative_selector " << I::value << " 3\n";
}
};
void
next_alternative()
{
std::cerr << "next_alternative 1\n";
if( ++inner_active_ >= static_cast<int>(variant_size::value) )
return;
std::cerr << "next_alternative 2\n";

mp11::mp_with_index< variant_size::value >(
inner_active_, alternative_selector{this} );
std::cerr << "next_alternative 3\n";
}

struct event_processor
{
converting_handler* self;
error_code& ec;
parse_event& event;
error_code* ec;
parse_event* event;

template< class I >
bool operator()( I ) const
{
std::cerr << "event_processor " << I::value << " 1\n";
auto& handler = variant2::get<I::value + 1>(self->inner_);
std::cerr << "event_processor " << I::value << " 2\n";
using Handler = remove_cvref<decltype(handler)>;
return variant2::visit(
event_visitor<Handler>{handler, ec}, event );
event_visitor<Handler>{handler, *ec}, *event );
}
};
bool process_events(error_code& ec)
{
std::cerr << "process_events 1\n";
constexpr std::size_t N = variant_size::value;

auto const last = events_.end();
auto first = last - 1;
bool ok = false;
std::cerr << "process_events 2 " << (&*first) << "\n";

if( inner_active_ < 0 )
next_alternative();
std::cerr << "process_events 3 " << (&*first) << "\n";
do
{
if( static_cast<std::size_t>(inner_active_) >= N )
{
BOOST_JSON_FAIL( ec, error::exhausted_variants );
return false;
}
std::cerr << "process_events 4 " << (&*first) << "\n";

for ( ; first != last; ++first )
// for ( ; first != last; ++first )
while ( first != last )
{
parse_event& event = *first;
std::cerr << "process_events 5 " << (&*first) << "\n";
ok = mp11::mp_with_index< N >(
inner_active_, event_processor{this, ec, *first} );
inner_active_, event_processor{this, &ec, &event} );
std::cerr << "process_events 6 " << (&*first) << "\n";
if( !ok )
{
first = events_.begin();
std::cerr << "process_events 7 " << (&*first) << "\n";
next_alternative();
std::cerr << "process_events 8 " << (&*first) << "\n";
ec.clear();
std::cerr << "process_events 9 " << (&*first) << "\n";
break;
}
std::cerr << "process_events 10 " << (&*first) << "\n";
++first;
std::cerr << "process_events 11 " << (&*first) << "\n";
}
std::cerr << "process_events 12\n";
}
while( !ok );

std::cerr << "process_events 13\n";
return true;
}

Expand Down
17 changes: 17 additions & 0 deletions test/parse_into.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#include <climits>
#include <map>
#include <iostream>

#include "test.hpp"
#include "test_suite.hpp"
Expand Down Expand Up @@ -59,41 +60,50 @@ class parse_into_test

template<class T> void testParseInto( T const& t )
{
std::cerr << "testParseInto 1" << '\n';
T t1( t );
std::string json = serialize( value_from( t1 ) );

std::cerr << "testParseInto 2" << '\n';
T t2{};
error_code jec;
parse_into(t2, json, jec);
BOOST_TEST( !jec.failed() ) && BOOST_TEST( t1 == t2 );

std::cerr << "testParseInto 3" << '\n';
T t3{};
std::error_code ec;
parse_into(t3, json, ec);
BOOST_TEST( !ec ) && BOOST_TEST( t1 == t3 );

std::cerr << "testParseInto 4" << '\n';
T t4{};
parse_into(t4, json);
BOOST_TEST( t1 == t4 );

std::cerr << "testParseInto 5" << '\n';
std::istringstream is(json);
T t5{};
jec = {};
parse_into(t5, is, jec);
BOOST_TEST( !jec.failed() ) && BOOST_TEST( t1 == t5 );

std::cerr << "testParseInto 6" << '\n';
is.clear();
is.seekg(0);
T t6{};
ec = {};
parse_into(t6, is, ec);
BOOST_TEST( !ec ) && BOOST_TEST( t1 == t6 );

std::cerr << "testParseInto 7" << '\n';
is.clear();
is.seekg(0);
T t7{};
parse_into(t7, is);
BOOST_TEST( t1 == t7 );

std::cerr << "testParseInto 8" << '\n';
}

void testNull()
Expand Down Expand Up @@ -218,7 +228,9 @@ class parse_into_test
template< template<class...> class Variant, class Monostate >
void testVariant()
{
std::cerr << "variant 1 " << '\n';
testParseInto< Variant<int> >( 1 );
std::cerr << "variant 2 " << '\n';
testParseInto< Variant<int, std::string> >( 1 );
testParseInto< Variant<int, std::string> >( "qwerty" );
testParseInto< Variant<Monostate, int, std::string> >( {} );
Expand All @@ -233,6 +245,7 @@ class parse_into_test
testParseInto< std::vector< Variant<int, std::string> > >(
{1, 2, 3, "four", 5, "six", "seven", 8});

std::cerr << "variant 3 " << '\n';
using V = Variant<
std::vector< int >,
std::tuple< int, std::string, std::map<std::string, int> >,
Expand All @@ -242,6 +255,8 @@ class parse_into_test
5,
"five",
std::map<std::string, double>{ {"one", 1}, {"pi", 3.14} }));

std::cerr << "variant 4 " << '\n';
}

void testOptional()
Expand Down Expand Up @@ -278,8 +293,10 @@ class parse_into_test
testStruct();
testEnum();
testOptional();
std::cerr << "before variant2" << '\n';
testVariant<variant2::variant, variant2::monostate>();
#ifndef BOOST_NO_CXX17_HDR_VARIANT
std::cerr << "before std::variant" << '\n';
testVariant<std::variant, std::monostate>();
#endif
}
Expand Down

0 comments on commit f73d03a

Please sign in to comment.