Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Length of boost::iostream::basic_array_sink/source

Tags:

c++

boost

I'm using to do some serialization stuff "as 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

like image 550
scooterman Avatar asked Apr 15 '26 21:04

scooterman


2 Answers

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.

like image 129
DXM Avatar answered Apr 17 '26 12:04

DXM


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;
like image 20
hkaiser Avatar answered Apr 17 '26 10:04

hkaiser



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!