Boost logo

Boost Users :

From: Robert Ramey (ramey_at_[hidden])
Date: 2006-11-06 20:42:53


Anders Wang Kristensen wrote:
> 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.

I don't think that's true. Maybe you mean it assums you are using
member serialize functions - but that won't be true either.

Basically I think your example should function. Where does it fail?
Does it throw and exception? where? does the assert fail?, etc.,etc.
Also what compiler, os, version of boost etc.?

Though its beside the point, I would also make your example more portable
wit the fillowing
changes:

...
struct A {
    template<class Archive>
    void serialize(Archive & ar, const unsigned int file_version);
    ...
};
template<class Archive>
void A::serialize(Archive & ar, const unsigned int file_version){
    ar & BOOST_SERIALIZATION_NVP(data);
}

struct B : public A {
    template<class Archive>
    void serialize(Archive & ar, const unsigned int file_version);
    ...
};

template<class Archive>
void B::serialize(Archive & 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> a1;
    std::stringstream ss;
   {
        boost::archive::polymorphic_text_oarchive oa(ss);
        //oa.template register_type<B>();
       oa << BOOST_SERIALIZATION_NVP(a);
    }
    try{
        boost::archive::polymorphic_text_iarchive ia(ss);
        //ia.template register_type<B>();
        ia >> BOOST_SERIALIZATION_NVP(a1);
    }
    catch(...){
        ...
    }
    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