Boost logo

Boost Users :

Subject: Re: [Boost-users] Boost Serialize and derived classes
From: Robert Ramey (ramey_at_[hidden])
Date: 2008-09-17 00:58:04


the base class MUST be polymorphic - that it it must have at least one
virtual function. A practical suggestion would be to make at least one
of those functions abstract ( a() = 0) so that you can't accidently
"slice" off the derived class.

Robert Ramey

John Yamokoski wrote:
> Yea I have done that. I have read the sections on exporting several
> times and thought my example was implementing this feature correctly.
> Yet every time I run it, the code only spits out the base class. The
> only complete example from the documentation shows the explicit
> declaration of derived class names. Once again, here is my test code:
>
> main.cpp:
>
> class base
> {
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive & ar, const unsigned int file_version)
> {
> ar & BOOST_SERIALIZATION_NVP(dval);
> }
> public:
> base() : dval(0.0) {}
> base(double b) : dval(b) {}
> double dval;
> };
>
> class derived : public base
> {
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive & ar, const unsigned int file_version)
> {
> boost::serialization::void_cast_register<derived,
> base>(static_cast<derived *>(NULL),static_cast<base *>(NULL));
> ar & BOOST_SERIALIZATION_NVP(ival);
> }
> public:
> derived() : base(0.0), ival(0) {}
> derived(int a, double b) : base(b), ival(a) {}
> int ival;
> };
>
> BOOST_CLASS_EXPORT_GUID(derived, "derived");
>
> class manager
> {
> friend class boost::serialization::access;
> template<class Archive>
> void serialize(Archive & ar, const unsigned int file_version) {
> ar & BOOST_SERIALIZATION_NVP(objs);
> }
> public:
> std::list<base*> objs;
> };
>
> void SerializeTest::testSerialize()
> {
> manager mgr;
>
> // Creat some data to save
> derived *d1, *d2;
> d1 = new derived(1, 1.0);
> d2 = new derived(2, 3.0);
>
> mgr.objs.push_back( d1 );
> mgr.objs.push_back( d2 );
>
> {
> std::ofstream ofs("out.xml");
> assert(ofs.good());
> boost::archive::xml_oarchive oa(ofs);
> oa << BOOST_SERIALIZATION_NVP(mgr);
> ofs.close();
> }
>
> manager new_mgr;
>
> {
> std::ifstream ifs("out.xml");
> assert(ifs.good());
> boost::archive::xml_iarchive ia(ifs);
> ia >> BOOST_SERIALIZATION_NVP(new_mgr);
> ifs.close();
> }
>
> {
> std::ofstream ofs("check.xml");
> assert(ofs.good());
> boost::archive::xml_oarchive oa(ofs);
> oa << BOOST_SERIALIZATION_NVP(new_mgr);
> ofs.close();
> }
>
> delete d1, d2;
> }
>
> And here is the output of out.xml:
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
> <!DOCTYPE boost_serialization>
> <boost_serialization signature="serialization::archive" version="4">
> <mgr class_id="0" tracking_level="0" version="0">
> <objs class_id="1" tracking_level="0" version="0">
> <count>2</count>
> <item_version>0</item_version>
> <item class_id="2" tracking_level="1" version="0"
> object_id="_0">
> <dval>1</dval>
> </item>
> <item class_id_reference="2" object_id="_1">
> <dval>3</dval>
> </item>
> </objs>
> </mgr>
>
>
>
>
>
> On Tue, Sep 16, 2008 at 6:32 PM, Robert Ramey <ramey_at_[hidden]> wrote:
>> Look in the documentation of "export" and BOOST_CLASS_EXPORT


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