/home/ntakagi/work/STLport-5.1.5/stlport/stl/_streambuf.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 #ifndef _STLP_INTERNAL_STREAMBUF
00019 #define _STLP_INTERNAL_STREAMBUF
00020 
00021 #ifndef _STLP_IOS_BASE_H
00022 #  include <stl/_ios_base.h>      // Needed for ios_base bitfield members.
00023 #endif                            // <ios_base> includes <iosfwd>.
00024 
00025 _STLP_BEGIN_NAMESPACE
00026 
00027 //----------------------------------------------------------------------
00028 // Class basic_streambuf<>, the base class of the streambuf hierarchy.
00029 
00030 // A basic_streambuf<> manages an input (get) area and an output (put)
00031 // area.  Each is described by three pointers: a beginning, an end, and a
00032 // current position.  basic_streambuf<> contains some very simple member
00033 // functions that manipulate those six pointers, but almost all of the real
00034 // functionality gets delegated to protected virtual member functions.
00035 // All of the public member functions are inline, and most of the protected
00036 // member functions are virtual.
00037 
00038 // Although basic_streambuf<> is not abstract, it is useful only as a base
00039 // class.  Its virtual member functions have default definitions such that
00040 // reading from a basic_streambuf<> will always yield EOF, and writing to a
00041 // basic_streambuf<> will always fail.
00042 
00043 // The second template parameter, _Traits, defaults to char_traits<_CharT>.
00044 // The default is declared in header <iosfwd>, and it isn't declared here
00045 // because C++ language rules do not allow it to be declared twice.
00046 
00047 template <class _CharT, class _Traits>
00048 class basic_streambuf {
00049   friend class basic_istream<_CharT, _Traits>;
00050   friend class basic_ostream<_CharT, _Traits>;
00051 
00052 public:                         // Typedefs.
00053   typedef _CharT                     char_type;
00054   typedef typename _Traits::int_type int_type;
00055   typedef typename _Traits::pos_type pos_type;
00056   typedef typename _Traits::off_type off_type;
00057   typedef _Traits                    traits_type;
00058 
00059 private:                        // Data members.
00060 
00061   char_type* _M_gbegin;         // Beginning of get area
00062   char_type* _M_gnext;          // Current position within the get area
00063   char_type* _M_gend;           // End of get area
00064 
00065   char_type* _M_pbegin;         // Beginning of put area
00066   char_type* _M_pnext;          // Current position within the put area
00067   char_type* _M_pend;           // End of put area
00068 
00069   locale _M_locale;             // The streambuf's locale object
00070 
00071 //public:                         // Extension: locking, for thread safety.
00072 //  _STLP_mutex _M_lock;
00073 
00074 public:                         // Destructor.
00075   virtual ~basic_streambuf();
00076 
00077 protected:                      // The default constructor.
00078   basic_streambuf()
00079 #if defined (_STLP_MSVC) && (_STLP_MSVC < 1300) && defined (_STLP_USE_STATIC_LIB)
00080     //We make it inline to avoid unresolved symbol.
00081     : _M_gbegin(0), _M_gnext(0), _M_gend(0),
00082       _M_pbegin(0), _M_pnext(0), _M_pend(0),
00083       _M_locale()
00084   {}
00085 #else
00086   ;
00087 #endif
00088 
00089 protected:                      // Protected interface to the get area.
00090   char_type* eback() const { return _M_gbegin; } // Beginning
00091   char_type* gptr()  const { return _M_gnext; }  // Current position
00092   char_type* egptr() const { return _M_gend; }   // End
00093 
00094   void gbump(int __n) { _M_gnext += __n; }
00095   void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend) {
00096     _M_gbegin = __gbegin;
00097     _M_gnext  = __gnext;
00098     _M_gend   = __gend;
00099   }
00100 
00101 public:
00102   // An alternate public interface to the above functions
00103   // which allows us to avoid using templated friends which
00104   // are not supported on some compilers.
00105   char_type* _M_eback() const { return eback(); }
00106   char_type* _M_gptr()  const { return gptr(); }
00107   char_type* _M_egptr() const { return egptr(); }
00108   void _M_gbump(int __n)      { gbump(__n); }
00109   void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
00110   { this->setg(__gbegin, __gnext, __gend); }
00111 
00112 protected:                      // Protected interface to the put area
00113 
00114   char_type* pbase() const { return _M_pbegin; } // Beginning
00115   char_type* pptr()  const { return _M_pnext; }  // Current position
00116   char_type* epptr() const { return _M_pend; }   // End
00117 
00118   void pbump(int __n) { _M_pnext += __n; }
00119   void setp(char_type* __pbegin, char_type* __pend) {
00120     _M_pbegin = __pbegin;
00121     _M_pnext  = __pbegin;
00122     _M_pend   = __pend;
00123   }
00124 
00125 protected:                      // Virtual buffer management functions.
00126 
00127   virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
00128 
00129   // Alters the stream position, using an integer offset.  In this
00130   // class seekoff does nothing; subclasses are expected to override it.
00131   virtual pos_type seekoff(off_type, ios_base::seekdir,
00132                            ios_base::openmode = ios_base::in | ios_base::out);
00133 
00134   // Alters the stream position, using a previously obtained streampos.  In
00135   // this class seekpos does nothing; subclasses are expected to override it.
00136   virtual pos_type
00137   seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
00138 
00139   // Synchronizes (i.e. flushes) the buffer.  All subclasses are expected to
00140   // override this virtual member function.
00141   virtual int sync();
00142 
00143 
00144 public:                         // Buffer management.
00145   basic_streambuf<_CharT, _Traits>* pubsetbuf(char_type* __s, streamsize __n)
00146   { return this->setbuf(__s, __n); }
00147 
00148   pos_type pubseekoff(off_type __offset, ios_base::seekdir __way,
00149                       ios_base::openmode __mod = ios_base::in | ios_base::out)
00150   { return this->seekoff(__offset, __way, __mod); }
00151 
00152   pos_type pubseekpos(pos_type __sp,
00153                       ios_base::openmode __mod = ios_base::in | ios_base::out)
00154   { return this->seekpos(__sp, __mod); }
00155 
00156   int pubsync() { return this->sync(); }
00157 
00158 protected:                      // Virtual get area functions, as defined in
00159                                 // 17.5.2.4.3 and 17.5.2.4.4 of the standard.
00160   // Returns a lower bound on the number of characters that we can read,
00161   // with underflow, before reaching end of file.  (-1 is a special value:
00162   // it means that underflow will fail.)  Most subclasses should probably
00163   // override this virtual member function.
00164   virtual streamsize showmanyc();
00165 
00166   // Reads up to __n characters.  Return value is the number of
00167   // characters read.
00168   virtual streamsize xsgetn(char_type* __s, streamsize __n);
00169 
00170   // Called when there is no read position, i.e. when gptr() is null
00171   // or when gptr() >= egptr().  Subclasses are expected to override
00172   // this virtual member function.
00173   virtual int_type underflow();
00174 
00175   // Similar to underflow(), but used for unbuffered input.  Most
00176   // subclasses should probably override this virtual member function.
00177   virtual int_type uflow();
00178 
00179   // Called when there is no putback position, i.e. when gptr() is null
00180   // or when gptr() == eback().  All subclasses are expected to override
00181   // this virtual member function.
00182   virtual int_type pbackfail(int_type = traits_type::eof());
00183 
00184 protected:                      // Virtual put area functions, as defined in
00185                                 // 27.5.2.4.5 of the standard.
00186 
00187   // Writes up to __n characters.  Return value is the number of characters
00188   // written.
00189   virtual streamsize xsputn(const char_type* __s, streamsize __n);
00190 
00191   // Extension: writes up to __n copies of __c.  Return value is the number
00192   // of characters written.
00193   virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
00194 
00195   // Called when there is no write position.  All subclasses are expected to
00196   // override this virtual member function.
00197   virtual int_type overflow(int_type = traits_type::eof());
00198 
00199 public:                         // Public members for writing characters.
00200   // Write a single character.
00201   int_type sputc(char_type __c) {
00202     return ((_M_pnext < _M_pend) ? _Traits::to_int_type(*_M_pnext++ = __c)
00203       : this->overflow(_Traits::to_int_type(__c)));
00204   }
00205 
00206   // Write __n characters.
00207   streamsize sputn(const char_type* __s, streamsize __n)
00208   { return this->xsputn(__s, __n); }
00209 
00210   // Extension: write __n copies of __c.
00211   streamsize _M_sputnc(char_type __c, streamsize __n)
00212   { return this->_M_xsputnc(__c, __n); }
00213 
00214 private:                        // Helper functions.
00215   int_type _M_snextc_aux();
00216 
00217 public:                         // Public members for reading characters.
00218   streamsize in_avail() {
00219     return (_M_gnext < _M_gend) ? (_M_gend - _M_gnext) : this->showmanyc();
00220   }
00221 
00222   // Advance to the next character and return it.
00223   int_type snextc() {
00224   return ( _M_gend - _M_gnext > 1 ?
00225              _Traits::to_int_type(*++_M_gnext) :
00226              this->_M_snextc_aux());
00227   }
00228 
00229   // Return the current character and advance to the next.
00230   int_type sbumpc() {
00231     return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext++)
00232       : this->uflow();
00233   }
00234 
00235   // Return the current character without advancing to the next.
00236   int_type sgetc() {
00237     return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext)
00238       : this->underflow();
00239   }
00240 
00241   streamsize sgetn(char_type* __s, streamsize __n)
00242   { return this->xsgetn(__s, __n); }
00243 
00244   int_type sputbackc(char_type __c) {
00245     return ((_M_gbegin < _M_gnext) && _Traits::eq(__c, *(_M_gnext - 1)))
00246       ? _Traits::to_int_type(*--_M_gnext)
00247       : this->pbackfail(_Traits::to_int_type(__c));
00248   }
00249 
00250   int_type sungetc() {
00251     return (_M_gbegin < _M_gnext)
00252       ? _Traits::to_int_type(*--_M_gnext)
00253       : this->pbackfail();
00254   }
00255 
00256 protected:                      // Virtual locale functions.
00257 
00258   // This is a hook, called by pubimbue() just before pubimbue()
00259   // sets the streambuf's locale to __loc.  Note that imbue should
00260   // not (and cannot, since it has no access to streambuf's private
00261   // members) set the streambuf's locale itself.
00262   virtual void imbue(const locale&);
00263 
00264 public:                         // Locale-related functions.
00265   locale pubimbue(const locale&);
00266   locale getloc() const { return _M_locale; }
00267 
00268 #if !defined (_STLP_NO_ANACHRONISMS)
00269   void stossc() { this->sbumpc(); }
00270 #endif
00271 #if defined (__MVS__) || defined (__OS400__)
00272 private: // Data members.
00273 
00274   char_type* _M_gbegin; // Beginning of get area
00275   char_type* _M_gnext; // Current position within the get area
00276   char_type* _M_gend; // End of get area
00277 
00278   char_type* _M_pbegin; // Beginning of put area
00279   char_type* _M_pnext; // Current position within the put area
00280   char_type* _M_pend; // End of put area
00281 #endif
00282 };
00283 
00284 #if defined (_STLP_USE_TEMPLATE_EXPORT)
00285 _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<char, char_traits<char> >;
00286 #  if !defined (_STLP_NO_WCHAR_T)
00287 _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<wchar_t, char_traits<wchar_t> >;
00288 #  endif // _STLP_NO_WCHAR_T
00289 #endif // _STLP_USE_TEMPLATE_EXPORT
00290 
00291 _STLP_END_NAMESPACE
00292 
00293 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
00294 #  include <stl/_streambuf.c>
00295 #endif
00296 
00297 #endif
00298 
00299 // Local Variables:
00300 // mode:C++
00301 // End:



Generated on Mon Mar 10 15:32:37 2008 by  doxygen 1.5.1