Boost logo

Boost Users :

Subject: [Boost-users] serialization over memory mapped files
From: Sadhan Sood (sadhan.sood_at_[hidden])
Date: 2010-11-21 10:18:05


Hi ,

I want to serialize my class objects ona memory mapped file. I am able to
write to the file but reading is throwing me exceptions:

namespace io = boost::iostreams;
typedef io::stream_buffer<io::mapped_file_source> in_streambuf;
typedef io::stream_buffer<io::mapped_file_sink> out_streambuf;

int main() {
    // create and open a character archive for output
    // std::ofstream ofs("filename");

boost::iostreams::mapped_file_params params;
        params.path = "filepath";
params.flags = io::mapped_file::mapmode::readwrite;
params.new_file_size = 10240;

out_streambuf obuf(params);
std::ostream ofs(&obuf);

    // create class instance
    const gps_position g(35, 59, 24.567f);

    // save data to archive
    {
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        oa << g;
     // archive and stream closed when destructors are called
    }

/* ready for reading */
params.flags = io::mapped_file::mapmode::readonly;
params.new_file_size = 0;

    // ... some time later restore the class instance to its orginal state
    gps_position newg;
    {
        // create and open an archive for input
in_streambuf ibuf(params); /* this is throwing me exceptions */
 std::istream ifs(&ibuf);
        //std::ifstream ifs("filename");
        boost::archive::text_iarchive ia(ifs);
        // read class state from archive
        ia >> newg;
        // archive and stream closed when destructors are called
    }
    return 0;
}

Any help is appreciated.
Thanks,
Sadhan



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