C ++装饰basic_stream :: underflow()
|
我想通过使用装饰器模式来扩展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;
};
基本目标(作为第一步)将是简单地将每个功能转发到装饰对象。因此,即使有指向它的基类的指针,也应该可以使用该装饰器。
一切对于编写方法都可以正常工作,但是我不知道如何处理underflow()函数,该函数从uflow()和sgetc()调用。
没有找到相关结果
已邀请:
2 个回复
屠创氓读叔
然后,您真正的实现会添加任何所需的“装饰”,例如
然后在输出流上使用它(伪代码,为清楚起见省略了模板定义)
脖呐
我们无法直接在装饰对象上调用“ 7”,因为它不可访问。 另外,我们不想获取自己的gptr(),而是想要装饰的。 但是,我们可以通过调用
和
间接实现此目的: