2 //===--------------------------- istream ----------------------------------===//
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 template <class charT, class traits, class T>
154 basic_istream<charT, traits>&
155 operator>>(basic_istream<charT, traits>&& is, T& x);
165 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
166 #pragma GCC system_header
170 #include <__undef_macros>
173 _LIBCPP_BEGIN_NAMESPACE_STD
175 template <class _CharT, class _Traits>
176 class _LIBCPP_TEMPLATE_VIS basic_istream
177 : virtual public basic_ios<_CharT, _Traits>
181 // types (inherited from basic_ios (27.5.4)):
182 typedef _CharT char_type;
183 typedef _Traits traits_type;
184 typedef typename traits_type::int_type int_type;
185 typedef typename traits_type::pos_type pos_type;
186 typedef typename traits_type::off_type off_type;
188 // 27.7.1.1.1 Constructor/destructor:
189 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
190 explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb) : __gc_(0)
191 { this->init(__sb); }
192 virtual ~basic_istream();
194 #ifndef _LIBCPP_CXX03_LANG
195 inline _LIBCPP_INLINE_VISIBILITY
196 basic_istream(basic_istream&& __rhs);
198 // 27.7.1.1.2 Assign/swap:
199 inline _LIBCPP_INLINE_VISIBILITY
200 basic_istream& operator=(basic_istream&& __rhs);
203 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
204 void swap(basic_istream& __rhs) {
205 _VSTD::swap(__gc_, __rhs.__gc_);
206 basic_ios<char_type, traits_type>::swap(__rhs);
209 #ifndef _LIBCPP_CXX03_LANG
210 basic_istream (const basic_istream& __rhs) = delete;
211 basic_istream& operator=(const basic_istream& __rhs) = delete;
215 // 27.7.1.1.3 Prefix/suffix:
216 class _LIBCPP_TEMPLATE_VIS sentry;
218 // 27.7.1.2 Formatted input:
219 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
220 basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&))
221 { return __pf(*this); }
223 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
224 basic_istream& operator>>(basic_ios<char_type, traits_type>&
225 (*__pf)(basic_ios<char_type, traits_type>&))
226 { __pf(*this); return *this; }
228 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
229 basic_istream& operator>>(ios_base& (*__pf)(ios_base&))
230 { __pf(*this); return *this; }
232 basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
233 basic_istream& operator>>(bool& __n);
234 basic_istream& operator>>(short& __n);
235 basic_istream& operator>>(unsigned short& __n);
236 basic_istream& operator>>(int& __n);
237 basic_istream& operator>>(unsigned int& __n);
238 basic_istream& operator>>(long& __n);
239 basic_istream& operator>>(unsigned long& __n);
240 basic_istream& operator>>(long long& __n);
241 basic_istream& operator>>(unsigned long long& __n);
242 basic_istream& operator>>(float& __f);
243 basic_istream& operator>>(double& __f);
244 basic_istream& operator>>(long double& __f);
245 basic_istream& operator>>(void*& __p);
247 // 27.7.1.3 Unformatted input:
248 _LIBCPP_INLINE_VISIBILITY
249 streamsize gcount() const {return __gc_;}
252 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
253 basic_istream& get(char_type& __c) {
254 int_type __ch = get();
255 if (__ch != traits_type::eof())
256 __c = traits_type::to_char_type(__ch);
260 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
261 basic_istream& get(char_type* __s, streamsize __n)
262 { return get(__s, __n, this->widen('\n')); }
264 basic_istream& get(char_type* __s, streamsize __n, char_type __dlm);
266 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
267 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb)
268 { return get(__sb, this->widen('\n')); }
270 basic_istream& get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm);
272 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
273 basic_istream& getline(char_type* __s, streamsize __n)
274 { return getline(__s, __n, this->widen('\n')); }
276 basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm);
278 basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof());
280 basic_istream& read (char_type* __s, streamsize __n);
281 streamsize readsome(char_type* __s, streamsize __n);
283 basic_istream& putback(char_type __c);
284 basic_istream& unget();
288 basic_istream& seekg(pos_type __pos);
289 basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
292 template <class _CharT, class _Traits>
293 class _LIBCPP_TEMPLATE_VIS basic_istream<_CharT, _Traits>::sentry
297 sentry(const sentry&); // = delete;
298 sentry& operator=(const sentry&); // = delete;
301 explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
302 // ~sentry() = default;
304 _LIBCPP_INLINE_VISIBILITY
306 operator bool() const {return __ok_;}
309 template <class _CharT, class _Traits>
310 basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is,
318 if (!__noskipws && (__is.flags() & ios_base::skipws))
320 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
321 const ctype<_CharT>& __ct = 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);
333 __is.setstate(ios_base::failbit);
336 #ifndef _LIBCPP_CXX03_LANG
338 template <class _CharT, class _Traits>
339 basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs)
346 template <class _CharT, class _Traits>
347 basic_istream<_CharT, _Traits>&
348 basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs)
354 #endif // _LIBCPP_CXX03_LANG
356 template <class _CharT, class _Traits>
357 basic_istream<_CharT, _Traits>::~basic_istream()
361 template <class _Tp, class _CharT, class _Traits>
362 _LIBCPP_INLINE_VISIBILITY
363 basic_istream<_CharT, _Traits>&
364 __input_arithmetic(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
365 ios_base::iostate __state = ios_base::goodbit;
366 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
369 #ifndef _LIBCPP_NO_EXCEPTIONS
372 #endif // _LIBCPP_NO_EXCEPTIONS
373 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
374 typedef num_get<_CharT, _Ip> _Fp;
375 use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __n);
376 #ifndef _LIBCPP_NO_EXCEPTIONS
380 __state |= ios_base::badbit;
381 __is.__setstate_nothrow(__state);
382 if (__is.exceptions() & ios_base::badbit)
388 __is.setstate(__state);
393 template <class _CharT, class _Traits>
394 basic_istream<_CharT, _Traits>&
395 basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n)
397 return _VSTD::__input_arithmetic<unsigned short>(*this, __n);
400 template <class _CharT, class _Traits>
401 basic_istream<_CharT, _Traits>&
402 basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n)
404 return _VSTD::__input_arithmetic<unsigned int>(*this, __n);
407 template <class _CharT, class _Traits>
408 basic_istream<_CharT, _Traits>&
409 basic_istream<_CharT, _Traits>::operator>>(long& __n)
411 return _VSTD::__input_arithmetic<long>(*this, __n);
414 template <class _CharT, class _Traits>
415 basic_istream<_CharT, _Traits>&
416 basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n)
418 return _VSTD::__input_arithmetic<unsigned long>(*this, __n);
421 template <class _CharT, class _Traits>
422 basic_istream<_CharT, _Traits>&
423 basic_istream<_CharT, _Traits>::operator>>(long long& __n)
425 return _VSTD::__input_arithmetic<long long>(*this, __n);
428 template <class _CharT, class _Traits>
429 basic_istream<_CharT, _Traits>&
430 basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n)
432 return _VSTD::__input_arithmetic<unsigned long long>(*this, __n);
435 template <class _CharT, class _Traits>
436 basic_istream<_CharT, _Traits>&
437 basic_istream<_CharT, _Traits>::operator>>(float& __n)
439 return _VSTD::__input_arithmetic<float>(*this, __n);
442 template <class _CharT, class _Traits>
443 basic_istream<_CharT, _Traits>&
444 basic_istream<_CharT, _Traits>::operator>>(double& __n)
446 return _VSTD::__input_arithmetic<double>(*this, __n);
449 template <class _CharT, class _Traits>
450 basic_istream<_CharT, _Traits>&
451 basic_istream<_CharT, _Traits>::operator>>(long double& __n)
453 return _VSTD::__input_arithmetic<long double>(*this, __n);
456 template <class _CharT, class _Traits>
457 basic_istream<_CharT, _Traits>&
458 basic_istream<_CharT, _Traits>::operator>>(bool& __n)
460 return _VSTD::__input_arithmetic<bool>(*this, __n);
463 template <class _CharT, class _Traits>
464 basic_istream<_CharT, _Traits>&
465 basic_istream<_CharT, _Traits>::operator>>(void*& __n)
467 return _VSTD::__input_arithmetic<void*>(*this, __n);
470 template <class _Tp, class _CharT, class _Traits>
471 _LIBCPP_INLINE_VISIBILITY
472 basic_istream<_CharT, _Traits>&
473 __input_arithmetic_with_numeric_limits(basic_istream<_CharT, _Traits>& __is, _Tp& __n) {
474 ios_base::iostate __state = ios_base::goodbit;
475 typename basic_istream<_CharT, _Traits>::sentry __s(__is);
478 #ifndef _LIBCPP_NO_EXCEPTIONS
481 #endif // _LIBCPP_NO_EXCEPTIONS
482 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
483 typedef num_get<_CharT, _Ip> _Fp;
485 use_facet<_Fp>(__is.getloc()).get(_Ip(__is), _Ip(), __is, __state, __temp);
486 if (__temp < numeric_limits<_Tp>::min())
488 __state |= ios_base::failbit;
489 __n = numeric_limits<_Tp>::min();
491 else if (__temp > numeric_limits<_Tp>::max())
493 __state |= ios_base::failbit;
494 __n = numeric_limits<_Tp>::max();
498 __n = static_cast<_Tp>(__temp);
500 #ifndef _LIBCPP_NO_EXCEPTIONS
504 __state |= ios_base::badbit;
505 __is.__setstate_nothrow(__state);
506 if (__is.exceptions() & ios_base::badbit)
511 #endif // _LIBCPP_NO_EXCEPTIONS
512 __is.setstate(__state);
517 template <class _CharT, class _Traits>
518 basic_istream<_CharT, _Traits>&
519 basic_istream<_CharT, _Traits>::operator>>(short& __n)
521 return _VSTD::__input_arithmetic_with_numeric_limits<short>(*this, __n);
524 template <class _CharT, class _Traits>
525 basic_istream<_CharT, _Traits>&
526 basic_istream<_CharT, _Traits>::operator>>(int& __n)
528 return _VSTD::__input_arithmetic_with_numeric_limits<int>(*this, __n);
531 template<class _CharT, class _Traits>
532 _LIBCPP_INLINE_VISIBILITY
533 basic_istream<_CharT, _Traits>&
534 __input_c_string(basic_istream<_CharT, _Traits>& __is, _CharT* __p, size_t __n)
536 ios_base::iostate __state = ios_base::goodbit;
537 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
540 #ifndef _LIBCPP_NO_EXCEPTIONS
545 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
546 while (__s != __p + (__n-1))
548 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
549 if (_Traits::eq_int_type(__i, _Traits::eof()))
551 __state |= ios_base::eofbit;
554 _CharT __ch = _Traits::to_char_type(__i);
555 if (__ct.is(__ct.space, __ch))
558 __is.rdbuf()->sbumpc();
563 __state |= ios_base::failbit;
564 #ifndef _LIBCPP_NO_EXCEPTIONS
568 __state |= ios_base::badbit;
569 __is.__setstate_nothrow(__state);
570 if (__is.exceptions() & ios_base::badbit)
576 __is.setstate(__state);
581 #if _LIBCPP_STD_VER > 17
583 template<class _CharT, class _Traits, size_t _Np>
584 inline _LIBCPP_INLINE_VISIBILITY
585 basic_istream<_CharT, _Traits>&
586 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT (&__buf)[_Np])
589 if (__is.width() > 0)
590 __n = _VSTD::min(size_t(__is.width()), _Np);
591 return _VSTD::__input_c_string(__is, __buf, __n);
594 template<class _Traits, size_t _Np>
595 inline _LIBCPP_INLINE_VISIBILITY
596 basic_istream<char, _Traits>&
597 operator>>(basic_istream<char, _Traits>& __is, unsigned char (&__buf)[_Np])
599 return __is >> (char(&)[_Np])__buf;
602 template<class _Traits, size_t _Np>
603 inline _LIBCPP_INLINE_VISIBILITY
604 basic_istream<char, _Traits>&
605 operator>>(basic_istream<char, _Traits>& __is, signed char (&__buf)[_Np])
607 return __is >> (char(&)[_Np])__buf;
612 template<class _CharT, class _Traits>
613 inline _LIBCPP_INLINE_VISIBILITY
614 basic_istream<_CharT, _Traits>&
615 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
617 streamsize __n = __is.width();
619 __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1;
620 return _VSTD::__input_c_string(__is, __s, size_t(__n));
623 template<class _Traits>
624 inline _LIBCPP_INLINE_VISIBILITY
625 basic_istream<char, _Traits>&
626 operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
628 return __is >> (char*)__s;
631 template<class _Traits>
632 inline _LIBCPP_INLINE_VISIBILITY
633 basic_istream<char, _Traits>&
634 operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
636 return __is >> (char*)__s;
639 #endif // _LIBCPP_STD_VER > 17
641 template<class _CharT, class _Traits>
642 basic_istream<_CharT, _Traits>&
643 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
645 ios_base::iostate __state = ios_base::goodbit;
646 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
649 #ifndef _LIBCPP_NO_EXCEPTIONS
653 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
654 if (_Traits::eq_int_type(__i, _Traits::eof()))
655 __state |= ios_base::eofbit | ios_base::failbit;
657 __c = _Traits::to_char_type(__i);
658 #ifndef _LIBCPP_NO_EXCEPTIONS
662 __state |= ios_base::badbit;
663 __is.__setstate_nothrow(__state);
664 if (__is.exceptions() & ios_base::badbit)
670 __is.setstate(__state);
675 template<class _Traits>
676 inline _LIBCPP_INLINE_VISIBILITY
677 basic_istream<char, _Traits>&
678 operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
680 return __is >> (char&)__c;
683 template<class _Traits>
684 inline _LIBCPP_INLINE_VISIBILITY
685 basic_istream<char, _Traits>&
686 operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
688 return __is >> (char&)__c;
691 template<class _CharT, class _Traits>
692 basic_istream<_CharT, _Traits>&
693 basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb)
695 ios_base::iostate __state = ios_base::goodbit;
697 sentry __s(*this, true);
702 #ifndef _LIBCPP_NO_EXCEPTIONS
705 #endif // _LIBCPP_NO_EXCEPTIONS
708 typename traits_type::int_type __i = this->rdbuf()->sgetc();
709 if (traits_type::eq_int_type(__i, _Traits::eof()))
711 __state |= ios_base::eofbit;
714 if (traits_type::eq_int_type(
715 __sb->sputc(traits_type::to_char_type(__i)),
719 this->rdbuf()->sbumpc();
722 __state |= ios_base::failbit;
723 #ifndef _LIBCPP_NO_EXCEPTIONS
727 __state |= ios_base::badbit;
729 __state |= ios_base::failbit;
731 this->__setstate_nothrow(__state);
732 if (this->exceptions() & ios_base::failbit || this->exceptions() & ios_base::badbit)
737 #endif // _LIBCPP_NO_EXCEPTIONS
741 __state |= ios_base::failbit;
743 this->setstate(__state);
748 template<class _CharT, class _Traits>
749 typename basic_istream<_CharT, _Traits>::int_type
750 basic_istream<_CharT, _Traits>::get()
752 ios_base::iostate __state = ios_base::goodbit;
754 int_type __r = traits_type::eof();
755 sentry __s(*this, true);
758 #ifndef _LIBCPP_NO_EXCEPTIONS
762 __r = this->rdbuf()->sbumpc();
763 if (traits_type::eq_int_type(__r, traits_type::eof()))
764 __state |= ios_base::failbit | ios_base::eofbit;
767 #ifndef _LIBCPP_NO_EXCEPTIONS
771 this->__setstate_nothrow(this->rdstate() | ios_base::badbit);
772 if (this->exceptions() & ios_base::badbit)
778 this->setstate(__state);
783 template<class _CharT, class _Traits>
784 basic_istream<_CharT, _Traits>&
785 basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm)
787 ios_base::iostate __state = ios_base::goodbit;
789 sentry __sen(*this, true);
794 #ifndef _LIBCPP_NO_EXCEPTIONS
798 while (__gc_ < __n-1)
800 int_type __i = this->rdbuf()->sgetc();
801 if (traits_type::eq_int_type(__i, traits_type::eof()))
803 __state |= ios_base::eofbit;
806 char_type __ch = traits_type::to_char_type(__i);
807 if (traits_type::eq(__ch, __dlm))
811 this->rdbuf()->sbumpc();
814 __state |= ios_base::failbit;
815 #ifndef _LIBCPP_NO_EXCEPTIONS
819 __state |= ios_base::badbit;
820 this->__setstate_nothrow(__state);
821 if (this->exceptions() & ios_base::badbit)
832 __state |= ios_base::failbit;
837 this->setstate(__state);
844 template<class _CharT, class _Traits>
845 basic_istream<_CharT, _Traits>&
846 basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb,
849 ios_base::iostate __state = ios_base::goodbit;
851 sentry __sen(*this, true);
854 #ifndef _LIBCPP_NO_EXCEPTIONS
857 #endif // _LIBCPP_NO_EXCEPTIONS
860 typename traits_type::int_type __i = this->rdbuf()->sgetc();
861 if (traits_type::eq_int_type(__i, traits_type::eof()))
863 __state |= ios_base::eofbit;
866 char_type __ch = traits_type::to_char_type(__i);
867 if (traits_type::eq(__ch, __dlm))
869 if (traits_type::eq_int_type(__sb.sputc(__ch), traits_type::eof()))
872 this->rdbuf()->sbumpc();
874 #ifndef _LIBCPP_NO_EXCEPTIONS
878 __state |= ios_base::badbit;
879 // according to the spec, exceptions here are caught but not rethrown
881 #endif // _LIBCPP_NO_EXCEPTIONS
883 __state |= ios_base::failbit;
884 this->setstate(__state);
889 template<class _CharT, class _Traits>
890 basic_istream<_CharT, _Traits>&
891 basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm)
893 ios_base::iostate __state = ios_base::goodbit;
895 sentry __sen(*this, true);
898 #ifndef _LIBCPP_NO_EXCEPTIONS
901 #endif // _LIBCPP_NO_EXCEPTIONS
904 typename traits_type::int_type __i = this->rdbuf()->sgetc();
905 if (traits_type::eq_int_type(__i, traits_type::eof()))
907 __state |= ios_base::eofbit;
910 char_type __ch = traits_type::to_char_type(__i);
911 if (traits_type::eq(__ch, __dlm))
913 this->rdbuf()->sbumpc();
919 __state |= ios_base::failbit;
923 this->rdbuf()->sbumpc();
926 #ifndef _LIBCPP_NO_EXCEPTIONS
930 __state |= ios_base::badbit;
931 this->__setstate_nothrow(__state);
932 if (this->exceptions() & ios_base::badbit)
937 __state |= ios_base::failbit;
941 #endif // _LIBCPP_NO_EXCEPTIONS
946 __state |= ios_base::failbit;
947 this->setstate(__state);
951 template<class _CharT, class _Traits>
952 basic_istream<_CharT, _Traits>&
953 basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm)
955 ios_base::iostate __state = ios_base::goodbit;
957 sentry __sen(*this, true);
960 #ifndef _LIBCPP_NO_EXCEPTIONS
963 #endif // _LIBCPP_NO_EXCEPTIONS
964 if (__n == numeric_limits<streamsize>::max())
968 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
969 if (traits_type::eq_int_type(__i, traits_type::eof()))
971 __state |= ios_base::eofbit;
975 if (traits_type::eq_int_type(__i, __dlm))
983 typename traits_type::int_type __i = this->rdbuf()->sbumpc();
984 if (traits_type::eq_int_type(__i, traits_type::eof()))
986 __state |= ios_base::eofbit;
990 if (traits_type::eq_int_type(__i, __dlm))
994 #ifndef _LIBCPP_NO_EXCEPTIONS
998 __state |= ios_base::badbit;
999 this->__setstate_nothrow(__state);
1000 if (this->exceptions() & ios_base::badbit)
1005 #endif // _LIBCPP_NO_EXCEPTIONS
1006 this->setstate(__state);
1011 template<class _CharT, class _Traits>
1012 typename basic_istream<_CharT, _Traits>::int_type
1013 basic_istream<_CharT, _Traits>::peek()
1015 ios_base::iostate __state = ios_base::goodbit;
1017 int_type __r = traits_type::eof();
1018 sentry __sen(*this, true);
1021 #ifndef _LIBCPP_NO_EXCEPTIONS
1024 #endif // _LIBCPP_NO_EXCEPTIONS
1025 __r = this->rdbuf()->sgetc();
1026 if (traits_type::eq_int_type(__r, traits_type::eof()))
1027 __state |= ios_base::eofbit;
1028 #ifndef _LIBCPP_NO_EXCEPTIONS
1032 __state |= ios_base::badbit;
1033 this->__setstate_nothrow(__state);
1034 if (this->exceptions() & ios_base::badbit)
1039 #endif // _LIBCPP_NO_EXCEPTIONS
1040 this->setstate(__state);
1045 template<class _CharT, class _Traits>
1046 basic_istream<_CharT, _Traits>&
1047 basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n)
1049 ios_base::iostate __state = ios_base::goodbit;
1051 sentry __sen(*this, true);
1054 #ifndef _LIBCPP_NO_EXCEPTIONS
1057 #endif // _LIBCPP_NO_EXCEPTIONS
1058 __gc_ = this->rdbuf()->sgetn(__s, __n);
1060 __state |= ios_base::failbit | ios_base::eofbit;
1061 #ifndef _LIBCPP_NO_EXCEPTIONS
1065 __state |= ios_base::badbit;
1066 this->__setstate_nothrow(__state);
1067 if (this->exceptions() & ios_base::badbit)
1072 #endif // _LIBCPP_NO_EXCEPTIONS
1076 __state |= ios_base::failbit;
1078 this->setstate(__state);
1082 template<class _CharT, class _Traits>
1084 basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n)
1086 ios_base::iostate __state = ios_base::goodbit;
1088 sentry __sen(*this, true);
1091 #ifndef _LIBCPP_NO_EXCEPTIONS
1094 #endif // _LIBCPP_NO_EXCEPTIONS
1095 streamsize __c = this->rdbuf()->in_avail();
1099 __state |= ios_base::eofbit;
1104 __n = _VSTD::min(__c, __n);
1105 __gc_ = this->rdbuf()->sgetn(__s, __n);
1107 __state |= ios_base::failbit | ios_base::eofbit;
1110 #ifndef _LIBCPP_NO_EXCEPTIONS
1114 __state |= ios_base::badbit;
1115 this->__setstate_nothrow(__state);
1116 if (this->exceptions() & ios_base::badbit)
1121 #endif // _LIBCPP_NO_EXCEPTIONS
1125 __state |= ios_base::failbit;
1127 this->setstate(__state);
1131 template<class _CharT, class _Traits>
1132 basic_istream<_CharT, _Traits>&
1133 basic_istream<_CharT, _Traits>::putback(char_type __c)
1135 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1137 this->clear(__state);
1138 sentry __sen(*this, true);
1141 #ifndef _LIBCPP_NO_EXCEPTIONS
1144 #endif // _LIBCPP_NO_EXCEPTIONS
1145 if (this->rdbuf() == 0 || this->rdbuf()->sputbackc(__c) == traits_type::eof())
1146 __state |= ios_base::badbit;
1147 #ifndef _LIBCPP_NO_EXCEPTIONS
1151 __state |= ios_base::badbit;
1152 this->__setstate_nothrow(__state);
1153 if (this->exceptions() & ios_base::badbit)
1158 #endif // _LIBCPP_NO_EXCEPTIONS
1162 __state |= ios_base::failbit;
1164 this->setstate(__state);
1168 template<class _CharT, class _Traits>
1169 basic_istream<_CharT, _Traits>&
1170 basic_istream<_CharT, _Traits>::unget()
1172 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1174 this->clear(__state);
1175 sentry __sen(*this, true);
1178 #ifndef _LIBCPP_NO_EXCEPTIONS
1181 #endif // _LIBCPP_NO_EXCEPTIONS
1182 if (this->rdbuf() == 0 || this->rdbuf()->sungetc() == traits_type::eof())
1183 __state |= ios_base::badbit;
1184 #ifndef _LIBCPP_NO_EXCEPTIONS
1188 __state |= ios_base::badbit;
1189 this->__setstate_nothrow(__state);
1190 if (this->exceptions() & ios_base::badbit)
1195 #endif // _LIBCPP_NO_EXCEPTIONS
1199 __state |= ios_base::failbit;
1201 this->setstate(__state);
1205 template<class _CharT, class _Traits>
1207 basic_istream<_CharT, _Traits>::sync()
1209 ios_base::iostate __state = ios_base::goodbit;
1211 sentry __sen(*this, true);
1214 #ifndef _LIBCPP_NO_EXCEPTIONS
1217 #endif // _LIBCPP_NO_EXCEPTIONS
1218 if (this->rdbuf() == 0)
1220 if (this->rdbuf()->pubsync() == -1)
1222 __state |= ios_base::badbit;
1225 #ifndef _LIBCPP_NO_EXCEPTIONS
1229 __state |= ios_base::badbit;
1230 this->__setstate_nothrow(__state);
1231 if (this->exceptions() & ios_base::badbit)
1236 #endif // _LIBCPP_NO_EXCEPTIONS
1237 this->setstate(__state);
1242 template<class _CharT, class _Traits>
1243 typename basic_istream<_CharT, _Traits>::pos_type
1244 basic_istream<_CharT, _Traits>::tellg()
1246 ios_base::iostate __state = ios_base::goodbit;
1248 sentry __sen(*this, true);
1251 #ifndef _LIBCPP_NO_EXCEPTIONS
1254 #endif // _LIBCPP_NO_EXCEPTIONS
1255 __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
1256 #ifndef _LIBCPP_NO_EXCEPTIONS
1260 __state |= ios_base::badbit;
1261 this->__setstate_nothrow(__state);
1262 if (this->exceptions() & ios_base::badbit)
1267 #endif // _LIBCPP_NO_EXCEPTIONS
1268 this->setstate(__state);
1273 template<class _CharT, class _Traits>
1274 basic_istream<_CharT, _Traits>&
1275 basic_istream<_CharT, _Traits>::seekg(pos_type __pos)
1277 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1278 this->clear(__state);
1279 sentry __sen(*this, true);
1282 #ifndef _LIBCPP_NO_EXCEPTIONS
1285 #endif // _LIBCPP_NO_EXCEPTIONS
1286 if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
1287 __state |= ios_base::failbit;
1288 #ifndef _LIBCPP_NO_EXCEPTIONS
1292 __state |= ios_base::badbit;
1293 this->__setstate_nothrow(__state);
1294 if (this->exceptions() & ios_base::badbit)
1299 #endif // _LIBCPP_NO_EXCEPTIONS
1300 this->setstate(__state);
1305 template<class _CharT, class _Traits>
1306 basic_istream<_CharT, _Traits>&
1307 basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir)
1309 ios_base::iostate __state = this->rdstate() & ~ios_base::eofbit;
1310 this->clear(__state);
1311 sentry __sen(*this, true);
1314 #ifndef _LIBCPP_NO_EXCEPTIONS
1317 #endif // _LIBCPP_NO_EXCEPTIONS
1318 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::in) == pos_type(-1))
1319 __state |= ios_base::failbit;
1320 #ifndef _LIBCPP_NO_EXCEPTIONS
1324 __state |= ios_base::badbit;
1325 this->__setstate_nothrow(__state);
1326 if (this->exceptions() & ios_base::badbit)
1331 #endif // _LIBCPP_NO_EXCEPTIONS
1332 this->setstate(__state);
1337 template <class _CharT, class _Traits>
1338 basic_istream<_CharT, _Traits>&
1339 ws(basic_istream<_CharT, _Traits>& __is)
1341 ios_base::iostate __state = ios_base::goodbit;
1342 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1345 #ifndef _LIBCPP_NO_EXCEPTIONS
1348 #endif // _LIBCPP_NO_EXCEPTIONS
1349 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
1352 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1353 if (_Traits::eq_int_type(__i, _Traits::eof()))
1355 __state |= ios_base::eofbit;
1358 if (!__ct.is(__ct.space, _Traits::to_char_type(__i)))
1360 __is.rdbuf()->sbumpc();
1362 #ifndef _LIBCPP_NO_EXCEPTIONS
1366 __state |= ios_base::badbit;
1367 __is.__setstate_nothrow(__state);
1368 if (__is.exceptions() & ios_base::badbit)
1373 #endif // _LIBCPP_NO_EXCEPTIONS
1374 __is.setstate(__state);
1379 #ifndef _LIBCPP_CXX03_LANG
1381 template <class _CharT, class _Traits, class _Tp>
1382 inline _LIBCPP_INLINE_VISIBILITY
1383 basic_istream<_CharT, _Traits>&
1384 operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp&& __x)
1386 __is >> _VSTD::forward<_Tp>(__x);
1390 #endif // _LIBCPP_CXX03_LANG
1392 template <class _CharT, class _Traits>
1393 class _LIBCPP_TEMPLATE_VIS basic_iostream
1394 : public basic_istream<_CharT, _Traits>,
1395 public basic_ostream<_CharT, _Traits>
1399 typedef _CharT char_type;
1400 typedef _Traits traits_type;
1401 typedef typename traits_type::int_type int_type;
1402 typedef typename traits_type::pos_type pos_type;
1403 typedef typename traits_type::off_type off_type;
1405 // constructor/destructor
1406 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
1407 explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb)
1408 : basic_istream<_CharT, _Traits>(__sb)
1411 virtual ~basic_iostream();
1413 #ifndef _LIBCPP_CXX03_LANG
1414 inline _LIBCPP_INLINE_VISIBILITY
1415 basic_iostream(basic_iostream&& __rhs);
1418 inline _LIBCPP_INLINE_VISIBILITY
1419 basic_iostream& operator=(basic_iostream&& __rhs);
1421 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
1422 void swap(basic_iostream& __rhs)
1423 { basic_istream<char_type, traits_type>::swap(__rhs); }
1426 #ifndef _LIBCPP_CXX03_LANG
1428 template <class _CharT, class _Traits>
1429 basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
1430 : basic_istream<_CharT, _Traits>(_VSTD::move(__rhs))
1434 template <class _CharT, class _Traits>
1435 basic_iostream<_CharT, _Traits>&
1436 basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs)
1442 #endif // _LIBCPP_CXX03_LANG
1444 template <class _CharT, class _Traits>
1445 basic_iostream<_CharT, _Traits>::~basic_iostream()
1449 template<class _CharT, class _Traits, class _Allocator>
1450 basic_istream<_CharT, _Traits>&
1451 operator>>(basic_istream<_CharT, _Traits>& __is,
1452 basic_string<_CharT, _Traits, _Allocator>& __str)
1454 ios_base::iostate __state = ios_base::goodbit;
1455 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1458 #ifndef _LIBCPP_NO_EXCEPTIONS
1463 streamsize __n = __is.width();
1465 __n = __str.max_size();
1467 __n = numeric_limits<streamsize>::max();
1469 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
1472 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1473 if (_Traits::eq_int_type(__i, _Traits::eof()))
1475 __state |= ios_base::eofbit;
1478 _CharT __ch = _Traits::to_char_type(__i);
1479 if (__ct.is(__ct.space, __ch))
1481 __str.push_back(__ch);
1483 __is.rdbuf()->sbumpc();
1487 __state |= ios_base::failbit;
1488 #ifndef _LIBCPP_NO_EXCEPTIONS
1492 __state |= ios_base::badbit;
1493 __is.__setstate_nothrow(__state);
1494 if (__is.exceptions() & ios_base::badbit)
1500 __is.setstate(__state);
1505 template<class _CharT, class _Traits, class _Allocator>
1506 basic_istream<_CharT, _Traits>&
1507 getline(basic_istream<_CharT, _Traits>& __is,
1508 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
1510 ios_base::iostate __state = ios_base::goodbit;
1511 typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1514 #ifndef _LIBCPP_NO_EXCEPTIONS
1519 streamsize __extr = 0;
1522 typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
1523 if (_Traits::eq_int_type(__i, _Traits::eof()))
1525 __state |= ios_base::eofbit;
1529 _CharT __ch = _Traits::to_char_type(__i);
1530 if (_Traits::eq(__ch, __dlm))
1532 __str.push_back(__ch);
1533 if (__str.size() == __str.max_size())
1535 __state |= ios_base::failbit;
1540 __state |= ios_base::failbit;
1541 #ifndef _LIBCPP_NO_EXCEPTIONS
1545 __state |= ios_base::badbit;
1546 __is.__setstate_nothrow(__state);
1547 if (__is.exceptions() & ios_base::badbit)
1553 __is.setstate(__state);
1558 template<class _CharT, class _Traits, class _Allocator>
1559 inline _LIBCPP_INLINE_VISIBILITY
1560 basic_istream<_CharT, _Traits>&
1561 getline(basic_istream<_CharT, _Traits>& __is,
1562 basic_string<_CharT, _Traits, _Allocator>& __str)
1564 return getline(__is, __str, __is.widen('\n'));
1567 #ifndef _LIBCPP_CXX03_LANG
1569 template<class _CharT, class _Traits, class _Allocator>
1570 inline _LIBCPP_INLINE_VISIBILITY
1571 basic_istream<_CharT, _Traits>&
1572 getline(basic_istream<_CharT, _Traits>&& __is,
1573 basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
1575 return getline(__is, __str, __dlm);
1578 template<class _CharT, class _Traits, class _Allocator>
1579 inline _LIBCPP_INLINE_VISIBILITY
1580 basic_istream<_CharT, _Traits>&
1581 getline(basic_istream<_CharT, _Traits>&& __is,
1582 basic_string<_CharT, _Traits, _Allocator>& __str)
1584 return getline(__is, __str, __is.widen('\n'));
1587 #endif // _LIBCPP_CXX03_LANG
1589 template <class _CharT, class _Traits, size_t _Size>
1590 basic_istream<_CharT, _Traits>&
1591 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
1593 ios_base::iostate __state = ios_base::goodbit;
1594 typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1597 #ifndef _LIBCPP_NO_EXCEPTIONS
1601 basic_string<_CharT, _Traits> __str;
1602 const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
1604 _CharT __zero = __ct.widen('0');
1605 _CharT __one = __ct.widen('1');
1608 typename _Traits::int_type __i = __is.rdbuf()->sgetc();
1609 if (_Traits::eq_int_type(__i, _Traits::eof()))
1611 __state |= ios_base::eofbit;
1614 _CharT __ch = _Traits::to_char_type(__i);
1615 if (!_Traits::eq(__ch, __zero) && !_Traits::eq(__ch, __one))
1617 __str.push_back(__ch);
1619 __is.rdbuf()->sbumpc();
1621 __x = bitset<_Size>(__str);
1623 __state |= ios_base::failbit;
1624 #ifndef _LIBCPP_NO_EXCEPTIONS
1628 __state |= ios_base::badbit;
1629 __is.__setstate_nothrow(__state);
1630 if (__is.exceptions() & ios_base::badbit)
1636 __is.setstate(__state);
1641 #ifndef _LIBCPP_DO_NOT_ASSUME_STREAMS_EXPLICIT_INSTANTIATION_IN_DYLIB
1642 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<char>)
1643 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_istream<wchar_t>)
1644 _LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_iostream<char>)
1647 _LIBCPP_END_NAMESPACE_STD
1651 #endif // _LIBCPP_ISTREAM