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 <__assert> // all public C++ headers provide the assertion handler
163 #include <__fwd/istream.h>
164 #include <__iterator/istreambuf_iterator.h>
165 #include <__type_traits/conjunction.h>
166 #include <__type_traits/enable_if.h>
167 #include <__type_traits/is_base_of.h>
168 #include <__utility/declval.h>
169 #include <__utility/forward.h>
173 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
174 # pragma GCC system_header
178 #include <__undef_macros>
181 _LIBCPP_BEGIN_NAMESPACE_STD
183 template <class _CharT, class _Traits>
184 class _LIBCPP_TEMPLATE_VIS basic_istream
185 : virtual public basic_ios<_CharT, _Traits>
189 _LIBCPP_HIDE_FROM_ABI void __inc_gcount() {
190 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
203 explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb) : __gc_(0)
204 { this->init(__sb); }
205 ~basic_istream() override;
207 inline _LIBCPP_INLINE_VISIBILITY
208 basic_istream(basic_istream&& __rhs);
210 // 27.7.1.1.2 Assign/swap:
211 inline _LIBCPP_INLINE_VISIBILITY
212 basic_istream& operator=(basic_istream&& __rhs);
214 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
215 void swap(basic_istream& __rhs) {
216 _VSTD::swap(__gc_, __rhs.__gc_);
217 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;
224 // 27.7.1.1.3 Prefix/suffix:
225 class _LIBCPP_TEMPLATE_VIS sentry;
227 // 27.7.1.2 Formatted input:
228 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
229 basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&))
230 { return __pf(*this); }
232 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
233 basic_istream& operator>>(basic_ios<char_type, traits_type>&
234 (*__pf)(basic_ios<char_type, traits_type>&))
235 { __pf(*this); return *this; }
237 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
238 basic_istream& operator>>(ios_base& (*__pf)(ios_base&))
239 { __pf(*this); return *this; }
241 basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
242 basic_istream& operator>>(bool& __n);
243 basic_istream& operator>>(short& __n);
244 basic_istream& operator>>(unsigned short& __n);
245 basic_istream& operator>>(int& __n);
246 basic_istream& operator>>(unsigned int& __n);
247 basic_istream& operator>>(long& __n);
248 basic_istream& operator>>(unsigned long& __n);
249 basic_istream& operator>>(long long& __n);
250 basic_istream& operator>>(unsigned long long& __n);
251 basic_istream& operator>>(float& __f);
252 basic_istream& operator>>(double& __f);
253 basic_istream& operator>>(long double& __f);
254 basic_istream& operator>>(void*& __p);
256 // 27.7.1.3 Unformatted input:
257 _LIBCPP_INLINE_VISIBILITY
258 streamsize gcount() const {return __gc_;}
261 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
262 basic_istream& get(char_type& __c) {
263 int_type __ch = get();
264 if (__ch != traits_type::eof())
265 __c = traits_type::to_char_type(__ch);
269 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
270 basic_istream& get(char_type* __s, streamsize __n)
271 { return get(__s, __n, this->widen('\n')); }
273 basic_istream& get(char_type* __s, streamsize __n, char_type __dlm);
275 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
276 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb)
277 { return get(__sb, this->widen('\n')); }
279 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm);
281 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
282 basic_istream& getline(char_type* __s, streamsize __n)
283 { return getline(__s, __n, this->widen('\n')); }
285 basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm);
287 basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof());
289 basic_istream& read (char_type* __s, streamsize __n);
290 streamsize readsome(char_type* __s, streamsize __n);
292 basic_istream& putback(char_type __c);
293 basic_istream& unget();
297 basic_istream& seekg(pos_type __pos);
298 basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
301 template <class _CharT, class _Traits>
302 class _LIBCPP_TEMPLATE_VIS basic_istream<_CharT, _Traits>::sentry
307 explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
308 // ~sentry() = default;
310 _LIBCPP_INLINE_VISIBILITY
311 explicit operator bool() const {return __ok_;}
313 sentry(const sentry&) = delete;
314 sentry& operator=(const sentry&) = delete;
317 template <class _CharT, class _Traits>
318 basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is,
326 if (!__noskipws && (__is.flags() & ios_base::skipws))
328 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
329 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
332 for (; __i != __eof; ++__i)
333 if (!__ct.is(__ct.space, *__i))
336 __is.setstate(ios_base::failbit | ios_base::eofbit);
341 __is.setstate(ios_base::failbit);
344 template <class _CharT, class _Traits>
345 basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs)
352 template <class _CharT, class _Traits>
353 basic_istream<_CharT, _Traits>&
354 basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs)
360 template <class _CharT, class _Traits>
361 basic_istream<_CharT, _Traits>::~basic_istream()
365 template <class _Tp, class _CharT, class _Traits>
366 _LIBCPP_INLINE_VISIBILITY
367 basic_istream<_CharT, _Traits>&
368 __input_arithmetic(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
369 ios_base::iostate __state = ios_base::goodbit;
370 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
373 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
376 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
377 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
378 typedef num_get<_CharT, _Ip> _Fp;
379 std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __n);
380 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
384 __state |= ios_base::badbit;
385 __is.__setstate_nothrow(__state);
386 if (__is.exceptions() & ios_base::badbit)
392 __is.setstate(__state);
397 template <class _CharT, class _Traits>
398 basic_istream<_CharT, _Traits>&
399 basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n)
401 return _VSTD::__input_arithmetic<unsigned short>(*this, __n);
404 template <class _CharT, class _Traits>
405 basic_istream<_CharT, _Traits>&
406 basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n)
408 return _VSTD::__input_arithmetic<unsigned int>(*this, __n);
411 template <class _CharT, class _Traits>
412 basic_istream<_CharT, _Traits>&
413 basic_istream<_CharT, _Traits>::operator>>(long& __n)
415 return _VSTD::__input_arithmetic<long>(*this, __n);
418 template <class _CharT, class _Traits>
419 basic_istream<_CharT, _Traits>&
420 basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n)
422 return _VSTD::__input_arithmetic<unsigned long>(*this, __n);
425 template <class _CharT, class _Traits>
426 basic_istream<_CharT, _Traits>&
427 basic_istream<_CharT, _Traits>::operator>>(long long& __n)
429 return _VSTD::__input_arithmetic<long long>(*this, __n);
432 template <class _CharT, class _Traits>
433 basic_istream<_CharT, _Traits>&
434 basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n)
436 return _VSTD::__input_arithmetic<unsigned long long>(*this, __n);
439 template <class _CharT, class _Traits>
440 basic_istream<_CharT, _Traits>&
441 basic_istream<_CharT, _Traits>::operator>>(float& __n)
443 return _VSTD::__input_arithmetic<float>(*this, __n);
446 template <class _CharT, class _Traits>
447 basic_istream<_CharT, _Traits>&
448 basic_istream<_CharT, _Traits>::operator>>(double& __n)
450 return _VSTD::__input_arithmetic<double>(*this, __n);
453 template <class _CharT, class _Traits>
454 basic_istream<_CharT, _Traits>&
455 basic_istream<_CharT, _Traits>::operator>>(long double& __n)
457 return _VSTD::__input_arithmetic<long double>(*this, __n);
460 template <class _CharT, class _Traits>
461 basic_istream<_CharT, _Traits>&
462 basic_istream<_CharT, _Traits>::operator>>(bool& __n)
464 return _VSTD::__input_arithmetic<bool>(*this, __n);
467 template <class _CharT, class _Traits>
468 basic_istream<_CharT, _Traits>&
469 basic_istream<_CharT, _Traits>::operator>>(void*& __n)
471 return _VSTD::__input_arithmetic<void*>(*this, __n);
474 template <class _Tp, class _CharT, class _Traits>
475 _LIBCPP_INLINE_VISIBILITY
476 basic_istream<_CharT, _Traits>&
477 __input_arithmetic_with_numeric_limits(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
478 ios_base::iostate __state = ios_base::goodbit;
479 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
482 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
485 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
486 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
487 typedef num_get<_CharT, _Ip> _Fp;
489 std::use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __temp);
490 if (__temp < numeric_limits<_Tp>::min())
492 __state |= ios_base::failbit;
493 __n = numeric_limits<_Tp>::min();
495 else if (__temp > numeric_limits<_Tp>::max())
497 __state |= ios_base::failbit;
498 __n = numeric_limits<_Tp>::max();
502 __n = static_cast<_Tp>(__temp);
504 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
508 __state |= ios_base::badbit;
509 __is.__setstate_nothrow(__state);
510 if (__is.exceptions() & ios_base::badbit)
515 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
516 __is.setstate(__state);
521 template <class _CharT, class _Traits>
522 basic_istream<_CharT, _Traits>&
523 basic_istream<_CharT, _Traits>::operator>>(short& __n)
525 return _VSTD::__input_arithmetic_with_numeric_limits<short>(*this, __n);
528 template <class _CharT, class _Traits>
529 basic_istream<_CharT, _Traits>&
530 basic_istream<_CharT, _Traits>::operator>>(int& __n)
532 return _VSTD::__input_arithmetic_with_numeric_limits<int>(*this, __n);
535 template<class _CharT, class _Traits>
536 _LIBCPP_INLINE_VISIBILITY
537 basic_istream<_CharT, _Traits>&
538 __input_c_string(basic_istream<_CharT, _Traits>& __is, _CharT* __p, size_t __n)
540 ios_base::iostate __state = ios_base::goodbit;
541 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
544 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
549 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
550 while (__s != __p + (__n-1))
552 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
553 if (_Traits::eq_int_type(__i, _Traits::eof()))
555 __state |= ios_base::eofbit;
558 _CharT __ch = _Traits::to_char_type(__i);
559 if (__ct.is(__ct.space, __ch))
562 __is.rdbuf()->sbumpc();
567 __state |= ios_base::failbit;
568 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
572 __state |= ios_base::badbit;
573 __is.__setstate_nothrow(__state);
574 if (__is.exceptions() & ios_base::badbit)
580 __is.setstate(__state);
585 #if _LIBCPP_STD_VER >= 20
587 template<class _CharT, class _Traits, size_t _Np>
588 inline _LIBCPP_INLINE_VISIBILITY
589 basic_istream<_CharT, _Traits>&
590 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np])
593 if (__is.width() > 0)
594 __n = _VSTD::min(size_t(__is.width()), _Np);
595 return _VSTD::__input_c_string(__is, __buf, __n);
598 template<class _Traits, size_t _Np>
599 inline _LIBCPP_INLINE_VISIBILITY
600 basic_istream<char, _Traits>&
601 operator>>(basic_istream<char, _Traits>& __is, unsigned char (&__buf)[_Np])
603 return __is >> (char(&)[_Np])__buf;
606 template<class _Traits, size_t _Np>
607 inline _LIBCPP_INLINE_VISIBILITY
608 basic_istream<char, _Traits>&
609 operator>>(basic_istream<char, _Traits>& __is, signed char (&__buf)[_Np])
611 return __is >> (char(&)[_Np])__buf;
616 template<class _CharT, class _Traits>
617 inline _LIBCPP_INLINE_VISIBILITY
618 basic_istream<_CharT, _Traits>&
619 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
621 streamsize __n = __is.width();
623 __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1;
624 return _VSTD::__input_c_string(__is, __s, size_t(__n));
627 template<class _Traits>
628 inline _LIBCPP_INLINE_VISIBILITY
629 basic_istream<char, _Traits>&
630 operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
632 return __is >> (char*)__s;
635 template<class _Traits>
636 inline _LIBCPP_INLINE_VISIBILITY
637 basic_istream<char, _Traits>&
638 operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
640 return __is >> (char*)__s;
643 #endif // _LIBCPP_STD_VER >= 20
645 template<class _CharT, class _Traits>
646 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
647 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
649 ios_base::iostate __state = ios_base::goodbit;
650 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
653 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
657 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
658 if (_Traits::eq_int_type(__i, _Traits::eof()))
659 __state |= ios_base::eofbit | ios_base::failbit;
661 __c = _Traits::to_char_type(__i);
662 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
666 __state |= ios_base::badbit;
667 __is.__setstate_nothrow(__state);
668 if (__is.exceptions() & ios_base::badbit)
674 __is.setstate(__state);
679 template<class _Traits>
680 inline _LIBCPP_INLINE_VISIBILITY
681 basic_istream<char, _Traits>&
682 operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
684 return __is >> (char&)__c;
687 template<class _Traits>
688 inline _LIBCPP_INLINE_VISIBILITY
689 basic_istream<char, _Traits>&
690 operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
692 return __is >> (char&)__c;
695 template<class _CharT, class _Traits>
696 basic_istream<_CharT, _Traits>&
697 basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb)
699 ios_base::iostate __state = ios_base::goodbit;
701 sentry __s(*this, true);
706 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
709 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
712 typename traits_type::int_type __i = this->rdbuf()->sgetc();
713 if (traits_type::eq_int_type(__i, _Traits::eof()))
715 __state |= ios_base::eofbit;
718 if (traits_type::eq_int_type(
719 __sb->sputc(traits_type::to_char_type(__i)),
723 this->rdbuf()->sbumpc();
726 __state |= ios_base::failbit;
727 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
731 __state |= ios_base::badbit;
733 __state |= ios_base::failbit;
735 this->__setstate_nothrow(__state);
736 if (this->exceptions() & ios_base::failbit || this->exceptions() & ios_base::badbit)
741 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
745 __state |= ios_base::failbit;
747 this->setstate(__state);
752 template<class _CharT, class _Traits>
753 typename basic_istream<_CharT, _Traits>::int_type
754 basic_istream<_CharT, _Traits>::get()
756 ios_base::iostate __state = ios_base::goodbit;
758 int_type __r = traits_type::eof();
759 sentry __s(*this, true);
762 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
766 __r = this->rdbuf()->sbumpc();
767 if (traits_type::eq_int_type(__r, traits_type::eof()))
768 __state |= ios_base::failbit | ios_base::eofbit;
771 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
775 this->__setstate_nothrow(this->rdstate() | ios_base::badbit);
776 if (this->exceptions() & ios_base::badbit)
782 this->setstate(__state);
787 template<class _CharT, class _Traits>
788 basic_istream<_CharT, _Traits>&
789 basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm)
791 ios_base::iostate __state = ios_base::goodbit;
793 sentry __sen(*this, true);
798 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
802 while (__gc_ < __n-1)
804 int_type __i = this->rdbuf()->sgetc();
805 if (traits_type::eq_int_type(__i, traits_type::eof()))
807 __state |= ios_base::eofbit;
810 char_type __ch = traits_type::to_char_type(__i);
811 if (traits_type::eq(__ch, __dlm))
815 this->rdbuf()->sbumpc();
818 __state |= ios_base::failbit;
819 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
823 __state |= ios_base::badbit;
824 this->__setstate_nothrow(__state);
825 if (this->exceptions() & ios_base::badbit)
836 __state |= ios_base::failbit;
841 this->setstate(__state);
848 template<class _CharT, class _Traits>
849 basic_istream<_CharT, _Traits>&
850 basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb,
853 ios_base::iostate __state = ios_base::goodbit;
855 sentry __sen(*this, true);
858 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
861 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
864 typename traits_type::int_type __i = this->rdbuf()->sgetc();
865 if (traits_type::eq_int_type(__i, traits_type::eof()))
867 __state |= ios_base::eofbit;
870 char_type __ch = traits_type::to_char_type(__i);
871 if (traits_type::eq(__ch, __dlm))
873 if (traits_type::eq_int_type(__sb.sputc(__ch), traits_type::eof()))
876 this->rdbuf()->sbumpc();
878 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
882 __state |= ios_base::badbit;
883 // according to the spec, exceptions here are caught but not rethrown
885 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
887 __state |= ios_base::failbit;
888 this->setstate(__state);
893 template<class _CharT, class _Traits>
894 basic_istream<_CharT, _Traits>&
895 basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm)
897 ios_base::iostate __state = ios_base::goodbit;
899 sentry __sen(*this, true);
902 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
905 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
908 typename traits_type::int_type __i = this->rdbuf()->sgetc();
909 if (traits_type::eq_int_type(__i, traits_type::eof()))
911 __state |= ios_base::eofbit;
914 char_type __ch = traits_type::to_char_type(__i);
915 if (traits_type::eq(__ch, __dlm))
917 this->rdbuf()->sbumpc();
923 __state |= ios_base::failbit;
927 this->rdbuf()->sbumpc();
930 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
934 __state |= ios_base::badbit;
935 this->__setstate_nothrow(__state);
936 if (this->exceptions() & ios_base::badbit)
941 __state |= ios_base::failbit;
945 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
950 __state |= ios_base::failbit;
951 this->setstate(__state);
955 template<class _CharT, class _Traits>
956 basic_istream<_CharT, _Traits>&
957 basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm)
959 ios_base::iostate __state = ios_base::goodbit;
961 sentry __sen(*this, true);
964 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
967 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
968 if (__n == numeric_limits<streamsize>::max())
972 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
973 if (traits_type::eq_int_type(__i, traits_type::eof()))
975 __state |= ios_base::eofbit;
979 if (traits_type::eq_int_type(__i, __dlm))
987 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
988 if (traits_type::eq_int_type(__i, traits_type::eof()))
990 __state |= ios_base::eofbit;
994 if (traits_type::eq_int_type(__i, __dlm))
998 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1002 __state |= ios_base::badbit;
1003 this->__setstate_nothrow(__state);
1004 if (this->exceptions() & ios_base::badbit)
1009 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1010 this->setstate(__state);
1015 template<class _CharT, class _Traits>
1016 typename basic_istream<_CharT, _Traits>::int_type
1017 basic_istream<_CharT, _Traits>::peek()
1019 ios_base::iostate __state = ios_base::goodbit;
1021 int_type __r = traits_type::eof();
1022 sentry __sen(*this, true);
1025 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1028 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1029 __r = this->rdbuf()->sgetc();
1030 if (traits_type::eq_int_type(__r, traits_type::eof()))
1031 __state |= ios_base::eofbit;
1032 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1036 __state |= ios_base::badbit;
1037 this->__setstate_nothrow(__state);
1038 if (this->exceptions() & ios_base::badbit)
1043 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1044 this->setstate(__state);
1049 template<class _CharT, class _Traits>
1050 basic_istream<_CharT, _Traits>&
1051 basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n)
1053 ios_base::iostate __state = ios_base::goodbit;
1055 sentry __sen(*this, true);
1058 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1061 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1062 __gc_ = this->rdbuf()->sgetn(__s, __n);
1064 __state |= ios_base::failbit | ios_base::eofbit;
1065 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1069 __state |= ios_base::badbit;
1070 this->__setstate_nothrow(__state);
1071 if (this->exceptions() & ios_base::badbit)
1076 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1080 __state |= ios_base::failbit;
1082 this->setstate(__state);
1086 template<class _CharT, class _Traits>
1088 basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n)
1090 ios_base::iostate __state = ios_base::goodbit;
1092 sentry __sen(*this, true);
1095 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1098 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1099 streamsize __c = this->rdbuf()->in_avail();
1103 __state |= ios_base::eofbit;
1108 __n = _VSTD::min(__c, __n);
1109 __gc_ = this->rdbuf()->sgetn(__s, __n);
1111 __state |= ios_base::failbit | ios_base::eofbit;
1114 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1118 __state |= ios_base::badbit;
1119 this->__setstate_nothrow(__state);
1120 if (this->exceptions() & ios_base::badbit)
1125 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1129 __state |= ios_base::failbit;
1131 this->setstate(__state);
1135 template<class _CharT, class _Traits>
1136 basic_istream<_CharT, _Traits>&
1137 basic_istream<_CharT, _Traits>::putback(char_type __c)
1139 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1141 this->clear(__state);
1142 sentry __sen(*this, true);
1145 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1148 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1149 if (this->rdbuf() == nullptr || this->rdbuf()->sputbackc(__c) == traits_type::eof())
1150 __state |= ios_base::badbit;
1151 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1155 __state |= ios_base::badbit;
1156 this->__setstate_nothrow(__state);
1157 if (this->exceptions() & ios_base::badbit)
1162 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1166 __state |= ios_base::failbit;
1168 this->setstate(__state);
1172 template<class _CharT, class _Traits>
1173 basic_istream<_CharT, _Traits>&
1174 basic_istream<_CharT, _Traits>::unget()
1176 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1178 this->clear(__state);
1179 sentry __sen(*this, true);
1182 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1185 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1186 if (this->rdbuf() == nullptr || this->rdbuf()->sungetc() == traits_type::eof())
1187 __state |= ios_base::badbit;
1188 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1192 __state |= ios_base::badbit;
1193 this->__setstate_nothrow(__state);
1194 if (this->exceptions() & ios_base::badbit)
1199 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1203 __state |= ios_base::failbit;
1205 this->setstate(__state);
1209 template<class _CharT, class _Traits>
1211 basic_istream<_CharT, _Traits>::sync()
1213 ios_base::iostate __state = ios_base::goodbit;
1215 sentry __sen(*this, true);
1218 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1221 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1222 if (this->rdbuf() == nullptr)
1224 if (this->rdbuf()->pubsync() == -1)
1226 __state |= ios_base::badbit;
1229 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1233 __state |= ios_base::badbit;
1234 this->__setstate_nothrow(__state);
1235 if (this->exceptions() & ios_base::badbit)
1240 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1241 this->setstate(__state);
1246 template<class _CharT, class _Traits>
1247 typename basic_istream<_CharT, _Traits>::pos_type
1248 basic_istream<_CharT, _Traits>::tellg()
1250 ios_base::iostate __state = ios_base::goodbit;
1252 sentry __sen(*this, true);
1255 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1258 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1259 __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
1260 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1264 __state |= ios_base::badbit;
1265 this->__setstate_nothrow(__state);
1266 if (this->exceptions() & ios_base::badbit)
1271 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1272 this->setstate(__state);
1277 template<class _CharT, class _Traits>
1278 basic_istream<_CharT, _Traits>&
1279 basic_istream<_CharT, _Traits>::seekg(pos_type __pos)
1281 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1282 this->clear(__state);
1283 sentry __sen(*this, true);
1286 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1289 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1290 if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
1291 __state |= ios_base::failbit;
1292 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1296 __state |= ios_base::badbit;
1297 this->__setstate_nothrow(__state);
1298 if (this->exceptions() & ios_base::badbit)
1303 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1304 this->setstate(__state);
1309 template<class _CharT, class _Traits>
1310 basic_istream<_CharT, _Traits>&
1311 basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir)
1313 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1314 this->clear(__state);
1315 sentry __sen(*this, true);
1318 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1321 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1322 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::in) == pos_type(-1))
1323 __state |= ios_base::failbit;
1324 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1328 __state |= ios_base::badbit;
1329 this->__setstate_nothrow(__state);
1330 if (this->exceptions() & ios_base::badbit)
1335 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1336 this->setstate(__state);
1341 template <class _CharT, class _Traits>
1342 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1343 ws(basic_istream<_CharT, _Traits>& __is)
1345 ios_base::iostate __state = ios_base::goodbit;
1346 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1349 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1352 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1353 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1356 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1357 if (_Traits::eq_int_type(__i, _Traits::eof()))
1359 __state |= ios_base::eofbit;
1362 if (!__ct.is(__ct.space, _Traits::to_char_type(__i)))
1364 __is.rdbuf()->sbumpc();
1366 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1370 __state |= ios_base::badbit;
1371 __is.__setstate_nothrow(__state);
1372 if (__is.exceptions() & ios_base::badbit)
1377 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
1378 __is.setstate(__state);
1383 template <class _Stream, class _Tp, class = void>
1384 struct __is_istreamable : false_type { };
1386 template <class _Stream, class _Tp>
1387 struct __is_istreamable<_Stream, _Tp, decltype(
1388 std::declval<_Stream>() >> std::declval<_Tp>(), void()
1391 template <class _Stream, class _Tp, __enable_if_t<
1392 _And<is_base_of<ios_base, _Stream>, __is_istreamable<_Stream&, _Tp&&> >::value, int> = 0>
1393 _LIBCPP_INLINE_VISIBILITY
1394 _Stream&& operator>>(_Stream&& __is, _Tp&& __x)
1396 __is >> _VSTD::forward<_Tp>(__x);
1397 return _VSTD::move(__is);
1400 template <class _CharT, class _Traits>
1401 class _LIBCPP_TEMPLATE_VIS basic_iostream
1402 : public basic_istream<_CharT, _Traits>,
1403 public basic_ostream<_CharT, _Traits>
1407 typedef _CharT char_type;
1408 typedef _Traits traits_type;
1409 typedef typename traits_type::int_type int_type;
1410 typedef typename traits_type::pos_type pos_type;
1411 typedef typename traits_type::off_type off_type;
1413 // constructor/destructor
1414 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
1415 explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb)
1416 : basic_istream<_CharT, _Traits>(__sb)
1419 ~basic_iostream() override;
1421 inline _LIBCPP_INLINE_VISIBILITY
1422 basic_iostream(basic_iostream&& __rhs);
1425 inline _LIBCPP_INLINE_VISIBILITY
1426 basic_iostream& operator=(basic_iostream&& __rhs);
1428 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
1429 void swap(basic_iostream& __rhs)
1430 { basic_istream<char_type, traits_type>::swap(__rhs); }
1433 template <class _CharT, class _Traits>
1434 basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
1435 : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs))
1439 template <class _CharT, class _Traits>
1440 basic_iostream<_CharT, _Traits>&
1441 basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs)
1447 template <class _CharT, class _Traits>
1448 basic_iostream<_CharT, _Traits>::~basic_iostream()
1452 template<class _CharT, class _Traits, class _Allocator>
1453 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1454 operator>>(basic_istream<_CharT, _Traits>& __is,
1455 basic_string<_CharT, _Traits, _Allocator>& __str)
1457 ios_base::iostate __state = ios_base::goodbit;
1458 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1461 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1466 streamsize __n = __is.width();
1468 __n = __str.max_size();
1470 __n = numeric_limits<streamsize>::max();
1472 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1475 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1476 if (_Traits::eq_int_type(__i, _Traits::eof()))
1478 __state |= ios_base::eofbit;
1481 _CharT __ch = _Traits::to_char_type(__i);
1482 if (__ct.is(__ct.space, __ch))
1484 __str.push_back(__ch);
1486 __is.rdbuf()->sbumpc();
1490 __state |= ios_base::failbit;
1491 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1495 __state |= ios_base::badbit;
1496 __is.__setstate_nothrow(__state);
1497 if (__is.exceptions() & ios_base::badbit)
1503 __is.setstate(__state);
1508 template<class _CharT, class _Traits, class _Allocator>
1509 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1510 getline(basic_istream<_CharT, _Traits>& __is,
1511 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
1513 ios_base::iostate __state = ios_base::goodbit;
1514 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1517 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1522 streamsize __extr = 0;
1525 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
1526 if (_Traits::eq_int_type(__i, _Traits::eof()))
1528 __state |= ios_base::eofbit;
1532 _CharT __ch = _Traits::to_char_type(__i);
1533 if (_Traits::eq(__ch, __dlm))
1535 __str.push_back(__ch);
1536 if (__str.size() == __str.max_size())
1538 __state |= ios_base::failbit;
1543 __state |= ios_base::failbit;
1544 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1548 __state |= ios_base::badbit;
1549 __is.__setstate_nothrow(__state);
1550 if (__is.exceptions() & ios_base::badbit)
1556 __is.setstate(__state);
1561 template<class _CharT, class _Traits, class _Allocator>
1562 inline _LIBCPP_INLINE_VISIBILITY
1563 basic_istream<_CharT, _Traits>&
1564 getline(basic_istream<_CharT, _Traits>& __is,
1565 basic_string<_CharT, _Traits, _Allocator>& __str)
1567 return std::getline(__is, __str, __is.widen('\n'));
1570 template<class _CharT, class _Traits, class _Allocator>
1571 inline _LIBCPP_INLINE_VISIBILITY
1572 basic_istream<_CharT, _Traits>&
1573 getline(basic_istream<_CharT, _Traits>&& __is,
1574 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
1576 return std::getline(__is, __str, __dlm);
1579 template<class _CharT, class _Traits, class _Allocator>
1580 inline _LIBCPP_INLINE_VISIBILITY
1581 basic_istream<_CharT, _Traits>&
1582 getline(basic_istream<_CharT, _Traits>&& __is,
1583 basic_string<_CharT, _Traits, _Allocator>& __str)
1585 return std::getline(__is, __str, __is.widen('\n'));
1588 template <class _CharT, class _Traits, size_t _Size>
1589 _LIBCPP_HIDE_FROM_ABI basic_istream<_CharT, _Traits>&
1590 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
1592 ios_base::iostate __state = ios_base::goodbit;
1593 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1596 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1600 basic_string<_CharT, _Traits> __str;
1601 const ctype<_CharT>& __ct = std::use_facet<ctype<_CharT> >(__is.getloc());
1603 _CharT __zero = __ct.widen('0');
1604 _CharT __one = __ct.widen('1');
1605 while (__c != _Size)
1607 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1608 if (_Traits::eq_int_type(__i, _Traits::eof()))
1610 __state |= ios_base::eofbit;
1613 _CharT __ch = _Traits::to_char_type(__i);
1614 if (!_Traits::eq(__ch, __zero) && !_Traits::eq(__ch, __one))
1616 __str.push_back(__ch);
1618 __is.rdbuf()->sbumpc();
1620 __x = bitset<_Size>(__str);
1621 if (_Size > 0 && __c == 0)
1622 __state |= ios_base::failbit;
1623 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
1627 __state |= ios_base::badbit;
1628 __is.__setstate_nothrow(__state);
1629 if (__is.exceptions() & ios_base::badbit)
1635 __is.setstate(__state);
1640 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<char>;
1641 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
1642 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<wchar_t>;
1644 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_iostream<char>;
1646 _LIBCPP_END_NAMESPACE_STD
1648 #if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20
1649 # include <concepts>
1651 # include <type_traits>
1656 #endif // _LIBCPP_ISTREAM