这是我想出的解决方案。它不需要实现您自己的流,并允许为每次下一个序列化重用相同的内存块。假设您为序列化安排了以下结构:
boost::iostreams::basic_array<char> sink; // target buffer
boost::iostreams::stream<boost::iostreams::basic_array<char> > os; // stream wrapper around it
boost::archive::binary_oarchive oa; // archive which uses this stream
然后要重用相同的缓冲区,只需重新打开流:
os.close();
os.open(sink);
应该与更改流中的一些内部指针一样快。不过,我还没有测试过实际速度。
试用代码:Writer 序列化传递的指向缓冲区的指针。读取器从同一个缓冲区反序列化指针(读取器和写入器共享同一个缓冲区)
#include <iostream>
#include <fstream>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/iostreams/device/array.hpp>
#include <boost/iostreams/stream.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/access.hpp>
class A;
class Writer {
char *buf;
int len;
boost::iostreams::basic_array<char> sink;
boost::iostreams::stream<boost::iostreams::basic_array<char> > os;
boost::archive::binary_oarchive oa;
public:
Writer(char *_buf, int _len): buf(_buf), len(_len), sink(buf, len), os(sink), oa(os) {}
void write(A* a) {
oa << a;
}
void reset() {
os.close();
os.open(sink);
}
};
class Reader {
char *buf;
int len;
boost::iostreams::basic_array_source<char> src;
boost::iostreams::stream<boost::iostreams::basic_array_source<char> > is;
boost::archive::binary_iarchive ia;
public:
Reader(char *_buf, int _len): buf(_buf), len(_len), src(buf, len), is(src), ia(is) {}
A* read() {
A* a;
ia >> a;
return a;
}
void reset() {
is.close();
is.open(src);
}
};
int main(int argc, char **argv) {
// to memory
char buffer[4096] = {0};
Writer w(buffer, sizeof(buffer));
A *a1 = new A(5);
w.write(a1);
Reader r(buffer, sizeof(buffer));
A *a2 (NULL);
a2 = r.read();
assert(*a1 == *a2);
std::cout << "Simple ok\n";
// test reuse
w.reset();
r.reset();
A *a3 (NULL);
w.write(new A(10));
a3 = r.read();
assert(*a3 == A(10));
std::cout << "Reuse ok\n";
};
class A
{
private:
friend class boost::serialization::access;
int i;
template <typename Archive>
void serialize(Archive& ar, const unsigned int version) {
std::cout << "serialize A\n";
ar & i;
}
public:
A(): i(0) {};
A(int _i): i(_i) {};
virtual bool operator==(const A&r) { return i == r.i; };
virtual ~A() {};
virtual void whoa() {std::cout << "I am A!\n";};
virtual const char* me() { return "A"; };
};