问题描述
我一直在使用 Boost 序列化库,它实际上非常好,让我可以制作简单的包装器来将我的可序列化对象保存为字符串,如下所示:
I've been using the Boost serialization library, which is actually pretty nice, and lets me make simple wrappers to save my serializable objects to strings, like so:
namespace bar = boost::archive;
namespace bio = boost::iostreams;
template <class T> inline std::string saveString(const T & o) {
std::ostringstream oss;
bar::binary_oarchive oa(oss);
oa << o;
return oss.str();
}
template <class T> inline void saveFile(const T & o, const char* fname) {
std::ofstream ofs(fname, std::ios::out|std::ios::binary|std::ios::trunc);
bar::binary_oarchive oa(ofs);
oa << o;
}
template <class T> inline void loadFile(T & o, const char* fname) {
std::ifstream ifs(fname, std::ios::in|std::ios::binary);
assert(ifs.good()); // XXX catch if file not found
bar::binary_iarchive ia(ifs);
ia >> o;
}
问题是,我刚刚发现也需要压缩我的序列化数据,所以我正在考虑使用 boost::iostreams 中的过滤器来做到这一点.我想出了如何使用文件成功做到这一点:
The thing is, I just found the need to compress my serialized data, too, so I'm looking at doing that with the filters in boost::iostreams. I figured out how to do it successfully with files:
template <class T> inline void saveGZFile(const T & o, const char* fname) {
std::ofstream ofs(fname, std::ios::out|std::ios::binary|std::ios::trunc);
bio::filtering_streambuf<bio::output> out;
out.push(boost::iostreams::gzip_compressor());
out.push(ofs);
bar::binary_oarchive oa(out);
oa << o;
}
template <class T> inline void loadGZFile(T & o, const char* fname) {
std::ifstream ifs(fname, std::ios::in|std::ios::binary);
assert(ifs.good()); // XXX catch if file not found
bio::filtering_streambuf<bio::input> in;
in.push(bio::gzip_decompressor());
in.push(ifs);
bar::binary_iarchive ia(in);
ia >> o;
}
但不知道如何正确保存到压缩字符串.问题是我没有刷新过滤器链,但我尝试过弹出和同步,但似乎没有任何效果.这是我损坏的代码:
But can't figure out how to save correctly to a compressed string. The problem is that I'm not flushing the chain of filters, but I've tried popping and syncing and nothing seems to work. Here's my broken code:
template <class T> inline std::string saveGZString(const T & o) {
std::ostringstream oss;
bio::filtering_streambuf<bio::output> out;
out.push(bio::gzip_compressor());
out.push(oss);
bar::binary_oarchive oa(out);
oa << o;
// XXX out.pop() twice? out.strict_sync()?? oss.flush()??
return oss.str();
}
因此,某些数据会卡在流缓冲区的某处,当我知道它应该是 43K 左右时,我总是会得到一些完整的压缩数据块(16K 或 32K),给定(有效)输出我从使用我的 saveGZFile 方法中获取.显然,连接 ofstream 会正确关闭并刷新,但连接 ostringstream 却没有.
As a result some data gets stuck in the stream buffer somewhere, and I always end up with a a few complete blocks (16K or 32K) of compressed data when I know it should be 43K or so given the (valid) output I get from using my saveGZFile method. Apparently hooking up the ofstream closes and flushes properly, but hooking up the ostringstream doesn't.
有什么帮助吗?(这是我的第一个 stackoverflow 问题——帮帮我,伙计们,你们是我唯一的希望!)
Any help? (This is my first stackoverflow question — help me, guys, you're my only hope!)
推荐答案
回到这个问题,我意识到我必须在去年某个时候修复它(因为我现在正在使用 saveGZString).挖掘看看我是如何修复它的,这很愚蠢/简单:
Returning to this question, I realized I must've fixed it sometime last year (as I'm using saveGZString right now). Digging to see how I fixed it, it was pretty silly/simple:
namespace bar = boost::archive;
namespace bio = boost::iostreams;
template <typename T> inline std::string saveGZString(const T & o) {
std::ostringstream oss;
{
bio::filtering_stream<bio::output> f;
f.push(bio::gzip_compressor());
f.push(oss);
bar::binary_oarchive oa(f);
oa << o;
} // gzip_compressor flushes when f goes out of scope
return oss.str();
}
只要让整个链条超出范围就行了!整洁的!为了完整起见,这是我的加载器:
Just let the whole chain go out of scope and it works! Neat! Here's my loader for completeness:
template <typename T> inline void loadGZString(T & o, const std::string& s) {
std::istringstream iss(s);
bio::filtering_stream<bio::input> f;
f.push(bio::gzip_decompressor());
f.push(iss);
bar::binary_iarchive ia(f);
ia >> o;
}
这篇关于如何连接 Boost 序列化 &iostreams 序列化 &gzip 一个对象到字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!