|
Boost Users : |
Subject: Re: [Boost-users] boost::serialization throwing "input stream error" on base class ptr XML serialization
From: Robert Ramey (ramey_at_[hidden])
Date: 2013-04-26 14:20:12
Jari wrote:
>
> //Why do these need to be wrappers??
> namespace boost
> {
> namespace serialization
> {
> template<> struct is_wrapper<A*> : mpl::true_ {};
> template<> struct is_wrapper<A> : mpl::true_ {};
> template<> struct is_wrapper<const A> : mpl::true_ {};
> }
> }
I don't know why the above is there. It should be taken out
>
> BOOST_SERIALIZATION_ASSUME_ABSTRACT(A);
> BOOST_CLASS_EXPORT(B)
> BOOST_CLASS_EXPORT(C)
>
> int main(int argc, char** argv)
> {
> //A* a = new A();
> A* b = new B();
> A* c = new C();
> //A* a1 = nullptr;
> A* a2 = nullptr;
> A* a3 = nullptr;
>
> ostringstream ostream;
> xml_oarchive oarchive(ostream);
>
> oarchive /*<< BOOST_SERIALIZATION_NVP(a)*/
> << BOOST_SERIALIZATION_NVP(b)
> << BOOST_SERIALIZATION_NVP(c);
>
> istringstream istream(ostream.str());
> xml_iarchive iarchive(istream);
>
> iarchive /*>> BOOST_SERIALIZATION_NVP(a1)*/
> >> BOOST_SERIALIZATION_NVP(a2)
> >> BOOST_SERIALIZATION_NVP(a3);
>
> //delete a;
> delete b;
> delete c;
> //delete a1;
> delete a2;
> delete a3;
> }
>
The problem is:
oarchive << BOOST_SERIALIZATION_NVP(a) creates an item <a ...> data </a>
iarchive >> BOOST_SERIALIZATION_NVP(a1) expects an item <a1 ...> data </a1>
The serialization library verifies that the name used to save is the name
used to load
because that's usually a bug. If you want the above to work you'l ahve to
use:
use the following instead so that the tag is always a
oarchive << make_nvp("a", a) creates an item <a ...> data </a>
iarchive >> make_nvp("a", a1) expects an item <a...> data </a1>
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net