首页 | 编程语言 | 网站建设 | 游戏天堂 | 冲浪宝典 | 网络安全 | 操作系统 | 软件时空 | 硬件指南 | 病毒相关 | IT 认证
软讯网络 > 编程语言 > C/C++ > C++的流basic_streambuf
【标  题】:C++的流basic_streambuf
【关键字】:C++,basic_streambuf
【来  源】:http://blog.csdn.net/linweixuan/archive/2006/09/09/1197233.aspx

C++的流basic_streambuf

 
不是个抽象类,但是他是C++流的基类.它提供了基本的缓冲区管理.它包含六个缓存区指针,三个分别是读指针(开头,结尾,当前),另外三个是写指针(开头,结尾,当前)
 
    读开始指针                            当前读指针                                    读结尾指针
_M_gbegin                          _M_gnext                      _M_gend
         
         =========================================================    
              ^                                                      ^                                              ^
       _M_pbegin                          _M_pnext                      _M_pend
           写开始指针                            当前写指针                           写结尾指针
 
template <class_CharT, class_Traits>
classbasic_streambuf
{
 friendclassbasic_istream<_CharT, _Traits>;
 friendclassbasic_ostream<_CharT, _Traits>;
public:                         // Typedefs.
 typedef_CharT                   char_type;
 typedeftypename_Traits::int_type        int_type;
 typedeftypename_Traits::pos_type     pos_type;
 typedeftypename_Traits::off_type       off_type;
 typedef_Traits                    traits_type;
 
public:                         // Destructor.
 virtual ~basic_streambuf() {}
 
public:                         // Locale-related functions.
 localepubimbue(constlocale& __loc) {
    this->imbue(__loc);
    locale__tmp = _M_locale;
    _M_locale = __loc;
    return__tmp;
 }
 localegetloc() const { return_M_locale; }
 
public:                         // Buffer management.
 //设置缓冲区和长度
 basic_streambuf* pubsetbuf(char_type* __s, streamsize__n)
    { returnthis->setbuf(__s, __n); }
 
 //设置缓冲区偏移量,简单调用seekoff()函数
 pos_typepubseekoff(off_type__offset, ios_base::seekdir__way,
                      ios_base::openmode__mod = ios_base::in | ios_base::out)
    { returnthis->seekoff(__offset, __way, __mod); }
//设置缓冲区位置,简单调用seekpos()函数
 pos_typepubseekpos(pos_type__sp,
                      ios_base::openmode__mod = ios_base::in | ios_base::out)
    { returnthis->seekpos(__sp, __mod); }
 
//放置缓冲区同步,简单调用sync()函数
 intpubsync() { returnthis->sync(); }
 
public:                       
  // 读取字符的公共成员函数 
 
//比较当前指针和结尾指针,如果在结尾之前返回剩下的缓冲区大小.否则调用showmanyc()函数
 streamsizein_avail() {
    return_M_gnext < _M_gend ? _M_gend - _M_gnext : this->showmanyc();
 }
 
 // 移动到下一个字符,并返回下一字符
 int_typesnextc() {
    return_M_gend - _M_gnext > 1 ? _Traits::to_int_type(*++_M_gnext)
                                  : this->_M_snextc_aux();
 }
 
 // 移动到下一个字符,并返回当前字符
 int_typesbumpc() {
    return_M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext++)
                              : this->uflow();
 }
 
//并返回当前字符,指针不移动到
 int_typesgetc() {
    return_M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext)
                              : this->underflow();
 }
   
 // 获取n个字符串到参数__s
 streamsizesgetn(char_type* __s, streamsize__n)
    { returnthis->xsgetn(__s, __n); }
 
 // 比较当前前一个字符是否与参数__c相等,相等则返回,移动当前指针当前前一个字符并返回
 int_typesputbackc(char_type__c) {
    return_M_gbegin < _M_gnext && _Traits::eq(__c, *(_M_gnext - 1))
      ? _Traits::to_int_type(*--_M_gnext)
      : this->pbackfail(_Traits::to_int_type(__c));
 }
 
 //移动当前指针当前前一个字符并返回
 int_typesungetc() {
    return_M_gbegin < _M_gnext
      ? _Traits::to_int_type(*--_M_gnext)
      : this->pbackfail();
 }
 
public:                        
// 公共写字符成员函数
 
 // 写入一个字符,
 int_typesputc(char_type__c) {
    return_M_pnext < _M_pend
      ? _Traits::to_int_type(*_M_pnext++ = __c)
      : this->overflow(_Traits::to_int_type(__c));
 }
 
 // 写入长度为n的字串
 streamsizesputn(constchar_type* __s, streamsize__n)
    { returnthis->xsputn(__s, __n); }
 
 // 扩展写n个字符函数
 streamsize_M_sputnc(char_type__c, streamsize__n)
    { returnthis->_M_xsputnc(__c, __n); }
 
public:                        
// 线程安全的锁
 _STL_mutex_lock _M_lock;
 
protected:                     
// 缺省构造函数
 basic_streambuf()
    : _M_gbegin(0), _M_gnext(0), _M_gend(0),
      _M_pbegin(0), _M_pnext(0), _M_pend(0),
      _M_locale()
    {
      _M_lock._M_initialize();
    }
 
protected:                     
// 保护的获取读指针的成员函数
 char_type* eback() const { return_M_gbegin; }  // 开头读指针
 char_type* gptr() const { return_M_gnext; }  // 当前读指针
 char_type* egptr() const { return_M_gend; }   // 结尾读指针
 
 // 将读指针向移动n个字符
 voidgbump(int__n) { _M_gnext += __n; }
 
 // 从新设置读的三个指针
 voidsetg(char_type* __gbegin, char_type* __gnext, char_type* __gend) {
    _M_gbegin = __gbegin;
    _M_gnext = __gnext;
    _M_gend   = __gend;
 }
 
protected:                     
// 保护的获取写指针的成员函数
        
 char_type* pbase() const { return_M_pbegin; } // 开头读指针
 char_type* pptr() const { return_M_pnext; } // 当前读指针
 char_type* epptr() const { return_M_pend; }   // 结尾读指针
 
 // 将写指针向移动n个字符
 voidpbump(int__n) { _M_pnext += __n; }
 
// 从新设置读的三个指针
 voidsetp(char_type* __pbegin, char_type* __pend) {
    _M_pbegin = __pbegin;
    _M_pnext = __pbegin;
    _M_pend   = __pend;
 } 
 
protected:                     
// 本地化虚函数
 virtualvoidimbue(constlocale&) {}
 
protected:                     
// 内存管理虚函数,当前置返回自己this
 virtualbasic_streambuf* setbuf(char_type*, streamsize)
    { returnthis; }
 
 
 // 通过一个整型偏移值,改变流的位置.该函数这里什么读没作,只返回-1,在子类中重载
 virtualpos_typeseekoff(off_type, ios_base::seekdir,
                           ios_base::openmode = ios_base::in | ios_base::out)
    { returnpos_type(-1); }
 
// 通过前面获取流位置,改变流的位置.该函数这里什么读没作,只返回-1,在子类中重载
 virtualpos_type
 seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out)
    { returnpos_type(-1); }
 
 // 同步(刷新).缓冲区.所有的子类中重载
 virtualintsync() { return 0; }
 
protected:                     
 // 返回在在抵达文件结尾更低级可以读取字符数. (-1 是特定值,意味着underflow 将失败.)
// 函数在子类中重载
 virtualstreamsizeshowmanyc()
    { return 0; }
 
 // 读取n的字符. 返回可读取的字符数
 virtualstreamsizexsgetn(char_type* __s, streamsize__n);
 
 // 当没有读取的位置时调用.: gptr() 返回为空时或者gptr() >= egptr().
 virtualint_typeunderflow()
    { return_Traits::eof(); }
 
 // 类似underflow(), 但是使用在unbuffered 输入.
 virtualint_typeuflow() {
    return_Traits::eq_int_type(this->underflow(), _Traits::eof())
      ? _Traits::eof()
      : _Traits::to_int_type(*_M_gnext++);
 }
 
// 当没有放置的位置时调用.: gptr() 返回为空时或者gptr() == eback().
 virtualint_typepbackfail(int_type__c = _Traits::eof())
    { return_Traits::eof(); }
 
protected:              
 // n 字符,返回写的字符数
 virtualstreamsizexsputn(constchar_type* __s, streamsize__n);
 
// 当没有写位置时调用
 virtualint_typeoverflow(int_type = _Traits::eof())
    { return_Traits::eof(); }
 
private:                       
 char_type* _M_gbegin;         // get 区的开始指针
 char_type* _M_gnext;          // get 区的当前指针
 char_type* _M_gend;           // get 区的结尾指针
 
 char_type* _M_pbegin;         // put 区的开始指针
 char_type* _M_pnext;          // put 区的当前指针
 char_type* _M_pend;           // put 区的结尾指针
 
 locale_M_locale;             // The streambuf's locale object
};
 
 
 
流氓修炼之谜 揭开流氓软件的技术原理:【上一篇】
泛化的ScopeGuard模板类:【下一篇】
【相关文章】
  • 算24游戏的C++实现
  • C++中的成员指针简介
  • C++成员指针
  • Visual C++程序调试方法入门(zz)
  • C++ 学习忠告50条!
  • C++私有构造函数
  • C/C++模版类/函数
  • VC++开发BHO插件——定制你的浏览器[转]
  • More Effective C++ 条款1:指针与引用的区别
  • More Effective C++ 条款6:自增(increment)、自减(decrement)操作符前缀形式与后缀形式的区别
  • 【随机文章】
  • VPN 组网方案
  • JavaScript实现DataGrid中的CheckBox全选与否
  • 用Delphi开发OPC客户端工具的方法研究
  • BackgroudWorker 范例
  • ResultSet can not re-read row data for column 1.
  • 使用第二个mysql.server instance
  • 用Visual Basic开发绿色软件的探讨
  • 企业经营管理系统开发实例导航-超市管理系统
  • 加注解的纸条问题
  • 传奇 灭天火的真正作用 另附魔法锁定方法
  • 【相关评论】
    没有相关评论
    【发表评论】
    姓名:
    邮件:
    随机码*
    评论*
          
    |  首 页  |  版权声明  |  联系我们   |  网站地图  |
    CopyRight © 2004-2007 软讯网络 All Rigths Reserved.