Fixed two problems found by Chris Jefferson: Made operator>> for char consistent...
[lldb.git] / libcxx / include / istream
1 // -*- C++ -*-
2 //===--------------------------- istream ----------------------------------===//
3 //
4 //                     The LLVM Compiler Infrastructure
5 //
6 // This file is dual licensed under the MIT and the University of Illinois Open
7 // Source Licenses. See LICENSE.TXT for details.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef _LIBCPP_ISTREAM
12 #define _LIBCPP_ISTREAM
13
14 /*
15     istream synopsis
16
17 template <class charT, class traits = char_traits<charT> >
18 class basic_istream
19     : virtual public basic_ios<charT,traits>
20 {
21 public:
22     // types (inherited from basic_ios (27.5.4)):
23     typedef charT                          char_type;
24     typedef traits                         traits_type;
25     typedef typename traits_type::int_type int_type;
26     typedef typename traits_type::pos_type pos_type;
27     typedef typename traits_type::off_type off_type;
28
29     // 27.7.1.1.1 Constructor/destructor:
30     explicit basic_istream(basic_streambuf<char_type, traits_type>* sb);
31     basic_istream(basic_istream&& rhs);
32     virtual ~basic_istream();
33
34     // 27.7.1.1.2 Assign/swap:
35     basic_istream& operator=(basic_istream&& rhs);
36     void swap(basic_istream& rhs);
37
38     // 27.7.1.1.3 Prefix/suffix:
39     class sentry;
40
41     // 27.7.1.2 Formatted input:
42     basic_istream& operator>>(basic_istream& (*pf)(basic_istream&));
43     basic_istream& operator>>(basic_ios<char_type, traits_type>&
44                               (*pf)(basic_ios<char_type, traits_type>&));
45     basic_istream& operator>>(ios_base& (*pf)(ios_base&));
46     basic_istream& operator>>(basic_streambuf<char_type, traits_type>* sb);
47     basic_istream& operator>>(bool& n);
48     basic_istream& operator>>(short& n);
49     basic_istream& operator>>(unsigned short& n);
50     basic_istream& operator>>(int& n);
51     basic_istream& operator>>(unsigned int& n);
52     basic_istream& operator>>(long& n);
53     basic_istream& operator>>(unsigned long& n);
54     basic_istream& operator>>(long long& n);
55     basic_istream& operator>>(unsigned long long& n);
56     basic_istream& operator>>(float& f);
57     basic_istream& operator>>(double& f);
58     basic_istream& operator>>(long double& f);
59     basic_istream& operator>>(void*& p);
60
61     // 27.7.1.3 Unformatted input:
62     streamsize gcount() const;
63     int_type get();
64     basic_istream& get(char_type& c);
65     basic_istream& get(char_type* s, streamsize n);
66     basic_istream& get(char_type* s, streamsize n, char_type delim);
67     basic_istream& get(basic_streambuf<char_type,traits_type>& sb);
68     basic_istream& get(basic_streambuf<char_type,traits_type>& sb, char_type delim);
69
70     basic_istream& getline(char_type* s, streamsize n);
71     basic_istream& getline(char_type* s, streamsize n, char_type delim);
72
73     basic_istream& ignore(streamsize n = 1, int_type delim = traits_type::eof());
74     int_type peek();
75     basic_istream& read (char_type* s, streamsize n);
76     streamsize readsome(char_type* s, streamsize n);
77
78     basic_istream& putback(char_type c);
79     basic_istream& unget();
80     int sync();
81
82     pos_type tellg();
83     basic_istream& seekg(pos_type);
84     basic_istream& seekg(off_type, ios_base::seekdir);
85 };
86
87 // 27.7.1.2.3 character extraction templates:
88 template<class charT, class traits>
89   basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&);
90
91 template<class traits>
92   basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char&);
93
94 template<class traits>
95   basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char&);
96
97 template<class charT, class traits>
98   basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT*);
99
100 template<class traits>
101   basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char*);
102
103 template<class traits>
104   basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char*);
105
106 template <class charT, class traits>
107   void
108   swap(basic_istream<charT, traits>& x, basic_istream<charT, traits>& y);
109
110 typedef basic_istream<char> istream;
111 typedef basic_istream<wchar_t> wistream;
112
113 template <class charT, class traits = char_traits<charT> >
114 class basic_iostream :
115     public basic_istream<charT,traits>,
116     public basic_ostream<charT,traits>
117 {
118 public:
119     // types:
120     typedef charT                          char_type;
121     typedef traits                         traits_type;
122     typedef typename traits_type::int_type int_type;
123     typedef typename traits_type::pos_type pos_type;
124     typedef typename traits_type::off_type off_type;
125
126     // constructor/destructor
127     explicit basic_iostream(basic_streambuf<char_type, traits_type>* sb);
128     basic_iostream(basic_iostream&& rhs);
129     virtual ~basic_iostream();
130
131     // assign/swap
132     basic_iostream& operator=(basic_iostream&& rhs);
133     void swap(basic_iostream& rhs);
134 };
135
136 template <class charT, class traits>
137   void
138   swap(basic_iostream<charT, traits>& x, basic_iostream<charT, traits>& y);
139
140 typedef basic_iostream<char> iostream;
141 typedef basic_iostream<wchar_t> wiostream;
142
143 template <class charT, class traits>
144   basic_istream<charT,traits>&
145   ws(basic_istream<charT,traits>& is);
146
147 template <class charT, class traits, class T>
148   basic_istream<charT, traits>&
149   operator>>(basic_istream<charT, traits>&& is, T& x);
150
151 }  // std
152
153 */
154
155 #include <__config>
156 #include <ostream>
157
158 #pragma GCC system_header
159
160 _LIBCPP_BEGIN_NAMESPACE_STD
161
162 template <class _CharT, class _Traits>
163 class _LIBCPP_VISIBLE basic_istream
164     : virtual public basic_ios<_CharT, _Traits>
165 {
166     streamsize __gc_;
167 public:
168     // types (inherited from basic_ios (27.5.4)):
169     typedef _CharT                         char_type;
170     typedef _Traits                        traits_type;
171     typedef typename traits_type::int_type int_type;
172     typedef typename traits_type::pos_type pos_type;
173     typedef typename traits_type::off_type off_type;
174
175     // 27.7.1.1.1 Constructor/destructor:
176     explicit basic_istream(basic_streambuf<char_type, traits_type>* __sb);
177     virtual ~basic_istream();
178 protected:
179 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
180     basic_istream(basic_istream&& __rhs);
181 #endif
182
183     // 27.7.1.1.2 Assign/swap:
184 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
185     basic_istream& operator=(basic_istream&& __rhs);
186 #endif
187     void swap(basic_istream& __rhs);
188 public:
189
190     // 27.7.1.1.3 Prefix/suffix:
191     class sentry;
192
193     // 27.7.1.2 Formatted input:
194     basic_istream& operator>>(basic_istream& (*__pf)(basic_istream&));
195     basic_istream& operator>>(basic_ios<char_type, traits_type>&
196                               (*__pf)(basic_ios<char_type, traits_type>&));
197     basic_istream& operator>>(ios_base& (*__pf)(ios_base&));
198     basic_istream& operator>>(basic_streambuf<char_type, traits_type>* __sb);
199     basic_istream& operator>>(bool& __n);
200     basic_istream& operator>>(short& __n);
201     basic_istream& operator>>(unsigned short& __n);
202     basic_istream& operator>>(int& __n);
203     basic_istream& operator>>(unsigned int& __n);
204     basic_istream& operator>>(long& __n);
205     basic_istream& operator>>(unsigned long& __n);
206     basic_istream& operator>>(long long& __n);
207     basic_istream& operator>>(unsigned long long& __n);
208     basic_istream& operator>>(float& __f);
209     basic_istream& operator>>(double& __f);
210     basic_istream& operator>>(long double& __f);
211     basic_istream& operator>>(void*& __p);
212
213     // 27.7.1.3 Unformatted input:
214     _LIBCPP_INLINE_VISIBILITY
215     streamsize gcount() const {return __gc_;}
216     int_type get();
217     basic_istream& get(char_type& __c);
218     basic_istream& get(char_type* __s, streamsize __n);
219     basic_istream& get(char_type* __s, streamsize __n, char_type __dlm);
220     basic_istream& get(basic_streambuf<char_type, traits_type>& __sb);
221     basic_istream& get(basic_streambuf<char_type, traits_type>& __sb, char_type __dlm);
222
223     basic_istream& getline(char_type* __s, streamsize __n);
224     basic_istream& getline(char_type* __s, streamsize __n, char_type __dlm);
225
226     basic_istream& ignore(streamsize __n = 1, int_type __dlm = traits_type::eof());
227     int_type peek();
228     basic_istream& read (char_type* __s, streamsize __n);
229     streamsize readsome(char_type* __s, streamsize __n);
230
231     basic_istream& putback(char_type __c);
232     basic_istream& unget();
233     int sync();
234
235     pos_type tellg();
236     basic_istream& seekg(pos_type __pos);
237     basic_istream& seekg(off_type __off, ios_base::seekdir __dir);
238 };
239
240 template <class _CharT, class _Traits>
241 class _LIBCPP_VISIBLE basic_istream<_CharT, _Traits>::sentry
242 {
243     bool __ok_;
244
245     sentry(const sentry&); // = delete;
246     sentry& operator=(const sentry&); // = delete;
247
248 public:
249     explicit sentry(basic_istream<_CharT, _Traits>& __is, bool __noskipws = false);
250 //    ~sentry() = default;
251
252     _LIBCPP_INLINE_VISIBILITY
253     // explicit
254         operator bool() const {return __ok_;}
255 };
256
257 template <class _CharT, class _Traits>
258 basic_istream<_CharT, _Traits>::sentry::sentry(basic_istream<_CharT, _Traits>& __is,
259                                                bool __noskipws)
260     : __ok_(false)
261 {
262     if (__is.good())
263     {
264         if (__is.tie())
265             __is.tie()->flush();
266         if (!__noskipws && (__is.flags() & ios_base::skipws))
267         {
268             typedef istreambuf_iterator<_CharT, _Traits> _I;
269             const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
270             _I __i(__is);
271             _I __eof;
272             for (; __i != __eof; ++__i)
273                 if (!__ct.is(__ct.space, *__i))
274                     break;
275             if (__i == __eof)
276                 __is.setstate(ios_base::failbit | ios_base::eofbit);
277         }
278         __ok_ = __is.good();
279     }
280     else
281         __is.setstate(ios_base::failbit);
282 }
283
284 template <class _CharT, class _Traits>
285 inline _LIBCPP_INLINE_VISIBILITY
286 basic_istream<_CharT, _Traits>::basic_istream(basic_streambuf<char_type, traits_type>* __sb)
287     : __gc_(0)
288 {
289     this->init(__sb);
290 }
291
292 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
293
294 template <class _CharT, class _Traits>
295 inline _LIBCPP_INLINE_VISIBILITY
296 basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs)
297     : __gc_(__rhs.__gc_)
298 {
299     __rhs.__gc_ = 0;
300     this->move(__rhs);
301 }
302
303 template <class _CharT, class _Traits>
304 inline _LIBCPP_INLINE_VISIBILITY
305 basic_istream<_CharT, _Traits>&
306 basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs)
307 {
308     swap(__rhs);
309     return *this;
310 }
311
312 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
313
314 template <class _CharT, class _Traits>
315 basic_istream<_CharT, _Traits>::~basic_istream()
316 {
317 }
318
319 template <class _CharT, class _Traits>
320 inline _LIBCPP_INLINE_VISIBILITY
321 void
322 basic_istream<_CharT, _Traits>::swap(basic_istream& __rhs)
323 {
324     _STD::swap(__gc_, __rhs.__gc_);
325     basic_ios<char_type, traits_type>::swap(__rhs);
326 }
327
328 template <class _CharT, class _Traits>
329 basic_istream<_CharT, _Traits>&
330 basic_istream<_CharT, _Traits>::operator>>(unsigned short& __n)
331 {
332 #ifndef _LIBCPP_NO_EXCEPTIONS
333     try
334     {
335 #endif  // _LIBCPP_NO_EXCEPTIONS
336         sentry __s(*this);
337         if (__s)
338         {
339             typedef istreambuf_iterator<char_type, traits_type> _I;
340             typedef num_get<char_type, _I> _F;
341             ios_base::iostate __err = ios_base::goodbit;
342             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
343             this->setstate(__err);
344         }
345 #ifndef _LIBCPP_NO_EXCEPTIONS
346     }
347     catch (...)
348     {
349         this->__set_badbit_and_consider_rethrow();
350     }
351 #endif  // _LIBCPP_NO_EXCEPTIONS
352     return *this;
353 }
354
355 template <class _CharT, class _Traits>
356 basic_istream<_CharT, _Traits>&
357 basic_istream<_CharT, _Traits>::operator>>(unsigned int& __n)
358 {
359 #ifndef _LIBCPP_NO_EXCEPTIONS
360     try
361     {
362 #endif  // _LIBCPP_NO_EXCEPTIONS
363         sentry __s(*this);
364         if (__s)
365         {
366             typedef istreambuf_iterator<char_type, traits_type> _I;
367             typedef num_get<char_type, _I> _F;
368             ios_base::iostate __err = ios_base::goodbit;
369             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
370             this->setstate(__err);
371         }
372 #ifndef _LIBCPP_NO_EXCEPTIONS
373     }
374     catch (...)
375     {
376         this->__set_badbit_and_consider_rethrow();
377     }
378 #endif  // _LIBCPP_NO_EXCEPTIONS
379     return *this;
380 }
381
382 template <class _CharT, class _Traits>
383 basic_istream<_CharT, _Traits>&
384 basic_istream<_CharT, _Traits>::operator>>(long& __n)
385 {
386 #ifndef _LIBCPP_NO_EXCEPTIONS
387     try
388     {
389 #endif  // _LIBCPP_NO_EXCEPTIONS
390         sentry __s(*this);
391         if (__s)
392         {
393             typedef istreambuf_iterator<char_type, traits_type> _I;
394             typedef num_get<char_type, _I> _F;
395             ios_base::iostate __err = ios_base::goodbit;
396             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
397             this->setstate(__err);
398         }
399 #ifndef _LIBCPP_NO_EXCEPTIONS
400     }
401     catch (...)
402     {
403         this->__set_badbit_and_consider_rethrow();
404     }
405 #endif  // _LIBCPP_NO_EXCEPTIONS
406     return *this;
407 }
408
409 template <class _CharT, class _Traits>
410 basic_istream<_CharT, _Traits>&
411 basic_istream<_CharT, _Traits>::operator>>(unsigned long& __n)
412 {
413 #ifndef _LIBCPP_NO_EXCEPTIONS
414     try
415     {
416 #endif  // _LIBCPP_NO_EXCEPTIONS
417         sentry __s(*this);
418         if (__s)
419         {
420             typedef istreambuf_iterator<char_type, traits_type> _I;
421             typedef num_get<char_type, _I> _F;
422             ios_base::iostate __err = ios_base::goodbit;
423             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
424             this->setstate(__err);
425         }
426 #ifndef _LIBCPP_NO_EXCEPTIONS
427     }
428     catch (...)
429     {
430         this->__set_badbit_and_consider_rethrow();
431     }
432 #endif  // _LIBCPP_NO_EXCEPTIONS
433     return *this;
434 }
435
436 template <class _CharT, class _Traits>
437 basic_istream<_CharT, _Traits>&
438 basic_istream<_CharT, _Traits>::operator>>(long long& __n)
439 {
440 #ifndef _LIBCPP_NO_EXCEPTIONS
441     try
442     {
443 #endif  // _LIBCPP_NO_EXCEPTIONS
444         sentry __s(*this);
445         if (__s)
446         {
447             typedef istreambuf_iterator<char_type, traits_type> _I;
448             typedef num_get<char_type, _I> _F;
449             ios_base::iostate __err = ios_base::goodbit;
450             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
451             this->setstate(__err);
452         }
453 #ifndef _LIBCPP_NO_EXCEPTIONS
454     }
455     catch (...)
456     {
457         this->__set_badbit_and_consider_rethrow();
458     }
459 #endif  // _LIBCPP_NO_EXCEPTIONS
460     return *this;
461 }
462
463 template <class _CharT, class _Traits>
464 basic_istream<_CharT, _Traits>&
465 basic_istream<_CharT, _Traits>::operator>>(unsigned long long& __n)
466 {
467 #ifndef _LIBCPP_NO_EXCEPTIONS
468     try
469     {
470 #endif  // _LIBCPP_NO_EXCEPTIONS
471         sentry __s(*this);
472         if (__s)
473         {
474             typedef istreambuf_iterator<char_type, traits_type> _I;
475             typedef num_get<char_type, _I> _F;
476             ios_base::iostate __err = ios_base::goodbit;
477             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
478             this->setstate(__err);
479         }
480 #ifndef _LIBCPP_NO_EXCEPTIONS
481     }
482     catch (...)
483     {
484         this->__set_badbit_and_consider_rethrow();
485     }
486 #endif  // _LIBCPP_NO_EXCEPTIONS
487     return *this;
488 }
489
490 template <class _CharT, class _Traits>
491 basic_istream<_CharT, _Traits>&
492 basic_istream<_CharT, _Traits>::operator>>(float& __n)
493 {
494 #ifndef _LIBCPP_NO_EXCEPTIONS
495     try
496     {
497 #endif  // _LIBCPP_NO_EXCEPTIONS
498         sentry __s(*this);
499         if (__s)
500         {
501             typedef istreambuf_iterator<char_type, traits_type> _I;
502             typedef num_get<char_type, _I> _F;
503             ios_base::iostate __err = ios_base::goodbit;
504             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
505             this->setstate(__err);
506         }
507 #ifndef _LIBCPP_NO_EXCEPTIONS
508     }
509     catch (...)
510     {
511         this->__set_badbit_and_consider_rethrow();
512     }
513 #endif  // _LIBCPP_NO_EXCEPTIONS
514     return *this;
515 }
516
517 template <class _CharT, class _Traits>
518 basic_istream<_CharT, _Traits>&
519 basic_istream<_CharT, _Traits>::operator>>(double& __n)
520 {
521 #ifndef _LIBCPP_NO_EXCEPTIONS
522     try
523     {
524 #endif  // _LIBCPP_NO_EXCEPTIONS
525         sentry __s(*this);
526         if (__s)
527         {
528             typedef istreambuf_iterator<char_type, traits_type> _I;
529             typedef num_get<char_type, _I> _F;
530             ios_base::iostate __err = ios_base::goodbit;
531             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
532             this->setstate(__err);
533         }
534 #ifndef _LIBCPP_NO_EXCEPTIONS
535     }
536     catch (...)
537     {
538         this->__set_badbit_and_consider_rethrow();
539     }
540 #endif  // _LIBCPP_NO_EXCEPTIONS
541     return *this;
542 }
543
544 template <class _CharT, class _Traits>
545 basic_istream<_CharT, _Traits>&
546 basic_istream<_CharT, _Traits>::operator>>(long double& __n)
547 {
548 #ifndef _LIBCPP_NO_EXCEPTIONS
549     try
550     {
551 #endif  // _LIBCPP_NO_EXCEPTIONS
552         sentry __s(*this);
553         if (__s)
554         {
555             typedef istreambuf_iterator<char_type, traits_type> _I;
556             typedef num_get<char_type, _I> _F;
557             ios_base::iostate __err = ios_base::goodbit;
558             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
559             this->setstate(__err);
560         }
561 #ifndef _LIBCPP_NO_EXCEPTIONS
562     }
563     catch (...)
564     {
565         this->__set_badbit_and_consider_rethrow();
566     }
567 #endif  // _LIBCPP_NO_EXCEPTIONS
568     return *this;
569 }
570
571 template <class _CharT, class _Traits>
572 basic_istream<_CharT, _Traits>&
573 basic_istream<_CharT, _Traits>::operator>>(bool& __n)
574 {
575 #ifndef _LIBCPP_NO_EXCEPTIONS
576     try
577     {
578 #endif  // _LIBCPP_NO_EXCEPTIONS
579         sentry __s(*this);
580         if (__s)
581         {
582             typedef istreambuf_iterator<char_type, traits_type> _I;
583             typedef num_get<char_type, _I> _F;
584             ios_base::iostate __err = ios_base::goodbit;
585             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
586             this->setstate(__err);
587         }
588 #ifndef _LIBCPP_NO_EXCEPTIONS
589     }
590     catch (...)
591     {
592         this->__set_badbit_and_consider_rethrow();
593     }
594 #endif  // _LIBCPP_NO_EXCEPTIONS
595     return *this;
596 }
597
598 template <class _CharT, class _Traits>
599 basic_istream<_CharT, _Traits>&
600 basic_istream<_CharT, _Traits>::operator>>(void*& __n)
601 {
602 #ifndef _LIBCPP_NO_EXCEPTIONS
603     try
604     {
605 #endif  // _LIBCPP_NO_EXCEPTIONS
606         sentry __s(*this);
607         if (__s)
608         {
609             typedef istreambuf_iterator<char_type, traits_type> _I;
610             typedef num_get<char_type, _I> _F;
611             ios_base::iostate __err = ios_base::goodbit;
612             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __n);
613             this->setstate(__err);
614         }
615 #ifndef _LIBCPP_NO_EXCEPTIONS
616     }
617     catch (...)
618     {
619         this->__set_badbit_and_consider_rethrow();
620     }
621 #endif  // _LIBCPP_NO_EXCEPTIONS
622     return *this;
623 }
624
625 template <class _CharT, class _Traits>
626 basic_istream<_CharT, _Traits>&
627 basic_istream<_CharT, _Traits>::operator>>(short& __n)
628 {
629 #ifndef _LIBCPP_NO_EXCEPTIONS
630     try
631     {
632 #endif  // _LIBCPP_NO_EXCEPTIONS
633         sentry __s(*this);
634         if (__s)
635         {
636             typedef istreambuf_iterator<char_type, traits_type> _I;
637             typedef num_get<char_type, _I> _F;
638             ios_base::iostate __err = ios_base::goodbit;
639             long __temp;
640             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __temp);
641             if (__temp < numeric_limits<short>::min())
642             {
643                 __err |= ios_base::failbit;
644                 __n = numeric_limits<short>::min();
645             }
646             else if (__temp > numeric_limits<short>::max())
647             {
648                 __err |= ios_base::failbit;
649                 __n = numeric_limits<short>::max();
650             }
651             else
652                 __n = static_cast<short>(__temp);
653             this->setstate(__err);
654         }
655 #ifndef _LIBCPP_NO_EXCEPTIONS
656     }
657     catch (...)
658     {
659         this->__set_badbit_and_consider_rethrow();
660     }
661 #endif  // _LIBCPP_NO_EXCEPTIONS
662     return *this;
663 }
664
665 template <class _CharT, class _Traits>
666 basic_istream<_CharT, _Traits>&
667 basic_istream<_CharT, _Traits>::operator>>(int& __n)
668 {
669 #ifndef _LIBCPP_NO_EXCEPTIONS
670     try
671     {
672 #endif  // _LIBCPP_NO_EXCEPTIONS
673         sentry __s(*this);
674         if (__s)
675         {
676             typedef istreambuf_iterator<char_type, traits_type> _I;
677             typedef num_get<char_type, _I> _F;
678             ios_base::iostate __err = ios_base::goodbit;
679             long __temp;
680             use_facet<_F>(this->getloc()).get(_I(*this), _I(), *this, __err, __temp);
681             if (__temp < numeric_limits<int>::min())
682             {
683                 __err |= ios_base::failbit;
684                 __n = numeric_limits<int>::min();
685             }
686             else if (__temp > numeric_limits<int>::max())
687             {
688                 __err |= ios_base::failbit;
689                 __n = numeric_limits<int>::max();
690             }
691             else
692                 __n = static_cast<int>(__temp);
693             this->setstate(__err);
694         }
695 #ifndef _LIBCPP_NO_EXCEPTIONS
696     }
697     catch (...)
698     {
699         this->__set_badbit_and_consider_rethrow();
700     }
701 #endif  // _LIBCPP_NO_EXCEPTIONS
702     return *this;
703 }
704
705 template <class _CharT, class _Traits>
706 inline _LIBCPP_INLINE_VISIBILITY
707 basic_istream<_CharT, _Traits>&
708 basic_istream<_CharT, _Traits>::operator>>(basic_istream& (*__pf)(basic_istream&))
709 {
710     return __pf(*this);
711 }
712
713 template <class _CharT, class _Traits>
714 inline _LIBCPP_INLINE_VISIBILITY
715 basic_istream<_CharT, _Traits>&
716 basic_istream<_CharT, _Traits>::operator>>(basic_ios<char_type, traits_type>&
717                                            (*__pf)(basic_ios<char_type, traits_type>&))
718 {
719     __pf(*this);
720     return *this;
721 }
722
723 template <class _CharT, class _Traits>
724 inline _LIBCPP_INLINE_VISIBILITY
725 basic_istream<_CharT, _Traits>&
726 basic_istream<_CharT, _Traits>::operator>>(ios_base& (*__pf)(ios_base&))
727 {
728     __pf(*this);
729     return *this;
730 }
731
732 template<class _CharT, class _Traits>
733 basic_istream<_CharT, _Traits>&
734 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT* __s)
735 {
736 #ifndef _LIBCPP_NO_EXCEPTIONS
737     try
738     {
739 #endif  // _LIBCPP_NO_EXCEPTIONS
740         typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
741         if (__sen)
742         {
743             typedef istreambuf_iterator<_CharT, _Traits> _I;
744             streamsize __n = __is.width();
745             if (__n == 0)
746                 __n = numeric_limits<streamsize>::max() / sizeof(_CharT) - 1;
747             streamsize __c = 0;
748             const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
749             _I __i(__is);
750             _I __eof;
751             for (; __i != __eof && __c < __n-1; ++__i, ++__s, ++__c)
752             {
753                 _CharT __ch = *__i;
754                 if (__ct.is(__ct.space, __ch))
755                     break;
756                 *__s = __ch;
757             }
758             *__s = _CharT();
759             __is.width(0);
760             ios_base::iostate __err = ios_base::goodbit;
761             if (__i == __eof)
762                __err |= ios_base::eofbit;
763             if (__c == 0)
764                __err |= ios_base::failbit;
765             __is.setstate(__err);
766         }
767 #ifndef _LIBCPP_NO_EXCEPTIONS
768     }
769     catch (...)
770     {
771         __is.__set_badbit_and_consider_rethrow();
772     }
773 #endif  // _LIBCPP_NO_EXCEPTIONS
774     return __is;
775 }
776
777 template<class _Traits>
778 inline _LIBCPP_INLINE_VISIBILITY
779 basic_istream<char, _Traits>&
780 operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
781 {
782     return __is >> (char*)__s;
783 }
784
785 template<class _Traits>
786 inline _LIBCPP_INLINE_VISIBILITY
787 basic_istream<char, _Traits>&
788 operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
789 {
790     return __is >> (char*)__s;
791 }
792
793 template<class _CharT, class _Traits>
794 basic_istream<_CharT, _Traits>&
795 operator>>(basic_istream<_CharT, _Traits>& __is, _CharT& __c)
796 {
797 #ifndef _LIBCPP_NO_EXCEPTIONS
798     try
799     {
800 #endif  // _LIBCPP_NO_EXCEPTIONS
801         typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
802         if (__sen)
803         {
804 #if 1
805             typename _Traits::int_type __i = __is.rdbuf()->sbumpc();
806             if (_Traits::eq_int_type(__i, _Traits::eof()))
807                 __is.setstate(ios_base::eofbit | ios_base::failbit);
808             else
809                 __c = _Traits::to_char_type(__i);
810 #else
811             typedef istreambuf_iterator<_CharT, _Traits> _I;
812             _I __i(__is);
813             _I __eof;
814             if (__i != __eof)
815             {
816                 __c = *__i;
817                 if (++__i == __eof)
818                     __is.setstate(ios_base::eofbit);
819             }
820             else
821                 __is.setstate(ios_base::eofbit | ios_base::failbit);
822 #endif
823         }
824 #ifndef _LIBCPP_NO_EXCEPTIONS
825     }
826     catch (...)
827     {
828         __is.__set_badbit_and_consider_rethrow();
829     }
830 #endif  // _LIBCPP_NO_EXCEPTIONS
831     return __is;
832 }
833
834 template<class _Traits>
835 inline _LIBCPP_INLINE_VISIBILITY
836 basic_istream<char, _Traits>&
837 operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
838 {
839     return __is >> (char&)__c;
840 }
841
842 template<class _Traits>
843 inline _LIBCPP_INLINE_VISIBILITY
844 basic_istream<char, _Traits>&
845 operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
846 {
847     return __is >> (char&)__c;
848 }
849
850 template<class _CharT, class _Traits>
851 basic_istream<_CharT, _Traits>&
852 basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb)
853 {
854     __gc_ = 0;
855 #ifndef _LIBCPP_NO_EXCEPTIONS
856     try
857     {
858 #endif  // _LIBCPP_NO_EXCEPTIONS
859         sentry __s(*this, true);
860         if (__s)
861         {
862             streamsize __c = 0;
863             if (__sb)
864             {
865 #ifndef _LIBCPP_NO_EXCEPTIONS
866                 try
867                 {
868 #endif  // _LIBCPP_NO_EXCEPTIONS
869                     typedef istreambuf_iterator<char_type, traits_type> _I;
870                     typedef ostreambuf_iterator<char_type, traits_type> _O;
871                     _I __i(*this);
872                     _I __eof;
873                     _O __o(__sb);
874                     for (; __i != __eof; ++__i, ++__o, ++__c)
875                     {
876                         *__o = *__i;
877                         if (__o.failed())
878                             break;
879                     }
880                     ios_base::iostate __err = ios_base::goodbit;
881                     if (__i == __eof)
882                        __err |= ios_base::eofbit;
883                     if (__c == 0)
884                        __err |= ios_base::failbit;
885                     this->setstate(__err);
886 #ifndef _LIBCPP_NO_EXCEPTIONS
887                 }
888                 catch (...)
889                 {
890                     if (__c == 0)
891                         this->__set_failbit_and_consider_rethrow();
892                 }
893 #endif  // _LIBCPP_NO_EXCEPTIONS
894             }
895             else
896                 this->setstate(ios_base::failbit);
897             __gc_ = __c;
898         }
899 #ifndef _LIBCPP_NO_EXCEPTIONS
900     }
901     catch (...)
902     {
903         this->__set_badbit_and_consider_rethrow();
904     }
905 #endif  // _LIBCPP_NO_EXCEPTIONS
906     return *this;
907 }
908
909 template<class _CharT, class _Traits>
910 typename basic_istream<_CharT, _Traits>::int_type
911 basic_istream<_CharT, _Traits>::get()
912 {
913     __gc_ = 0;
914     int_type __r = traits_type::eof();
915 #ifndef _LIBCPP_NO_EXCEPTIONS
916     try
917     {
918 #endif  // _LIBCPP_NO_EXCEPTIONS
919         sentry __s(*this, true);
920         if (__s)
921         {
922             streamsize __c = 0;
923             typedef istreambuf_iterator<char_type, traits_type> _I;
924             _I __i(*this);
925             _I __eof;
926             ios_base::iostate __err = ios_base::goodbit;
927             if (__i != __eof)
928             {
929                 __r = traits_type::to_int_type(*__i);
930                 ++__c;
931                 if (++__i == __eof)
932                     __err |= ios_base::eofbit;
933             }
934             else
935                 __err |= ios_base::failbit | ios_base::eofbit;
936             this->setstate(__err);
937             __gc_ = __c;
938         }
939 #ifndef _LIBCPP_NO_EXCEPTIONS
940     }
941     catch (...)
942     {
943         this->__set_badbit_and_consider_rethrow();
944     }
945 #endif  // _LIBCPP_NO_EXCEPTIONS
946     return __r;
947 }
948
949 template<class _CharT, class _Traits>
950 inline _LIBCPP_INLINE_VISIBILITY
951 basic_istream<_CharT, _Traits>&
952 basic_istream<_CharT, _Traits>::get(char_type& __c)
953 {
954     int_type __ch = get();
955     if (__ch != traits_type::eof())
956         __c = traits_type::to_char_type(__ch);
957     return *this;
958 }
959
960 template<class _CharT, class _Traits>
961 basic_istream<_CharT, _Traits>&
962 basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm)
963 {
964     __gc_ = 0;
965 #ifndef _LIBCPP_NO_EXCEPTIONS
966     try
967     {
968 #endif  // _LIBCPP_NO_EXCEPTIONS
969         sentry __sen(*this, true);
970         if (__sen)
971         {
972             streamsize __c = 0;
973             if (__n > 0)
974             {
975                 typedef istreambuf_iterator<char_type, traits_type> _I;
976                 _I __i(*this);
977                 _I __eof;
978                 for (; __i != __eof && __n > 1; ++__i, ++__s, ++__c)
979                 {
980                     char_type __ch = *__i;
981                     if (traits_type::eq(__ch, __dlm))
982                         break;
983                     *__s = __ch;
984                 }
985                 *__s = char_type();
986                 ios_base::iostate __err = ios_base::goodbit;
987                 if (__i == __eof)
988                    __err |= ios_base::eofbit;
989                 if (__c == 0)
990                    __err |= ios_base::failbit;
991                 this->setstate(__err);
992             }
993             else
994                 this->setstate(ios_base::failbit);
995             __gc_ = __c;
996         }
997 #ifndef _LIBCPP_NO_EXCEPTIONS
998     }
999     catch (...)
1000     {
1001         this->__set_badbit_and_consider_rethrow();
1002     }
1003 #endif  // _LIBCPP_NO_EXCEPTIONS
1004     return *this;
1005 }
1006
1007 template<class _CharT, class _Traits>
1008 inline _LIBCPP_INLINE_VISIBILITY
1009 basic_istream<_CharT, _Traits>&
1010 basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n)
1011 {
1012     return get(__s, __n, this->widen('\n'));
1013 }
1014
1015 template<class _CharT, class _Traits>
1016 basic_istream<_CharT, _Traits>&
1017 basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb,
1018                                     char_type __dlm)
1019 {
1020     __gc_ = 0;
1021 #ifndef _LIBCPP_NO_EXCEPTIONS
1022     try
1023     {
1024 #endif  // _LIBCPP_NO_EXCEPTIONS
1025         sentry __sen(*this, true);
1026         if (__sen)
1027         {
1028             streamsize __c = 0;
1029             ios_base::iostate __err = ios_base::goodbit;
1030 #ifndef _LIBCPP_NO_EXCEPTIONS
1031             try
1032             {
1033 #endif  // _LIBCPP_NO_EXCEPTIONS
1034                 typedef istreambuf_iterator<char_type, traits_type> _I;
1035                 typedef ostreambuf_iterator<char_type, traits_type> _O;
1036                 _I __i(*this);
1037                 _I __eof;
1038                 _O __o(&__sb);
1039                 for (; __i != __eof; ++__i, ++__o, ++__c)
1040                 {
1041                     char_type __ch = *__i;
1042                     if (traits_type::eq(__ch, __dlm))
1043                         break;
1044                     *__o = __ch;
1045                     if (__o.failed())
1046                         break;
1047                 }
1048                 if (__i == __eof)
1049                    __err |= ios_base::eofbit;
1050 #ifndef _LIBCPP_NO_EXCEPTIONS
1051             }
1052             catch (...)
1053             {
1054             }
1055 #endif  // _LIBCPP_NO_EXCEPTIONS
1056             if (__c == 0)
1057                __err |= ios_base::failbit;
1058             this->setstate(__err);
1059             __gc_ = __c;
1060         }
1061 #ifndef _LIBCPP_NO_EXCEPTIONS
1062     }
1063     catch (...)
1064     {
1065         this->__set_badbit_and_consider_rethrow();
1066     }
1067 #endif  // _LIBCPP_NO_EXCEPTIONS
1068     return *this;
1069 }
1070
1071 template<class _CharT, class _Traits>
1072 inline _LIBCPP_INLINE_VISIBILITY
1073 basic_istream<_CharT, _Traits>&
1074 basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb)
1075 {
1076     return get(__sb, this->widen('\n'));
1077 }
1078
1079 template<class _CharT, class _Traits>
1080 basic_istream<_CharT, _Traits>&
1081 basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm)
1082 {
1083     __gc_ = 0;
1084 #ifndef _LIBCPP_NO_EXCEPTIONS
1085     try
1086     {
1087 #endif  // _LIBCPP_NO_EXCEPTIONS
1088         sentry __sen(*this, true);
1089         if (__sen)
1090         {
1091             streamsize __c = 0;
1092             typedef istreambuf_iterator<char_type, traits_type> _I;
1093             _I __i(*this);
1094             _I __eof;
1095             for (; __i != __eof; ++__s, --__n)
1096             {
1097                 char_type __ch = *__i;
1098                 ++__i;
1099                 ++__c;
1100                 if (traits_type::eq(__ch, __dlm))
1101                     break;
1102                 if (__n < 2)
1103                 {
1104                     this->setstate(ios_base::failbit);
1105                     break;
1106                 }
1107                 *__s = __ch;
1108             }
1109             if (__n)
1110                 *__s = char_type();
1111             ios_base::iostate __err = ios_base::goodbit;
1112             if (__i == __eof)
1113                __err |= ios_base::eofbit;
1114             if (__c == 0)
1115                __err |= ios_base::failbit;
1116             this->setstate(__err);
1117             __gc_ = __c;
1118         }
1119 #ifndef _LIBCPP_NO_EXCEPTIONS
1120     }
1121     catch (...)
1122     {
1123         this->__set_badbit_and_consider_rethrow();
1124     }
1125 #endif  // _LIBCPP_NO_EXCEPTIONS
1126     return *this;
1127 }
1128
1129 template<class _CharT, class _Traits>
1130 inline _LIBCPP_INLINE_VISIBILITY
1131 basic_istream<_CharT, _Traits>&
1132 basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n)
1133 {
1134     return getline(__s, __n, this->widen('\n'));
1135 }
1136
1137 template<class _CharT, class _Traits>
1138 basic_istream<_CharT, _Traits>&
1139 basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm)
1140 {
1141     __gc_ = 0;
1142 #ifndef _LIBCPP_NO_EXCEPTIONS
1143     try
1144     {
1145 #endif  // _LIBCPP_NO_EXCEPTIONS
1146         sentry __sen(*this, true);
1147         if (__sen)
1148         {
1149             streamsize __c = 0;
1150             typedef istreambuf_iterator<char_type, traits_type> _I;
1151             _I __i(*this);
1152             _I __eof;
1153             if (__n != numeric_limits<streamsize>::max())
1154             {
1155                 for (; __n > 0 && __i != __eof; --__n)
1156                 {
1157                     char_type __ch = *__i;
1158                     ++__i;
1159                     ++__c;
1160                     if (traits_type::eq(__ch, __dlm))
1161                         break;
1162                 }
1163             }
1164             else
1165             {
1166                 while (__i != __eof)
1167                 {
1168                     char_type __ch = *__i;
1169                     ++__i;
1170                     ++__c;
1171                     if (traits_type::eq(__ch, __dlm))
1172                         break;
1173                 }
1174             }
1175             if (__i == __eof)
1176                 this->setstate(ios_base::eofbit);
1177             __gc_ = __c;
1178         }
1179 #ifndef _LIBCPP_NO_EXCEPTIONS
1180     }
1181     catch (...)
1182     {
1183         this->__set_badbit_and_consider_rethrow();
1184     }
1185 #endif  // _LIBCPP_NO_EXCEPTIONS
1186     return *this;
1187 }
1188
1189 template<class _CharT, class _Traits>
1190 typename basic_istream<_CharT, _Traits>::int_type
1191 basic_istream<_CharT, _Traits>::peek()
1192 {
1193     __gc_ = 0;
1194     int_type __r = traits_type::eof();
1195 #ifndef _LIBCPP_NO_EXCEPTIONS
1196     try
1197     {
1198 #endif  // _LIBCPP_NO_EXCEPTIONS
1199         sentry __sen(*this, true);
1200         if (__sen)
1201             __r = this->rdbuf()->sgetc();
1202 #ifndef _LIBCPP_NO_EXCEPTIONS
1203     }
1204     catch (...)
1205     {
1206         this->__set_badbit_and_consider_rethrow();
1207     }
1208 #endif  // _LIBCPP_NO_EXCEPTIONS
1209     return __r;
1210 }
1211
1212 template<class _CharT, class _Traits>
1213 basic_istream<_CharT, _Traits>&
1214 basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n)
1215 {
1216     __gc_ = 0;
1217 #ifndef _LIBCPP_NO_EXCEPTIONS
1218     try
1219     {
1220 #endif  // _LIBCPP_NO_EXCEPTIONS
1221         sentry __sen(*this, true);
1222         if (__sen)
1223         {
1224             streamsize __c = 0;
1225             typedef istreambuf_iterator<char_type, traits_type> _I;
1226             _I __i(*this);
1227             _I __eof;
1228             for (; __i != __eof && __n > 0; ++__i, ++__s, ++__c, --__n)
1229                 *__s = *__i;
1230             if (__i == __eof)
1231             {
1232                 ios_base::iostate __err = ios_base::eofbit;
1233                 if (__n > 0)
1234                     __err |= ios_base::failbit;
1235                 this->setstate(__err);
1236             }
1237             __gc_ = __c;
1238         }
1239         else
1240             this->setstate(ios_base::failbit);
1241 #ifndef _LIBCPP_NO_EXCEPTIONS
1242     }
1243     catch (...)
1244     {
1245         this->__set_badbit_and_consider_rethrow();
1246     }
1247 #endif  // _LIBCPP_NO_EXCEPTIONS
1248     return *this;
1249 }
1250
1251 template<class _CharT, class _Traits>
1252 streamsize
1253 basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n)
1254 {
1255     __gc_ = 0;
1256     streamsize __c = 0;
1257 #ifndef _LIBCPP_NO_EXCEPTIONS
1258     try
1259     {
1260 #endif  // _LIBCPP_NO_EXCEPTIONS
1261         sentry __sen(*this, true);
1262         if (__sen)
1263         {
1264             typedef istreambuf_iterator<char_type, traits_type> _I;
1265             _I __i(*this);
1266             _I __eof;
1267             __c = this->rdbuf()->in_avail();
1268             switch (__c)
1269             {
1270             case -1:
1271                 __i = __eof;
1272                 break;
1273             case 0:
1274                 break;
1275             default:
1276                 __c = _STD::min(__c, __n);
1277                 for (streamsize __k = 0; __k < __c; ++__k, ++__s, ++__i)
1278                     *__s = *__i;
1279             }
1280             if (__i == __eof)
1281                 this->setstate(ios_base::eofbit);
1282             __gc_ = __c;
1283         }
1284         else
1285             this->setstate(ios_base::failbit);
1286 #ifndef _LIBCPP_NO_EXCEPTIONS
1287     }
1288     catch (...)
1289     {
1290         this->__set_badbit_and_consider_rethrow();
1291     }
1292 #endif  // _LIBCPP_NO_EXCEPTIONS
1293     return __c;
1294 }
1295
1296 template<class _CharT, class _Traits>
1297 basic_istream<_CharT, _Traits>&
1298 basic_istream<_CharT, _Traits>::putback(char_type __c)
1299 {
1300     __gc_ = 0;
1301 #ifndef _LIBCPP_NO_EXCEPTIONS
1302     try
1303     {
1304 #endif  // _LIBCPP_NO_EXCEPTIONS
1305         sentry __sen(*this, true);
1306         if (__sen)
1307         {
1308             if (this->rdbuf() == 0 || this->rdbuf()->sputbackc(__c) == traits_type::eof())
1309                 this->setstate(ios_base::badbit);
1310         }
1311         else
1312             this->setstate(ios_base::failbit);
1313 #ifndef _LIBCPP_NO_EXCEPTIONS
1314     }
1315     catch (...)
1316     {
1317         this->__set_badbit_and_consider_rethrow();
1318     }
1319 #endif  // _LIBCPP_NO_EXCEPTIONS
1320     return *this;
1321 }
1322
1323 template<class _CharT, class _Traits>
1324 basic_istream<_CharT, _Traits>&
1325 basic_istream<_CharT, _Traits>::unget()
1326 {
1327     __gc_ = 0;
1328 #ifndef _LIBCPP_NO_EXCEPTIONS
1329     try
1330     {
1331 #endif  // _LIBCPP_NO_EXCEPTIONS
1332         sentry __sen(*this, true);
1333         if (__sen)
1334         {
1335             if (this->rdbuf() == 0 || this->rdbuf()->sungetc() == traits_type::eof())
1336                 this->setstate(ios_base::badbit);
1337         }
1338         else
1339             this->setstate(ios_base::failbit);
1340 #ifndef _LIBCPP_NO_EXCEPTIONS
1341     }
1342     catch (...)
1343     {
1344         this->__set_badbit_and_consider_rethrow();
1345     }
1346 #endif  // _LIBCPP_NO_EXCEPTIONS
1347     return *this;
1348 }
1349
1350 template<class _CharT, class _Traits>
1351 int
1352 basic_istream<_CharT, _Traits>::sync()
1353 {
1354     int __r = 0;
1355 #ifndef _LIBCPP_NO_EXCEPTIONS
1356     try
1357     {
1358 #endif  // _LIBCPP_NO_EXCEPTIONS
1359         sentry __sen(*this, true);
1360         if (__sen)
1361         {
1362             if (this->rdbuf() == 0)
1363                 return -1;
1364             if (this->rdbuf()->pubsync() == -1)
1365             {
1366                 this->setstate(ios_base::badbit);
1367                 return -1;
1368             }
1369         }
1370 #ifndef _LIBCPP_NO_EXCEPTIONS
1371     }
1372     catch (...)
1373     {
1374         this->__set_badbit_and_consider_rethrow();
1375     }
1376 #endif  // _LIBCPP_NO_EXCEPTIONS
1377     return __r;
1378 }
1379
1380 template<class _CharT, class _Traits>
1381 typename basic_istream<_CharT, _Traits>::pos_type
1382 basic_istream<_CharT, _Traits>::tellg()
1383 {
1384     pos_type __r(-1);
1385 #ifndef _LIBCPP_NO_EXCEPTIONS
1386     try
1387     {
1388 #endif  // _LIBCPP_NO_EXCEPTIONS
1389         sentry __sen(*this, true);
1390         if (__sen)
1391             __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
1392 #ifndef _LIBCPP_NO_EXCEPTIONS
1393     }
1394     catch (...)
1395     {
1396         this->__set_badbit_and_consider_rethrow();
1397     }
1398 #endif  // _LIBCPP_NO_EXCEPTIONS
1399     return __r;
1400 }
1401
1402 template<class _CharT, class _Traits>
1403 basic_istream<_CharT, _Traits>&
1404 basic_istream<_CharT, _Traits>::seekg(pos_type __pos)
1405 {
1406 #ifndef _LIBCPP_NO_EXCEPTIONS
1407     try
1408     {
1409 #endif  // _LIBCPP_NO_EXCEPTIONS
1410         sentry __sen(*this, true);
1411         if (__sen)
1412             if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
1413                 this->setstate(ios_base::failbit);
1414 #ifndef _LIBCPP_NO_EXCEPTIONS
1415     }
1416     catch (...)
1417     {
1418         this->__set_badbit_and_consider_rethrow();
1419     }
1420 #endif  // _LIBCPP_NO_EXCEPTIONS
1421     return *this;
1422 }
1423
1424 template<class _CharT, class _Traits>
1425 basic_istream<_CharT, _Traits>&
1426 basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir)
1427 {
1428 #ifndef _LIBCPP_NO_EXCEPTIONS
1429     try
1430     {
1431 #endif  // _LIBCPP_NO_EXCEPTIONS
1432         sentry __sen(*this, true);
1433         if (__sen)
1434             this->rdbuf()->pubseekoff(__off, __dir, ios_base::in);
1435 #ifndef _LIBCPP_NO_EXCEPTIONS
1436     }
1437     catch (...)
1438     {
1439         this->__set_badbit_and_consider_rethrow();
1440     }
1441 #endif  // _LIBCPP_NO_EXCEPTIONS
1442     return *this;
1443 }
1444
1445 template <class _CharT, class _Traits>
1446 basic_istream<_CharT, _Traits>&
1447 ws(basic_istream<_CharT, _Traits>& __is)
1448 {
1449 #ifndef _LIBCPP_NO_EXCEPTIONS
1450     try
1451     {
1452 #endif  // _LIBCPP_NO_EXCEPTIONS
1453         typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1454         if (__sen)
1455         {
1456             typedef istreambuf_iterator<_CharT, _Traits> _I;
1457             const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
1458             _I __i(__is);
1459             _I __eof;
1460             for (; __i != __eof; ++__i)
1461                 if (!__ct.is(__ct.space, *__i))
1462                     break;
1463             if (__i == __eof)
1464                 __is.setstate(ios_base::failbit | ios_base::eofbit);
1465         }
1466 #ifndef _LIBCPP_NO_EXCEPTIONS
1467     }
1468     catch (...)
1469     {
1470         __is.__set_badbit_and_consider_rethrow();
1471     }
1472 #endif  // _LIBCPP_NO_EXCEPTIONS
1473     return __is;
1474 }
1475
1476 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1477
1478 template <class _CharT, class _Traits, class _Tp>
1479 inline _LIBCPP_INLINE_VISIBILITY
1480 basic_istream<_CharT, _Traits>&
1481 operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
1482 {
1483     __is >> __x;
1484     return __is;
1485 }
1486
1487 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1488
1489 template <class _CharT, class _Traits>
1490 class _LIBCPP_VISIBLE basic_iostream
1491     : public basic_istream<_CharT, _Traits>,
1492       public basic_ostream<_CharT, _Traits>
1493 {
1494 public:
1495     // types:
1496     typedef _CharT                         char_type;
1497     typedef _Traits                        traits_type;
1498     typedef typename traits_type::int_type int_type;
1499     typedef typename traits_type::pos_type pos_type;
1500     typedef typename traits_type::off_type off_type;
1501
1502     // constructor/destructor
1503     explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb);
1504     virtual ~basic_iostream();
1505 protected:
1506 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1507     basic_iostream(basic_iostream&& __rhs);
1508 #endif
1509
1510     // assign/swap
1511 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1512     basic_iostream& operator=(basic_iostream&& __rhs);
1513 #endif
1514     void swap(basic_iostream& __rhs);
1515 public:
1516 };
1517
1518 template <class _CharT, class _Traits>
1519 inline _LIBCPP_INLINE_VISIBILITY
1520 basic_iostream<_CharT, _Traits>::basic_iostream(basic_streambuf<char_type, traits_type>* __sb)
1521     : basic_istream<_CharT, _Traits>(__sb)
1522 {
1523 }
1524
1525 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1526
1527 template <class _CharT, class _Traits>
1528 inline _LIBCPP_INLINE_VISIBILITY
1529 basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
1530     : basic_istream<_CharT, _Traits>(_STD::move(__rhs))
1531 {
1532 }
1533
1534 template <class _CharT, class _Traits>
1535 inline _LIBCPP_INLINE_VISIBILITY
1536 basic_iostream<_CharT, _Traits>&
1537 basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs)
1538 {
1539     swap(__rhs);
1540     return *this;
1541 }
1542
1543 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1544
1545 template <class _CharT, class _Traits>
1546 basic_iostream<_CharT, _Traits>::~basic_iostream()
1547 {
1548 }
1549
1550 template <class _CharT, class _Traits>
1551 inline _LIBCPP_INLINE_VISIBILITY
1552 void
1553 basic_iostream<_CharT, _Traits>::swap(basic_iostream& __rhs)
1554 {
1555     basic_istream<char_type, traits_type>::swap(__rhs);
1556 }
1557
1558 template<class _CharT, class _Traits, class _Allocator>
1559 basic_istream<_CharT, _Traits>&
1560 operator>>(basic_istream<_CharT, _Traits>& __is,
1561            basic_string<_CharT, _Traits, _Allocator>& __str)
1562 {
1563 #ifndef _LIBCPP_NO_EXCEPTIONS
1564     try
1565     {
1566 #endif  // _LIBCPP_NO_EXCEPTIONS
1567         typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1568         if (__sen)
1569         {
1570             __str.clear();
1571             typedef istreambuf_iterator<_CharT, _Traits> _I;
1572             streamsize __n = __is.width();
1573             if (__n == 0)
1574                 __n = __str.max_size();
1575             if (__n < 0)
1576                 __n = numeric_limits<streamsize>::max();
1577             streamsize __c = 0;
1578             const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
1579             _I __i(__is);
1580             _I __eof;
1581             for (; __i != __eof && __c < __n; ++__i, ++__c)
1582             {
1583                 _CharT __ch = *__i;
1584                 if (__ct.is(__ct.space, __ch))
1585                     break;
1586                 __str.push_back(__ch);
1587             }
1588             __is.width(0);
1589             ios_base::iostate __err = ios_base::goodbit;
1590             if (__i == __eof)
1591                __err |= ios_base::eofbit;
1592             if (__c == 0)
1593                __err |= ios_base::failbit;
1594             __is.setstate(__err);
1595         }
1596         else
1597             __is.setstate(ios_base::failbit);
1598 #ifndef _LIBCPP_NO_EXCEPTIONS
1599     }
1600     catch (...)
1601     {
1602         __is.__set_badbit_and_consider_rethrow();
1603     }
1604 #endif  // _LIBCPP_NO_EXCEPTIONS
1605     return __is;
1606 }
1607
1608 template<class _CharT, class _Traits, class _Allocator>
1609 basic_istream<_CharT, _Traits>&
1610 getline(basic_istream<_CharT, _Traits>& __is,
1611         basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
1612 {
1613 #ifndef _LIBCPP_NO_EXCEPTIONS
1614     try
1615     {
1616 #endif  // _LIBCPP_NO_EXCEPTIONS
1617         typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1618         if (__sen)
1619         {
1620             __str.clear();
1621             streamsize __c = 0;
1622             typedef istreambuf_iterator<_CharT, _Traits> _I;
1623             _I __i(__is);
1624             _I __eof;
1625             streamsize __n = __str.max_size();
1626             if (__n < 0)
1627                 __n = numeric_limits<streamsize>::max();
1628             for (; __i != __eof;)
1629             {
1630                 _CharT __ch = *__i;
1631                 ++__i;
1632                 ++__c;
1633                 if (_Traits::eq(__ch, __dlm))
1634                     break;
1635                 if (__c == __n)
1636                 {
1637                     __is.setstate(ios_base::failbit);
1638                     break;
1639                 }
1640                 __str.push_back(__ch);
1641             }
1642             ios_base::iostate __err = ios_base::goodbit;
1643             if (__i == __eof)
1644                __err |= ios_base::eofbit;
1645             if (__c == 0)
1646                __err |= ios_base::failbit;
1647             __is.setstate(__err);
1648         }
1649 #ifndef _LIBCPP_NO_EXCEPTIONS
1650     }
1651     catch (...)
1652     {
1653         __is.__set_badbit_and_consider_rethrow();
1654     }
1655 #endif  // _LIBCPP_NO_EXCEPTIONS
1656     return __is;
1657 }
1658
1659 template<class _CharT, class _Traits, class _Allocator>
1660 inline _LIBCPP_INLINE_VISIBILITY
1661 basic_istream<_CharT, _Traits>&
1662 getline(basic_istream<_CharT, _Traits>& __is,
1663         basic_string<_CharT, _Traits, _Allocator>& __str)
1664 {
1665     return getline(__is, __str, __is.widen('\n'));
1666 }
1667
1668 #ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1669
1670 template<class _CharT, class _Traits, class _Allocator>
1671 inline _LIBCPP_INLINE_VISIBILITY
1672 basic_istream<_CharT, _Traits>&
1673 getline(basic_istream<_CharT, _Traits>&& __is,
1674         basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
1675 {
1676     return getline(__is, __str, __dlm);
1677 }
1678
1679 template<class _CharT, class _Traits, class _Allocator>
1680 inline _LIBCPP_INLINE_VISIBILITY
1681 basic_istream<_CharT, _Traits>&
1682 getline(basic_istream<_CharT, _Traits>&& __is,
1683         basic_string<_CharT, _Traits, _Allocator>& __str)
1684 {
1685     return getline(__is, __str, __is.widen('\n'));
1686 }
1687
1688 #endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1689
1690 template <class _CharT, class _Traits, size_t _Size>
1691 basic_istream<_CharT, _Traits>&
1692 operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
1693 {
1694 #ifndef _LIBCPP_NO_EXCEPTIONS
1695     try
1696     {
1697 #endif  // _LIBCPP_NO_EXCEPTIONS
1698         typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1699         if (__sen)
1700         {
1701             basic_string<_CharT, _Traits> __str;
1702             const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
1703             typedef istreambuf_iterator<_CharT, _Traits> _I;
1704             streamsize __c = 0;
1705             _CharT __zero = __ct.widen('0');
1706             _CharT __one = __ct.widen('1');
1707             _I __i(__is);
1708             _I __eof;
1709             for (; __i != __eof && __c < _Size; ++__i, ++__c)
1710             {
1711                 _CharT __ch = *__i;
1712                 if (__ch != __zero && __ch != __one)
1713                     break;
1714                 __str.push_back(__ch);
1715             }
1716             __is.width(0);
1717             __x = bitset<_Size>(__str);
1718             ios_base::iostate __err = ios_base::goodbit;
1719             if (__i == __eof)
1720                __err |= ios_base::eofbit;
1721             if (__c == 0)
1722                __err |= ios_base::failbit;
1723             __is.setstate(__err);
1724         }
1725         else
1726             __is.setstate(ios_base::failbit);
1727 #ifndef _LIBCPP_NO_EXCEPTIONS
1728     }
1729     catch (...)
1730     {
1731         __is.__set_badbit_and_consider_rethrow();
1732     }
1733 #endif  // _LIBCPP_NO_EXCEPTIONS
1734     return __is;
1735 }
1736
1737 extern template class basic_istream<char>;
1738 extern template class basic_istream<wchar_t>;
1739 extern template class basic_iostream<char>;
1740
1741 _LIBCPP_END_NAMESPACE_STD
1742
1743 #endif  // _LIBCPP_ISTREAM