使用内存映射文件进行序列化

时间:2010-11-20 18:03:44

标签: c++ memory serialization boost mapping

我想在内存映射文件上序列化我的类对象,但结果是boost序列化仅适用于文件流。这是一个例子:

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

是否可以通过内存映射文件完成。我正在使用Windows API CreateFileMapping和MapViewOfFile进行内存映射。

编辑:

这是我尝试使用boost iostream库和内存映射文件。

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

现在我对boost不太熟悉,但是这段代码在运行时失败了。所以,任何帮助都非常感谢。失败发生在这里“out_streambuf obuf(params);”。 谢谢你们!

3 个答案:

答案 0 :(得分:3)

您可能需要查看boost.interprocess bufferstream

  

bufferstream类提供   iostream界面直接   格式化固定大小的内存   具有缓冲保护的缓冲区   溢出。

答案 1 :(得分:1)

归档类只适用于现有流,因此您需要一个能够读取/写入映射内存区域的流。我不知道有任何现成的解决方案,因此您可能只需要编写自己的streambuf类来执行此操作。完成此操作后,将其附加到标准istream

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

答案 2 :(得分:1)

五年后,

SELECT * FROM posts WHERE pid='$user1' OR pid='$user2' order by id desc limit 50

Etvoilà!