如何基于basic_streambuf构建filtered_streambuf?

发布于 2024-08-29 06:30:11 字数 549 浏览 3 评论 0原文

我有一个项目,要求我将过滤器插入流中,以便根据过滤器修改传出数据。经过一番研究,我似乎想要做的是创建一个像这样的filtered_streambuf:

template <class StreamBuf>
class filtered_streambuf: public StreamBuf
{ ... }

然后将filtered_streambuf<>插入到我需要过滤的任何流中。我的问题是,我不知道在过滤流时需要维护哪些不变量,以确保

  • 派生类可以按预期工作。特别是,我可能会发现我已经在其他filtered_streambufs上构建了filtered_streambufs。
  • 所有各种流插入器、提取器和操纵器都按预期工作。

问题是我似乎无法弄清楚我需要提供什么最小接口才能保证 iostream 拥有正常工作所需的功能。

特别是,我是否需要伪造受保护指针变量的移动?我是否需要一个假数据缓冲区?我可以重写公共函数,根据基本streambuf重写它们吗?还是这太简单了?

I have a project that requires me to insert a filter into a stream so that outgoing data will be modified according to the filter. After some research, it seems that what I want to do is create a filtered_streambuf like this:

template <class StreamBuf>
class filtered_streambuf: public StreamBuf
{ ... }

And then insert a filtered_streambuf<> into whichever stream I need to be filtered. My problem is that I don't know what invariants I need to maintain while filtering a stream, in order to ensure that

  • Derived classes can work as expected. In particular, I may find I have filtered_streambufs built over other filtered_streambufs.
  • All the various stream inserters, extractors and manipulators work as expected.

The trouble is that I just can't seem to work out what the minimal interface is that I need to supply in order to guarantee that an iostream will have what it needs to work correctly.

In particular, do I need to fake the movement of the protected pointer variables, or not? Do I need a fake data buffer, or not? Can I just override the public functions, rewriting them in terms of the base streambuf, or is that too simplistic?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

渡你暖光 2024-09-05 06:30:11

Boost.Iostreams 可能对您有用。

从文档中:

Boost.Iostreams 有三个目标:

  • 为了轻松创建标准 C++ 流和流缓冲区
    访问新的源和接收器。
  • 提供一个框架来定义过滤器并将其附加到标准
    流和流缓冲区。
  • 提供一系列即用型过滤器、源和
    水槽。

我自己几乎没有使用过该库,所以我无法进一步发表评论。

Boost.Iostreams may be useful to you.

From the documentation:

Boost.Iostreams has three aims:

  • To make it easy to create standard C++ streams and stream buffers for
    accessing new Sources and Sinks.
  • To provide a framework for defining Filters and attaching them to standard
    streams and stream buffers.
  • To provide a collection of ready-to-use Filters, Sources and
    Sinks.

I've barely used that libary myself, so I can't comment any further.

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