Boost logo

Boost Users :

Subject: [Boost-users] boost::serialization throwing “input stream error” on base class ptr XML serialization
From: jvetter (jvetter_at_[hidden])
Date: 2013-04-17 08:15:44


Hello,

This seems straight forward to me, but yet I'm stilling getting a input
stream error and I don't understand why.

The weird part about it is if I un-comment the serialization of the base
class in my example it works, otherwise it does not. Here is the code:

struct A
{
     virtual ~A() {};

     int i1 = 10;

     friend class boost::serialization::access;
     template <class Archive>
     void serialize(Archive& ar, const unsigned int version)
     {
         ar & make_nvp("int", i1);
     }
};
struct B : A
{
     virtual ~B() {};

     float f1 = 5.0F

     friend class boost::serialization::access;
     template <class Archive>
     void serialize(Archive& ar, const unsigned int version)
     {
         ar & boost::serialization::base_object<A>(*this);
         ar & make_nvp("float", f1);
     }
};
struct C : A
{
     virtual ~C() {};

     long l1 = 24;

     friend class boost::serialization::access;
     template <class Archive>
     void serialize(Archive& ar, const unsigned int version)
     {
         ar & boost::serialization::base_object<A>(*this);
         ar & make_nvp("long", l1);
     }
};

//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_ {};
}
}

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;
}

It's worth mentioning that this is with Boost 1.53 and GCC 4.8.0 w/
--std=c++11.

The same code works fine in both cases worth the text and binary
archives. I have tried changing the nvp's in the main to have the same
key and in the input case no key, but I get the same error.

Am I doing something wrong or is there an issue with the xml archive?

Thanks,
Jaime


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