Boost logo

Boost Users :

Subject: [Boost-users] Serialization of a complex data structure
From: Marco Meoni - Sbaush (sbaush_at_[hidden])
Date: 2010-04-28 12:06:18


Hi all.

I've to serialize a structure that has as attributes two structures.
An example is:
struct Example{
 Container* results_container;
MyMap this_is_a_map;
        Example(){}
        ...
        ...
};

where Container* is a pointer to another struct, defined by me, and MyMap is
a c++ map.

I've tried to serialize it using the shared_ptr example of boost
serialization:

struct Example{
friend class boost::serialization::access;
        template<class Archive>
        void serialize(Archive & ar, const unsigned int /* file_version */){
}
        Container* results_container;
MyMap this_is_a_map;
        Example(){}
        ...
        ...
};

and in the main i call:

boost::shared_ptr<Example> myExample(new Example);
...
        ...
// serialize it
    {
        std::ofstream ofs(filename.c_str());
        boost::archive::text_oarchive oa(ofs);
        oa << myExample;

    }
 boost::shared_ptr<Example> myNewExample(new Example);
    {
        // open the archive
        std::ifstream ifs(filename.c_str());
        boost::archive::text_iarchive ia(ifs);

        // restore the schedule from the archive
        ia >> myNewExample;

    }

Now myNewExample is a well format instance of Example, but the attributes
are empty. Can anyone help me?
I would not have to change all files with nested structures, as are many,
not just two as in the example

Thanks all.

Marco.

-- 
Marco Meoni - Sbaush
www.marcomeoni.net


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