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
#include
#include
#include
#include
#include
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(a.get());
B* y = dynamic_cast(b.get());
assert(*x == *y);
return 0;
}