开发者

Length of boost::iostream::basic_array_sink/source

开发者 https://www.devze.com 2023-01-05 21:26 出处:网络
I\'m using to do some serialization stuff \"a开发者_JS百科s it can be seen here\". That worked fine, but I couldn\'t figure how to get the size of the written buffer.I\'ve searched on boost documentat

I'm using to do some serialization stuff "a开发者_JS百科s it can be seen here". That worked fine, but I couldn't figure how to get the size of the written buffer.I've searched on boost documentation and apparently there is no way to do this aside of building a sink/source by myself?

Thanks


boost::iostreams::basic_array_sink models a SinkDevice only, which gives you write-only semantics and no way of telling how many bytes have been written.

OTOH, its sibling boost::iostreams::basic_array models a SeekableDevice allowing to utilize the seek() member function of your stream:

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;


Interestingly enough, I just tried the solution hkaiser proposed and instead of getting number of bytes written, I got number of bytes in the initial array (i.e. seeking to the end went ALL the way to the end of the buffer).

I had to slightly tweak that call to be:

(int)boost::iostreams::seek( s, 0, std::ios_base::cur )

Maybe they changed something in the boost library that made it behave differently. I think we are using latest and greatest as of 20 Jan 2011.

0

精彩评论

暂无评论...
验证码 换一张
取 消