如何混合 std::stream 和 Delphi TStream?
我正在使用 C++Builder,并尝试慢慢地将代码迁移到使用 C++ 标准库,而不是使用 Delphi VCL。
VCL 有一个基于 TStream
类的流式架构,我改用 std::stream
来代替。然而,从短期来看,我仍然需要一种“混合”使用两种流类型的方法。
我可以使用中间 std::stringstream/TStringStream
对象来完成此操作,但这似乎有点低效且麻烦。有人有更好的建议吗?
编辑:
TStream 提供与 std::streams 类似的功能,但并非从中派生。您可以创建不同类型的流(TFileStream、TMemoryStream、TStringStream)并从它们读取/写入数据。请参阅Embarcadero docwiki TStream 参考。
编辑:
示例 - 假设我有一个 std::ostream,我已经向其中写入了一些内容,现在我想使用 TJPEGImage.SaveToStream(str : TStream) 向其中附加一个 JPEG 图像。而且,我稍后想从 std::istream 读取它......
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...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
也许您可以编写一个类似于 VCL TStreamAdapter 的适配器/代理类,它为 TStream 实现了 IStream 接口。
Maybe you can write an adapter/proxy class similar to the VCL TStreamAdapter which implements an IStream interface for a TStream.
好吧,我对 C++ 不太了解,但我确实知道如何将两个不兼容的类与类似的功能混合在一起,那就是使用包装类。在我看来,C++ 层次结构中的基本流类是定义方法的抽象类,但将其留给后代以不同的方式实现它们。因此,创建一个从 iostream 派生的类(大多数 Delphi 流都是双向的),并在其构造函数中采用 TStream 对象,然后通过调用等效的方法来实现 iostream 方法内部
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 theiostream
methods by calling the equivalent methods on the internalTStream
object.