[英]serialization using a memory mapped file
我想在內存映射文件上序列化我的類對象,但事實證明,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 :
bufferstream類為iostream接口提供了在固定大小的內存緩沖區中直接格式化的iostream接口,可防止緩沖區溢出。
五年后,
#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;
等等!
存檔類僅適用於現有流,因此您需要一個能夠讀取/寫入映射的內存區域的流。 我不知道有任何現成的解決方案,因此您可能只需要編寫自己的streambuf
類即可。 一旦有了這個,就很容易將它附加到標准istream
:
std::istream is(your_streambuf);
boost::archive::text_iarchive ia(is);
...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.