简体   繁体   中英

serialization using a memory mapped file

I want to serialize my class objects on a memory mapped file but turns out boost serialization only works with file streams. Here is an example:

class gps_position
{
private:
    friend class boost::serialization::access;
    // When the class Archive corresponds to an output archive, the
    // & operator is defined similar to <<.  Likewise, when the class Archive
    // is a type of input archive the & operator is defined similar to >>.
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
        ar & degrees;
        ar & minutes;
        ar & seconds;
    }
    int degrees;
    int minutes;
    float seconds;
public:
    gps_position(){};
    gps_position(int d, int m, float s) :
        degrees(d), minutes(m), seconds(s)
    {}
};

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

    // 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
    }

    // ... some time later restore the class instance to its orginal state
    gps_position newg;
    {
        // create and open an archive for input
        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;
}

Is there a away it can be done over memory mapped files. I am using windows API CreateFileMapping and MapViewOfFile for memory mapping.

edit:

This is what I tried to do using boost iostream library and memory mapped files.

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");  /*commented this */

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

    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
    }

    // ... 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);
    std::istream ifs(&ibuf);

        //std::ifstream ifs("filename");  /* commented this */

        boost::archive::text_iarchive ia(ifs);

        // read class state from archive

        ia >> newg;
        // archive and stream closed when destructors are called
    }
    return 0;
}

Now I am not too familiar with boost but this code fails at run time. So, any help is really appreciated. The failure happens here itself "out_streambuf obuf(params);". Thanks guys!

You might want to look into boost.interprocess bufferstream :

The bufferstream classes offer iostream interface with direct formatting in a fixed size memory buffer with protection against buffer overflows.

Five years later,

#include <boost/interprocess/shared_memory_object.hpp>
#include <boost/interprocess/mapped_region.hpp>
#include <boost/interprocess/streams/bufferstream.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
using namespace boost::interprocess;
....
_shm = new shared_memory_object(open_or_create,shm_name,read_write);
_shm->truncate(shm_size);
_reg = new mapped_region( _shm,read_write);
...
// write the serializable structure
obufferstream bs(static_cast<char*>(region.get_address()),_reg->get_size());
boost::archive::binary_oarchive arch(dynamic_cast<ostream&>(bs));
arch << my_struct;
...
// read the serializable structure
ibufferstream bs(static_cast<char*>(_reg->get_address()),_reg->get_size());
boost::archive::binary_oarchive arch(dynamic_cast<istream&>(bs));
arch >> my_struct;

Et voilà !

The archive class simply works on an existing stream, so you need a stream that is capable of reading from / writing to the mapped memory area. I'm not aware of any ready-made solution for this, so you may just have to write your own streambuf class that does this. Once you have this, it's simple to attach it to a standard istream :

std::istream is(your_streambuf);
boost::archive::text_iarchive ia(is);
...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM