直接升压序列化字符数组 [英] Direct boost serialization to char array
问题描述
升压序列化的文档的断言,这样序列化/反序列化项目使用与底层结构的流二进制/文本存档。如果我wan't使用序列化的数据作为一个的std :: string这工作得很好,但我的本意是将其直接转换为char *缓冲区。我怎样才能做到这一点,而无需创建一个临时字符串?
解决了!对于那些想要一个例子中的:
字符缓冲区[4096];提高::输入输出流:: basic_array_sink<&烧焦GT; SR(缓冲,BUFFER_SIZE);
提高::输入输出流::流<提高::输入输出流:: basic_array_sink<&烧焦GT; >源(SR);提高::档案:: binary_oarchive OA(源);OA<< serializable_object;
IIUC,你想写信给固定大小的preallocated阵列。
您可以使用<一个href=\"http://www.boost.org/doc/libs/1_43_0/libs/iostreams/doc/index.html?path=4.2.1.3\">boost::iostreams::array_sink (包裹着<一个href=\"http://www.boost.org/doc/libs/1_43_0/libs/iostreams/doc/index.html?page=http://www.boost.org/doc/libs/1_43_0/libs/iostreams/doc/guide/generic_streams.html%23stream\">stream给它一个的std :: ostream的接口)为。
Boost serialization doc's assert that the way to serialize/deserialize items is using a binary/text archive with a stream on the underlying structure. This works fine if I wan't to use the serialized data as an std::string, but my intention is to convert it directly to a char* buffer. How can I achieve this without creating a temporary string?
Solved! For the ones that wanted a example:
char buffer[4096];
boost::iostreams::basic_array_sink<char> sr(buffer, buffer_size);
boost::iostreams::stream< boost::iostreams::basic_array_sink<char> > source(sr);
boost::archive::binary_oarchive oa(source);
oa << serializable_object;
IIUC, you would like to write to a preallocated array of fixed size.
You could use a boost::iostreams::array_sink (wrapped with stream to give it an std::ostream interface) for that.
这篇关于直接升压序列化字符数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!