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

标签 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);”。 谢谢大家!

最佳答案

您可能想查看 boost.interprocess bufferstream :

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

关于c++ - 使用内存映射文件进行序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4234165/

相关文章:

csv - 使用 Cassava 在内存中加载 CSV

c++ - 清理 RAM(也许是 win32 api)?

python - 为什么覆盖 resetful JSONWebTokenSerializer 只保持返回 token ? Python

c++ - 什么是 RPC 回调线程?

c++ - 在特定时间后调用函数的推荐或最精确的方法是什么?

c++ - 控制台应用程序在 Visual Studio 之外无法正常运行

ios - UIGraphicsGetImageFromCurrentImageContext 内存泄漏与预览

c++ - 根据处理速度每 x 秒循环一次

c++ - 如何序列化类型 boost::labeled_graph

gwt - 如何申请SerializationStreamWriter进行存储