epoc32/include/tools/stlport/stl/_streambuf.h
author William Roberts <williamr@symbian.org>
Wed, 31 Mar 2010 12:33:34 +0100
branchSymbian3
changeset 4 837f303aceeb
permissions -rw-r--r--
Current Symbian^3 public API header files (from PDK 3.0.h)
This is the epoc32/include tree with the "platform" subtrees removed, and
all but a selected few mbg and rsg files removed.
     1 /*
     2  * Copyright (c) 1999
     3  * Silicon Graphics Computer Systems, Inc.
     4  *
     5  * Copyright (c) 1999
     6  * Boris Fomitchev
     7  *
     8  * This material is provided "as is", with absolutely no warranty expressed
     9  * or implied. Any use is at your own risk.
    10  *
    11  * Permission to use or copy this software for any purpose is hereby granted
    12  * without fee, provided the above notices are retained on all copies.
    13  * Permission to modify the code and to distribute modified code is granted,
    14  * provided the above notices are retained, and a notice that the code was
    15  * modified is included with the above copyright notice.
    16  *
    17  */
    18 #ifndef _STLP_INTERNAL_STREAMBUF
    19 #define _STLP_INTERNAL_STREAMBUF
    20 
    21 #ifndef _STLP_IOS_BASE_H
    22 #  include <stl/_ios_base.h>      // Needed for ios_base bitfield members.
    23 #endif                            // <ios_base> includes <iosfwd>.
    24 
    25 _STLP_BEGIN_NAMESPACE
    26 
    27 //----------------------------------------------------------------------
    28 // Class basic_streambuf<>, the base class of the streambuf hierarchy.
    29 
    30 // A basic_streambuf<> manages an input (get) area and an output (put)
    31 // area.  Each is described by three pointers: a beginning, an end, and a
    32 // current position.  basic_streambuf<> contains some very simple member
    33 // functions that manipulate those six pointers, but almost all of the real
    34 // functionality gets delegated to protected virtual member functions.
    35 // All of the public member functions are inline, and most of the protected
    36 // member functions are virtual.
    37 
    38 // Although basic_streambuf<> is not abstract, it is useful only as a base
    39 // class.  Its virtual member functions have default definitions such that
    40 // reading from a basic_streambuf<> will always yield EOF, and writing to a
    41 // basic_streambuf<> will always fail.
    42 
    43 // The second template parameter, _Traits, defaults to char_traits<_CharT>.
    44 // The default is declared in header <iosfwd>, and it isn't declared here
    45 // because C++ language rules do not allow it to be declared twice.
    46 
    47 template <class _CharT, class _Traits>
    48 class basic_streambuf {
    49   friend class basic_istream<_CharT, _Traits>;
    50   friend class basic_ostream<_CharT, _Traits>;
    51 
    52 public:                         // Typedefs.
    53   typedef _CharT                     char_type;
    54   typedef typename _Traits::int_type int_type;
    55   typedef typename _Traits::pos_type pos_type;
    56   typedef typename _Traits::off_type off_type;
    57   typedef _Traits                    traits_type;
    58 
    59 private:                        // Data members.
    60 
    61   char_type* _M_gbegin;         // Beginning of get area
    62   char_type* _M_gnext;          // Current position within the get area
    63   char_type* _M_gend;           // End of get area
    64 
    65   char_type* _M_pbegin;         // Beginning of put area
    66   char_type* _M_pnext;          // Current position within the put area
    67   char_type* _M_pend;           // End of put area
    68 
    69   locale _M_locale;             // The streambuf's locale object
    70 
    71 //public:                         // Extension: locking, for thread safety.
    72 //  _STLP_mutex _M_lock;
    73 
    74 public:                         // Destructor.
    75   virtual ~basic_streambuf();
    76 
    77 protected:                      // The default constructor.
    78   basic_streambuf()
    79 #if defined (_STLP_MSVC) && (_STLP_MSVC < 1300) && defined (_STLP_USE_STATIC_LIB)
    80     //We make it inline to avoid unresolved symbol.
    81     : _M_gbegin(0), _M_gnext(0), _M_gend(0),
    82       _M_pbegin(0), _M_pnext(0), _M_pend(0),
    83       _M_locale()
    84   {}
    85 #else
    86   ;
    87 #endif
    88 
    89 protected:                      // Protected interface to the get area.
    90   char_type* eback() const { return _M_gbegin; } // Beginning
    91   char_type* gptr()  const { return _M_gnext; }  // Current position
    92   char_type* egptr() const { return _M_gend; }   // End
    93 
    94   void gbump(int __n) { _M_gnext += __n; }
    95   void setg(char_type* __gbegin, char_type* __gnext, char_type* __gend) {
    96     _M_gbegin = __gbegin;
    97     _M_gnext  = __gnext;
    98     _M_gend   = __gend;
    99   }
   100 
   101 public:
   102   // An alternate public interface to the above functions
   103   // which allows us to avoid using templated friends which
   104   // are not supported on some compilers.
   105   char_type* _M_eback() const { return eback(); }
   106   char_type* _M_gptr()  const { return gptr(); }
   107   char_type* _M_egptr() const { return egptr(); }
   108   void _M_gbump(int __n)      { gbump(__n); }
   109   void _M_setg(char_type* __gbegin, char_type* __gnext, char_type* __gend)
   110   { this->setg(__gbegin, __gnext, __gend); }
   111 
   112 protected:                      // Protected interface to the put area
   113 
   114   char_type* pbase() const { return _M_pbegin; } // Beginning
   115   char_type* pptr()  const { return _M_pnext; }  // Current position
   116   char_type* epptr() const { return _M_pend; }   // End
   117 
   118   void pbump(int __n) { _M_pnext += __n; }
   119   void setp(char_type* __pbegin, char_type* __pend) {
   120     _M_pbegin = __pbegin;
   121     _M_pnext  = __pbegin;
   122     _M_pend   = __pend;
   123   }
   124 
   125 protected:                      // Virtual buffer management functions.
   126 
   127   virtual basic_streambuf<_CharT, _Traits>* setbuf(char_type*, streamsize);
   128 
   129   // Alters the stream position, using an integer offset.  In this
   130   // class seekoff does nothing; subclasses are expected to override it.
   131   virtual pos_type seekoff(off_type, ios_base::seekdir,
   132                            ios_base::openmode = ios_base::in | ios_base::out);
   133 
   134   // Alters the stream position, using a previously obtained streampos.  In
   135   // this class seekpos does nothing; subclasses are expected to override it.
   136   virtual pos_type
   137   seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
   138 
   139   // Synchronizes (i.e. flushes) the buffer.  All subclasses are expected to
   140   // override this virtual member function.
   141   virtual int sync();
   142 
   143 
   144 public:                         // Buffer management.
   145   basic_streambuf<_CharT, _Traits>* pubsetbuf(char_type* __s, streamsize __n)
   146   { return this->setbuf(__s, __n); }
   147 
   148   pos_type pubseekoff(off_type __offset, ios_base::seekdir __way,
   149                       ios_base::openmode __mod = ios_base::in | ios_base::out)
   150   { return this->seekoff(__offset, __way, __mod); }
   151 
   152   pos_type pubseekpos(pos_type __sp,
   153                       ios_base::openmode __mod = ios_base::in | ios_base::out)
   154   { return this->seekpos(__sp, __mod); }
   155 
   156   int pubsync() { return this->sync(); }
   157 
   158 protected:                      // Virtual get area functions, as defined in
   159                                 // 17.5.2.4.3 and 17.5.2.4.4 of the standard.
   160   // Returns a lower bound on the number of characters that we can read,
   161   // with underflow, before reaching end of file.  (-1 is a special value:
   162   // it means that underflow will fail.)  Most subclasses should probably
   163   // override this virtual member function.
   164   virtual streamsize showmanyc();
   165 
   166   // Reads up to __n characters.  Return value is the number of
   167   // characters read.
   168   virtual streamsize xsgetn(char_type* __s, streamsize __n);
   169 
   170   // Called when there is no read position, i.e. when gptr() is null
   171   // or when gptr() >= egptr().  Subclasses are expected to override
   172   // this virtual member function.
   173   virtual int_type underflow();
   174 
   175   // Similar to underflow(), but used for unbuffered input.  Most
   176   // subclasses should probably override this virtual member function.
   177   virtual int_type uflow();
   178 
   179   // Called when there is no putback position, i.e. when gptr() is null
   180   // or when gptr() == eback().  All subclasses are expected to override
   181   // this virtual member function.
   182   virtual int_type pbackfail(int_type = traits_type::eof());
   183 
   184 protected:                      // Virtual put area functions, as defined in
   185                                 // 27.5.2.4.5 of the standard.
   186 
   187   // Writes up to __n characters.  Return value is the number of characters
   188   // written.
   189   virtual streamsize xsputn(const char_type* __s, streamsize __n);
   190 
   191   // Extension: writes up to __n copies of __c.  Return value is the number
   192   // of characters written.
   193   virtual streamsize _M_xsputnc(char_type __c, streamsize __n);
   194 
   195   // Called when there is no write position.  All subclasses are expected to
   196   // override this virtual member function.
   197   virtual int_type overflow(int_type = traits_type::eof());
   198 
   199 public:                         // Public members for writing characters.
   200   // Write a single character.
   201   int_type sputc(char_type __c) {
   202     return ((_M_pnext < _M_pend) ? _Traits::to_int_type(*_M_pnext++ = __c)
   203       : this->overflow(_Traits::to_int_type(__c)));
   204   }
   205 
   206   // Write __n characters.
   207   streamsize sputn(const char_type* __s, streamsize __n)
   208   { return this->xsputn(__s, __n); }
   209 
   210   // Extension: write __n copies of __c.
   211   streamsize _M_sputnc(char_type __c, streamsize __n)
   212   { return this->_M_xsputnc(__c, __n); }
   213 
   214 private:                        // Helper functions.
   215   int_type _M_snextc_aux();
   216 
   217 public:                         // Public members for reading characters.
   218   streamsize in_avail() {
   219     return (_M_gnext < _M_gend) ? (_M_gend - _M_gnext) : this->showmanyc();
   220   }
   221 
   222   // Advance to the next character and return it.
   223   int_type snextc() {
   224   return ( _M_gend - _M_gnext > 1 ?
   225              _Traits::to_int_type(*++_M_gnext) :
   226              this->_M_snextc_aux());
   227   }
   228 
   229   // Return the current character and advance to the next.
   230   int_type sbumpc() {
   231     return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext++)
   232       : this->uflow();
   233   }
   234 
   235   // Return the current character without advancing to the next.
   236   int_type sgetc() {
   237     return _M_gnext < _M_gend ? _Traits::to_int_type(*_M_gnext)
   238       : this->underflow();
   239   }
   240 
   241   streamsize sgetn(char_type* __s, streamsize __n)
   242   { return this->xsgetn(__s, __n); }
   243 
   244   int_type sputbackc(char_type __c) {
   245     return ((_M_gbegin < _M_gnext) && _Traits::eq(__c, *(_M_gnext - 1)))
   246       ? _Traits::to_int_type(*--_M_gnext)
   247       : this->pbackfail(_Traits::to_int_type(__c));
   248   }
   249 
   250   int_type sungetc() {
   251     return (_M_gbegin < _M_gnext)
   252       ? _Traits::to_int_type(*--_M_gnext)
   253       : this->pbackfail();
   254   }
   255 
   256 protected:                      // Virtual locale functions.
   257 
   258   // This is a hook, called by pubimbue() just before pubimbue()
   259   // sets the streambuf's locale to __loc.  Note that imbue should
   260   // not (and cannot, since it has no access to streambuf's private
   261   // members) set the streambuf's locale itself.
   262   virtual void imbue(const locale&);
   263 
   264 public:                         // Locale-related functions.
   265   locale pubimbue(const locale&);
   266   locale getloc() const { return _M_locale; }
   267 
   268 #if !defined (_STLP_NO_ANACHRONISMS)
   269   void stossc() { this->sbumpc(); }
   270 #endif
   271 #if defined (__MVS__) || defined (__OS400__)
   272 private: // Data members.
   273 
   274   char_type* _M_gbegin; // Beginning of get area
   275   char_type* _M_gnext; // Current position within the get area
   276   char_type* _M_gend; // End of get area
   277 
   278   char_type* _M_pbegin; // Beginning of put area
   279   char_type* _M_pnext; // Current position within the put area
   280   char_type* _M_pend; // End of put area
   281 #endif
   282 };
   283 
   284 #if defined (_STLP_USE_TEMPLATE_EXPORT)
   285 _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<char, char_traits<char> >;
   286 #  if !defined (_STLP_NO_WCHAR_T)
   287 _STLP_EXPORT_TEMPLATE_CLASS basic_streambuf<wchar_t, char_traits<wchar_t> >;
   288 #  endif // _STLP_NO_WCHAR_T
   289 #endif // _STLP_USE_TEMPLATE_EXPORT
   290 
   291 _STLP_END_NAMESPACE
   292 
   293 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION)
   294 #  include <stl/_streambuf.c>
   295 #endif
   296 
   297 #endif
   298 
   299 // Local Variables:
   300 // mode:C++
   301 // End: