C++ BinaryBuffer?

Started by
2 comments, last by D_Linden 16 years, 7 months ago
Is there such an object / stream in C++ that allows you to create a BinaryStream like object so that one could (like a file stream with ios::binary mode): data_buf << someobj << someotherobj << somebasicdata << floatdata; ? Someone mentioned to me about the std::stringstream, but does that have a binary mode or something along similar lines, if so how does one set it? Regards
Advertisement
Quote:Original post by Pickl3d
Is there such an object / stream in C++ that allows you to create a BinaryStream like object so that one could (like a file stream with ios::binary mode):

data_buf << someobj << someotherobj << somebasicdata << floatdata; ?
I'm fairly certain that there is nothing of that sort in the C++ standard library.

You might, however, find this to be of interest, as it comes very close to what you describe above.

[Edit: A couple of other quick notes. AFAIK, the only effect that the ios::binary flag has is to ensure that data is read and written byte-for-byte, with no text-related alterations (such as those involving 'newlines') made. Some SC++L stream classes do support binary reading and writing via the read() and write() functions, but these are fairly crude and certainly don't offer the sort of 'smart' serialization that your example alludes to.]
Not in the standard library, since the << are for formatted output.

However, you can roll your own fairly easily. I've used these before:

class BinaryStream{private:    std::ofstream os;public:    BinaryStream(){ /* open the file etc in binary mode */ }    BinaryStream &operator<<(int I){ os.write(reinterpret_cast<char*>(&I),sizeof(int)); return *this; }    // etc, I guess you could use a member template here instead};class Mine{public:    int X,Y;};BinaryStream &operator<<(BinaryStream &B,const Mine &m){ return B << m.X << m.Y; }


There are probably some pretty good reasons people will suggest for not doing this, but I've used this approach for convinently loading to and from binary files in the past.

[EDIT] May have missed the point of your question - to send user-defined types to a stream like the one above would involve writing specific overloads as for the Mine class above, either as member functions of the stream class or non-member functions.
I've created a simple but useful object that does something similar to what you want to achieve. As the previous author said you need to overload the operator for each type if you want to use the "<<" operator.

My class uses templates as shown below:
	ByteStream stream;	Foo FooObject;	stream.Write<Foo>(FooObject);	Foo FooObject2;	FooObject2 = stream.Read<Foo>().Get();


You can download the source files here (I hope):
http://www.mediafire.com/?3tvl12mgtzd

Just a note: I wouldn't trust the multithread lock on applications that need high performance.

Regards
Daniel

EDIT: Just noticed that a line of code had gone missing in ByteStream.hpp, it must have disappeared when I cleaned the file of deprecated functions. Just add:
#endif
to the end of the file and it will happily compile.

[Edited by - D_Linden on September 18, 2007 2:06:06 PM]

This topic is closed to new replies.

Advertisement