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