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