streambuf

Go to the documentation of this file.
00001 // Stream buffer classes -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
00004 // Free Software Foundation, Inc.
00005 //
00006 // This file is part of the GNU ISO C++ Library.  This library is free
00007 // software; you can redistribute it and/or modify it under the
00008 // terms of the GNU General Public License as published by the
00009 // Free Software Foundation; either version 2, or (at your option)
00010 // any later version.
00011 
00012 // This library is distributed in the hope that it will be useful,
00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015 // GNU General Public License for more details.
00016 
00017 // You should have received a copy of the GNU General Public License along
00018 // with this library; see the file COPYING.  If not, write to the Free
00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00020 // USA.
00021 
00022 // As a special exception, you may use this file as part of a free software
00023 // library without restriction.  Specifically, if other files instantiate
00024 // templates or use macros or inline functions from this file, or you compile
00025 // this file and link it with other files to produce an executable, this
00026 // file does not by itself cause the resulting executable to be covered by
00027 // the GNU General Public License.  This exception does not however
00028 // invalidate any other reasons why the executable file might be covered by
00029 // the GNU General Public License.
00030 
00031 //
00032 // ISO C++ 14882: 27.5  Stream buffers
00033 //
00034 
00040 #ifndef _CLIBXX_STREAMBUF
00041 #define _CLIBXX_STREAMBUF 1
00042 
00043 #pragma GCC system_header
00044 
00045 #include <bits/c++config.h>
00046 #include <iosfwd>
00047 #include <bits/localefwd.h>
00048 #include <bits/ios_base.h>
00049 
00050 namespace std
00051 {
00057   template<typename _CharT, typename _Traits>
00058     streamsize
00059     __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin,
00060               basic_streambuf<_CharT, _Traits>* __sbout);
00061   
00122   template<typename _CharT, typename _Traits>
00123     class basic_streambuf 
00124     {
00125     public:
00127 
00132       typedef _CharT                    char_type;
00133       typedef _Traits                   traits_type;
00134       typedef typename traits_type::int_type        int_type;
00135       typedef typename traits_type::pos_type        pos_type;
00136       typedef typename traits_type::off_type        off_type;
00138 
00140 
00145       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00147       
00148       friend class basic_ios<char_type, traits_type>;
00149       friend class basic_istream<char_type, traits_type>;
00150       friend class basic_ostream<char_type, traits_type>;
00151       friend class istreambuf_iterator<char_type, traits_type>;
00152       friend class ostreambuf_iterator<char_type, traits_type>;
00153 
00154       friend streamsize
00155       __copy_streambufs<>(__streambuf_type* __sbin,
00156               __streambuf_type* __sbout);
00157       
00158     protected:
00160 
00169       char_type*        _M_in_beg;     // Start of get area. 
00170       char_type*        _M_in_cur;     // Current read area. 
00171       char_type*        _M_in_end;     // End of get area. 
00172       char_type*        _M_out_beg;    // Start of put area. 
00173       char_type*        _M_out_cur;    // Current put area. 
00174       char_type*        _M_out_end;    // End of put area.
00175 
00181       locale            _M_buf_locale;  
00182 
00183   public:
00185       virtual 
00186       ~basic_streambuf() 
00187       { }
00188 
00189       // [27.5.2.2.1] locales
00197       locale 
00198       pubimbue(const locale &__loc)
00199       {
00200     locale __tmp(this->getloc());
00201     this->imbue(__loc);
00202     _M_buf_locale = __loc;
00203     return __tmp;
00204       }
00205 
00214       locale   
00215       getloc() const
00216       { return _M_buf_locale; } 
00217 
00218       // [27.5.2.2.2] buffer management and positioning
00220 
00227       __streambuf_type* 
00228       pubsetbuf(char_type* __s, streamsize __n) 
00229       { return this->setbuf(__s, __n); }
00230 
00231       pos_type 
00232       pubseekoff(off_type __off, ios_base::seekdir __way, 
00233          ios_base::openmode __mode = ios_base::in | ios_base::out)
00234       { return this->seekoff(__off, __way, __mode); }
00235 
00236       pos_type 
00237       pubseekpos(pos_type __sp,
00238          ios_base::openmode __mode = ios_base::in | ios_base::out)
00239       { return this->seekpos(__sp, __mode); }
00240 
00241       int 
00242       pubsync() { return this->sync(); }
00244 
00245       // [27.5.2.2.3] get area
00254       streamsize 
00255       in_avail() 
00256       { 
00257     const streamsize __ret = this->egptr() - this->gptr();
00258     return __ret ? __ret : this->showmanyc();
00259       }
00260 
00268       int_type 
00269       snextc()
00270       {
00271     int_type __ret = traits_type::eof();
00272     if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(), 
00273                                __ret), true))
00274       __ret = this->sgetc();
00275     return __ret;
00276       }
00277 
00286       int_type 
00287       sbumpc()
00288       {
00289     int_type __ret;
00290     if (__builtin_expect(this->gptr() < this->egptr(), true))
00291       {
00292         __ret = traits_type::to_int_type(*this->gptr());
00293         this->gbump(1);
00294       }
00295     else 
00296       __ret = this->uflow();
00297     return __ret;
00298       }
00299 
00308       int_type 
00309       sgetc()
00310       {
00311     int_type __ret;
00312     if (__builtin_expect(this->gptr() < this->egptr(), true))
00313       __ret = traits_type::to_int_type(*this->gptr());
00314     else 
00315       __ret = this->underflow();
00316     return __ret;
00317       }
00318 
00327       streamsize 
00328       sgetn(char_type* __s, streamsize __n)
00329       { return this->xsgetn(__s, __n); }
00330 
00331       // [27.5.2.2.4] putback
00341       int_type 
00342       sputbackc(char_type __c)
00343       {
00344     int_type __ret;
00345     const bool __testpos = this->eback() < this->gptr();
00346     if (__builtin_expect(!__testpos || 
00347                  !traits_type::eq(__c, this->gptr()[-1]), false))
00348       __ret = this->pbackfail(traits_type::to_int_type(__c));
00349     else 
00350       {
00351         this->gbump(-1);
00352         __ret = traits_type::to_int_type(*this->gptr());
00353       }
00354     return __ret;
00355       }
00356 
00366       int_type 
00367       sungetc()
00368       {
00369     int_type __ret;
00370     if (__builtin_expect(this->eback() < this->gptr(), true))
00371       {
00372         this->gbump(-1);
00373         __ret = traits_type::to_int_type(*this->gptr());
00374       }
00375     else 
00376       __ret = this->pbackfail();
00377     return __ret;
00378       }
00379 
00380       // [27.5.2.2.5] put area
00393       int_type 
00394       sputc(char_type __c)
00395       {
00396     int_type __ret;
00397     if (__builtin_expect(this->pptr() < this->epptr(), true))
00398       {
00399         *this->pptr() = __c;
00400         this->pbump(1);
00401         __ret = traits_type::to_int_type(__c);
00402       }
00403     else
00404       __ret = this->overflow(traits_type::to_int_type(__c));
00405     return __ret;
00406       }
00407 
00419       streamsize 
00420       sputn(const char_type* __s, streamsize __n)
00421       { return this->xsputn(__s, __n); }
00422 
00423     protected:
00433       basic_streambuf()
00434       : _M_in_beg(0), _M_in_cur(0), _M_in_end(0), 
00435       _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
00436       _M_buf_locale(locale()) 
00437       { }
00438 
00439       // [27.5.2.3.1] get area access
00441 
00451       char_type* 
00452       eback() const { return _M_in_beg; }
00453 
00454       char_type* 
00455       gptr()  const { return _M_in_cur;  }
00456 
00457       char_type* 
00458       egptr() const { return _M_in_end; }
00460 
00467       void 
00468       gbump(int __n) { _M_in_cur += __n; }
00469 
00478       void 
00479       setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
00480       {
00481     _M_in_beg = __gbeg;
00482     _M_in_cur = __gnext;
00483     _M_in_end = __gend;
00484       }
00485 
00486       // [27.5.2.3.2] put area access
00488 
00498       char_type* 
00499       pbase() const { return _M_out_beg; }
00500 
00501       char_type* 
00502       pptr() const { return _M_out_cur; }
00503 
00504       char_type* 
00505       epptr() const { return _M_out_end; }
00507 
00514       void 
00515       pbump(int __n) { _M_out_cur += __n; }
00516 
00524       void 
00525       setp(char_type* __pbeg, char_type* __pend)
00526       { 
00527     _M_out_beg = _M_out_cur = __pbeg; 
00528     _M_out_end = __pend;
00529       }
00530 
00531       // [27.5.2.4] virtual functions
00532       // [27.5.2.4.1] locales
00545       virtual void 
00546       imbue(const locale&) 
00547       { }
00548 
00549       // [27.5.2.4.2] buffer management and positioning
00560       virtual basic_streambuf<char_type,_Traits>* 
00561       setbuf(char_type*, streamsize)
00562       { return this; }
00563       
00571       virtual pos_type 
00572       seekoff(off_type, ios_base::seekdir,
00573           ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
00574       { return pos_type(off_type(-1)); } 
00575 
00583       virtual pos_type 
00584       seekpos(pos_type, 
00585           ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
00586       { return pos_type(off_type(-1)); } 
00587 
00596       virtual int 
00597       sync() { return 0; }
00598 
00599       // [27.5.2.4.3] get area
00618       virtual streamsize 
00619       showmanyc() { return 0; }
00620 
00634       virtual streamsize 
00635       xsgetn(char_type* __s, streamsize __n);
00636 
00656       virtual int_type 
00657       underflow()
00658       { return traits_type::eof(); }
00659 
00669       virtual int_type 
00670       uflow() 
00671       {
00672     int_type __ret = traits_type::eof();
00673     const bool __testeof = traits_type::eq_int_type(this->underflow(), 
00674                             __ret);
00675     if (!__testeof)
00676       {
00677         __ret = traits_type::to_int_type(*this->gptr());
00678         this->gbump(1);
00679       }
00680     return __ret;    
00681       }
00682 
00683       // [27.5.2.4.4] putback
00693       virtual int_type 
00694       pbackfail(int_type /* __c */  = traits_type::eof())
00695       { return traits_type::eof(); }
00696 
00697       // Put area:
00711       virtual streamsize 
00712       xsputn(const char_type* __s, streamsize __n);
00713 
00736       virtual int_type 
00737       overflow(int_type /* __c */ = traits_type::eof())
00738       { return traits_type::eof(); }
00739 
00740 #ifdef _GLIBCXX_DEPRECATED
00741     // Annex D.6
00742     public:
00755       void 
00756       stossc() 
00757       {
00758     if (this->gptr() < this->egptr()) 
00759       this->gbump(1);
00760     else 
00761       this->uflow();
00762       }
00763 #endif
00764 
00765     private:
00766       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00767       // Side effect of DR 50. 
00768       basic_streambuf(const __streambuf_type& __sb)
00769       : _M_in_beg(__sb._M_in_beg), _M_in_cur(__sb._M_in_cur), 
00770       _M_in_end(__sb._M_in_end), _M_out_beg(__sb._M_out_beg), 
00771       _M_out_cur(__sb._M_out_cur), _M_out_end(__sb._M_out_cur),
00772       _M_buf_locale(__sb._M_buf_locale) 
00773       { }
00774 
00775       __streambuf_type& 
00776       operator=(const __streambuf_type&) { return *this; };
00777     };
00778 } // namespace std
00779 
00780 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00781 # include <bits/streambuf.tcc>
00782 #endif
00783 
00784 #endif /* _GLIBCXX_STREAMBUF */

Generated on Tue Jan 30 17:31:55 2007 for GNU C++ STL by doxygen 1.3.6