Home | History | Annotate | Line # | Download | only in std
      1 // Stream buffer classes -*- C++ -*-
      2 
      3 // Copyright (C) 1997-2024 Free Software Foundation, Inc.
      4 //
      5 // This file is part of the GNU ISO C++ Library.  This library is free
      6 // software; you can redistribute it and/or modify it under the
      7 // terms of the GNU General Public License as published by the
      8 // Free Software Foundation; either version 3, or (at your option)
      9 // any later version.
     10 
     11 // This library is distributed in the hope that it will be useful,
     12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     14 // GNU General Public License for more details.
     15 
     16 // Under Section 7 of GPL version 3, you are granted additional
     17 // permissions described in the GCC Runtime Library Exception, version
     18 // 3.1, as published by the Free Software Foundation.
     19 
     20 // You should have received a copy of the GNU General Public License and
     21 // a copy of the GCC Runtime Library Exception along with this program;
     22 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
     23 // <http://www.gnu.org/licenses/>.
     24 
     25 /** @file include/streambuf
     26  *  This is a Standard C++ Library header.
     27  */
     28 
     29 //
     30 // ISO C++ 14882: 27.5  Stream buffers
     31 //
     32 
     33 #ifndef _GLIBXX_STREAMBUF
     34 #define _GLIBXX_STREAMBUF 1
     35 
     36 #pragma GCC system_header
     37 
     38 #include <bits/requires_hosted.h> // iostreams
     39 
     40 #include <bits/c++config.h>
     41 #include <iosfwd>
     42 #include <bits/localefwd.h>
     43 #include <bits/ios_base.h>
     44 #include <bits/cpp_type_traits.h>
     45 #include <ext/type_traits.h>
     46 
     47 namespace std _GLIBCXX_VISIBILITY(default)
     48 {
     49 _GLIBCXX_BEGIN_NAMESPACE_VERSION
     50 
     51 #define _IsUnused __attribute__ ((__unused__))
     52 
     53   template<typename _CharT, typename _Traits>
     54     streamsize
     55     __copy_streambufs_eof(basic_streambuf<_CharT, _Traits>*,
     56 			  basic_streambuf<_CharT, _Traits>*, bool&);
     57 
     58   /**
     59    *  @brief  The actual work of input and output (interface).
     60    *  @ingroup io
     61    *
     62    *  @tparam _CharT  Type of character stream.
     63    *  @tparam _Traits  Traits for character type, defaults to
     64    *                   char_traits<_CharT>.
     65    *
     66    *  This is a base class.  Derived stream buffers each control a
     67    *  pair of character sequences:  one for input, and one for output.
     68    *
     69    *  Section [27.5.1] of the standard describes the requirements and
     70    *  behavior of stream buffer classes.  That section (three paragraphs)
     71    *  is reproduced here, for simplicity and accuracy.
     72    *
     73    *  -# Stream buffers can impose various constraints on the sequences
     74    *     they control.  Some constraints are:
     75    *     - The controlled input sequence can be not readable.
     76    *     - The controlled output sequence can be not writable.
     77    *     - The controlled sequences can be associated with the contents of
     78    *       other representations for character sequences, such as external
     79    *       files.
     80    *     - The controlled sequences can support operations @e directly to or
     81    *       from associated sequences.
     82    *     - The controlled sequences can impose limitations on how the
     83    *       program can read characters from a sequence, write characters to
     84    *       a sequence, put characters back into an input sequence, or alter
     85    *       the stream position.
     86    *     .
     87    *  -# Each sequence is characterized by three pointers which, if non-null,
     88    *     all point into the same @c charT array object.  The array object
     89    *     represents, at any moment, a (sub)sequence of characters from the
     90    *     sequence.  Operations performed on a sequence alter the values
     91    *     stored in these pointers, perform reads and writes directly to or
     92    *     from associated sequences, and alter <em>the stream position</em> and
     93    *     conversion state as needed to maintain this subsequence relationship.
     94    *     The three pointers are:
     95    *     - the <em>beginning pointer</em>, or lowest element address in the
     96    *       array (called @e xbeg here);
     97    *     - the <em>next pointer</em>, or next element address that is a
     98    *       current candidate for reading or writing (called @e xnext here);
     99    *     - the <em>end pointer</em>, or first element address beyond the
    100    *       end of the array (called @e xend here).
    101    *     .
    102    *  -# The following semantic constraints shall always apply for any set
    103    *     of three pointers for a sequence, using the pointer names given
    104    *     immediately above:
    105    *     - If @e xnext is not a null pointer, then @e xbeg and @e xend shall
    106    *       also be non-null pointers into the same @c charT array, as
    107    *       described above; otherwise, @e xbeg and @e xend shall also be null.
    108    *     - If @e xnext is not a null pointer and @e xnext < @e xend for an
    109    *       output sequence, then a <em>write position</em> is available.
    110    *       In this case, @e *xnext shall be assignable as the next element
    111    *       to write (to put, or to store a character value, into the sequence).
    112    *     - If @e xnext is not a null pointer and @e xbeg < @e xnext for an
    113    *       input sequence, then a <em>putback position</em> is available.
    114    *       In this case, @e xnext[-1] shall have a defined value and is the
    115    *       next (preceding) element to store a character that is put back
    116    *       into the input sequence.
    117    *     - If @e xnext is not a null pointer and @e xnext< @e xend for an
    118    *       input sequence, then a <em>read position</em> is available.
    119    *       In this case, @e *xnext shall have a defined value and is the
    120    *       next element to read (to get, or to obtain a character value,
    121    *       from the sequence).
    122   */
    123   template<typename _CharT, typename _Traits>
    124     class basic_streambuf
    125     {
    126     public:
    127       ///@{
    128       /**
    129        *  These are standard types.  They permit a standardized way of
    130        *  referring to names of (or names dependent on) the template
    131        *  parameters, which are specific to the implementation.
    132       */
    133       typedef _CharT 					char_type;
    134       typedef _Traits 					traits_type;
    135       typedef typename traits_type::int_type 		int_type;
    136       typedef typename traits_type::pos_type 		pos_type;
    137       typedef typename traits_type::off_type 		off_type;
    138       ///@}
    139 
    140       ///@{
    141       /// This is a non-standard type.
    142       typedef basic_streambuf<char_type, traits_type>  	__streambuf_type;
    143       ///@}
    144 
    145       friend class basic_ios<char_type, traits_type>;
    146       friend class basic_istream<char_type, traits_type>;
    147       friend class basic_ostream<char_type, traits_type>;
    148       friend class istreambuf_iterator<char_type, traits_type>;
    149       friend class ostreambuf_iterator<char_type, traits_type>;
    150 
    151       friend streamsize
    152       __copy_streambufs_eof<>(basic_streambuf*, basic_streambuf*, bool&);
    153 
    154       template<bool _IsMove, typename _CharT2>
    155         friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
    156 					       _CharT2*>::__type
    157         __copy_move_a2(istreambuf_iterator<_CharT2>,
    158 		       istreambuf_iterator<_CharT2>, _CharT2*);
    159 
    160       template<typename _CharT2>
    161         friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
    162 				  istreambuf_iterator<_CharT2> >::__type
    163         find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
    164 	     const _CharT2&);
    165 
    166       template<typename _CharT2, typename _Distance>
    167         friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
    168 					       void>::__type
    169         advance(istreambuf_iterator<_CharT2>&, _Distance);
    170 
    171       friend void __istream_extract(istream&, char*, streamsize);
    172 
    173       template<typename _CharT2, typename _Traits2, typename _Alloc>
    174         friend basic_istream<_CharT2, _Traits2>&
    175         operator>>(basic_istream<_CharT2, _Traits2>&,
    176 		   basic_string<_CharT2, _Traits2, _Alloc>&);
    177 
    178       template<typename _CharT2, typename _Traits2, typename _Alloc>
    179         friend basic_istream<_CharT2, _Traits2>&
    180         getline(basic_istream<_CharT2, _Traits2>&,
    181 		basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2);
    182 
    183     protected:
    184       /*
    185        *  This is based on _IO_FILE, just reordered to be more consistent,
    186        *  and is intended to be the most minimal abstraction for an
    187        *  internal buffer.
    188        *  -  get == input == read
    189        *  -  put == output == write
    190       */
    191       char_type* 		_M_in_beg;     ///< Start of get area.
    192       char_type* 		_M_in_cur;     ///< Current read area.
    193       char_type* 		_M_in_end;     ///< End of get area.
    194       char_type* 		_M_out_beg;    ///< Start of put area.
    195       char_type* 		_M_out_cur;    ///< Current put area.
    196       char_type* 		_M_out_end;    ///< End of put area.
    197 
    198       /// Current locale setting.
    199       locale 			_M_buf_locale;
    200 
    201   public:
    202       /// Destructor deallocates no buffer space.
    203       virtual
    204       ~basic_streambuf()
    205       { }
    206 
    207       // [27.5.2.2.1] locales
    208       /**
    209        *  @brief  Entry point for imbue().
    210        *  @param  __loc  The new locale.
    211        *  @return  The previous locale.
    212        *
    213        *  Calls the derived imbue(__loc).
    214       */
    215       locale
    216       pubimbue(const locale& __loc)
    217       {
    218 	locale __tmp(this->getloc());
    219 	this->imbue(__loc);
    220 	_M_buf_locale = __loc;
    221 	return __tmp;
    222       }
    223 
    224       /**
    225        *  @brief  Locale access.
    226        *  @return  The current locale in effect.
    227        *
    228        *  If pubimbue(loc) has been called, then the most recent @c loc
    229        *  is returned.  Otherwise the global locale in effect at the time
    230        *  of construction is returned.
    231       */
    232       locale
    233       getloc() const
    234       { return _M_buf_locale; }
    235 
    236       // [27.5.2.2.2] buffer management and positioning
    237       ///@{
    238       /**
    239        *  @brief  Entry points for derived buffer functions.
    240        *
    241        *  The public versions of @c pubfoo dispatch to the protected
    242        *  derived @c foo member functions, passing the arguments (if any)
    243        *  and returning the result unchanged.
    244       */
    245       basic_streambuf*
    246       pubsetbuf(char_type* __s, streamsize __n)
    247       { return this->setbuf(__s, __n); }
    248 
    249       /**
    250        *  @brief  Alters the stream position.
    251        *  @param  __off  Offset.
    252        *  @param  __way  Value for ios_base::seekdir.
    253        *  @param  __mode Value for ios_base::openmode.
    254        *
    255        *  Calls virtual seekoff function.
    256       */
    257       pos_type
    258       pubseekoff(off_type __off, ios_base::seekdir __way,
    259 		 ios_base::openmode __mode = ios_base::in | ios_base::out)
    260       { return this->seekoff(__off, __way, __mode); }
    261 
    262       /**
    263        *  @brief  Alters the stream position.
    264        *  @param  __sp  Position
    265        *  @param  __mode Value for ios_base::openmode.
    266        *
    267        *  Calls virtual seekpos function.
    268       */
    269       pos_type
    270       pubseekpos(pos_type __sp,
    271 		 ios_base::openmode __mode = ios_base::in | ios_base::out)
    272       { return this->seekpos(__sp, __mode); }
    273 
    274       /**
    275        *  @brief  Calls virtual sync function.
    276       */
    277       int
    278       pubsync() { return this->sync(); }
    279       ///@}
    280 
    281       // [27.5.2.2.3] get area
    282       /**
    283        *  @brief  Looking ahead into the stream.
    284        *  @return  The number of characters available.
    285        *
    286        *  If a read position is available, returns the number of characters
    287        *  available for reading before the buffer must be refilled.
    288        *  Otherwise returns the derived @c showmanyc().
    289       */
    290       streamsize
    291       in_avail()
    292       {
    293 	const streamsize __ret = this->egptr() - this->gptr();
    294 	return __ret ? __ret : this->showmanyc();
    295       }
    296 
    297       /**
    298        *  @brief  Getting the next character.
    299        *  @return  The next character, or eof.
    300        *
    301        *  Calls @c sbumpc(), and if that function returns
    302        *  @c traits::eof(), so does this function.  Otherwise, @c sgetc().
    303       */
    304       int_type
    305       snextc()
    306       {
    307 	int_type __ret = traits_type::eof();
    308 	if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
    309 						       __ret), true))
    310 	  __ret = this->sgetc();
    311 	return __ret;
    312       }
    313 
    314       /**
    315        *  @brief  Getting the next character.
    316        *  @return  The next character, or eof.
    317        *
    318        *  If the input read position is available, returns that character
    319        *  and increments the read pointer, otherwise calls and returns
    320        *  @c uflow().
    321       */
    322       int_type
    323       sbumpc()
    324       {
    325 	int_type __ret;
    326 	if (__builtin_expect(this->gptr() < this->egptr(), true))
    327 	  {
    328 	    __ret = traits_type::to_int_type(*this->gptr());
    329 	    this->gbump(1);
    330 	  }
    331 	else
    332 	  __ret = this->uflow();
    333 	return __ret;
    334       }
    335 
    336       /**
    337        *  @brief  Getting the next character.
    338        *  @return  The next character, or eof.
    339        *
    340        *  If the input read position is available, returns that character,
    341        *  otherwise calls and returns @c underflow().  Does not move the
    342        *  read position after fetching the character.
    343       */
    344       int_type
    345       sgetc()
    346       {
    347 	int_type __ret;
    348 	if (__builtin_expect(this->gptr() < this->egptr(), true))
    349 	  __ret = traits_type::to_int_type(*this->gptr());
    350 	else
    351 	  __ret = this->underflow();
    352 	return __ret;
    353       }
    354 
    355       /**
    356        *  @brief  Entry point for xsgetn.
    357        *  @param  __s  A buffer area.
    358        *  @param  __n  A count.
    359        *
    360        *  Returns xsgetn(__s,__n).  The effect is to fill @a __s[0] through
    361        *  @a __s[__n-1] with characters from the input sequence, if possible.
    362       */
    363       streamsize
    364       sgetn(char_type* __s, streamsize __n)
    365       { return this->xsgetn(__s, __n); }
    366 
    367       // [27.5.2.2.4] putback
    368       /**
    369        *  @brief  Pushing characters back into the input stream.
    370        *  @param  __c  The character to push back.
    371        *  @return  The previous character, if possible.
    372        *
    373        *  Similar to sungetc(), but @a __c is pushed onto the stream
    374        *  instead of <em>the previous character.</em> If successful,
    375        *  the next character fetched from the input stream will be @a
    376        *  __c.
    377       */
    378       int_type
    379       sputbackc(char_type __c)
    380       {
    381 	int_type __ret;
    382 	const bool __testpos = this->eback() < this->gptr();
    383 	if (__builtin_expect(!__testpos ||
    384 			     !traits_type::eq(__c, this->gptr()[-1]), false))
    385 	  __ret = this->pbackfail(traits_type::to_int_type(__c));
    386 	else
    387 	  {
    388 	    this->gbump(-1);
    389 	    __ret = traits_type::to_int_type(*this->gptr());
    390 	  }
    391 	return __ret;
    392       }
    393 
    394       /**
    395        *  @brief  Moving backwards in the input stream.
    396        *  @return  The previous character, if possible.
    397        *
    398        *  If a putback position is available, this function decrements
    399        *  the input pointer and returns that character.  Otherwise,
    400        *  calls and returns pbackfail().  The effect is to @a unget
    401        *  the last character @a gotten.
    402       */
    403       int_type
    404       sungetc()
    405       {
    406 	int_type __ret;
    407 	if (__builtin_expect(this->eback() < this->gptr(), true))
    408 	  {
    409 	    this->gbump(-1);
    410 	    __ret = traits_type::to_int_type(*this->gptr());
    411 	  }
    412 	else
    413 	  __ret = this->pbackfail();
    414 	return __ret;
    415       }
    416 
    417       // [27.5.2.2.5] put area
    418       /**
    419        *  @brief  Entry point for all single-character output functions.
    420        *  @param  __c  A character to output.
    421        *  @return  @a __c, if possible.
    422        *
    423        *  One of two public output functions.
    424        *
    425        *  If a write position is available for the output sequence (i.e.,
    426        *  the buffer is not full), stores @a __c in that position, increments
    427        *  the position, and returns @c traits::to_int_type(__c).  If a write
    428        *  position is not available, returns @c overflow(__c).
    429       */
    430       int_type
    431       sputc(char_type __c)
    432       {
    433 	int_type __ret;
    434 	if (__builtin_expect(this->pptr() < this->epptr(), true))
    435 	  {
    436 	    *this->pptr() = __c;
    437 	    this->pbump(1);
    438 	    __ret = traits_type::to_int_type(__c);
    439 	  }
    440 	else
    441 	  __ret = this->overflow(traits_type::to_int_type(__c));
    442 	return __ret;
    443       }
    444 
    445       /**
    446        *  @brief  Entry point for all single-character output functions.
    447        *  @param  __s  A buffer read area.
    448        *  @param  __n  A count.
    449        *
    450        *  One of two public output functions.
    451        *
    452        *
    453        *  Returns xsputn(__s,__n).  The effect is to write @a __s[0] through
    454        *  @a __s[__n-1] to the output sequence, if possible.
    455       */
    456       streamsize
    457       sputn(const char_type* __s, streamsize __n)
    458       { return this->xsputn(__s, __n); }
    459 
    460     protected:
    461       /**
    462        *  @brief  Base constructor.
    463        *
    464        *  Only called from derived constructors, and sets up all the
    465        *  buffer data to zero, including the pointers described in the
    466        *  basic_streambuf class description.  Note that, as a result,
    467        *  - the class starts with no read nor write positions available,
    468        *  - this is not an error
    469       */
    470       basic_streambuf()
    471       : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
    472       _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
    473       _M_buf_locale(locale())
    474       { }
    475 
    476       // [27.5.2.3.1] get area access
    477       ///@{
    478       /**
    479        *  @brief  Access to the get area.
    480        *
    481        *  These functions are only available to other protected functions,
    482        *  including derived classes.
    483        *
    484        *  - eback() returns the beginning pointer for the input sequence
    485        *  - gptr() returns the next pointer for the input sequence
    486        *  - egptr() returns the end pointer for the input sequence
    487       */
    488       char_type*
    489       eback() const { return _M_in_beg; }
    490 
    491 // Required to silence false-positive warnings originating from istream_iterator::operator++,
    492 // see PR105580.
    493 #pragma GCC diagnostic push
    494 #pragma GCC diagnostic ignored "-Wnull-dereference"
    495       char_type*
    496       gptr()  const { return _M_in_cur;  }
    497 
    498       char_type*
    499       egptr() const { return _M_in_end; }
    500       ///@}
    501 
    502       /**
    503        *  @brief  Moving the read position.
    504        *  @param  __n  The delta by which to move.
    505        *
    506        *  This just advances the read position without returning any data.
    507       */
    508       void
    509       gbump(int __n) { _M_in_cur += __n; }
    510 #pragma GCC diagnostic pop
    511 
    512       /**
    513        *  @brief  Setting the three read area pointers.
    514        *  @param  __gbeg  A pointer.
    515        *  @param  __gnext  A pointer.
    516        *  @param  __gend  A pointer.
    517        *  @post  @a __gbeg == @c eback(), @a __gnext == @c gptr(), and
    518        *         @a __gend == @c egptr()
    519       */
    520       void
    521       setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
    522       {
    523 	_M_in_beg = __gbeg;
    524 	_M_in_cur = __gnext;
    525 	_M_in_end = __gend;
    526       }
    527 
    528       // [27.5.2.3.2] put area access
    529       ///@{
    530       /**
    531        *  @brief  Access to the put area.
    532        *
    533        *  These functions are only available to other protected functions,
    534        *  including derived classes.
    535        *
    536        *  - pbase() returns the beginning pointer for the output sequence
    537        *  - pptr() returns the next pointer for the output sequence
    538        *  - epptr() returns the end pointer for the output sequence
    539       */
    540       char_type*
    541       pbase() const { return _M_out_beg; }
    542 
    543       char_type*
    544       pptr() const { return _M_out_cur; }
    545 
    546       char_type*
    547       epptr() const { return _M_out_end; }
    548       ///@}
    549 
    550       /**
    551        *  @brief  Moving the write position.
    552        *  @param  __n  The delta by which to move.
    553        *
    554        *  This just advances the write position without returning any data.
    555       */
    556       void
    557       pbump(int __n) { _M_out_cur += __n; }
    558 
    559       /**
    560        *  @brief  Setting the three write area pointers.
    561        *  @param  __pbeg  A pointer.
    562        *  @param  __pend  A pointer.
    563        *  @post  @a __pbeg == @c pbase(), @a __pbeg == @c pptr(), and
    564        *         @a __pend == @c epptr()
    565       */
    566       void
    567       setp(char_type* __pbeg, char_type* __pend)
    568       {
    569 	_M_out_beg = _M_out_cur = __pbeg;
    570 	_M_out_end = __pend;
    571       }
    572 
    573       // [27.5.2.4] virtual functions
    574       // [27.5.2.4.1] locales
    575       /**
    576        *  @brief  Changes translations.
    577        *  @param  __loc  A new locale.
    578        *
    579        *  Translations done during I/O which depend on the current
    580        *  locale are changed by this call.  The standard adds,
    581        *  <em>Between invocations of this function a class derived
    582        *  from streambuf can safely cache results of calls to locale
    583        *  functions and to members of facets so obtained.</em>
    584        *
    585        *  @note  Base class version does nothing.
    586       */
    587       virtual void
    588       imbue(const locale& __loc _IsUnused)
    589       { }
    590 
    591       // [27.5.2.4.2] buffer management and positioning
    592       /**
    593        *  @brief  Manipulates the buffer.
    594        *
    595        *  Each derived class provides its own appropriate behavior.  See
    596        *  the next-to-last paragraph of
    597        *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
    598        *  for more on this function.
    599        *
    600        *  @note  Base class version does nothing, returns @c this.
    601       */
    602       virtual basic_streambuf<char_type,_Traits>*
    603       setbuf(char_type*, streamsize)
    604       {	return this; }
    605 
    606       /**
    607        *  @brief  Alters the stream positions.
    608        *
    609        *  Each derived class provides its own appropriate behavior.
    610        *  @note  Base class version does nothing, returns a @c pos_type
    611        *         that represents an invalid stream position.
    612       */
    613       virtual pos_type
    614       seekoff(off_type, ios_base::seekdir,
    615 	      ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
    616       { return pos_type(off_type(-1)); }
    617 
    618       /**
    619        *  @brief  Alters the stream positions.
    620        *
    621        *  Each derived class provides its own appropriate behavior.
    622        *  @note  Base class version does nothing, returns a @c pos_type
    623        *         that represents an invalid stream position.
    624       */
    625       virtual pos_type
    626       seekpos(pos_type,
    627 	      ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
    628       { return pos_type(off_type(-1)); }
    629 
    630       /**
    631        *  @brief  Synchronizes the buffer arrays with the controlled sequences.
    632        *  @return  -1 on failure.
    633        *
    634        *  Each derived class provides its own appropriate behavior,
    635        *  including the definition of @a failure.
    636        *  @note  Base class version does nothing, returns zero.
    637       */
    638       virtual int
    639       sync() { return 0; }
    640 
    641       // [27.5.2.4.3] get area
    642       /**
    643        *  @brief  Investigating the data available.
    644        *  @return  An estimate of the number of characters available in the
    645        *           input sequence, or -1.
    646        *
    647        *  <em>If it returns a positive value, then successive calls to
    648        *  @c underflow() will not return @c traits::eof() until at
    649        *  least that number of characters have been supplied.  If @c
    650        *  showmanyc() returns -1, then calls to @c underflow() or @c
    651        *  uflow() will fail.</em> [27.5.2.4.3]/1
    652        *
    653        *  @note  Base class version does nothing, returns zero.
    654        *  @note  The standard adds that <em>the intention is not only that the
    655        *         calls [to underflow or uflow] will not return @c eof() but
    656        *         that they will return immediately.</em>
    657        *  @note  The standard adds that <em>the morphemes of @c showmanyc are
    658        *         @b es-how-many-see, not @b show-manic.</em>
    659       */
    660       virtual streamsize
    661       showmanyc() { return 0; }
    662 
    663       /**
    664        *  @brief  Multiple character extraction.
    665        *  @param  __s  A buffer area.
    666        *  @param  __n  Maximum number of characters to assign.
    667        *  @return  The number of characters assigned.
    668        *
    669        *  Fills @a __s[0] through @a __s[__n-1] with characters from the input
    670        *  sequence, as if by @c sbumpc().  Stops when either @a __n characters
    671        *  have been copied, or when @c traits::eof() would be copied.
    672        *
    673        *  It is expected that derived classes provide a more efficient
    674        *  implementation by overriding this definition.
    675       */
    676       virtual streamsize
    677       xsgetn(char_type* __s, streamsize __n);
    678 
    679       /**
    680        *  @brief  Fetches more data from the controlled sequence.
    681        *  @return  The first character from the <em>pending sequence</em>.
    682        *
    683        *  Informally, this function is called when the input buffer is
    684        *  exhausted (or does not exist, as buffering need not actually be
    685        *  done).  If a buffer exists, it is @a refilled.  In either case, the
    686        *  next available character is returned, or @c traits::eof() to
    687        *  indicate a null pending sequence.
    688        *
    689        *  For a formal definition of the pending sequence, see a good text
    690        *  such as Langer & Kreft, or [27.5.2.4.3]/7-14.
    691        *
    692        *  A functioning input streambuf can be created by overriding only
    693        *  this function (no buffer area will be used).  For an example, see
    694        *  https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html
    695        *
    696        *  @note  Base class version does nothing, returns eof().
    697       */
    698       virtual int_type
    699       underflow()
    700       { return traits_type::eof(); }
    701 
    702       /**
    703        *  @brief  Fetches more data from the controlled sequence.
    704        *  @return  The first character from the <em>pending sequence</em>.
    705        *
    706        *  Informally, this function does the same thing as @c underflow(),
    707        *  and in fact is required to call that function.  It also returns
    708        *  the new character, like @c underflow() does.  However, this
    709        *  function also moves the read position forward by one.
    710       */
    711       virtual int_type
    712       uflow()
    713       {
    714 	int_type __ret = traits_type::eof();
    715 	const bool __testeof = traits_type::eq_int_type(this->underflow(),
    716 							__ret);
    717 	if (!__testeof)
    718 	  {
    719 	    __ret = traits_type::to_int_type(*this->gptr());
    720 	    this->gbump(1);
    721 	  }
    722 	return __ret;
    723       }
    724 
    725       // [27.5.2.4.4] putback
    726       /**
    727        *  @brief  Tries to back up the input sequence.
    728        *  @param  __c  The character to be inserted back into the sequence.
    729        *  @return  eof() on failure, <em>some other value</em> on success
    730        *  @post  The constraints of @c gptr(), @c eback(), and @c pptr()
    731        *         are the same as for @c underflow().
    732        *
    733        *  @note  Base class version does nothing, returns eof().
    734       */
    735       virtual int_type
    736       pbackfail(int_type __c _IsUnused  = traits_type::eof())
    737       { return traits_type::eof(); }
    738 
    739       // Put area:
    740       /**
    741        *  @brief  Multiple character insertion.
    742        *  @param  __s  A buffer area.
    743        *  @param  __n  Maximum number of characters to write.
    744        *  @return  The number of characters written.
    745        *
    746        *  Writes @a __s[0] through @a __s[__n-1] to the output sequence, as if
    747        *  by @c sputc().  Stops when either @a n characters have been
    748        *  copied, or when @c sputc() would return @c traits::eof().
    749        *
    750        *  It is expected that derived classes provide a more efficient
    751        *  implementation by overriding this definition.
    752       */
    753       virtual streamsize
    754       xsputn(const char_type* __s, streamsize __n);
    755 
    756       /**
    757        *  @brief  Consumes data from the buffer; writes to the
    758        *          controlled sequence.
    759        *  @param  __c  An additional character to consume.
    760        *  @return  eof() to indicate failure, something else (usually
    761        *           @a __c, or not_eof())
    762        *
    763        *  Informally, this function is called when the output buffer
    764        *  is full (or does not exist, as buffering need not actually
    765        *  be done).  If a buffer exists, it is @a consumed, with
    766        *  <em>some effect</em> on the controlled sequence.
    767        *  (Typically, the buffer is written out to the sequence
    768        *  verbatim.)  In either case, the character @a c is also
    769        *  written out, if @a __c is not @c eof().
    770        *
    771        *  For a formal definition of this function, see a good text
    772        *  such as Langer & Kreft, or [27.5.2.4.5]/3-7.
    773        *
    774        *  A functioning output streambuf can be created by overriding only
    775        *  this function (no buffer area will be used).
    776        *
    777        *  @note  Base class version does nothing, returns eof().
    778       */
    779       virtual int_type
    780       overflow(int_type __c _IsUnused  = traits_type::eof())
    781       { return traits_type::eof(); }
    782 
    783 #if _GLIBCXX_USE_DEPRECATED && __cplusplus <= 201402L
    784     // Annex D.6 (removed in C++17)
    785     public:
    786       /**
    787        *  @brief  Tosses a character.
    788        *
    789        *  Advances the read pointer, ignoring the character that would have
    790        *  been read.
    791        *
    792        *  See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
    793        */
    794       _GLIBCXX_DEPRECATED_SUGGEST("std::basic_streambuf::sbumpc")
    795       void
    796       stossc()
    797       {
    798 	if (this->gptr() < this->egptr())
    799 	  this->gbump(1);
    800 	else
    801 	  this->uflow();
    802       }
    803 #endif
    804 
    805       // Also used by specializations for char and wchar_t in src.
    806       void
    807       __safe_gbump(streamsize __n) { _M_in_cur += __n; }
    808 
    809       void
    810       __safe_pbump(streamsize __n) { _M_out_cur += __n; }
    811 
    812 #if __cplusplus < 201103L
    813     private:
    814 #else
    815     protected:
    816 #endif
    817       basic_streambuf(const basic_streambuf&);
    818 
    819       basic_streambuf&
    820       operator=(const basic_streambuf&);
    821 
    822 #if __cplusplus >= 201103L
    823       void
    824       swap(basic_streambuf& __sb)
    825       {
    826 	std::swap(_M_in_beg, __sb._M_in_beg);
    827 	std::swap(_M_in_cur, __sb._M_in_cur);
    828 	std::swap(_M_in_end, __sb._M_in_end);
    829 	std::swap(_M_out_beg, __sb._M_out_beg);
    830 	std::swap(_M_out_cur, __sb._M_out_cur);
    831 	std::swap(_M_out_end, __sb._M_out_end);
    832 	std::swap(_M_buf_locale, __sb._M_buf_locale);
    833       }
    834 #endif
    835     };
    836 
    837 #if __cplusplus >= 201103L
    838   template<typename _CharT, typename _Traits>
    839     std::basic_streambuf<_CharT, _Traits>::
    840     basic_streambuf(const basic_streambuf&) = default;
    841 
    842   template<typename _CharT, typename _Traits>
    843     std::basic_streambuf<_CharT, _Traits>&
    844     std::basic_streambuf<_CharT, _Traits>::
    845     operator=(const basic_streambuf&) = default;
    846 #endif
    847 
    848   // Explicit specialization declarations, defined in src/streambuf.cc.
    849   template<>
    850     streamsize
    851     __copy_streambufs_eof(basic_streambuf<char>* __sbin,
    852 			  basic_streambuf<char>* __sbout, bool& __ineof);
    853 #ifdef _GLIBCXX_USE_WCHAR_T
    854   template<>
    855     streamsize
    856     __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
    857 			  basic_streambuf<wchar_t>* __sbout, bool& __ineof);
    858 #endif
    859 
    860 #undef _IsUnused
    861 
    862 _GLIBCXX_END_NAMESPACE_VERSION
    863 } // namespace
    864 
    865 #include <bits/streambuf.tcc>
    866 
    867 #endif /* _GLIBCXX_STREAMBUF */
    868