C++装饰 basic_iostream 类
我想做类似以下代码所示的事情:
class foo
{
private:
std::fstream* m_stream;
public:
foo(std::fstream* stream) : m_stream(stream) { }
foo& write(char const* s, std::streamsize count)
{
if (/*condition*/)
{
m_stream->write(s, count);
}
else
{
// ...
}
return *this;
}
foo& read(char* s, std::streamsize count)
{
if (/*condition*/)
{
m_stream->read(s, count);
}
else
{
// ...
}
return *this;
}
};
我需要向所有类似的方法添加相同的行为(例如 put
)。这不应仅应用于文件流,而应应用于所有其他流类。有没有简单的方法来允许这些功能?
I want to do something like the following code shows:
class foo
{
private:
std::fstream* m_stream;
public:
foo(std::fstream* stream) : m_stream(stream) { }
foo& write(char const* s, std::streamsize count)
{
if (/*condition*/)
{
m_stream->write(s, count);
}
else
{
// ...
}
return *this;
}
foo& read(char* s, std::streamsize count)
{
if (/*condition*/)
{
m_stream->read(s, count);
}
else
{
// ...
}
return *this;
}
};
I would need to add the same behavior to all similar methods (e.g. put
). This shouldn't be applied to file streams only, but all other stream classes. Is there any easy way to allow these functionality?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
许多格式化输出运算符 (
operator<<
) 直接写入底层流缓冲区。为了以一般方式完成此任务,您需要做的是从 std::basic_streambuf 派生一个类,它将所有数据转发到另一个 std::basic_streambuf,然后选择创建一个最小的 std::basic_ostream 实现来使用您的流缓冲更容易。不过,我不会说这特别容易,但这是以可以影响所有流类型的方式做到这一点的唯一方法。
下面是一个最小流缓冲区的示例,它转发到另一个流缓冲区(并执行一些无意义的转换,只是为了演示您可以做什么),以及一个附带的流:
这可以非常简单地使用:
它将输出
Gpp< /代码>。我希望这对您有所帮助。
Many of the formatted output operators (
operator<<
) write directly to the underlying stream buffer. What you need to do in order to accomplish this in a general fashion is derive a class from std::basic_streambuf that forwards all data to another std::basic_streambuf, and then optionally create a minimal std::basic_ostream implementation to make using your stream buffer easier.I wouldn't say this is particularly easy, though, but it's the only way to do this in a way that can affect all stream types.
Here is an example of a minimal stream buffer that forwards to another stream buffer (and performs some meaningless transformation just to demonstrate what you can do), and an accompanying stream:
This can be used very simply:
Which would output
Gpp
. I hope that helps you on your way.像这样的东西吗?
Something like this?
如果我理解正确的话,您想装饰任何 iostream 的方法。因此,只需让您的装饰器采用 iostream 来作为装饰者(而不是 fstream ,它是 iostream 的子类)。
If I understand you correctly, you want to decorate methods of any
iostream
. So just make your decorator take aniostream
as decoratee (as opposed to anfstream
, which is a subclass ofiostream
).作为当前的方法,将指针放在结构内部是危险且容易出错的。相反,只需派生此类
stream
类并实现基本构造函数并包装您的自定义方法,例如write()
。用法:
Having pointer inside a structure as your current approach is dangerous and error prone. Instead just derive such
stream
classes and implement basic constructors and wrap around your custom methods such aswrite()
.Usage:
解决此类问题的通常方法是使用模板。那里
std::istream
或std::ostream 中的函数不是很多吗
<<需要覆盖,并且
和
>>的良好模板成员应该
<<涵盖了很多案例。在大多数情况下我都这样做了
仅提供
或
>>`。 (一般来说,我不需要双向流。)
至于处理其他类型的流,只需使用
std::iostream
即可std::fstream
。 (一般来说,除了打开文件时,您不应该看到
fstream
部分。)The usual solution for this sort of problem is to use templates. There
aren't that many functions in an
std::istream
or andstd::ostream
<<which need covering, and a good template member for
and
>>should
<<cover a lot of the cases. In most of the cases I've done this, I've
only offerred
or
>>`. (Generally speaking, I've not neededbidirectional streams.)
As for handling other types of streams, just use
std::iostream
insteadof
std::fstream
. (In general, except when opening files, youshouldn't see the
fstream
part.)