C++的流basic_streambuf

 
不是个抽象类 , 但是他是 C++ 流的基类 . 它提供了基本的缓冲区管理 . 它包含六个缓存区指针 , 三个分别是读指针 ( 开头 , 结尾 , 当前 ), 另外三个是写指针 ( 开头 , 结尾 , 当前 )
 
    读开始指针                             当前读指针                                     读结尾指针
_M_gbegin                          _M_gnext                      _M_gend
         
         =========================================================    
              ^                                                      ^                                              ^
       _M_pbegin                          _M_pnext                      _M_pend
           写开始指针                             当前写指针                             写结尾指针
 
template < class _CharT , class _Traits >
class basic_streambuf
{
  friend class basic_istream < _CharT , _Traits >;
  friend class basic_ostream < _CharT , _Traits >;
public :                         // Typedefs.
  typedef _CharT                    char_type ;
  typedef typename _Traits :: int_type        int_type ;
  typedef typename _Traits :: pos_type     pos_type ;
  typedef typename _Traits :: off_type       off_type ;
  typedef _Traits                      traits_type ;
 
public :                         // Destructor.
  virtual ~ basic_streambuf () {}
 
public :                         // Locale-related functions.
  locale pubimbue ( const locale & __loc ) {
    this -> imbue ( __loc );
    locale __tmp = _M_locale ;
    _M_locale = __loc ;
    return __tmp ;
 }
  locale getloc () const { return _M_locale ; }
 
public :                         // Buffer management.
  // 设置缓冲区和长度
  basic_streambuf * pubsetbuf ( char_type * __s , streamsize __n )
    { return this -> setbuf ( __s , __n ); }
 
 // 设置缓冲区偏移量 , 简单调用 seekoff() 函数
  pos_type pubseekoff ( off_type __offset , ios_base :: seekdir __way ,
                      ios_base :: openmode __mod = ios_base :: in | ios_base :: out )
    { return this -> seekoff ( __offset , __way , __mod ); }
// 设置缓冲区位置 , 简单调用 seekpos() 函数
  pos_type pubseekpos ( pos_type __sp ,
                      ios_base :: openmode __mod = ios_base :: in | ios_base :: out )
    { return this -> seekpos ( __sp , __mod ); }
 
// 放置缓冲区同步 , 简单调用 sync() 函数
  int pubsync () { return this -> sync (); }
 
public :                       
  // 读取字符的公共成员函数  
 
// 比较当前指针和结尾指针 , 如果在结尾之前返回剩下的缓冲区大小 . 否则调用 showmanyc() 函数
  streamsize in_avail () {
    return _M_gnext < _M_gend ? _M_gend - _M_gnext : this -> showmanyc ();
 }
 
 // 移动到下一个字符 , 并返回下一字符
  int_type snextc () {
    return _M_gend - _M_gnext > 1 ? _Traits :: to_int_type (*++ _M_gnext )
                                  : this -> _M_snextc_aux ();
 }
 
 // 移动到下一个字符 , 并返回当前字符
  int_type sbumpc () {
    return _M_gnext < _M_gend ? _Traits :: to_int_type (* _M_gnext ++)
                              : this -> uflow ();
 }
 
// 并返回当前字符 , 指针不移动到
  int_type sgetc () {
    return _M_gnext < _M_gend ? _Traits :: to_int_type (* _M_gnext )
                              : this -> underflow ();
 }
   
 // 获取 n 个字符串到参数 __s
  streamsize sgetn ( char_type * __s , streamsize __n )
    { return this -> xsgetn ( __s , __n ); }
 
 // 比较当前前一个字符是否与参数 __c 相等 , 相等则返回 , 移动当前指针 当前前一个字符并返回
  int_type sputbackc ( 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_type sungetc () {
    return _M_gbegin < _M_gnext
      ? _Traits :: to_int_type (*-- _M_gnext )
      : this -> pbackfail ();
 }
 
public :                        
// 公共写字符成员函数
 
 // 写入一个字符 ,
  int_type sputc ( char_type __c ) {
    return _M_pnext < _M_pend
      ? _Traits :: to_int_type (* _M_pnext ++ = __c )
      : this -> overflow ( _Traits :: to_int_type ( __c ));
 }
 
 // 写入长度为 n 的字串
  streamsize sputn ( const char_type * __s , streamsize __n )
    { return this -> xsputn ( __s , __n ); }
 
 // 扩展写 n 个字符函数
  streamsize _M_sputnc ( char_type __c , streamsize __n )
    { return this -> _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 个字符
  void gbump ( int __n ) { _M_gnext += __n ; }
 
 // 从新设置读的三个指针
  void setg ( 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 个字符
  void pbump ( int __n ) { _M_pnext += __n ; }
 
// 从新设置读的三个指针
  void setp ( char_type * __pbegin , char_type * __pend ) {
    _M_pbegin = __pbegin ;
    _M_pnext  = __pbegin ;
    _M_pend    = __pend ;
 } 
 
protected :                     
// 本地化虚函数
  virtual void imbue ( const locale &) {}
 
protected :                     
// 内存管理 虚函数 , 当前置返回自己 this
  virtual basic_streambuf * setbuf ( char_type *, streamsize )
    { return this ; }
 
 
 // 通过一个整型偏移值 , 改变流的位置 . 该函数这里什么读没作 , 只返回 -1, 在子类中重载
  virtual pos_type seekoff ( off_type , ios_base :: seekdir ,
                           ios_base :: openmode = ios_base :: in | ios_base :: out )
    { return pos_type (-1); }
 
// 通过前面获取流位置 , 改变流的位置 . 该函数这里什么读没作 , 只返回 -1, 在子类中重载
  virtual pos_type
  seekpos ( pos_type , ios_base :: openmode = ios_base :: in | ios_base :: out )
    { return pos_type (-1); }
 
 // 同步 ( 刷新 ). 缓冲区 . 所有的子类中重载
  virtual int sync () { return 0; }
 
protected :                     
 // 返回在在抵达文件结尾更低级可以读取字符数 . (-1 是特定值 , 意味着 underflow 将失败 .)
// 函数在子类中重载
  virtual streamsize showmanyc ()
    { return 0; }
 
 // 读取 n 的字符 . 返回可读取的字符数
  virtual streamsize xsgetn ( char_type * __s , streamsize __n );
 
 // 当没有读取的位置时调用 . : gptr() 返回为空时或者 gptr() >= egptr().
  virtual int_type underflow ()
    { return _Traits :: eof (); }
 
 // 类似 underflow(), 但是使用在 unbuffered 输入 .
  virtual int_type uflow () {
    return _Traits :: eq_int_type ( this -> underflow (), _Traits :: eof ())
      ? _Traits :: eof ()
      : _Traits :: to_int_type (* _M_gnext ++);
 }
 
// 当没有放置的位置时调用 . : gptr() 返回为空时或者 gptr() == eback().
  virtual int_type pbackfail ( int_type __c = _Traits :: eof ())
    { return _Traits :: eof (); }
 
protected :              
 // n 字符 , 返回写的字符数
  virtual streamsize xsputn ( const char_type * __s , streamsize __n );
 
// 当没有写位置时调用
  virtual int_type overflow ( 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
};
 
 
 
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值