streambuf 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  1. // Stream buffer classes -*- C++ -*-
  2. // Copyright (C) 1997-2022 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. friend void __istream_extract(istream&, char*, streamsize);
  150. template<typename _CharT2, typename _Traits2, typename _Alloc>
  151. friend basic_istream<_CharT2, _Traits2>&
  152. operator>>(basic_istream<_CharT2, _Traits2>&,
  153. basic_string<_CharT2, _Traits2, _Alloc>&);
  154. template<typename _CharT2, typename _Traits2, typename _Alloc>
  155. friend basic_istream<_CharT2, _Traits2>&
  156. getline(basic_istream<_CharT2, _Traits2>&,
  157. basic_string<_CharT2, _Traits2, _Alloc>&, _CharT2);
  158. protected:
  159. /*
  160. * This is based on _IO_FILE, just reordered to be more consistent,
  161. * and is intended to be the most minimal abstraction for an
  162. * internal buffer.
  163. * - get == input == read
  164. * - put == output == write
  165. */
  166. char_type* _M_in_beg; ///< Start of get area.
  167. char_type* _M_in_cur; ///< Current read area.
  168. char_type* _M_in_end; ///< End of get area.
  169. char_type* _M_out_beg; ///< Start of put area.
  170. char_type* _M_out_cur; ///< Current put area.
  171. char_type* _M_out_end; ///< End of put area.
  172. /// Current locale setting.
  173. locale _M_buf_locale;
  174. public:
  175. /// Destructor deallocates no buffer space.
  176. virtual
  177. ~basic_streambuf()
  178. { }
  179. // [27.5.2.2.1] locales
  180. /**
  181. * @brief Entry point for imbue().
  182. * @param __loc The new locale.
  183. * @return The previous locale.
  184. *
  185. * Calls the derived imbue(__loc).
  186. */
  187. locale
  188. pubimbue(const locale& __loc)
  189. {
  190. locale __tmp(this->getloc());
  191. this->imbue(__loc);
  192. _M_buf_locale = __loc;
  193. return __tmp;
  194. }
  195. /**
  196. * @brief Locale access.
  197. * @return The current locale in effect.
  198. *
  199. * If pubimbue(loc) has been called, then the most recent @c loc
  200. * is returned. Otherwise the global locale in effect at the time
  201. * of construction is returned.
  202. */
  203. locale
  204. getloc() const
  205. { return _M_buf_locale; }
  206. // [27.5.2.2.2] buffer management and positioning
  207. ///@{
  208. /**
  209. * @brief Entry points for derived buffer functions.
  210. *
  211. * The public versions of @c pubfoo dispatch to the protected
  212. * derived @c foo member functions, passing the arguments (if any)
  213. * and returning the result unchanged.
  214. */
  215. basic_streambuf*
  216. pubsetbuf(char_type* __s, streamsize __n)
  217. { return this->setbuf(__s, __n); }
  218. /**
  219. * @brief Alters the stream position.
  220. * @param __off Offset.
  221. * @param __way Value for ios_base::seekdir.
  222. * @param __mode Value for ios_base::openmode.
  223. *
  224. * Calls virtual seekoff function.
  225. */
  226. pos_type
  227. pubseekoff(off_type __off, ios_base::seekdir __way,
  228. ios_base::openmode __mode = ios_base::in | ios_base::out)
  229. { return this->seekoff(__off, __way, __mode); }
  230. /**
  231. * @brief Alters the stream position.
  232. * @param __sp Position
  233. * @param __mode Value for ios_base::openmode.
  234. *
  235. * Calls virtual seekpos function.
  236. */
  237. pos_type
  238. pubseekpos(pos_type __sp,
  239. ios_base::openmode __mode = ios_base::in | ios_base::out)
  240. { return this->seekpos(__sp, __mode); }
  241. /**
  242. * @brief Calls virtual sync function.
  243. */
  244. int
  245. pubsync() { return this->sync(); }
  246. ///@}
  247. // [27.5.2.2.3] get area
  248. /**
  249. * @brief Looking ahead into the stream.
  250. * @return The number of characters available.
  251. *
  252. * If a read position is available, returns the number of characters
  253. * available for reading before the buffer must be refilled.
  254. * Otherwise returns the derived @c showmanyc().
  255. */
  256. streamsize
  257. in_avail()
  258. {
  259. const streamsize __ret = this->egptr() - this->gptr();
  260. return __ret ? __ret : this->showmanyc();
  261. }
  262. /**
  263. * @brief Getting the next character.
  264. * @return The next character, or eof.
  265. *
  266. * Calls @c sbumpc(), and if that function returns
  267. * @c traits::eof(), so does this function. Otherwise, @c sgetc().
  268. */
  269. int_type
  270. snextc()
  271. {
  272. int_type __ret = traits_type::eof();
  273. if (__builtin_expect(!traits_type::eq_int_type(this->sbumpc(),
  274. __ret), true))
  275. __ret = this->sgetc();
  276. return __ret;
  277. }
  278. /**
  279. * @brief Getting the next character.
  280. * @return The next character, or eof.
  281. *
  282. * If the input read position is available, returns that character
  283. * and increments the read pointer, otherwise calls and returns
  284. * @c uflow().
  285. */
  286. int_type
  287. sbumpc()
  288. {
  289. int_type __ret;
  290. if (__builtin_expect(this->gptr() < this->egptr(), true))
  291. {
  292. __ret = traits_type::to_int_type(*this->gptr());
  293. this->gbump(1);
  294. }
  295. else
  296. __ret = this->uflow();
  297. return __ret;
  298. }
  299. /**
  300. * @brief Getting the next character.
  301. * @return The next character, or eof.
  302. *
  303. * If the input read position is available, returns that character,
  304. * otherwise calls and returns @c underflow(). Does not move the
  305. * read position after fetching the character.
  306. */
  307. int_type
  308. sgetc()
  309. {
  310. int_type __ret;
  311. if (__builtin_expect(this->gptr() < this->egptr(), true))
  312. __ret = traits_type::to_int_type(*this->gptr());
  313. else
  314. __ret = this->underflow();
  315. return __ret;
  316. }
  317. /**
  318. * @brief Entry point for xsgetn.
  319. * @param __s A buffer area.
  320. * @param __n A count.
  321. *
  322. * Returns xsgetn(__s,__n). The effect is to fill @a __s[0] through
  323. * @a __s[__n-1] with characters from the input sequence, if possible.
  324. */
  325. streamsize
  326. sgetn(char_type* __s, streamsize __n)
  327. { return this->xsgetn(__s, __n); }
  328. // [27.5.2.2.4] putback
  329. /**
  330. * @brief Pushing characters back into the input stream.
  331. * @param __c The character to push back.
  332. * @return The previous character, if possible.
  333. *
  334. * Similar to sungetc(), but @a __c is pushed onto the stream
  335. * instead of <em>the previous character.</em> If successful,
  336. * the next character fetched from the input stream will be @a
  337. * __c.
  338. */
  339. int_type
  340. sputbackc(char_type __c)
  341. {
  342. int_type __ret;
  343. const bool __testpos = this->eback() < this->gptr();
  344. if (__builtin_expect(!__testpos ||
  345. !traits_type::eq(__c, this->gptr()[-1]), false))
  346. __ret = this->pbackfail(traits_type::to_int_type(__c));
  347. else
  348. {
  349. this->gbump(-1);
  350. __ret = traits_type::to_int_type(*this->gptr());
  351. }
  352. return __ret;
  353. }
  354. /**
  355. * @brief Moving backwards in the input stream.
  356. * @return The previous character, if possible.
  357. *
  358. * If a putback position is available, this function decrements
  359. * the input pointer and returns that character. Otherwise,
  360. * calls and returns pbackfail(). The effect is to @a unget
  361. * the last character @a gotten.
  362. */
  363. int_type
  364. sungetc()
  365. {
  366. int_type __ret;
  367. if (__builtin_expect(this->eback() < this->gptr(), true))
  368. {
  369. this->gbump(-1);
  370. __ret = traits_type::to_int_type(*this->gptr());
  371. }
  372. else
  373. __ret = this->pbackfail();
  374. return __ret;
  375. }
  376. // [27.5.2.2.5] put area
  377. /**
  378. * @brief Entry point for all single-character output functions.
  379. * @param __c A character to output.
  380. * @return @a __c, if possible.
  381. *
  382. * One of two public output functions.
  383. *
  384. * If a write position is available for the output sequence (i.e.,
  385. * the buffer is not full), stores @a __c in that position, increments
  386. * the position, and returns @c traits::to_int_type(__c). If a write
  387. * position is not available, returns @c overflow(__c).
  388. */
  389. int_type
  390. sputc(char_type __c)
  391. {
  392. int_type __ret;
  393. if (__builtin_expect(this->pptr() < this->epptr(), true))
  394. {
  395. *this->pptr() = __c;
  396. this->pbump(1);
  397. __ret = traits_type::to_int_type(__c);
  398. }
  399. else
  400. __ret = this->overflow(traits_type::to_int_type(__c));
  401. return __ret;
  402. }
  403. /**
  404. * @brief Entry point for all single-character output functions.
  405. * @param __s A buffer read area.
  406. * @param __n A count.
  407. *
  408. * One of two public output functions.
  409. *
  410. *
  411. * Returns xsputn(__s,__n). The effect is to write @a __s[0] through
  412. * @a __s[__n-1] to the output sequence, if possible.
  413. */
  414. streamsize
  415. sputn(const char_type* __s, streamsize __n)
  416. { return this->xsputn(__s, __n); }
  417. protected:
  418. /**
  419. * @brief Base constructor.
  420. *
  421. * Only called from derived constructors, and sets up all the
  422. * buffer data to zero, including the pointers described in the
  423. * basic_streambuf class description. Note that, as a result,
  424. * - the class starts with no read nor write positions available,
  425. * - this is not an error
  426. */
  427. basic_streambuf()
  428. : _M_in_beg(0), _M_in_cur(0), _M_in_end(0),
  429. _M_out_beg(0), _M_out_cur(0), _M_out_end(0),
  430. _M_buf_locale(locale())
  431. { }
  432. // [27.5.2.3.1] get area access
  433. ///@{
  434. /**
  435. * @brief Access to the get area.
  436. *
  437. * These functions are only available to other protected functions,
  438. * including derived classes.
  439. *
  440. * - eback() returns the beginning pointer for the input sequence
  441. * - gptr() returns the next pointer for the input sequence
  442. * - egptr() returns the end pointer for the input sequence
  443. */
  444. char_type*
  445. eback() const { return _M_in_beg; }
  446. char_type*
  447. gptr() const { return _M_in_cur; }
  448. char_type*
  449. egptr() const { return _M_in_end; }
  450. ///@}
  451. /**
  452. * @brief Moving the read position.
  453. * @param __n The delta by which to move.
  454. *
  455. * This just advances the read position without returning any data.
  456. */
  457. void
  458. gbump(int __n) { _M_in_cur += __n; }
  459. /**
  460. * @brief Setting the three read area pointers.
  461. * @param __gbeg A pointer.
  462. * @param __gnext A pointer.
  463. * @param __gend A pointer.
  464. * @post @a __gbeg == @c eback(), @a __gnext == @c gptr(), and
  465. * @a __gend == @c egptr()
  466. */
  467. void
  468. setg(char_type* __gbeg, char_type* __gnext, char_type* __gend)
  469. {
  470. _M_in_beg = __gbeg;
  471. _M_in_cur = __gnext;
  472. _M_in_end = __gend;
  473. }
  474. // [27.5.2.3.2] put area access
  475. ///@{
  476. /**
  477. * @brief Access to the put area.
  478. *
  479. * These functions are only available to other protected functions,
  480. * including derived classes.
  481. *
  482. * - pbase() returns the beginning pointer for the output sequence
  483. * - pptr() returns the next pointer for the output sequence
  484. * - epptr() returns the end pointer for the output sequence
  485. */
  486. char_type*
  487. pbase() const { return _M_out_beg; }
  488. char_type*
  489. pptr() const { return _M_out_cur; }
  490. char_type*
  491. epptr() const { return _M_out_end; }
  492. ///@}
  493. /**
  494. * @brief Moving the write position.
  495. * @param __n The delta by which to move.
  496. *
  497. * This just advances the write position without returning any data.
  498. */
  499. void
  500. pbump(int __n) { _M_out_cur += __n; }
  501. /**
  502. * @brief Setting the three write area pointers.
  503. * @param __pbeg A pointer.
  504. * @param __pend A pointer.
  505. * @post @a __pbeg == @c pbase(), @a __pbeg == @c pptr(), and
  506. * @a __pend == @c epptr()
  507. */
  508. void
  509. setp(char_type* __pbeg, char_type* __pend)
  510. {
  511. _M_out_beg = _M_out_cur = __pbeg;
  512. _M_out_end = __pend;
  513. }
  514. // [27.5.2.4] virtual functions
  515. // [27.5.2.4.1] locales
  516. /**
  517. * @brief Changes translations.
  518. * @param __loc A new locale.
  519. *
  520. * Translations done during I/O which depend on the current
  521. * locale are changed by this call. The standard adds,
  522. * <em>Between invocations of this function a class derived
  523. * from streambuf can safely cache results of calls to locale
  524. * functions and to members of facets so obtained.</em>
  525. *
  526. * @note Base class version does nothing.
  527. */
  528. virtual void
  529. imbue(const locale& __loc _IsUnused)
  530. { }
  531. // [27.5.2.4.2] buffer management and positioning
  532. /**
  533. * @brief Manipulates the buffer.
  534. *
  535. * Each derived class provides its own appropriate behavior. See
  536. * the next-to-last paragraph of
  537. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html#io.streambuf.buffering
  538. * for more on this function.
  539. *
  540. * @note Base class version does nothing, returns @c this.
  541. */
  542. virtual basic_streambuf<char_type,_Traits>*
  543. setbuf(char_type*, streamsize)
  544. { return this; }
  545. /**
  546. * @brief Alters the stream positions.
  547. *
  548. * Each derived class provides its own appropriate behavior.
  549. * @note Base class version does nothing, returns a @c pos_type
  550. * that represents an invalid stream position.
  551. */
  552. virtual pos_type
  553. seekoff(off_type, ios_base::seekdir,
  554. ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
  555. { return pos_type(off_type(-1)); }
  556. /**
  557. * @brief Alters the stream positions.
  558. *
  559. * Each derived class provides its own appropriate behavior.
  560. * @note Base class version does nothing, returns a @c pos_type
  561. * that represents an invalid stream position.
  562. */
  563. virtual pos_type
  564. seekpos(pos_type,
  565. ios_base::openmode /*__mode*/ = ios_base::in | ios_base::out)
  566. { return pos_type(off_type(-1)); }
  567. /**
  568. * @brief Synchronizes the buffer arrays with the controlled sequences.
  569. * @return -1 on failure.
  570. *
  571. * Each derived class provides its own appropriate behavior,
  572. * including the definition of @a failure.
  573. * @note Base class version does nothing, returns zero.
  574. */
  575. virtual int
  576. sync() { return 0; }
  577. // [27.5.2.4.3] get area
  578. /**
  579. * @brief Investigating the data available.
  580. * @return An estimate of the number of characters available in the
  581. * input sequence, or -1.
  582. *
  583. * <em>If it returns a positive value, then successive calls to
  584. * @c underflow() will not return @c traits::eof() until at
  585. * least that number of characters have been supplied. If @c
  586. * showmanyc() returns -1, then calls to @c underflow() or @c
  587. * uflow() will fail.</em> [27.5.2.4.3]/1
  588. *
  589. * @note Base class version does nothing, returns zero.
  590. * @note The standard adds that <em>the intention is not only that the
  591. * calls [to underflow or uflow] will not return @c eof() but
  592. * that they will return immediately.</em>
  593. * @note The standard adds that <em>the morphemes of @c showmanyc are
  594. * @b es-how-many-see, not @b show-manic.</em>
  595. */
  596. virtual streamsize
  597. showmanyc() { return 0; }
  598. /**
  599. * @brief Multiple character extraction.
  600. * @param __s A buffer area.
  601. * @param __n Maximum number of characters to assign.
  602. * @return The number of characters assigned.
  603. *
  604. * Fills @a __s[0] through @a __s[__n-1] with characters from the input
  605. * sequence, as if by @c sbumpc(). Stops when either @a __n characters
  606. * have been copied, or when @c traits::eof() would be copied.
  607. *
  608. * It is expected that derived classes provide a more efficient
  609. * implementation by overriding this definition.
  610. */
  611. virtual streamsize
  612. xsgetn(char_type* __s, streamsize __n);
  613. /**
  614. * @brief Fetches more data from the controlled sequence.
  615. * @return The first character from the <em>pending sequence</em>.
  616. *
  617. * Informally, this function is called when the input buffer is
  618. * exhausted (or does not exist, as buffering need not actually be
  619. * done). If a buffer exists, it is @a refilled. In either case, the
  620. * next available character is returned, or @c traits::eof() to
  621. * indicate a null pending sequence.
  622. *
  623. * For a formal definition of the pending sequence, see a good text
  624. * such as Langer & Kreft, or [27.5.2.4.3]/7-14.
  625. *
  626. * A functioning input streambuf can be created by overriding only
  627. * this function (no buffer area will be used). For an example, see
  628. * https://gcc.gnu.org/onlinedocs/libstdc++/manual/streambufs.html
  629. *
  630. * @note Base class version does nothing, returns eof().
  631. */
  632. virtual int_type
  633. underflow()
  634. { return traits_type::eof(); }
  635. /**
  636. * @brief Fetches more data from the controlled sequence.
  637. * @return The first character from the <em>pending sequence</em>.
  638. *
  639. * Informally, this function does the same thing as @c underflow(),
  640. * and in fact is required to call that function. It also returns
  641. * the new character, like @c underflow() does. However, this
  642. * function also moves the read position forward by one.
  643. */
  644. virtual int_type
  645. uflow()
  646. {
  647. int_type __ret = traits_type::eof();
  648. const bool __testeof = traits_type::eq_int_type(this->underflow(),
  649. __ret);
  650. if (!__testeof)
  651. {
  652. __ret = traits_type::to_int_type(*this->gptr());
  653. this->gbump(1);
  654. }
  655. return __ret;
  656. }
  657. // [27.5.2.4.4] putback
  658. /**
  659. * @brief Tries to back up the input sequence.
  660. * @param __c The character to be inserted back into the sequence.
  661. * @return eof() on failure, <em>some other value</em> on success
  662. * @post The constraints of @c gptr(), @c eback(), and @c pptr()
  663. * are the same as for @c underflow().
  664. *
  665. * @note Base class version does nothing, returns eof().
  666. */
  667. virtual int_type
  668. pbackfail(int_type __c _IsUnused = traits_type::eof())
  669. { return traits_type::eof(); }
  670. // Put area:
  671. /**
  672. * @brief Multiple character insertion.
  673. * @param __s A buffer area.
  674. * @param __n Maximum number of characters to write.
  675. * @return The number of characters written.
  676. *
  677. * Writes @a __s[0] through @a __s[__n-1] to the output sequence, as if
  678. * by @c sputc(). Stops when either @a n characters have been
  679. * copied, or when @c sputc() would return @c traits::eof().
  680. *
  681. * It is expected that derived classes provide a more efficient
  682. * implementation by overriding this definition.
  683. */
  684. virtual streamsize
  685. xsputn(const char_type* __s, streamsize __n);
  686. /**
  687. * @brief Consumes data from the buffer; writes to the
  688. * controlled sequence.
  689. * @param __c An additional character to consume.
  690. * @return eof() to indicate failure, something else (usually
  691. * @a __c, or not_eof())
  692. *
  693. * Informally, this function is called when the output buffer
  694. * is full (or does not exist, as buffering need not actually
  695. * be done). If a buffer exists, it is @a consumed, with
  696. * <em>some effect</em> on the controlled sequence.
  697. * (Typically, the buffer is written out to the sequence
  698. * verbatim.) In either case, the character @a c is also
  699. * written out, if @a __c is not @c eof().
  700. *
  701. * For a formal definition of this function, see a good text
  702. * such as Langer & Kreft, or [27.5.2.4.5]/3-7.
  703. *
  704. * A functioning output streambuf can be created by overriding only
  705. * this function (no buffer area will be used).
  706. *
  707. * @note Base class version does nothing, returns eof().
  708. */
  709. virtual int_type
  710. overflow(int_type __c _IsUnused = traits_type::eof())
  711. { return traits_type::eof(); }
  712. #if _GLIBCXX_USE_DEPRECATED && __cplusplus <= 201402L
  713. // Annex D.6 (removed in C++17)
  714. public:
  715. /**
  716. * @brief Tosses a character.
  717. *
  718. * Advances the read pointer, ignoring the character that would have
  719. * been read.
  720. *
  721. * See http://gcc.gnu.org/ml/libstdc++/2002-05/msg00168.html
  722. */
  723. _GLIBCXX_DEPRECATED_SUGGEST("std::basic_streambuf::sbumpc")
  724. void
  725. stossc()
  726. {
  727. if (this->gptr() < this->egptr())
  728. this->gbump(1);
  729. else
  730. this->uflow();
  731. }
  732. #endif
  733. // Also used by specializations for char and wchar_t in src.
  734. void
  735. __safe_gbump(streamsize __n) { _M_in_cur += __n; }
  736. void
  737. __safe_pbump(streamsize __n) { _M_out_cur += __n; }
  738. #if __cplusplus < 201103L
  739. private:
  740. #else
  741. protected:
  742. #endif
  743. basic_streambuf(const basic_streambuf&);
  744. basic_streambuf&
  745. operator=(const basic_streambuf&);
  746. #if __cplusplus >= 201103L
  747. void
  748. swap(basic_streambuf& __sb)
  749. {
  750. std::swap(_M_in_beg, __sb._M_in_beg);
  751. std::swap(_M_in_cur, __sb._M_in_cur);
  752. std::swap(_M_in_end, __sb._M_in_end);
  753. std::swap(_M_out_beg, __sb._M_out_beg);
  754. std::swap(_M_out_cur, __sb._M_out_cur);
  755. std::swap(_M_out_end, __sb._M_out_end);
  756. std::swap(_M_buf_locale, __sb._M_buf_locale);
  757. }
  758. #endif
  759. };
  760. #if __cplusplus >= 201103L
  761. template<typename _CharT, typename _Traits>
  762. std::basic_streambuf<_CharT, _Traits>::
  763. basic_streambuf(const basic_streambuf&) = default;
  764. template<typename _CharT, typename _Traits>
  765. std::basic_streambuf<_CharT, _Traits>&
  766. std::basic_streambuf<_CharT, _Traits>::
  767. operator=(const basic_streambuf&) = default;
  768. #endif
  769. // Explicit specialization declarations, defined in src/streambuf.cc.
  770. template<>
  771. streamsize
  772. __copy_streambufs_eof(basic_streambuf<char>* __sbin,
  773. basic_streambuf<char>* __sbout, bool& __ineof);
  774. #ifdef _GLIBCXX_USE_WCHAR_T
  775. template<>
  776. streamsize
  777. __copy_streambufs_eof(basic_streambuf<wchar_t>* __sbin,
  778. basic_streambuf<wchar_t>* __sbout, bool& __ineof);
  779. #endif
  780. #undef _IsUnused
  781. _GLIBCXX_END_NAMESPACE_VERSION
  782. } // namespace
  783. #include <bits/streambuf.tcc>
  784. #endif /* _GLIBCXX_STREAMBUF */