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.
我可以使用中间std::stringstream/TStringStream
对象来做到这一点,但这似乎有点低效且麻烦。有人有更好的建议吗?
编辑:
TStream 提供与 std::streams 类似的功能,但不是从中派生的。您可以创建不同类型的流(TFileStream、TMemoryStream、TStringStream)并从它们读取/写入数据。请参阅Embarcadero docwiki TStream 参考。
编辑:
示例 - 假设我有一个 std::ostream,我已经向其中写入了一些内容,现在我想使用 TJPEGImage.SaveToStream(str : TStream) 向其中附加一个 JPEG 图像。而且,我稍后想从 std::istream 读取它......
也许您可以编写一个类似于 VCL TStreamAdapter 的适配器/代理类,它为 TStream 实现了 IStream 接口。
好吧,我对 C++ 不太了解,但我确实知道如何将两个不兼容的类与类似的功能混合在一起,那就是使用包装类。在我看来,C++ 层次结构中的基本流类是定义方法的抽象类,但将其留给后代以不同的方式实现它们。因此,创建一个派生类iostream
(大多数 Delphi 流是双向的)并在其构造函数中采用 TStream 对象,然后iostream
通过调用内部TStream
对象上的等效方法来实现方法。