parse_into clears sequences before filling them

This commit is contained in:
Dmitry Arkhipov 2023-09-30 09:12:11 +03:00
parent f144f382d8
commit 75981e701e
3 changed files with 53 additions and 7 deletions

View File

@ -65,6 +65,10 @@ struct options
iterator
end();
void
clear()
{ }
};
struct coordinate
@ -112,6 +116,10 @@ struct accumulator
iterator
end() { return nullptr; }
void
clear()
{ }
};
struct coordinates2

View File

@ -502,6 +502,32 @@ std::true_type check_inserter( It1, It2 )
return {};
}
template<class T>
void
clear_container(
T&,
mp11::mp_int<2>)
{
}
template<class T>
void
clear_container(
T& target,
mp11::mp_int<1>)
{
target.clear();
}
template<class T>
void
clear_container(
T& target,
mp11::mp_int<0>)
{
target.clear();
}
template< class V, class P >
class converting_handler<sequence_conversion_tag, V, P>
: public composite_handler<
@ -553,14 +579,11 @@ public:
bool on_array_begin( error_code& ec )
{
if( this->inner_active_ )
{
return this->inner_.on_array_begin( ec );
}
else
{
this->inner_active_ = true;
return true;
}
this->inner_active_ = true;
clear_container( *value_, inserter_implementation<V>() );
return true;
}
bool on_array_end( error_code& ec )
@ -605,6 +628,7 @@ public:
if( this->inner_active_ )
return this->inner_.on_object_begin(ec);
clear_container( *value_, inserter_implementation<V>() );
return true;
}

View File

@ -252,6 +252,13 @@ public:
error::size_mismatch, {1, 2, 3} );
testParseInto< std::vector<std::array<int, 4>> >( {arr,arr,arr} );
std::vector<int> v;
parse_into(v, "[1,2,3,4]");
BOOST_TEST( v.size() == 4 );
parse_into(v, "[5,6,7]");
BOOST_TEST( v.size() == 3 );
}
void testMap()
@ -272,6 +279,13 @@ public:
error::not_object, { "1", 1, "2", 2} );
testParseIntoErrors< std::map<std::string, std::map<std::string, int>> >(
error::not_object, { {"1", {}}, {"2", {"3", 4}} } );
std::map<std::string, int> m;
parse_into(m, R"( {"1": 1, "2": 2, "3": 3} )");
BOOST_TEST( m.size() == 3 );
parse_into(m, R"( {"4": 4, "5": 5} )");
BOOST_TEST( m.size() == 2 );
}
void testTuple()