首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >C++装饰basic_stream::underflow()

C++装饰basic_stream::underflow()
EN

Stack Overflow用户
提问于 2011-06-23 19:44:44
回答 2查看 541关注 0票数 1

我想通过使用装饰器模式来扩展basic_streambuf对象的行为。这就是我目前得到的:

代码语言:javascript
复制
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()调用的。

EN

回答 2

Stack Overflow用户

发布于 2011-06-23 22:32:36

我可能遗漏了一些东西,但你的设计对我来说没有多大意义。

在我看来,扩展streambuf是通过覆盖受保护的虚拟接口来完成的,如果你想使用装饰器模式,你的装饰器基类就可以做到这一点。

代码语言:javascript
复制
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.
};

然后,您的实际实现将添加任何所需的“修饰”。

代码语言:javascript
复制
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.
};

然后在输出流上使用它(伪代码,为清楚起见,省略了模板定义)

代码语言:javascript
复制
ostream outputstream;
// ....
add_timestamp_decorator decorator = new add_timestamp_decorator(outputstream.rdbuf());
outputstream.rdbuf(decorator);

outputstream << "some lines\ntimestamps will be inserted\n\n";
票数 3
EN

Stack Overflow用户

发布于 2011-06-23 23:15:54

本规范对uflow()的描述如下(27.6.3.4.3.15-17):

underflow要求:除了将结果字符从挂起序列传输到备份序列之外,其他约束与

  • ()相同,并且在调用underflow()行为之前,挂起序列不能为空。如果underflow()返回特征::eof(),则返回特征::eof()。否则,返回特征(*gptr())的值并递增输入sequence.
  • Returns: traits::to_int_type::eof()的下一个指针的值,以指示失败。

在C++中,此外观将如下所示:

代码语言:javascript
复制
if (traits::eq_int_type(this->underflow(), traits::eof())
    return traits::eof();    
return *fetch_gptr_and_inc();

我们不能直接在修饰对象上调用underflow(),因为它是不可访问的。此外,我们不想获取我们自己的gptr(),而是获取经过修饰的gptr()。但是,我们可以通过调用sgetc()sbumpc间接实现这一点

代码语言:javascript
复制
template<typename char_type, class traits_type>
typename forwarding_basic_streambuf<char_type, traits_type>::int_type
forwarding_basic_streambuf<char_type, traits_type>::uflow()
{   
    if (traits_type::eq_int_type(this->m_fwd->sgetc(), traits_type::eof()))
        return traits_type::eof();
    return this->m_fwd->sbumpc();
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6453472

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档