- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想使用装饰器模式扩展 basic_streambuf 对象的行为。这就是我目前得到的:
template<typename char_type, class traits_type>
class forwarding_basic_streambuf
: boost::noncopyable,
public std::basic_streambuf<char_type, traits_type>
{
public:
typedef std::basic_streambuf<char_type, traits_type> forwarded_type;
forwarding_basic_streambuf(forwarded_type& fwd_buf)
: m_fwd(&fwd_buf) { }
virtual ~forwarding_basic_streambuf() { }
// locales:
// std::locale pubimbue(std::locale const& loc);
// => Calls: imbue(loc) | Returns: Previous value of getloc();
// std::locale getloc () const;
// => Returns: If pubimbue() has ever been called, then the last value of loc supplied, otherwise the
// current global locale, locale(), in effect at the time of construction. If called after
// pubimbue() has been called but before pubimbue has returned (i.e., from within the call
// of imbue()) then it returns the previous value.
// buffer management and positioning:
// forwarded_type* pubsetbuf (char_type* s, std::streamsize n); => Returns: setbuf(s, n)
// pos_type pubseekoff(off_type off, std::ios_base::seekdir way,
// std::ios_base::openmode which = std::ios_base::in | std::ios_base::out);
// => Returns seekoff(off, way, which)
// pos_type pubseekpos(pos_type sp,
// std::ios_base::openmode which = std::ios_base::in | std::ios_base::out);
// => Returns: seekpos(sp, which)
// int pubsync (); => Returns: sync()
// get and put areas:
// get area:
// std::streamsize sgetn (char_type* s, std::streamsize n); => Returns: xsgetn(s, n)
// put area:
// std::streamsize sputn(char_type const* s, std::streamsize n); => Returns: xsputn(s, n)
protected:
// virtual functions:
// locales:
virtual void imbue(std::locale const& loc) { this->m_fwd->pubimbue(loc); }
// buffer management and positioning:
virtual forwarded_type* setbuf (char_type* s, std::streamsize n)
{ return this->m_fwd->pubsetbuf(s, n); }
virtual pos_type seekoff(off_type off, std::ios_base::seekdir way,
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
{ return this->m_fwd->pubseekoff(off, way); }
virtual pos_type seekpos(pos_type sp,
std::ios_base::openmode which = std::ios_base::in | std::ios_base::out)
{ return this->m_fwd->pubseekpos(sp, which); }
virtual int sync ()
{ return this->m_fwd->pubsync(); }
// get and put areas:
// get area:
virtual std::streamsize xsgetn(char_type* s, std::streamsize n)
{ return this->m_fwd->sgetn(s, n); }
virtual int_type uflow()
{
if (traits_type::eq_int_type(this->underflow(), traits_type::eof()))
return traits_type::eof();
return this->m_fwd->sgetc();
}
// put area:
virtual std::streamsize xsputn (char_type const* s, std::streamsize n)
{ return this->m_fwd->sputn(s, n); }
virtual int_type overflow(int_type c = traits_type::eof())
{
if (traits_type::eq_int_type(c, traits_type::eof()))
return traits_type::not_eof(c);
return this->m_fwd->sputc(traits_type::to_char_type(c));
}
private:
forwarded_type* m_fwd;
};
基本目标(作为第一步)是将每个功能简单地转发给装饰对象。因此,即使有一个指向它的基类的指针,也应该可以使用这个装饰器。
对于编写方法来说一切正常,但我不知道如何处理从 uflow() 和 sgetc() 调用的 underflow() 函数。
最佳答案
我可能遗漏了一些东西,但您的设计对我来说意义不大。
在我看来,扩展 streambuf
是通过覆盖 protected 虚拟接口(interface)来完成的,如果您想使用装饰器模式,您的装饰器基类就可以做到这一点。
template<typename char_type, class traits_type>
class forwarding_basic_streambuf
: boost::noncopyable,
public std::basic_streambuf<char_type, traits_type>
{
public:
typedef std::basic_streambuf<char_type, traits_type> forwarded_type;
forwarding_basic_streambuf(forwarded_type& fwd_buf)
: m_fwd(&fwd_buf) { }
virtual ~forwarding_basic_streambuf() { }
protected:
virtual streamsize xsputn ( const char * s, streamsize n ) {
m_fwd->xsputn(s,n);
}
virtual int overflow ( int c) {
m_fwd->overflow(c);
}
// etc.
};
然后您的实际实现将添加任何需要的“装饰”,例如
template<typename char_type, class traits_type>
class add_timestamp_decorator
: public forwarding_basic_streambuf<char_type, traits_type>
{
public:
typedef std::forwarding_basic_streambuf<char_type, traits_type> base_type;
add_timestamp_decorator(base_type::forwarded_type& fwd_buf)
: base_type(&fwd_buf) { }
virtual ~add_timestamp_decorator() { }
protected:
virtual streamsize xsputn ( const char * s, streamsize n ) {
// detect and remember when a newline is written
// before the next char is written output the timestamp
base_type::xsputn(s, n);
}
// etc.
};
然后在输出流上使用它(伪代码,为清楚起见省略了模板定义)
ostream outputstream;
// ....
add_timestamp_decorator decorator = new add_timestamp_decorator(outputstream.rdbuf());
outputstream.rdbuf(decorator);
outputstream << "some lines\ntimestamps will be inserted\n\n";
关于C++ 修饰 basic_stream::underflow(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6453472/
为什么我会收到错误“EXEC_BAD_INSTRUCTION”: let r = Double(arc4random_uniform(181) - 90) 虽然这工作正常: let r = Doubl
我想使用装饰器模式扩展 basic_streambuf 对象的行为。这就是我目前得到的: template class forwarding_basic_streambuf : boost::
尝试将 double 插入 Oracle 表的 DOUBLE PRECISION 列时,当 double 超出范围(在我的例子中:太小)时出现异常,但仅当使用 PreparedStatement(如果
我试图定义一个简单的指数,无论我在指数参数中输入的数字如何,我都会得到以下结果: FloatingPointError: underflow encountered in exp 我试图定义的数组如下
我有一个大小为 100 万的随机序列,为了使用 NIST test suite ,我将Windows txt格式存储的序列转换为BCD,显然文件的长度变成了400万。因此,我运行测试并将比特流设置为
我有一个从弱到强的NSMapTable。当我尝试调用 removeObjectForKey:method 时,我的应用程序崩溃了。 代码如下:self.requestedObjectMap是一个从弱到
我编写了一段 Java 代码,用于通过 Socket 将 PDF 转换的 postscript 脚本发送到网络打印机。 这些文件以完美的形状打印,但每个作业都附带一到两页额外的页面,其中包含诸如 ps
我使用 Kryo 将对象写入字节数组。它工作正常。但是当字节数组转换为对象时,它会抛出 com.esotericsoftware.kryo.KryoException: Buffer underflo
我的 WPF 测试应用程序(非常简单,只有一个窗口)正在使用第 3 方托管的 dll(比如 X.dll)。此托管 dll 使用一些非托管 dll。因此,假设我编写了一个仅引用 X.dll 的小型 wp
为什么下面的代码会抛出异常?请注意,该文件是一个 /proc/pid/stat 文件,因此它可能会受到内核的干扰。 // Checked that file does exist try { st
我正在为我的项目使用 rippleeffect 库。但是在 Android Nougat 和 Marshmallow 中,应用程序由于这个库而崩溃: 编译'com.github.traex.rippl
我公司的产品运行在许多合格的 Linux 硬件/软件配置上。从历史上看,使用的编译器是 GNU C++。出于本文的目的,让我们将版本 3.2.3 视为基线,因为我们的软件在该版本中“按预期工作”。 随
问题的标题是错误本身,但我也会在下面包含它: error: argument of type ‘char (CharStack::)()const throw (CharStack::Underflo
我是一名优秀的程序员,十分优秀!