views:

1414

answers:

3

I am working on a client-server application that uses boost::serialization library for it's serialization needs.

I need to serialize and deserialize polymorphic objects that does not seem to work. The documentation does say that it is supported but none of the related examples demonstrate what I'm trying to do here. So, I am not very sure. My question is can serialize/deserialize polymorphic objects using boost? If yes, what am I doing wrong here?

Thanks!

code:

using namespace std;  

class base {  
  public:
    int data1;  

    friend class boost::serialization::access;  

    void serialize(boost::archive::polymorphic_iarchive & ar, 
                   const unsigned int file_version) {  
        ar & data1;  
    }  

    void serialize(boost::archive::polymorphic_oarchive & ar, 
                   const unsigned int file_version){  
        ar & data1;  
    }  

  public:  
    base() {};  
    base(int _d) : data1(_d) {}  
    virtual void foo() const {std::cout << "base" << std::endl;}  
};  

class derived : public base {  
  public:  
    int data2;  

    friend class boost::serialization::access;  

    void serialize(boost::archive::polymorphic_iarchive & ar, 
                   const unsigned int file_version) {  
        ar & boost::serialization::base_object<base>(*this) & data2;  
    }  

    void serialize(boost::archive::polymorphic_oarchive & ar, 
                   const unsigned int file_version){  
        ar & boost::serialization::base_object<base>(*this) & data2;  
    }  

  public:  
    derived() {};  
    derived(int _b, int _d) : base(_b), data2(_d) {}  
    virtual void foo() const {std::cout << "derived" << std::endl;}  
};  

int main(int argc, char *argv[]) {  
    // client  
    const base *b1 = new derived(1, 2);  

    std::ostringstream oss;  
    boost::archive::polymorphic_text_oarchive oa(oss);  
    oa << *b1;  

    // server  
    base *b2 = new derived(3, 4);  

    std::istringstream iss(oss.str());  
    boost::archive::polymorphic_text_iarchive ia(iss);  
    ia >> *b2;  

    // prints 1, ok  
    cout << b2->data1 << endl;  

    // prints 4, why wasn't the derived class data written?
    cout << (dynamic_cast<derived*>(b2))->data2 << endl;  

    return 0;  
}  
+4  A: 
Sameer
You just changed from polymorphic archive to the traditional template archive. It works, but with caveats. Dumpbin /exports on you exe to see how much bloat it gives you on Windows. It's truly stunning.
kizzx2
+2  A: 

Just a few comments...

First, you can use the same operation to serialize and deserialize using a templated version:

template<class Archive>
void load(Archive & ar, const unsigned int version)
{
   ...
}

Also, instead of the macro, you can "configure" the archive to expect those types recognized as pointers:

ar.register_type(static_cast<your_static_type_here *>(NULL));

Regards,

diego.

Diego Sevilla
A: 

Hi,

Thank you so much on the answer. Just curious to know whether that piece of code is ready to compile? Because whenever I'm try to compile the source code in my Linux box, I get this error:

expected constructor, destructor, or type conversion before ';' token

Do you have any idea on how this going to solve?

THanks @!

huahsin68