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
};
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章