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, 2005
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 
00039 #ifndef _GLIBXX_STREAMBUF
00040 #define _GLIBXX_STREAMBUF 1
00041 
00042 #pragma GCC system_header
00043 
00044 #include <bits/c++config.h>
00045 #include <iosfwd>
00046 #include <bits/localefwd.h>
00047 #include <bits/ios_base.h>
00048 
00049 namespace std
00050 {
00056   template<typename _CharT, typename _Traits>
00057     streamsize
00058     __copy_streambufs(basic_streambuf<_CharT, _Traits>* __sbin,
00059               basic_streambuf<_CharT, _Traits>* __sbout);
00060   
00121   template<typename _CharT, typename _Traits>
00122     class basic_streambuf 
00123     {
00124     public:
00126 
00131       typedef _CharT                    char_type;
00132       typedef _Traits                   traits_type;
00133       typedef typename traits_type::int_type        int_type;
00134       typedef typename traits_type::pos_type        pos_type;
00135       typedef typename traits_type::off_type        off_type;
00137 
00139 
00144       typedef basic_streambuf<char_type, traits_type>   __streambuf_type;
00146       
00147       friend class basic_ios<char_type, traits_type>;
00148       friend class basic_istream<char_type, traits_type>;
00149       friend class basic_ostream<char_type, traits_type>;
00150       friend class istreambuf_iterator<char_type, traits_type>;
00151       friend class ostreambuf_iterator<char_type, traits_type>;
00152 
00153       friend streamsize
00154       __copy_streambufs<>(__streambuf_type* __sbin,
00155               __streambuf_type* __sbout);
00156       
00157       template<typename _CharT2, typename _Traits2>
00158         friend basic_istream<_CharT2, _Traits2>&
00159         operator>>(basic_istream<_CharT2, _Traits2>&, _CharT2*);
00160       
00161       template<typename _CharT2, typename _Traits2, typename _Alloc>
00162         friend basic_istream<_CharT2, _Traits2>&
00163         operator>>(basic_istream<_CharT2, _Traits2>&,
00164            basic_string<_CharT2, _Traits2, _Alloc>&);
00165       
00166       template<typename _CharT2, typename _Traits2, typename _Alloc>
00167         friend basic_istream<_CharT2, _Traits2>&
00168         getline(basic_istream<_CharT2, _Traits2>&,
00169         basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2);
00170 
00171     protected:
00173 
00182       char_type*        _M_in_beg;     // Start of get area. 
00183       char_type*        _M_in_cur;     // Current read area. 
00184       char_type*        _M_in_end;     // End of get area. 
00185       char_type*        _M_out_beg;    // Start of put area. 
00186       char_type*        _M_out_cur;    // Current put area. 
00187       char_type*        _M_out_end;    // End of put area.
00188 
00194       locale            _M_buf_locale;  
00195 
00196   public:
00198       virtual 
00199       ~basic_streambuf() 
00200       { }
00201 
00202       // [27.5.2.2.1] locales
00210       locale 
00211       pubimbue(const locale &__loc)
00212       {
00213     locale __tmp(this->getloc());
00214     this->imbue(__loc);
00215     _M_buf_locale = __loc;
00216     return __tmp;
00217       }
00218 
00227       locale   
00228       getloc() const
00229       { return _M_buf_locale; } 
00230 
00231       // [27.5.2.2.2] buffer management and positioning
00233 
00240       __streambuf_type* 
00241       pubsetbuf(char_type* __s, streamsize __n) 
00242       { return this->setbuf(__s, __n); }
00243 
00244       pos_type 
00245       pubseekoff(off_type __off, ios_base::seekdir __way, 
00246          ios_base::openmode __mode = ios_base::in | ios_base::out)
00247       { return this->seekoff(__off, __way, __mode); }
00248 
00249       pos_type 
00250       pubseekpos(pos_type __sp,
00251          ios_base::openmode __mode = ios_base::in | ios_base::out)
00252       { return this->seekpos(__sp, __mode); }
00253 
00254       int 
00255       pubsync() { return this->sync(); }
00257 
00258       // [27.5.2.2.3] get area
00267       streamsize 
00268       in_avail() 
00269       { 
00270     const streamsize __ret = this->egptr() - this->gptr();
00271     return __ret ? __ret : this->showmanyc();
00272       }
00273 
00281       int_type 
00282       snextc()
00283       {
00284     int_type __ret = traits_type::eof();
00285     if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(), 
00286                                __ret), true))
00287       __ret = this->sgetc();
00288     return __ret;
00289       }
00290 
00299       int_type 
00300       sbumpc()
00301       {
00302     int_type __ret;
00303     if (__builtin_expect(this->gptr() < this->egptr(), true))
00304       {
00305         __ret = traits_type::to_int_type(*this->gptr());
00306         this->gbump(1);
00307       }
00308     else 
00309       __ret = this->uflow();
00310     return __ret;
00311       }
00312 
00321       int_type 
00322       sgetc()
00323       {
00324     int_type __ret;
00325     if (__builtin_expect(this->gptr() < this->egptr(), true))
00326       __ret = traits_type::to_int_type(*this->gptr());
00327     else 
00328       __ret = this->underflow();
00329     return __ret;
00330       }
00331 
00340       streamsize 
00341       sgetn(char_type* __s, streamsize __n)
00342       { return this->xsgetn(__s, __n); }
00343 
00344       // [27.5.2.2.4] putback
00354       int_type 
00355       sputbackc(char_type __c)
00356       {
00357     int_type __ret;
00358     const bool __testpos = this->eback() < this->gptr();
00359     if (__builtin_expect(!__testpos || 
00360                  !traits_type::eq(__c, this->gptr()[-1]), false))
00361       __ret = this->pbackfail(traits_type::to_int_type(__c));
00362     else 
00363       {
00364         this->gbump(-1);
00365         __ret = traits_type::to_int_type(*this->gptr());
00366       }
00367     return __ret;
00368       }
00369 
00379       int_type 
00380       sungetc()
00381       {
00382     int_type __ret;
00383     if (__builtin_expect(this->eback() < this->gptr(), true))
00384       {
00385         this->gbump(-1);
00386         __ret = traits_type::to_int_type(*this->gptr());
00387       }
00388     else 
00389       __ret = this->pbackfail();
00390     return __ret;
00391       }
00392 
00393       // [27.5.2.2.5] put area
00406       int_type 
00407       sputc(char_type __c)
00408       {
00409     int_type __ret;
00410     if (__builtin_expect(this->pptr() < this->epptr(), true))
00411       {
00412         *this->pptr() = __c;
00413         this->pbump(1);
00414         __ret = traits_type::to_int_type(__c);
00415       }
00416     else
00417       __ret = this->overflow(traits_type::to_int_type(__c));
00418     return __ret;
00419       }
00420 
00432       streamsize 
00433       sputn(const char_type* __s, streamsize __n)
00434       { return this->xsputn(__s, __n); }
00435 
00436     protected:
00446       basic_streambuf()
00447       : _M_in_beg(0), _M_in_cur(0), _M_in_end(0), 
00448       _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
00449       _M_buf_locale(locale()) 
00450       { }
00451 
00452       // [27.5.2.3.1] get area access
00454 
00464       char_type* 
00465       eback() const { return _M_in_beg; }
00466 
00467       char_type* 
00468       gptr()  const { return _M_in_cur;  }
00469 
00470       char_type* 
00471       egptr() const { return _M_in_end; }
00473 
00480       void 
00481       gbump(int __n) { _M_in_cur += __n; }
00482 
00491       void 
00492       setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
00493       {
00494     _M_in_beg = __gbeg;
00495     _M_in_cur = __gnext;
00496     _M_in_end = __gend;
00497       }
00498 
00499       // [27.5.2.3.2] put area access
00501 
00511       char_type* 
00512       pbase() const { return _M_out_beg; }
00513 
00514       char_type* 
00515       pptr() const { return _M_out_cur; }
00516 
00517       char_type* 
00518       epptr() const { return _M_out_end; }
00520 
00527       void 
00528       pbump(int __n) { _M_out_cur += __n; }
00529 
00537       void 
00538       setp(char_type* __pbeg, char_type* __pend)
00539       { 
00540     _M_out_beg = _M_out_cur = __pbeg; 
00541     _M_out_end = __pend;
00542       }
00543 
00544       // [27.5.2.4] virtual functions
00545       // [27.5.2.4.1] locales
00558       virtual void 
00559       imbue(const locale&) 
00560       { }
00561 
00562       // [27.5.2.4.2] buffer management and positioning
00573       virtual basic_streambuf<char_type,_Traits>* 
00574       setbuf(char_type*, streamsize)
00575       { return this; }
00576       
00584       virtual pos_type 
00585       seekoff(off_type, ios_base::seekdir,
00586           ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
00587       { return pos_type(off_type(-1)); } 
00588 
00596       virtual pos_type 
00597       seekpos(pos_type, 
00598           ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
00599       { return pos_type(off_type(-1)); } 
00600 
00609       virtual int 
00610       sync() { return 0; }
00611 
00612       // [27.5.2.4.3] get area
00631       virtual streamsize 
00632       showmanyc() { return 0; }
00633 
00647       virtual streamsize 
00648       xsgetn(char_type* __s, streamsize __n);
00649 
00669       virtual int_type 
00670       underflow()
00671       { return traits_type::eof(); }
00672 
00682       virtual int_type 
00683       uflow() 
00684       {
00685     int_type __ret = traits_type::eof();
00686     const bool __testeof = traits_type::eq_int_type(this->underflow(), 
00687                             __ret);
00688     if (!__testeof)
00689       {
00690         __ret = traits_type::to_int_type(*this->gptr());
00691         this->gbump(1);
00692       }
00693     return __ret;    
00694       }
00695 
00696       // [27.5.2.4.4] putback
00706       virtual int_type 
00707       pbackfail(int_type /* __c */  = traits_type::eof())
00708       { return traits_type::eof(); }
00709 
00710       // Put area:
00724       virtual streamsize 
00725       xsputn(const char_type* __s, streamsize __n);
00726 
00749       virtual int_type 
00750       overflow(int_type /* __c */ = traits_type::eof())
00751       { return traits_type::eof(); }
00752 
00753 #ifdef _GLIBCXX_DEPRECATED
00754     // Annex D.6
00755     public:
00768       void 
00769       stossc() 
00770       {
00771     if (this->gptr() < this->egptr()) 
00772       this->gbump(1);
00773     else 
00774       this->uflow();
00775       }
00776 #endif
00777 
00778     private:
00779       // _GLIBCXX_RESOLVE_LIB_DEFECTS
00780       // Side effect of DR 50. 
00781       basic_streambuf(const __streambuf_type& __sb)
00782       : _M_in_beg(__sb._M_in_beg), _M_in_cur(__sb._M_in_cur), 
00783       _M_in_end(__sb._M_in_end), _M_out_beg(__sb._M_out_beg), 
00784       _M_out_cur(__sb._M_out_cur), _M_out_end(__sb._M_out_cur),
00785       _M_buf_locale(__sb._M_buf_locale) 
00786       { }
00787 
00788       __streambuf_type& 
00789       operator=(const __streambuf_type&) { return *this; };
00790     };
00791 
00792   // Explicit specialization declarations, defined in src/streambuf.cc.
00793   template<>
00794     streamsize
00795     __copy_streambufs(basic_streambuf<char>* __sbin,
00796               basic_streambuf<char>* __sbout);
00797 #ifdef _GLIBCXX_USE_WCHAR_T
00798   template<>
00799     streamsize
00800     __copy_streambufs(basic_streambuf<wchar_t>* __sbin,
00801               basic_streambuf<wchar_t>* __sbout);
00802 #endif
00803 } // namespace std
00804 
00805 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00806 # include <bits/streambuf.tcc>
00807 #endif
00808 
00809 #endif /* _GLIBCXX_STREAMBUF */

Generated on Tue Feb 2 16:56:41 2010 for GNU C++ STL by  doxygen 1.4.7