Boost logo

Boost Users :

From: Peter Dimov (pdimov_at_[hidden])
Date: 2007-12-11 19:29:00


Darryl Lawson:

>I realise that what I am doing is no doubt an unconventional usage of
> boost::serialization, but I wonder if any one has done a similar thing
> before.
>
> I wish to serialize a derived class through a baseclass pointer, but on
> deserilize I do not want the object to be created (I do not want memory
> allocated) - as the object will have all ready been created.
>
> An example, hopefully showing what I mean, is given at the end.

...

> struct A {
> A() {
> _m = new derived;
> cout << "construct A, _m = " << (unsigned long)_m << endl;
> }
>
> template<class Archive>
> void serialize(Archive& ar, const unsigned int version) {
> // On deserialize this will create a new object, but I do not
> want it to,
> // rather I would like it to deserialize into the all ready created
> // object.
> ar & _m;
> cout << "serialise A, _m = " << (unsigned long)_m << endl;
> }
>
> base* _m;
> };
>
> void save(const A& a, const char* filename) {
> std::ofstream ofs(filename);
> boost::archive::text_oarchive oa(ofs);
> oa << a;
> }
>
> void load(A& a, const char* filename) {
> std::ifstream ifs(filename);
> boost::archive::text_iarchive ia(ifs);
> ia >> a;
> }

You can add

    virtual void load( boost::archive::text_iarchive& ar, unsigned v ) = 0;
    virtual void save( boost::archive::text_oarchive& ar, unsigned v ) = 0;

to base and call them in A::serialize, but this obviously only works for a
specific archive type.


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