views:

107

answers:

2

I'm using C++Builder, and am trying to slowly migrate code to using C++ standard library in preference to the Delphi VCL.

The VCL has a streaming architecture based around the TStream class, and I'm switching to using std::stream instead. However, in the short term, I still need a way of 'mixing' use of the two stream types.

I can do this using intermediate std::stringstream/TStringStream objects, but this seems a little inefficient and cumbersome. Does anyone have a better suggestion?

Edit:

TStream provides similar functionality to std::streams, but is not derived from it. You can create different kinds of streams (TFileStream, TMemoryStream, TStringStream) and read/write data to/from them. See the Embarcadero docwiki TStream reference.

Edit:

Example - Imagine I have a std::ostream that I have written some stuff to, and I now want to append a JPEG Image to it using TJPEGImage.SaveToStream(str : TStream). And, I'll want to read it from a std::istream later...

+1  A: 

Maybe you can write an adapter/proxy class similar to the VCL TStreamAdapter which implements an IStream interface for a TStream.

Ulrich Gerhardt
A: 

Well, I don't know too much about C++, but I do know how to mix two incompatible classes with similar functionality, and that's with a wrapper class. It looks to me like the base stream classes in the C++ hierarchy are abstract classes that define methods but leave it to the descendants to implement them in different ways. So create a class that descends from iostream (most Delphi streams are two-way) and takes a TStream object in its constructor, and then implements the iostream methods by calling the equivalent methods on the internal TStream object.

Mason Wheeler