开发者

How to mix std::stream and Delphi TStream?

开发者 https://www.devze.com 2023-01-06 20:54 出处:网络
I\'m using C++Builder, and am trying to slowly migrate code to using C++ standard library in preference to the Delphi VCL.

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 st开发者_运维问答uff 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...


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


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.

0

精彩评论

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