2 //===----------------------------------------------------------------------===//
4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5 // See https://llvm.org/LICENSE.txt for license information.
6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8 //===----------------------------------------------------------------------===//
10 #ifndef _LIBCPP_ISTREAM
11 #define _LIBCPP_ISTREAM
16 template <class charT, class traits = char_traits<charT> >
18 : virtual public basic_ios<charT,traits>
21 // types (inherited from basic_ios (27.5.4)):
22 typedef charT char_type;
23 typedef traits traits_type;
24 typedef typename traits_type::int_type int_type;
25 typedef typename traits_type::pos_type pos_type;
26 typedef typename traits_type::off_type off_type;
28 // 27.7.1.1.1 Constructor/destructor:
29 explicit basic_istream(basic_streambuf<char_type, traits_type>* sb);
30 basic_istream(basic_istream&& rhs);
31 virtual ~basic_istream();
33 // 27.7.1.1.2 Assign/swap:
34 basic_istream& operator=(basic_istream&& rhs);
35 void swap(basic_istream& rhs);
37 // 27.7.1.1.3 Prefix/suffix:
40 // 27.7.1.2 Formatted input:
41 basic_istream& operator>>(basic_istream& (*pf)(basic_istream&));
42 basic_istream& operator>>(basic_ios<char_type, traits_type>&
43 (*pf)(basic_ios<char_type, traits_type>&));
44 basic_istream& operator>>(ios_base& (*pf)(ios_base&));
45 basic_istream& operator>>(basic_streambuf<char_type, traits_type>* sb);
46 basic_istream& operator>>(bool& n);
47 basic_istream& operator>>(short& n);
48 basic_istream& operator>>(unsigned short& n);
49 basic_istream& operator>>(int& n);
50 basic_istream& operator>>(unsigned int& n);
51 basic_istream& operator>>(long& n);
52 basic_istream& operator>>(unsigned long& n);
53 basic_istream& operator>>(long long& n);
54 basic_istream& operator>>(unsigned long long& n);
55 basic_istream& operator>>(float& f);
56 basic_istream& operator>>(double& f);
57 basic_istream& operator>>(long double& f);
58 basic_istream& operator>>(void*& p);
60 // 27.7.1.3 Unformatted input:
61 streamsize gcount() const;
63 basic_istream& get(char_type& c);
64 basic_istream& get(char_type* s, streamsize n);
65 basic_istream& get(char_type* s, streamsize n, char_type delim);
66 basic_istream& get(basic_streambuf<char_type,traits_type>& sb);
67 basic_istream& get(basic_streambuf<char_type,traits_type>& sb, char_type delim);
69 basic_istream& getline(char_type* s, streamsize n);
70 basic_istream& getline(char_type* s, streamsize n, char_type delim);
72 basic_istream& ignore(streamsize n = 1, int_type delim = traits_type::eof());
74 basic_istream& read (char_type* s, streamsize n);
75 streamsize readsome(char_type* s, streamsize n);
77 basic_istream& putback(char_type c);
78 basic_istream& unget();
82 basic_istream& seekg(pos_type);
83 basic_istream& seekg(off_type, ios_base::seekdir);
85 basic_istream(const basic_istream& rhs) = delete;
86 basic_istream(basic_istream&& rhs);
87 // 27.7.2.1.2 Assign/swap:
88 basic_istream& operator=(const basic_istream& rhs) = delete;
89 basic_istream& operator=(basic_istream&& rhs);
90 void swap(basic_istream& rhs);
93 // 27.7.1.2.3 character extraction templates:
94 template<class charT, class traits>
95 basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&);
97 template<class traits>
98 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char&);
100 template<class traits>
101 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char&);
103 template<class charT, class traits>
104 basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT*);
106 template<class traits>
107 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char*);
109 template<class traits>
110 basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char*);
112 template <class charT, class traits>
114 swap(basic_istream<charT, traits>& x, basic_istream<charT, traits>& y);
116 typedef basic_istream<char> istream;
117 typedef basic_istream<wchar_t> wistream;
119 template <class charT, class traits = char_traits<charT> >
120 class basic_iostream :
121 public basic_istream<charT,traits>,
122 public basic_ostream<charT,traits>
126 typedef charT char_type;
127 typedef traits traits_type;
128 typedef typename traits_type::int_type int_type;
129 typedef typename traits_type::pos_type pos_type;
130 typedef typename traits_type::off_type off_type;
132 // constructor/destructor
133 explicit basic_iostream(basic_streambuf<char_type, traits_type>* sb);
134 basic_iostream(basic_iostream&& rhs);
135 virtual ~basic_iostream();
138 basic_iostream& operator=(basic_iostream&& rhs);
139 void swap(basic_iostream& rhs);
142 template <class charT, class traits>
144 swap(basic_iostream<charT, traits>& x, basic_iostream<charT, traits>& y);
146 typedef basic_iostream<char> iostream;
147 typedef basic_iostream<wchar_t> wiostream;
149 template <class charT, class traits>
150 basic_istream<charT,traits>&
151 ws(basic_istream<charT,traits>& is);
153 // rvalue stream extraction
154 template <class Stream, class T>
155 Stream&& operator>>(Stream&& is, T&& x);
161 #include <__cxx03/__config>
162 #include <__cxx03/__fwd/istream.h>
163 #include <__cxx03/__iterator/istreambuf_iterator.h>
164 #include <__cxx03/__ostream/basic_ostream.h>
165 #include <__cxx03/__type_traits/conjunction.h>
166 #include <__cxx03/__type_traits/enable_if.h>
167 #include <__cxx03/__type_traits/is_base_of.h>
168 #include <__cxx03/__utility/declval.h>
169 #include <__cxx03/__utility/forward.h>
170 #include <__cxx03/bitset>
171 #include <__cxx03/ios>
172 #include <__cxx03/locale>
173 #include <__cxx03/version>
175 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
176 # pragma GCC system_header
180 #include <__cxx03/__undef_macros>
182 _LIBCPP_BEGIN_NAMESPACE_STD
184 template <class _CharT, class _Traits>
185 class _LIBCPP_TEMPLATE_VIS basic_istream : virtual public basic_ios<_CharT, _Traits> {
188 _LIBCPP_HIDE_FROM_ABI void __inc_gcount() {
189 if (__gc_ < numeric_limits<streamsize>::max())
194 // types (inherited from basic_ios (27.5.4)):
195 typedef _CharT char_type;
196 typedef _Traits traits_type;
197 typedef typename traits_type::int_type int_type;
198 typedef typename traits_type::pos_type pos_type;
199 typedef typename traits_type::off_type off_type;
201 // 27.7.1.1.1 Constructor/destructor:
202 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb)
206 ~basic_istream() override;
209 inline _LIBCPP_HIDE_FROM_ABI basic_istream(basic_istream&& __rhs);
211 // 27.7.1.1.2 Assign/swap:
212 inline _LIBCPP_HIDE_FROM_ABI basic_istream& operator=(basic_istream&& __rhs);
214 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_istream& __rhs) {
215 std::swap(__gc_, __rhs.__gc_);
216 basic_ios<char_type, traits_type>::swap(__rhs);
220 basic_istream(const basic_istream& __rhs) = delete;
221 basic_istream& operator=(const basic_istream& __rhs) = delete;
223 // 27.7.1.1.3 Prefix/suffix:
224 class _LIBCPP_TEMPLATE_VIS sentry;
226 // 27.7.1.2 Formatted input:
227 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&)) {
231 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream&
232 operator>>(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) {
237 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& operator>>(ios_base& (*__pf)(ios_base&)) {
242 basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
243 basic_istream& operator>>(bool& __n);
244 basic_istream& operator>>(short& __n);
245 basic_istream& operator>>(unsigned short& __n);
246 basic_istream& operator>>(int& __n);
247 basic_istream& operator>>(unsigned int& __n);
248 basic_istream& operator>>(long& __n);
249 basic_istream& operator>>(unsigned long& __n);
250 basic_istream& operator>>(long long& __n);
251 basic_istream& operator>>(unsigned long long& __n);
252 basic_istream& operator>>(float& __f);
253 basic_istream& operator>>(double& __f);
254 basic_istream& operator>>(long double& __f);
255 basic_istream& operator>>(void*& __p);
257 // 27.7.1.3 Unformatted input:
258 _LIBCPP_HIDE_FROM_ABI streamsize gcount() const { return __gc_; }
261 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(char_type& __c) {
262 int_type __ch = get();
263 if (__ch != traits_type::eof())
264 __c = traits_type::to_char_type(__ch);
268 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(char_type* __s, streamsize __n) {
269 return get(__s, __n, this->widen('\n'));
272 basic_istream& get(char_type* __s, streamsize __n, char_type __dlm);
274 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb) {
275 return get(__sb, this->widen('\n'));
278 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm);
280 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_istream& getline(char_type* __s, streamsize __n) {
281 return getline(__s, __n, this->widen('\n'));
284 basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm);
286 basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof());
288 basic_istream& read(char_type* __s, streamsize __n);
289 streamsize readsome(char_type* __s, streamsize __n);
291 basic_istream& putback(char_type __c);
292 basic_istream& unget();
296 basic_istream& seekg(pos_type __pos);
297 basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
300 template <class _CharT, class _Traits>
301 class _LIBCPP_TEMPLATE_VIS basic_istream<_CharT, _Traits>::sentry {
305 explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
306 // ~sentry() = default;
308 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }
310 sentry(const sentry&) = delete;
311 sentry& operator=(const sentry&) = delete;
314 template <class _CharT, class _Traits>
315 basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws) : __ok_(false) {
319 if (!__noskipws && (__is.flags() & ios_base::skipws)) {
320 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
321 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
324 for (; __i != __eof; ++__i)
325 if (!__ct.is(__ct.space, *__i))
328 __is.setstate(ios_base::failbit | ios_base::eofbit);
332 __is.setstate(ios_base::failbit);
335 template <class _CharT, class _Traits>
336 basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs) : __gc_(__rhs.__gc_) {
341 template <class _CharT, class _Traits>
342 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs) {
347 template <class _CharT, class _Traits>
348 basic_istream<_CharT, _Traits>::~basic_istream() {}
350 template <class _Tp, class _CharT, class _Traits>
351 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
352 __input_arithmetic(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
353 ios_base::iostate __state = ios_base::goodbit;
354 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
356 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
358 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
359 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
360 typedef num_get<_CharT, _Ip> _Fp;
361 std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __n);
362 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
364 __state |= ios_base::badbit;
365 __is.__setstate_nothrow(__state);
366 if (__is.exceptions() & ios_base::badbit) {
371 __is.setstate(__state);
376 template <class _CharT, class _Traits>
377 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n) {
378 return std::__input_arithmetic<unsigned short>(*this, __n);
381 template <class _CharT, class _Traits>
382 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n) {
383 return std::__input_arithmetic<unsigned int>(*this, __n);
386 template <class _CharT, class _Traits>
387 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long& __n) {
388 return std::__input_arithmetic<long>(*this, __n);
391 template <class _CharT, class _Traits>
392 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n) {
393 return std::__input_arithmetic<unsigned long>(*this, __n);
396 template <class _CharT, class _Traits>
397 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long long& __n) {
398 return std::__input_arithmetic<long long>(*this, __n);
401 template <class _CharT, class _Traits>
402 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n) {
403 return std::__input_arithmetic<unsigned long long>(*this, __n);
406 template <class _CharT, class _Traits>
407 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(float& __n) {
408 return std::__input_arithmetic<float>(*this, __n);
411 template <class _CharT, class _Traits>
412 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(double& __n) {
413 return std::__input_arithmetic<double>(*this, __n);
416 template <class _CharT, class _Traits>
417 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(long double& __n) {
418 return std::__input_arithmetic<long double>(*this, __n);
421 template <class _CharT, class _Traits>
422 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(bool& __n) {
423 return std::__input_arithmetic<bool>(*this, __n);
426 template <class _CharT, class _Traits>
427 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(void*& __n) {
428 return std::__input_arithmetic<void*>(*this, __n);
431 template <class _Tp, class _CharT, class _Traits>
432 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
433 __input_arithmetic_with_numeric_limits(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
434 ios_base::iostate __state = ios_base::goodbit;
435 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
437 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
439 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
440 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
441 typedef num_get<_CharT, _Ip> _Fp;
443 std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __temp);
444 if (__temp < numeric_limits<_Tp>::min()) {
445 __state |= ios_base::failbit;
446 __n = numeric_limits<_Tp>::min();
447 } else if (__temp > numeric_limits<_Tp>::max()) {
448 __state |= ios_base::failbit;
449 __n = numeric_limits<_Tp>::max();
451 __n = static_cast<_Tp>(__temp);
453 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
455 __state |= ios_base::badbit;
456 __is.__setstate_nothrow(__state);
457 if (__is.exceptions() & ios_base::badbit) {
461 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
462 __is.setstate(__state);
467 template <class _CharT, class _Traits>
468 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(short& __n) {
469 return std::__input_arithmetic_with_numeric_limits<short>(*this, __n);
472 template <class _CharT, class _Traits>
473 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::operator>>(int& __n) {
474 return std::__input_arithmetic_with_numeric_limits<int>(*this, __n);
477 template <class _CharT, class _Traits>
478 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
479 __input_c_string(basic_istream<_CharT, _Traits>& __is, _CharT* __p, size_t __n) {
480 ios_base::iostate __state = ios_base::goodbit;
481 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
483 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
487 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
488 while (__s != __p + (__n - 1)) {
489 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
490 if (_Traits::eq_int_type(__i, _Traits::eof())) {
491 __state |= ios_base::eofbit;
494 _CharT __ch = _Traits::to_char_type(__i);
495 if (__ct.is(__ct.space, __ch))
498 __is.rdbuf()->sbumpc();
503 __state |= ios_base::failbit;
504 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
506 __state |= ios_base::badbit;
507 __is.__setstate_nothrow(__state);
508 if (__is.exceptions() & ios_base::badbit) {
513 __is.setstate(__state);
518 #if _LIBCPP_STD_VER >= 20
520 template <class _CharT, class _Traits, size_t _Np>
521 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
522 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np]) {
524 if (__is.width() > 0)
525 __n = std::min(size_t(__is.width()), _Np);
526 return std::__input_c_string(__is, __buf, __n);
529 template <class _Traits, size_t _Np>
530 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
531 operator>>(basic_istream<char, _Traits>& __is, unsigned char (&__buf)[_Np]) {
532 return __is >> (char(&)[_Np])__buf;
535 template <class _Traits, size_t _Np>
536 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
537 operator>>(basic_istream<char, _Traits>& __is, signed char (&__buf)[_Np]) {
538 return __is >> (char(&)[_Np])__buf;
543 template <class _CharT, class _Traits>
544 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
545 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s) {
546 streamsize __n = __is.width();
548 __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1;
549 return std::__input_c_string(__is, __s, size_t(__n));
552 template <class _Traits>
553 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
554 operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s) {
555 return __is >> (char*)__s;
558 template <class _Traits>
559 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
560 operator>>(basic_istream<char, _Traits>& __is, signed char* __s) {
561 return __is >> (char*)__s;
564 #endif // _LIBCPP_STD_VER >= 20
566 template <class _CharT, class _Traits>
567 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c) {
568 ios_base::iostate __state = ios_base::goodbit;
569 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
571 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
574 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
575 if (_Traits::eq_int_type(__i, _Traits::eof()))
576 __state |= ios_base::eofbit | ios_base::failbit;
578 __c = _Traits::to_char_type(__i);
579 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
581 __state |= ios_base::badbit;
582 __is.__setstate_nothrow(__state);
583 if (__is.exceptions() & ios_base::badbit) {
588 __is.setstate(__state);
593 template <class _Traits>
594 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
595 operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c) {
596 return __is >> (char&)__c;
599 template <class _Traits>
600 inline _LIBCPP_HIDE_FROM_ABI basic_istream<char, _Traits>&
601 operator>>(basic_istream<char, _Traits>& __is, signed char& __c) {
602 return __is >> (char&)__c;
605 template <class _CharT, class _Traits>
606 basic_istream<_CharT, _Traits>&
607 basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb) {
608 ios_base::iostate __state = ios_base::goodbit;
610 sentry __s(*this, true);
613 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
615 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
617 typename traits_type::int_type __i = this->rdbuf()->sgetc();
618 if (traits_type::eq_int_type(__i, _Traits::eof())) {
619 __state |= ios_base::eofbit;
622 if (traits_type::eq_int_type(__sb->sputc(traits_type::to_char_type(__i)), traits_type::eof()))
625 this->rdbuf()->sbumpc();
628 __state |= ios_base::failbit;
629 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
631 __state |= ios_base::badbit;
633 __state |= ios_base::failbit;
635 this->__setstate_nothrow(__state);
636 if (this->exceptions() & ios_base::failbit || this->exceptions() & ios_base::badbit) {
640 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
642 __state |= ios_base::failbit;
644 this->setstate(__state);
649 template <class _CharT, class _Traits>
650 typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>::get() {
651 ios_base::iostate __state = ios_base::goodbit;
653 int_type __r = traits_type::eof();
654 sentry __s(*this, true);
656 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
659 __r = this->rdbuf()->sbumpc();
660 if (traits_type::eq_int_type(__r, traits_type::eof()))
661 __state |= ios_base::failbit | ios_base::eofbit;
664 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
666 this->__setstate_nothrow(this->rdstate() | ios_base::badbit);
667 if (this->exceptions() & ios_base::badbit) {
672 this->setstate(__state);
677 template <class _CharT, class _Traits>
678 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm) {
679 ios_base::iostate __state = ios_base::goodbit;
681 sentry __sen(*this, true);
684 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
687 while (__gc_ < __n - 1) {
688 int_type __i = this->rdbuf()->sgetc();
689 if (traits_type::eq_int_type(__i, traits_type::eof())) {
690 __state |= ios_base::eofbit;
693 char_type __ch = traits_type::to_char_type(__i);
694 if (traits_type::eq(__ch, __dlm))
698 this->rdbuf()->sbumpc();
701 __state |= ios_base::failbit;
702 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
704 __state |= ios_base::badbit;
705 this->__setstate_nothrow(__state);
706 if (this->exceptions() & ios_base::badbit) {
714 __state |= ios_base::failbit;
719 this->setstate(__state);
726 template <class _CharT, class _Traits>
727 basic_istream<_CharT, _Traits>&
728 basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm) {
729 ios_base::iostate __state = ios_base::goodbit;
731 sentry __sen(*this, true);
733 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
735 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
737 typename traits_type::int_type __i = this->rdbuf()->sgetc();
738 if (traits_type::eq_int_type(__i, traits_type::eof())) {
739 __state |= ios_base::eofbit;
742 char_type __ch = traits_type::to_char_type(__i);
743 if (traits_type::eq(__ch, __dlm))
745 if (traits_type::eq_int_type(__sb.sputc(__ch), traits_type::eof()))
748 this->rdbuf()->sbumpc();
750 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
752 __state |= ios_base::badbit;
753 // according to the spec, exceptions here are caught but not rethrown
755 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
757 __state |= ios_base::failbit;
758 this->setstate(__state);
763 template <class _CharT, class _Traits>
764 basic_istream<_CharT, _Traits>&
765 basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm) {
766 ios_base::iostate __state = ios_base::goodbit;
768 sentry __sen(*this, true);
770 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
772 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
774 typename traits_type::int_type __i = this->rdbuf()->sgetc();
775 if (traits_type::eq_int_type(__i, traits_type::eof())) {
776 __state |= ios_base::eofbit;
779 char_type __ch = traits_type::to_char_type(__i);
780 if (traits_type::eq(__ch, __dlm)) {
781 this->rdbuf()->sbumpc();
785 if (__gc_ >= __n - 1) {
786 __state |= ios_base::failbit;
790 this->rdbuf()->sbumpc();
793 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
795 __state |= ios_base::badbit;
796 this->__setstate_nothrow(__state);
797 if (this->exceptions() & ios_base::badbit) {
801 __state |= ios_base::failbit;
805 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
810 __state |= ios_base::failbit;
811 this->setstate(__state);
815 template <class _CharT, class _Traits>
816 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm) {
817 ios_base::iostate __state = ios_base::goodbit;
819 sentry __sen(*this, true);
821 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
823 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
824 if (__n == numeric_limits<streamsize>::max()) {
826 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
827 if (traits_type::eq_int_type(__i, traits_type::eof())) {
828 __state |= ios_base::eofbit;
832 if (traits_type::eq_int_type(__i, __dlm))
836 while (__gc_ < __n) {
837 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
838 if (traits_type::eq_int_type(__i, traits_type::eof())) {
839 __state |= ios_base::eofbit;
843 if (traits_type::eq_int_type(__i, __dlm))
847 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
849 __state |= ios_base::badbit;
850 this->__setstate_nothrow(__state);
851 if (this->exceptions() & ios_base::badbit) {
855 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
856 this->setstate(__state);
861 template <class _CharT, class _Traits>
862 typename basic_istream<_CharT, _Traits>::int_type basic_istream<_CharT, _Traits>::peek() {
863 ios_base::iostate __state = ios_base::goodbit;
865 int_type __r = traits_type::eof();
866 sentry __sen(*this, true);
868 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
870 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
871 __r = this->rdbuf()->sgetc();
872 if (traits_type::eq_int_type(__r, traits_type::eof()))
873 __state |= ios_base::eofbit;
874 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
876 __state |= ios_base::badbit;
877 this->__setstate_nothrow(__state);
878 if (this->exceptions() & ios_base::badbit) {
882 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
883 this->setstate(__state);
888 template <class _CharT, class _Traits>
889 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n) {
890 ios_base::iostate __state = ios_base::goodbit;
892 sentry __sen(*this, true);
894 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
896 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
897 __gc_ = this->rdbuf()->sgetn(__s, __n);
899 __state |= ios_base::failbit | ios_base::eofbit;
900 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
902 __state |= ios_base::badbit;
903 this->__setstate_nothrow(__state);
904 if (this->exceptions() & ios_base::badbit) {
908 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
910 __state |= ios_base::failbit;
912 this->setstate(__state);
916 template <class _CharT, class _Traits>
917 streamsize basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n) {
918 ios_base::iostate __state = ios_base::goodbit;
920 sentry __sen(*this, true);
922 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
924 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
925 streamsize __c = this->rdbuf()->in_avail();
928 __state |= ios_base::eofbit;
933 __n = std::min(__c, __n);
934 __gc_ = this->rdbuf()->sgetn(__s, __n);
936 __state |= ios_base::failbit | ios_base::eofbit;
939 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
941 __state |= ios_base::badbit;
942 this->__setstate_nothrow(__state);
943 if (this->exceptions() & ios_base::badbit) {
947 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
949 __state |= ios_base::failbit;
951 this->setstate(__state);
955 template <class _CharT, class _Traits>
956 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::putback(char_type __c) {
957 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
959 this->clear(__state);
960 sentry __sen(*this, true);
962 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
964 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
965 if (this->rdbuf() == nullptr || this->rdbuf()->sputbackc(__c) == traits_type::eof())
966 __state |= ios_base::badbit;
967 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
969 __state |= ios_base::badbit;
970 this->__setstate_nothrow(__state);
971 if (this->exceptions() & ios_base::badbit) {
975 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
977 __state |= ios_base::failbit;
979 this->setstate(__state);
983 template <class _CharT, class _Traits>
984 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::unget() {
985 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
987 this->clear(__state);
988 sentry __sen(*this, true);
990 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
992 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
993 if (this->rdbuf() == nullptr || this->rdbuf()->sungetc() == traits_type::eof())
994 __state |= ios_base::badbit;
995 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
997 __state |= ios_base::badbit;
998 this->__setstate_nothrow(__state);
999 if (this->exceptions() & ios_base::badbit) {
1003 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1005 __state |= ios_base::failbit;
1007 this->setstate(__state);
1011 template <class _CharT, class _Traits>
1012 int basic_istream<_CharT, _Traits>::sync() {
1013 ios_base::iostate __state = ios_base::goodbit;
1014 sentry __sen(*this, true);
1015 if (this->rdbuf() == nullptr)
1020 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1022 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1023 if (this->rdbuf()->pubsync() == -1) {
1024 __state |= ios_base::badbit;
1027 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1029 __state |= ios_base::badbit;
1030 this->__setstate_nothrow(__state);
1031 if (this->exceptions() & ios_base::badbit) {
1035 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1036 this->setstate(__state);
1041 template <class _CharT, class _Traits>
1042 typename basic_istream<_CharT, _Traits>::pos_type basic_istream<_CharT, _Traits>::tellg() {
1043 ios_base::iostate __state = ios_base::goodbit;
1045 sentry __sen(*this, true);
1047 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1049 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1050 __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
1051 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1053 __state |= ios_base::badbit;
1054 this->__setstate_nothrow(__state);
1055 if (this->exceptions() & ios_base::badbit) {
1059 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1060 this->setstate(__state);
1065 template <class _CharT, class _Traits>
1066 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::seekg(pos_type __pos) {
1067 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1068 this->clear(__state);
1069 sentry __sen(*this, true);
1071 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1073 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1074 if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
1075 __state |= ios_base::failbit;
1076 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1078 __state |= ios_base::badbit;
1079 this->__setstate_nothrow(__state);
1080 if (this->exceptions() & ios_base::badbit) {
1084 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1085 this->setstate(__state);
1090 template <class _CharT, class _Traits>
1091 basic_istream<_CharT, _Traits>& basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir) {
1092 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1093 this->clear(__state);
1094 sentry __sen(*this, true);
1096 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1098 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1099 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::in) == pos_type(-1))
1100 __state |= ios_base::failbit;
1101 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1103 __state |= ios_base::badbit;
1104 this->__setstate_nothrow(__state);
1105 if (this->exceptions() & ios_base::badbit) {
1109 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1110 this->setstate(__state);
1115 template <class _CharT, class _Traits>
1116 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>& ws(basic_istream<_CharT, _Traits>& __is) {
1117 ios_base::iostate __state = ios_base::goodbit;
1118 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1120 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1122 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1123 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1125 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1126 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1127 __state |= ios_base::eofbit;
1130 if (!__ct.is(__ct.space, _Traits::to_char_type(__i)))
1132 __is.rdbuf()->sbumpc();
1134 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1136 __state |= ios_base::badbit;
1137 __is.__setstate_nothrow(__state);
1138 if (__is.exceptions() & ios_base::badbit) {
1142 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1143 __is.setstate(__state);
1148 template <class _Stream, class _Tp, class = void>
1149 struct __is_istreamable : false_type {};
1151 template <class _Stream, class _Tp>
1152 struct __is_istreamable<_Stream, _Tp, decltype(std::declval<_Stream>() >> std::declval<_Tp>(), void())> : true_type {};
1154 template <class _Stream,
1156 __enable_if_t< _And<is_base_of<ios_base, _Stream>, __is_istreamable<_Stream&, _Tp&&> >::value, int> = 0>
1157 _LIBCPP_HIDE_FROM_ABI _Stream&& operator>>(_Stream&& __is, _Tp&& __x) {
1158 __is >> std::forward<_Tp>(__x);
1159 return std::move(__is);
1162 template <class _CharT, class _Traits>
1163 class _LIBCPP_TEMPLATE_VIS basic_iostream
1164 : public basic_istream<_CharT, _Traits>,
1165 public basic_ostream<_CharT, _Traits> {
1168 typedef _CharT char_type;
1169 typedef _Traits traits_type;
1170 typedef typename traits_type::int_type int_type;
1171 typedef typename traits_type::pos_type pos_type;
1172 typedef typename traits_type::off_type off_type;
1174 // constructor/destructor
1175 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb)
1176 : basic_istream<_CharT, _Traits>(__sb) {}
1178 ~basic_iostream() override;
1181 inline _LIBCPP_HIDE_FROM_ABI basic_iostream(basic_iostream&& __rhs);
1184 inline _LIBCPP_HIDE_FROM_ABI basic_iostream& operator=(basic_iostream&& __rhs);
1186 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_iostream& __rhs) {
1187 basic_istream<char_type, traits_type>::swap(__rhs);
1191 template <class _CharT, class _Traits>
1192 basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
1193 : basic_istream<_CharT, _Traits>(std::move(__rhs)) {}
1195 template <class _CharT, class _Traits>
1196 basic_iostream<_CharT, _Traits>& basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs) {
1201 template <class _CharT, class _Traits>
1202 basic_iostream<_CharT, _Traits>::~basic_iostream() {}
1204 template <class _CharT, class _Traits, class _Allocator>
1205 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1206 operator>>(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1207 ios_base::iostate __state = ios_base::goodbit;
1208 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1210 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1214 streamsize __n = __is.width();
1216 __n = __str.max_size();
1218 __n = numeric_limits<streamsize>::max();
1220 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1222 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1223 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1224 __state |= ios_base::eofbit;
1227 _CharT __ch = _Traits::to_char_type(__i);
1228 if (__ct.is(__ct.space, __ch))
1230 __str.push_back(__ch);
1232 __is.rdbuf()->sbumpc();
1236 __state |= ios_base::failbit;
1237 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1239 __state |= ios_base::badbit;
1240 __is.__setstate_nothrow(__state);
1241 if (__is.exceptions() & ios_base::badbit) {
1246 __is.setstate(__state);
1251 template <class _CharT, class _Traits, class _Allocator>
1252 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1253 getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) {
1254 ios_base::iostate __state = ios_base::goodbit;
1255 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1257 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1261 streamsize __extr = 0;
1263 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
1264 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1265 __state |= ios_base::eofbit;
1269 _CharT __ch = _Traits::to_char_type(__i);
1270 if (_Traits::eq(__ch, __dlm))
1272 __str.push_back(__ch);
1273 if (__str.size() == __str.max_size()) {
1274 __state |= ios_base::failbit;
1279 __state |= ios_base::failbit;
1280 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1282 __state |= ios_base::badbit;
1283 __is.__setstate_nothrow(__state);
1284 if (__is.exceptions() & ios_base::badbit) {
1289 __is.setstate(__state);
1294 template <class _CharT, class _Traits, class _Allocator>
1295 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1296 getline(basic_istream<_CharT, _Traits>& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1297 return std::getline(__is, __str, __is.widen('\n'));
1300 template <class _CharT, class _Traits, class _Allocator>
1301 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1302 getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm) {
1303 return std::getline(__is, __str, __dlm);
1306 template <class _CharT, class _Traits, class _Allocator>
1307 inline _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1308 getline(basic_istream<_CharT, _Traits>&& __is, basic_string<_CharT, _Traits, _Allocator>& __str) {
1309 return std::getline(__is, __str, __is.widen('\n'));
1312 template <class _CharT, class _Traits, size_t _Size>
1313 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1314 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x) {
1315 ios_base::iostate __state = ios_base::goodbit;
1316 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1318 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1321 basic_string<_CharT, _Traits> __str;
1322 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1324 _CharT __zero = __ct.widen('0');
1325 _CharT __one = __ct.widen('1');
1326 while (__c != _Size) {
1327 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1328 if (_Traits::eq_int_type(__i, _Traits::eof())) {
1329 __state |= ios_base::eofbit;
1332 _CharT __ch = _Traits::to_char_type(__i);
1333 if (!_Traits::eq(__ch, __zero) && !_Traits::eq(__ch, __one))
1335 __str.push_back(__ch);
1337 __is.rdbuf()->sbumpc();
1339 __x = bitset<_Size>(__str);
1340 if (_Size > 0 && __c == 0)
1341 __state |= ios_base::failbit;
1342 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1344 __state |= ios_base::badbit;
1345 __is.__setstate_nothrow(__state);
1346 if (__is.exceptions() & ios_base::badbit) {
1351 __is.setstate(__state);
1356 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<char>;
1357 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1358 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<wchar_t>;
1360 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_iostream<char>;
1362 _LIBCPP_END_NAMESPACE_STD
1364 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1365 # include <__cxx03/concepts>
1366 # include <__cxx03/iosfwd>
1367 # include <__cxx03/ostream>
1368 # include <__cxx03/type_traits>
1373 #endif // _LIBCPP_ISTREAM