2
votes

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...

2
From the fact that there are >35,000 questions tagged C++, but only about ~200 tagged vcl or c++builder, I draw the conclusion that knowledge of TStream is rare here. So you might want to improve your answer by adding some information about TStream's interface. - sbi
What do you mean by "mixing"? Write one stream with one libary and read with the other? Use both kind of streams at the same time? An example would be welcome. - user160694

2 Answers

3
votes

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

1
votes

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.