【发布时间】:2011-03-12 23:29:41
【问题描述】:
我用来做一些序列化的东西"as it can be seen here"。效果很好,但我不知道如何获取写入缓冲区的大小。我已经搜索了 boost 文档,显然除了自己构建接收器/源之外没有办法做到这一点?
谢谢
【问题讨论】:
我用来做一些序列化的东西"as it can be seen here"。效果很好,但我不知道如何获取写入缓冲区的大小。我已经搜索了 boost 文档,显然除了自己构建接收器/源之外没有办法做到这一点?
谢谢
【问题讨论】:
boost::iostreams::basic_array_sink 仅对SinkDevice 建模,它为您提供只写语义,并且无法告诉您已写入多少字节。
OTOH,它的兄弟 boost::iostreams::basic_array 模拟了一个 SeekableDevice 允许利用您的流的 seek() 成员函数:
namespace io = boost::iostreams;
char buffer[4096];
io::stream<io::basic_array<char> > source(buffer, buffer_size);
boost::archive::binary_oarchive oa(source);
oa << serializable_object;
// move current stream position to the end, io::seek() returns new position
std::cout << "Bytes written: "
<< io::seek(source, 0, std::ios_base::end)
<< std::endl;
【讨论】:
有趣的是,我只是尝试了 hkaiser 提出的解决方案,而不是获取写入的字节数,而是在初始数组中获得了字节数(即,寻找到最后一直到缓冲区的末尾)。
我不得不稍微调整一下这个调用:
(int)boost::iostreams::seek( s, 0, std::ios_base::cur )
也许他们更改了 boost 库中的某些内容,使其行为不同。我认为我们正在使用截至 2011 年 1 月 20 日的最新和最好的。
【讨论】: