You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993
  1. // Input streams -*- C++ -*-
  2. // Copyright (C) 1997-2020 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. //
  21. // ISO C++ 14882: 27.6.1 Input streams
  22. //
  23. /** @file include/istream
  24. * This is a Standard C++ Library header.
  25. */
  26. #ifndef _GLIBCXX_ISTREAM
  27. #define _GLIBCXX_ISTREAM 1
  28. #pragma GCC system_header
  29. #include <ios>
  30. #include <ostream>
  31. namespace std _GLIBCXX_VISIBILITY(default)
  32. {
  33. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  34. /**
  35. * @brief Template class basic_istream.
  36. * @ingroup io
  37. *
  38. * @tparam _CharT Type of character stream.
  39. * @tparam _Traits Traits for character type, defaults to
  40. * char_traits<_CharT>.
  41. *
  42. * This is the base class for all input streams. It provides text
  43. * formatting of all builtin types, and communicates with any class
  44. * derived from basic_streambuf to do the actual input.
  45. */
  46. template<typename _CharT, typename _Traits>
  47. class basic_istream : virtual public basic_ios<_CharT, _Traits>
  48. {
  49. public:
  50. // Types (inherited from basic_ios (27.4.4)):
  51. typedef _CharT char_type;
  52. typedef typename _Traits::int_type int_type;
  53. typedef typename _Traits::pos_type pos_type;
  54. typedef typename _Traits::off_type off_type;
  55. typedef _Traits traits_type;
  56. // Non-standard Types:
  57. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  58. typedef basic_ios<_CharT, _Traits> __ios_type;
  59. typedef basic_istream<_CharT, _Traits> __istream_type;
  60. typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
  61. __num_get_type;
  62. typedef ctype<_CharT> __ctype_type;
  63. protected:
  64. // Data Members:
  65. /**
  66. * The number of characters extracted in the previous unformatted
  67. * function; see gcount().
  68. */
  69. streamsize _M_gcount;
  70. public:
  71. /**
  72. * @brief Base constructor.
  73. *
  74. * This ctor is almost never called by the user directly, rather from
  75. * derived classes' initialization lists, which pass a pointer to
  76. * their own stream buffer.
  77. */
  78. explicit
  79. basic_istream(__streambuf_type* __sb)
  80. : _M_gcount(streamsize(0))
  81. { this->init(__sb); }
  82. /**
  83. * @brief Base destructor.
  84. *
  85. * This does very little apart from providing a virtual base dtor.
  86. */
  87. virtual
  88. ~basic_istream()
  89. { _M_gcount = streamsize(0); }
  90. /// Safe prefix/suffix operations.
  91. class sentry;
  92. friend class sentry;
  93. //@{
  94. /**
  95. * @brief Interface for manipulators.
  96. *
  97. * Manipulators such as @c std::ws and @c std::dec use these
  98. * functions in constructs like
  99. * <code>std::cin >> std::ws</code>.
  100. * For more information, see the iomanip header.
  101. */
  102. __istream_type&
  103. operator>>(__istream_type& (*__pf)(__istream_type&))
  104. { return __pf(*this); }
  105. __istream_type&
  106. operator>>(__ios_type& (*__pf)(__ios_type&))
  107. {
  108. __pf(*this);
  109. return *this;
  110. }
  111. __istream_type&
  112. operator>>(ios_base& (*__pf)(ios_base&))
  113. {
  114. __pf(*this);
  115. return *this;
  116. }
  117. //@}
  118. //@{
  119. /**
  120. * @name Extractors
  121. *
  122. * All the @c operator>> functions (aka <em>formatted input
  123. * functions</em>) have some common behavior. Each starts by
  124. * constructing a temporary object of type std::basic_istream::sentry
  125. * with the second argument (noskipws) set to false. This has several
  126. * effects, concluding with the setting of a status flag; see the
  127. * sentry documentation for more.
  128. *
  129. * If the sentry status is good, the function tries to extract
  130. * whatever data is appropriate for the type of the argument.
  131. *
  132. * If an exception is thrown during extraction, ios_base::badbit
  133. * will be turned on in the stream's error state (without causing an
  134. * ios_base::failure to be thrown) and the original exception will
  135. * be rethrown if badbit is set in the exceptions mask.
  136. */
  137. //@{
  138. /**
  139. * @brief Integer arithmetic extractors
  140. * @param __n A variable of builtin integral type.
  141. * @return @c *this if successful
  142. *
  143. * These functions use the stream's current locale (specifically, the
  144. * @c num_get facet) to parse the input data.
  145. */
  146. __istream_type&
  147. operator>>(bool& __n)
  148. { return _M_extract(__n); }
  149. __istream_type&
  150. operator>>(short& __n);
  151. __istream_type&
  152. operator>>(unsigned short& __n)
  153. { return _M_extract(__n); }
  154. __istream_type&
  155. operator>>(int& __n);
  156. __istream_type&
  157. operator>>(unsigned int& __n)
  158. { return _M_extract(__n); }
  159. __istream_type&
  160. operator>>(long& __n)
  161. { return _M_extract(__n); }
  162. __istream_type&
  163. operator>>(unsigned long& __n)
  164. { return _M_extract(__n); }
  165. #ifdef _GLIBCXX_USE_LONG_LONG
  166. __istream_type&
  167. operator>>(long long& __n)
  168. { return _M_extract(__n); }
  169. __istream_type&
  170. operator>>(unsigned long long& __n)
  171. { return _M_extract(__n); }
  172. #endif
  173. //@}
  174. //@{
  175. /**
  176. * @brief Floating point arithmetic extractors
  177. * @param __f A variable of builtin floating point type.
  178. * @return @c *this if successful
  179. *
  180. * These functions use the stream's current locale (specifically, the
  181. * @c num_get facet) to parse the input data.
  182. */
  183. __istream_type&
  184. operator>>(float& __f)
  185. { return _M_extract(__f); }
  186. __istream_type&
  187. operator>>(double& __f)
  188. { return _M_extract(__f); }
  189. __istream_type&
  190. operator>>(long double& __f)
  191. { return _M_extract(__f); }
  192. //@}
  193. /**
  194. * @brief Basic arithmetic extractors
  195. * @param __p A variable of pointer type.
  196. * @return @c *this if successful
  197. *
  198. * These functions use the stream's current locale (specifically, the
  199. * @c num_get facet) to parse the input data.
  200. */
  201. __istream_type&
  202. operator>>(void*& __p)
  203. { return _M_extract(__p); }
  204. /**
  205. * @brief Extracting into another streambuf.
  206. * @param __sb A pointer to a streambuf
  207. *
  208. * This function behaves like one of the basic arithmetic extractors,
  209. * in that it also constructs a sentry object and has the same error
  210. * handling behavior.
  211. *
  212. * If @p __sb is NULL, the stream will set failbit in its error state.
  213. *
  214. * Characters are extracted from this stream and inserted into the
  215. * @p __sb streambuf until one of the following occurs:
  216. *
  217. * - the input stream reaches end-of-file,
  218. * - insertion into the output buffer fails (in this case, the
  219. * character that would have been inserted is not extracted), or
  220. * - an exception occurs (and in this case is caught)
  221. *
  222. * If the function inserts no characters, failbit is set.
  223. */
  224. __istream_type&
  225. operator>>(__streambuf_type* __sb);
  226. //@}
  227. // [27.6.1.3] unformatted input
  228. /**
  229. * @brief Character counting
  230. * @return The number of characters extracted by the previous
  231. * unformatted input function dispatched for this stream.
  232. */
  233. streamsize
  234. gcount() const
  235. { return _M_gcount; }
  236. //@{
  237. /**
  238. * @name Unformatted Input Functions
  239. *
  240. * All the unformatted input functions have some common behavior.
  241. * Each starts by constructing a temporary object of type
  242. * std::basic_istream::sentry with the second argument (noskipws)
  243. * set to true. This has several effects, concluding with the
  244. * setting of a status flag; see the sentry documentation for more.
  245. *
  246. * If the sentry status is good, the function tries to extract
  247. * whatever data is appropriate for the type of the argument.
  248. *
  249. * The number of characters extracted is stored for later retrieval
  250. * by gcount().
  251. *
  252. * If an exception is thrown during extraction, ios_base::badbit
  253. * will be turned on in the stream's error state (without causing an
  254. * ios_base::failure to be thrown) and the original exception will
  255. * be rethrown if badbit is set in the exceptions mask.
  256. */
  257. /**
  258. * @brief Simple extraction.
  259. * @return A character, or eof().
  260. *
  261. * Tries to extract a character. If none are available, sets failbit
  262. * and returns traits::eof().
  263. */
  264. int_type
  265. get();
  266. /**
  267. * @brief Simple extraction.
  268. * @param __c The character in which to store data.
  269. * @return *this
  270. *
  271. * Tries to extract a character and store it in @a __c. If none are
  272. * available, sets failbit and returns traits::eof().
  273. *
  274. * @note This function is not overloaded on signed char and
  275. * unsigned char.
  276. */
  277. __istream_type&
  278. get(char_type& __c);
  279. /**
  280. * @brief Simple multiple-character extraction.
  281. * @param __s Pointer to an array.
  282. * @param __n Maximum number of characters to store in @a __s.
  283. * @param __delim A "stop" character.
  284. * @return *this
  285. *
  286. * Characters are extracted and stored into @a __s until one of the
  287. * following happens:
  288. *
  289. * - @c __n-1 characters are stored
  290. * - the input sequence reaches EOF
  291. * - the next character equals @a __delim, in which case the character
  292. * is not extracted
  293. *
  294. * If no characters are stored, failbit is set in the stream's error
  295. * state.
  296. *
  297. * In any case, a null character is stored into the next location in
  298. * the array.
  299. *
  300. * @note This function is not overloaded on signed char and
  301. * unsigned char.
  302. */
  303. __istream_type&
  304. get(char_type* __s, streamsize __n, char_type __delim);
  305. /**
  306. * @brief Simple multiple-character extraction.
  307. * @param __s Pointer to an array.
  308. * @param __n Maximum number of characters to store in @a s.
  309. * @return *this
  310. *
  311. * Returns @c get(__s,__n,widen(&apos;\\n&apos;)).
  312. */
  313. __istream_type&
  314. get(char_type* __s, streamsize __n)
  315. { return this->get(__s, __n, this->widen('\n')); }
  316. /**
  317. * @brief Extraction into another streambuf.
  318. * @param __sb A streambuf in which to store data.
  319. * @param __delim A "stop" character.
  320. * @return *this
  321. *
  322. * Characters are extracted and inserted into @a __sb until one of the
  323. * following happens:
  324. *
  325. * - the input sequence reaches EOF
  326. * - insertion into the output buffer fails (in this case, the
  327. * character that would have been inserted is not extracted)
  328. * - the next character equals @a __delim (in this case, the character
  329. * is not extracted)
  330. * - an exception occurs (and in this case is caught)
  331. *
  332. * If no characters are stored, failbit is set in the stream's error
  333. * state.
  334. */
  335. __istream_type&
  336. get(__streambuf_type& __sb, char_type __delim);
  337. /**
  338. * @brief Extraction into another streambuf.
  339. * @param __sb A streambuf in which to store data.
  340. * @return *this
  341. *
  342. * Returns @c get(__sb,widen(&apos;\\n&apos;)).
  343. */
  344. __istream_type&
  345. get(__streambuf_type& __sb)
  346. { return this->get(__sb, this->widen('\n')); }
  347. /**
  348. * @brief String extraction.
  349. * @param __s A character array in which to store the data.
  350. * @param __n Maximum number of characters to extract.
  351. * @param __delim A "stop" character.
  352. * @return *this
  353. *
  354. * Extracts and stores characters into @a __s until one of the
  355. * following happens. Note that these criteria are required to be
  356. * tested in the order listed here, to allow an input line to exactly
  357. * fill the @a __s array without setting failbit.
  358. *
  359. * -# the input sequence reaches end-of-file, in which case eofbit
  360. * is set in the stream error state
  361. * -# the next character equals @c __delim, in which case the character
  362. * is extracted (and therefore counted in @c gcount()) but not stored
  363. * -# @c __n-1 characters are stored, in which case failbit is set
  364. * in the stream error state
  365. *
  366. * If no characters are extracted, failbit is set. (An empty line of
  367. * input should therefore not cause failbit to be set.)
  368. *
  369. * In any case, a null character is stored in the next location in
  370. * the array.
  371. */
  372. __istream_type&
  373. getline(char_type* __s, streamsize __n, char_type __delim);
  374. /**
  375. * @brief String extraction.
  376. * @param __s A character array in which to store the data.
  377. * @param __n Maximum number of characters to extract.
  378. * @return *this
  379. *
  380. * Returns @c getline(__s,__n,widen(&apos;\\n&apos;)).
  381. */
  382. __istream_type&
  383. getline(char_type* __s, streamsize __n)
  384. { return this->getline(__s, __n, this->widen('\n')); }
  385. /**
  386. * @brief Discarding characters
  387. * @param __n Number of characters to discard.
  388. * @param __delim A "stop" character.
  389. * @return *this
  390. *
  391. * Extracts characters and throws them away until one of the
  392. * following happens:
  393. * - if @a __n @c != @c std::numeric_limits<int>::max(), @a __n
  394. * characters are extracted
  395. * - the input sequence reaches end-of-file
  396. * - the next character equals @a __delim (in this case, the character
  397. * is extracted); note that this condition will never occur if
  398. * @a __delim equals @c traits::eof().
  399. *
  400. * NB: Provide three overloads, instead of the single function
  401. * (with defaults) mandated by the Standard: this leads to a
  402. * better performing implementation, while still conforming to
  403. * the Standard.
  404. */
  405. __istream_type&
  406. ignore(streamsize __n, int_type __delim);
  407. __istream_type&
  408. ignore(streamsize __n);
  409. __istream_type&
  410. ignore();
  411. /**
  412. * @brief Looking ahead in the stream
  413. * @return The next character, or eof().
  414. *
  415. * If, after constructing the sentry object, @c good() is false,
  416. * returns @c traits::eof(). Otherwise reads but does not extract
  417. * the next input character.
  418. */
  419. int_type
  420. peek();
  421. /**
  422. * @brief Extraction without delimiters.
  423. * @param __s A character array.
  424. * @param __n Maximum number of characters to store.
  425. * @return *this
  426. *
  427. * If the stream state is @c good(), extracts characters and stores
  428. * them into @a __s until one of the following happens:
  429. * - @a __n characters are stored
  430. * - the input sequence reaches end-of-file, in which case the error
  431. * state is set to @c failbit|eofbit.
  432. *
  433. * @note This function is not overloaded on signed char and
  434. * unsigned char.
  435. */
  436. __istream_type&
  437. read(char_type* __s, streamsize __n);
  438. /**
  439. * @brief Extraction until the buffer is exhausted, but no more.
  440. * @param __s A character array.
  441. * @param __n Maximum number of characters to store.
  442. * @return The number of characters extracted.
  443. *
  444. * Extracts characters and stores them into @a __s depending on the
  445. * number of characters remaining in the streambuf's buffer,
  446. * @c rdbuf()->in_avail(), called @c A here:
  447. * - if @c A @c == @c -1, sets eofbit and extracts no characters
  448. * - if @c A @c == @c 0, extracts no characters
  449. * - if @c A @c > @c 0, extracts @c min(A,n)
  450. *
  451. * The goal is to empty the current buffer, and to not request any
  452. * more from the external input sequence controlled by the streambuf.
  453. */
  454. streamsize
  455. readsome(char_type* __s, streamsize __n);
  456. /**
  457. * @brief Unextracting a single character.
  458. * @param __c The character to push back into the input stream.
  459. * @return *this
  460. *
  461. * If @c rdbuf() is not null, calls @c rdbuf()->sputbackc(c).
  462. *
  463. * If @c rdbuf() is null or if @c sputbackc() fails, sets badbit in
  464. * the error state.
  465. *
  466. * @note This function first clears eofbit. Since no characters
  467. * are extracted, the next call to @c gcount() will return 0,
  468. * as required by DR 60.
  469. */
  470. __istream_type&
  471. putback(char_type __c);
  472. /**
  473. * @brief Unextracting the previous character.
  474. * @return *this
  475. *
  476. * If @c rdbuf() is not null, calls @c rdbuf()->sungetc(c).
  477. *
  478. * If @c rdbuf() is null or if @c sungetc() fails, sets badbit in
  479. * the error state.
  480. *
  481. * @note This function first clears eofbit. Since no characters
  482. * are extracted, the next call to @c gcount() will return 0,
  483. * as required by DR 60.
  484. */
  485. __istream_type&
  486. unget();
  487. /**
  488. * @brief Synchronizing the stream buffer.
  489. * @return 0 on success, -1 on failure
  490. *
  491. * If @c rdbuf() is a null pointer, returns -1.
  492. *
  493. * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
  494. * sets badbit and returns -1.
  495. *
  496. * Otherwise, returns 0.
  497. *
  498. * @note This function does not count the number of characters
  499. * extracted, if any, and therefore does not affect the next
  500. * call to @c gcount().
  501. */
  502. int
  503. sync();
  504. /**
  505. * @brief Getting the current read position.
  506. * @return A file position object.
  507. *
  508. * If @c fail() is not false, returns @c pos_type(-1) to indicate
  509. * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,in).
  510. *
  511. * @note This function does not count the number of characters
  512. * extracted, if any, and therefore does not affect the next
  513. * call to @c gcount(). At variance with putback, unget and
  514. * seekg, eofbit is not cleared first.
  515. */
  516. pos_type
  517. tellg();
  518. /**
  519. * @brief Changing the current read position.
  520. * @param __pos A file position object.
  521. * @return *this
  522. *
  523. * If @c fail() is not true, calls @c rdbuf()->pubseekpos(__pos). If
  524. * that function fails, sets failbit.
  525. *
  526. * @note This function first clears eofbit. It does not count the
  527. * number of characters extracted, if any, and therefore does
  528. * not affect the next call to @c gcount().
  529. */
  530. __istream_type&
  531. seekg(pos_type);
  532. /**
  533. * @brief Changing the current read position.
  534. * @param __off A file offset object.
  535. * @param __dir The direction in which to seek.
  536. * @return *this
  537. *
  538. * If @c fail() is not true, calls @c rdbuf()->pubseekoff(__off,__dir).
  539. * If that function fails, sets failbit.
  540. *
  541. * @note This function first clears eofbit. It does not count the
  542. * number of characters extracted, if any, and therefore does
  543. * not affect the next call to @c gcount().
  544. */
  545. __istream_type&
  546. seekg(off_type, ios_base::seekdir);
  547. //@}
  548. protected:
  549. basic_istream()
  550. : _M_gcount(streamsize(0))
  551. { this->init(0); }
  552. #if __cplusplus >= 201103L
  553. basic_istream(const basic_istream&) = delete;
  554. basic_istream(basic_istream&& __rhs)
  555. : __ios_type(), _M_gcount(__rhs._M_gcount)
  556. {
  557. __ios_type::move(__rhs);
  558. __rhs._M_gcount = 0;
  559. }
  560. // 27.7.3.3 Assign/swap
  561. basic_istream& operator=(const basic_istream&) = delete;
  562. basic_istream&
  563. operator=(basic_istream&& __rhs)
  564. {
  565. swap(__rhs);
  566. return *this;
  567. }
  568. void
  569. swap(basic_istream& __rhs)
  570. {
  571. __ios_type::swap(__rhs);
  572. std::swap(_M_gcount, __rhs._M_gcount);
  573. }
  574. #endif
  575. template<typename _ValueT>
  576. __istream_type&
  577. _M_extract(_ValueT& __v);
  578. };
  579. /// Explicit specialization declarations, defined in src/istream.cc.
  580. template<>
  581. basic_istream<char>&
  582. basic_istream<char>::
  583. getline(char_type* __s, streamsize __n, char_type __delim);
  584. template<>
  585. basic_istream<char>&
  586. basic_istream<char>::
  587. ignore(streamsize __n);
  588. template<>
  589. basic_istream<char>&
  590. basic_istream<char>::
  591. ignore(streamsize __n, int_type __delim);
  592. #ifdef _GLIBCXX_USE_WCHAR_T
  593. template<>
  594. basic_istream<wchar_t>&
  595. basic_istream<wchar_t>::
  596. getline(char_type* __s, streamsize __n, char_type __delim);
  597. template<>
  598. basic_istream<wchar_t>&
  599. basic_istream<wchar_t>::
  600. ignore(streamsize __n);
  601. template<>
  602. basic_istream<wchar_t>&
  603. basic_istream<wchar_t>::
  604. ignore(streamsize __n, int_type __delim);
  605. #endif
  606. /**
  607. * @brief Performs setup work for input streams.
  608. *
  609. * Objects of this class are created before all of the standard
  610. * extractors are run. It is responsible for <em>exception-safe
  611. * prefix and suffix operations,</em> although only prefix actions
  612. * are currently required by the standard.
  613. */
  614. template<typename _CharT, typename _Traits>
  615. class basic_istream<_CharT, _Traits>::sentry
  616. {
  617. // Data Members.
  618. bool _M_ok;
  619. public:
  620. /// Easy access to dependent types.
  621. typedef _Traits traits_type;
  622. typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
  623. typedef basic_istream<_CharT, _Traits> __istream_type;
  624. typedef typename __istream_type::__ctype_type __ctype_type;
  625. typedef typename _Traits::int_type __int_type;
  626. /**
  627. * @brief The constructor performs all the work.
  628. * @param __is The input stream to guard.
  629. * @param __noskipws Whether to consume whitespace or not.
  630. *
  631. * If the stream state is good (@a __is.good() is true), then the
  632. * following actions are performed, otherwise the sentry state
  633. * is false (<em>not okay</em>) and failbit is set in the
  634. * stream state.
  635. *
  636. * The sentry's preparatory actions are:
  637. *
  638. * -# if the stream is tied to an output stream, @c is.tie()->flush()
  639. * is called to synchronize the output sequence
  640. * -# if @a __noskipws is false, and @c ios_base::skipws is set in
  641. * @c is.flags(), the sentry extracts and discards whitespace
  642. * characters from the stream. The currently imbued locale is
  643. * used to determine whether each character is whitespace.
  644. *
  645. * If the stream state is still good, then the sentry state becomes
  646. * true (@a okay).
  647. */
  648. explicit
  649. sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
  650. /**
  651. * @brief Quick status checking.
  652. * @return The sentry state.
  653. *
  654. * For ease of use, sentries may be converted to booleans. The
  655. * return value is that of the sentry state (true == okay).
  656. */
  657. #if __cplusplus >= 201103L
  658. explicit
  659. #endif
  660. operator bool() const
  661. { return _M_ok; }
  662. };
  663. //@{
  664. /**
  665. * @brief Character extractors
  666. * @param __in An input stream.
  667. * @param __c A character reference.
  668. * @return in
  669. *
  670. * Behaves like one of the formatted arithmetic extractors described in
  671. * std::basic_istream. After constructing a sentry object with good
  672. * status, this function extracts a character (if one is available) and
  673. * stores it in @a __c. Otherwise, sets failbit in the input stream.
  674. */
  675. template<typename _CharT, typename _Traits>
  676. basic_istream<_CharT, _Traits>&
  677. operator>>(basic_istream<_CharT, _Traits>& __in, _CharT& __c);
  678. template<class _Traits>
  679. inline basic_istream<char, _Traits>&
  680. operator>>(basic_istream<char, _Traits>& __in, unsigned char& __c)
  681. { return (__in >> reinterpret_cast<char&>(__c)); }
  682. template<class _Traits>
  683. inline basic_istream<char, _Traits>&
  684. operator>>(basic_istream<char, _Traits>& __in, signed char& __c)
  685. { return (__in >> reinterpret_cast<char&>(__c)); }
  686. //@}
  687. //@{
  688. /**
  689. * @brief Character string extractors
  690. * @param __in An input stream.
  691. * @param __s A pointer to a character array.
  692. * @return __in
  693. *
  694. * Behaves like one of the formatted arithmetic extractors described in
  695. * std::basic_istream. After constructing a sentry object with good
  696. * status, this function extracts up to @c n characters and stores them
  697. * into the array starting at @a __s. @c n is defined as:
  698. *
  699. * - if @c width() is greater than zero, @c n is width() otherwise
  700. * - @c n is <em>the number of elements of the largest array of *
  701. * - @c char_type that can store a terminating @c eos.</em>
  702. * - [27.6.1.2.3]/6
  703. *
  704. * Characters are extracted and stored until one of the following happens:
  705. * - @c n-1 characters are stored
  706. * - EOF is reached
  707. * - the next character is whitespace according to the current locale
  708. * - the next character is a null byte (i.e., @c charT() )
  709. *
  710. * @c width(0) is then called for the input stream.
  711. *
  712. * If no characters are extracted, sets failbit.
  713. */
  714. template<typename _CharT, typename _Traits>
  715. basic_istream<_CharT, _Traits>&
  716. operator>>(basic_istream<_CharT, _Traits>& __in, _CharT* __s);
  717. // Explicit specialization declaration, defined in src/istream.cc.
  718. template<>
  719. basic_istream<char>&
  720. operator>>(basic_istream<char>& __in, char* __s);
  721. template<class _Traits>
  722. inline basic_istream<char, _Traits>&
  723. operator>>(basic_istream<char, _Traits>& __in, unsigned char* __s)
  724. { return (__in >> reinterpret_cast<char*>(__s)); }
  725. template<class _Traits>
  726. inline basic_istream<char, _Traits>&
  727. operator>>(basic_istream<char, _Traits>& __in, signed char* __s)
  728. { return (__in >> reinterpret_cast<char*>(__s)); }
  729. //@}
  730. /**
  731. * @brief Template class basic_iostream
  732. * @ingroup io
  733. *
  734. * @tparam _CharT Type of character stream.
  735. * @tparam _Traits Traits for character type, defaults to
  736. * char_traits<_CharT>.
  737. *
  738. * This class multiply inherits from the input and output stream classes
  739. * simply to provide a single interface.
  740. */
  741. template<typename _CharT, typename _Traits>
  742. class basic_iostream
  743. : public basic_istream<_CharT, _Traits>,
  744. public basic_ostream<_CharT, _Traits>
  745. {
  746. public:
  747. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  748. // 271. basic_iostream missing typedefs
  749. // Types (inherited):
  750. typedef _CharT char_type;
  751. typedef typename _Traits::int_type int_type;
  752. typedef typename _Traits::pos_type pos_type;
  753. typedef typename _Traits::off_type off_type;
  754. typedef _Traits traits_type;
  755. // Non-standard Types:
  756. typedef basic_istream<_CharT, _Traits> __istream_type;
  757. typedef basic_ostream<_CharT, _Traits> __ostream_type;
  758. /**
  759. * @brief Constructor does nothing.
  760. *
  761. * Both of the parent classes are initialized with the same
  762. * streambuf pointer passed to this constructor.
  763. */
  764. explicit
  765. basic_iostream(basic_streambuf<_CharT, _Traits>* __sb)
  766. : __istream_type(__sb), __ostream_type(__sb) { }
  767. /**
  768. * @brief Destructor does nothing.
  769. */
  770. virtual
  771. ~basic_iostream() { }
  772. protected:
  773. basic_iostream()
  774. : __istream_type(), __ostream_type() { }
  775. #if __cplusplus >= 201103L
  776. basic_iostream(const basic_iostream&) = delete;
  777. basic_iostream(basic_iostream&& __rhs)
  778. : __istream_type(std::move(__rhs)), __ostream_type(*this)
  779. { }
  780. // 27.7.3.3 Assign/swap
  781. basic_iostream& operator=(const basic_iostream&) = delete;
  782. basic_iostream&
  783. operator=(basic_iostream&& __rhs)
  784. {
  785. swap(__rhs);
  786. return *this;
  787. }
  788. void
  789. swap(basic_iostream& __rhs)
  790. { __istream_type::swap(__rhs); }
  791. #endif
  792. };
  793. /**
  794. * @brief Quick and easy way to eat whitespace
  795. *
  796. * This manipulator extracts whitespace characters, stopping when the
  797. * next character is non-whitespace, or when the input sequence is empty.
  798. * If the sequence is empty, @c eofbit is set in the stream, but not
  799. * @c failbit.
  800. *
  801. * The current locale is used to distinguish whitespace characters.
  802. *
  803. * Example:
  804. * @code
  805. * MyClass mc;
  806. *
  807. * std::cin >> std::ws >> mc;
  808. * @endcode
  809. * will skip leading whitespace before calling operator>> on cin and your
  810. * object. Note that the same effect can be achieved by creating a
  811. * std::basic_istream::sentry inside your definition of operator>>.
  812. */
  813. template<typename _CharT, typename _Traits>
  814. basic_istream<_CharT, _Traits>&
  815. ws(basic_istream<_CharT, _Traits>& __is);
  816. #if __cplusplus >= 201103L
  817. template<typename _Ch, typename _Up>
  818. basic_istream<_Ch, _Up>&
  819. __is_convertible_to_basic_istream_test(basic_istream<_Ch, _Up>*);
  820. template<typename _Tp, typename = void>
  821. struct __is_convertible_to_basic_istream_impl
  822. {
  823. using __istream_type = void;
  824. };
  825. template<typename _Tp>
  826. using __do_is_convertible_to_basic_istream_impl =
  827. decltype(__is_convertible_to_basic_istream_test
  828. (declval<typename remove_reference<_Tp>::type*>()));
  829. template<typename _Tp>
  830. struct __is_convertible_to_basic_istream_impl
  831. <_Tp,
  832. __void_t<__do_is_convertible_to_basic_istream_impl<_Tp>>>
  833. {
  834. using __istream_type =
  835. __do_is_convertible_to_basic_istream_impl<_Tp>;
  836. };
  837. template<typename _Tp>
  838. struct __is_convertible_to_basic_istream
  839. : __is_convertible_to_basic_istream_impl<_Tp>
  840. {
  841. public:
  842. using type = __not_<is_void<
  843. typename __is_convertible_to_basic_istream_impl<_Tp>::__istream_type>>;
  844. constexpr static bool value = type::value;
  845. };
  846. template<typename _Istream, typename _Tp, typename = void>
  847. struct __is_extractable : false_type {};
  848. template<typename _Istream, typename _Tp>
  849. struct __is_extractable<_Istream, _Tp,
  850. __void_t<decltype(declval<_Istream&>()
  851. >> declval<_Tp>())>>
  852. : true_type {};
  853. template<typename _Istream>
  854. using __rvalue_istream_type =
  855. typename __is_convertible_to_basic_istream<
  856. _Istream>::__istream_type;
  857. // [27.7.1.6] Rvalue stream extraction
  858. // _GLIBCXX_RESOLVE_LIB_DEFECTS
  859. // 2328. Rvalue stream extraction should use perfect forwarding
  860. /**
  861. * @brief Generic extractor for rvalue stream
  862. * @param __is An input stream.
  863. * @param __x A reference to the extraction target.
  864. * @return is
  865. *
  866. * This is just a forwarding function to allow extraction from
  867. * rvalue streams since they won't bind to the extractor functions
  868. * that take an lvalue reference.
  869. */
  870. template<typename _Istream, typename _Tp>
  871. inline
  872. typename enable_if<__and_<__not_<is_lvalue_reference<_Istream>>,
  873. __is_convertible_to_basic_istream<_Istream>,
  874. __is_extractable<
  875. __rvalue_istream_type<_Istream>,
  876. _Tp&&>>::value,
  877. __rvalue_istream_type<_Istream>>::type
  878. operator>>(_Istream&& __is, _Tp&& __x)
  879. {
  880. __rvalue_istream_type<_Istream> __ret_is = __is;
  881. __ret_is >> std::forward<_Tp>(__x);
  882. return __ret_is;
  883. }
  884. #endif // C++11
  885. _GLIBCXX_END_NAMESPACE_VERSION
  886. } // namespace
  887. #include <bits/istream.tcc>
  888. #endif /* _GLIBCXX_ISTREAM */