如何混合 std::stream 和 Delphi TStream?

发布于 2024-09-08 18:51:42 字数 711 浏览 1 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

↙厌世 2024-09-15 18:51:42

也许您可以编写一个类似于 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.

泅渡 2024-09-15 18:51:42

好吧,我对 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 the iostream methods by calling the equivalent methods on the internal TStream object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文