streambuf 29 KB

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