Boost logo

Boost Users :

From: Anders Wang Kristensen (awk_at_[hidden])
Date: 2006-11-06 19:04:45


Hi,

I'm having trouble serializing through the a pointer to the base as shown
below.
If I manually register class B with template register_type<B>() it works,
but I
would rather use the BOOST_CLASS_EXPORT macros since that is more
convenient.

Unfortunately BOOST_CLASS_EXPORT assumes you are using the templated
version of serialize, and not polymorphic_iarchive/oarchive, that I'm using.

Anyone know how to get around that problem?

Thanks!

-- wang

Example:

#include <boost/archive/polymorphic_text_iarchive.hpp>
#include <boost/archive/polymorphic_text_oarchive.hpp>
#include <boost/archive/polymorphic_xml_iarchive.hpp>
#include <boost/archive/polymorphic_xml_oarchive.hpp>
#include <boost/serialization/shared_ptr.hpp>
#include <boost/serialization/export.hpp>

struct A {
    void serialize(boost::archive::polymorphic_iarchive & ar, const unsigned
int file_version);
    void serialize(boost::archive::polymorphic_oarchive & ar, const unsigned
int file_version);
    int data;
    bool operator==(const A & rhs) const {
        return data == rhs.data;
    }
    A() :
        data(0)
    {}

 virtual ~A() = 0 {}
};

void A::serialize(
    boost::archive::polymorphic_iarchive & ar,
    const unsigned int file_version
){
    ar & BOOST_SERIALIZATION_NVP(data);
}
void A::serialize(
    boost::archive::polymorphic_oarchive & ar,
    const unsigned int file_version
){
    ar & BOOST_SERIALIZATION_NVP(data);
}

struct B : public A {

 void serialize(boost::archive::polymorphic_iarchive & ar, const unsigned
int file_version);
    void serialize(boost::archive::polymorphic_oarchive & ar, const unsigned
int file_version);
    int foo;
    bool operator==(const B & rhs) const {
        return data==rhs.data && foo==rhs.foo;
    }
    B() :
        foo(2)
    {}
};

void B::serialize(
    boost::archive::polymorphic_iarchive & ar,
    const unsigned int file_version
){
 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A);
    ar & BOOST_SERIALIZATION_NVP(foo);
}
void B::serialize(
    boost::archive::polymorphic_oarchive & ar,
    const unsigned int file_version
){
 ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(A);
    ar & BOOST_SERIALIZATION_NVP(foo);
}

BOOST_CLASS_EXPORT(B)

int main(int argc, const char* argv[])
{
 boost::shared_ptr<A> a(new B);
 boost::shared_ptr<A> b;

 {
        std::stringstream ss;
        {
            boost::archive::polymorphic_text_oarchive oa(ss);
   //oa.template register_type<B>();
            oa << BOOST_SERIALIZATION_NVP(a);
        }
        {
            boost::archive::polymorphic_text_iarchive ia(ss);
   //ia.template register_type<B>();
            ia >> BOOST_SERIALIZATION_NVP(b);
        }
    }

 B* x = dynamic_cast<B*>(a.get());
 B* y = dynamic_cast<B*>(b.get());

 assert(*x == *y);
 return 0;
}


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