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
17template <class charT, class traits = char_traits<charT> >
18class basic_istream
19    : virtual public basic_ios<charT,traits>
20{
21public:
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:
88template<class charT, class traits>
89  basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT&);
90
91template<class traits>
92  basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char&);
93
94template<class traits>
95  basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char&);
96
97template<class charT, class traits>
98  basic_istream<charT,traits>& operator>>(basic_istream<charT,traits>&, charT*);
99
100template<class traits>
101  basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, unsigned char*);
102
103template<class traits>
104  basic_istream<char,traits>& operator>>(basic_istream<char,traits>&, signed char*);
105
106template <class charT, class traits>
107  void
108  swap(basic_istream<charT, traits>& x, basic_istream<charT, traits>& y);
109
110typedef basic_istream<char> istream;
111typedef basic_istream<wchar_t> wistream;
112
113template <class charT, class traits = char_traits<charT> >
114class basic_iostream :
115    public basic_istream<charT,traits>,
116    public basic_ostream<charT,traits>
117{
118public:
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
136template <class charT, class traits>
137  void
138  swap(basic_iostream<charT, traits>& x, basic_iostream<charT, traits>& y);
139
140typedef basic_iostream<char> iostream;
141typedef basic_iostream<wchar_t> wiostream;
142
143template <class charT, class traits>
144  basic_istream<charT,traits>&
145  ws(basic_istream<charT,traits>& is);
146
147template <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
162template <class _CharT, class _Traits>
163class _LIBCPP_VISIBLE basic_istream
164    : virtual public basic_ios<_CharT, _Traits>
165{
166    streamsize __gc_;
167public:
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();
178protected:
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);
188public:
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
240template <class _CharT, class _Traits>
241class _LIBCPP_VISIBLE basic_istream<_CharT, _Traits>::sentry
242{
243    bool __ok_;
244
245    sentry(const sentry&); // = delete;
246    sentry& operator=(const sentry&); // = delete;
247
248public:
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
257template <class _CharT, class _Traits>
258basic_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
284template <class _CharT, class _Traits>
285inline _LIBCPP_INLINE_VISIBILITY
286basic_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
294template <class _CharT, class _Traits>
295inline _LIBCPP_INLINE_VISIBILITY
296basic_istream<_CharT, _Traits>::basic_istream(basic_istream&& __rhs)
297    : __gc_(__rhs.__gc_)
298{
299    __rhs.__gc_ = 0;
300    this->move(__rhs);
301}
302
303template <class _CharT, class _Traits>
304inline _LIBCPP_INLINE_VISIBILITY
305basic_istream<_CharT, _Traits>&
306basic_istream<_CharT, _Traits>::operator=(basic_istream&& __rhs)
307{
308    swap(__rhs);
309    return *this;
310}
311
312#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
313
314template <class _CharT, class _Traits>
315basic_istream<_CharT, _Traits>::~basic_istream()
316{
317}
318
319template <class _CharT, class _Traits>
320inline _LIBCPP_INLINE_VISIBILITY
321void
322basic_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
328template <class _CharT, class _Traits>
329basic_istream<_CharT, _Traits>&
330basic_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
355template <class _CharT, class _Traits>
356basic_istream<_CharT, _Traits>&
357basic_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
382template <class _CharT, class _Traits>
383basic_istream<_CharT, _Traits>&
384basic_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
409template <class _CharT, class _Traits>
410basic_istream<_CharT, _Traits>&
411basic_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
436template <class _CharT, class _Traits>
437basic_istream<_CharT, _Traits>&
438basic_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
463template <class _CharT, class _Traits>
464basic_istream<_CharT, _Traits>&
465basic_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
490template <class _CharT, class _Traits>
491basic_istream<_CharT, _Traits>&
492basic_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
517template <class _CharT, class _Traits>
518basic_istream<_CharT, _Traits>&
519basic_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
544template <class _CharT, class _Traits>
545basic_istream<_CharT, _Traits>&
546basic_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
571template <class _CharT, class _Traits>
572basic_istream<_CharT, _Traits>&
573basic_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
598template <class _CharT, class _Traits>
599basic_istream<_CharT, _Traits>&
600basic_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
625template <class _CharT, class _Traits>
626basic_istream<_CharT, _Traits>&
627basic_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
665template <class _CharT, class _Traits>
666basic_istream<_CharT, _Traits>&
667basic_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
705template <class _CharT, class _Traits>
706inline _LIBCPP_INLINE_VISIBILITY
707basic_istream<_CharT, _Traits>&
708basic_istream<_CharT, _Traits>::operator>>(basic_istream& (*__pf)(basic_istream&))
709{
710    return __pf(*this);
711}
712
713template <class _CharT, class _Traits>
714inline _LIBCPP_INLINE_VISIBILITY
715basic_istream<_CharT, _Traits>&
716basic_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
723template <class _CharT, class _Traits>
724inline _LIBCPP_INLINE_VISIBILITY
725basic_istream<_CharT, _Traits>&
726basic_istream<_CharT, _Traits>::operator>>(ios_base& (*__pf)(ios_base&))
727{
728    __pf(*this);
729    return *this;
730}
731
732template<class _CharT, class _Traits>
733basic_istream<_CharT, _Traits>&
734operator>>(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
777template<class _Traits>
778inline _LIBCPP_INLINE_VISIBILITY
779basic_istream<char, _Traits>&
780operator>>(basic_istream<char, _Traits>& __is, unsigned char* __s)
781{
782    return __is >> (char*)__s;
783}
784
785template<class _Traits>
786inline _LIBCPP_INLINE_VISIBILITY
787basic_istream<char, _Traits>&
788operator>>(basic_istream<char, _Traits>& __is, signed char* __s)
789{
790    return __is >> (char*)__s;
791}
792
793template<class _CharT, class _Traits>
794basic_istream<_CharT, _Traits>&
795operator>>(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            typedef istreambuf_iterator<_CharT, _Traits> _I;
805            _I __i(__is);
806            _I __eof;
807            if (__i != __eof)
808            {
809                __c = *__i;
810                if (++__i == __eof)
811                    __is.setstate(ios_base::eofbit);
812            }
813            else
814                __is.setstate(ios_base::eofbit | ios_base::failbit);
815        }
816#ifndef _LIBCPP_NO_EXCEPTIONS
817    }
818    catch (...)
819    {
820        __is.__set_badbit_and_consider_rethrow();
821    }
822#endif  // _LIBCPP_NO_EXCEPTIONS
823    return __is;
824}
825
826template<class _Traits>
827inline _LIBCPP_INLINE_VISIBILITY
828basic_istream<char, _Traits>&
829operator>>(basic_istream<char, _Traits>& __is, unsigned char& __c)
830{
831    return __is >> (char&)__c;
832}
833
834template<class _Traits>
835inline _LIBCPP_INLINE_VISIBILITY
836basic_istream<char, _Traits>&
837operator>>(basic_istream<char, _Traits>& __is, signed char& __c)
838{
839    return __is >> (char&)__c;
840}
841
842template<class _CharT, class _Traits>
843basic_istream<_CharT, _Traits>&
844basic_istream<_CharT, _Traits>::operator>>(basic_streambuf<char_type, traits_type>* __sb)
845{
846    __gc_ = 0;
847#ifndef _LIBCPP_NO_EXCEPTIONS
848    try
849    {
850#endif  // _LIBCPP_NO_EXCEPTIONS
851        sentry __s(*this, true);
852        if (__s)
853        {
854            streamsize __c = 0;
855            if (__sb)
856            {
857#ifndef _LIBCPP_NO_EXCEPTIONS
858                try
859                {
860#endif  // _LIBCPP_NO_EXCEPTIONS
861                    typedef istreambuf_iterator<char_type, traits_type> _I;
862                    typedef ostreambuf_iterator<char_type, traits_type> _O;
863                    _I __i(*this);
864                    _I __eof;
865                    _O __o(__sb);
866                    for (; __i != __eof; ++__i, ++__o, ++__c)
867                    {
868                        *__o = *__i;
869                        if (__o.failed())
870                            break;
871                    }
872                    ios_base::iostate __err = ios_base::goodbit;
873                    if (__i == __eof)
874                       __err |= ios_base::eofbit;
875                    if (__c == 0)
876                       __err |= ios_base::failbit;
877                    this->setstate(__err);
878#ifndef _LIBCPP_NO_EXCEPTIONS
879                }
880                catch (...)
881                {
882                    if (__c == 0)
883                        this->__set_failbit_and_consider_rethrow();
884                }
885#endif  // _LIBCPP_NO_EXCEPTIONS
886            }
887            else
888                this->setstate(ios_base::failbit);
889            __gc_ = __c;
890        }
891#ifndef _LIBCPP_NO_EXCEPTIONS
892    }
893    catch (...)
894    {
895        this->__set_badbit_and_consider_rethrow();
896    }
897#endif  // _LIBCPP_NO_EXCEPTIONS
898    return *this;
899}
900
901template<class _CharT, class _Traits>
902typename basic_istream<_CharT, _Traits>::int_type
903basic_istream<_CharT, _Traits>::get()
904{
905    __gc_ = 0;
906    int_type __r = traits_type::eof();
907#ifndef _LIBCPP_NO_EXCEPTIONS
908    try
909    {
910#endif  // _LIBCPP_NO_EXCEPTIONS
911        sentry __s(*this, true);
912        if (__s)
913        {
914            streamsize __c = 0;
915            typedef istreambuf_iterator<char_type, traits_type> _I;
916            _I __i(*this);
917            _I __eof;
918            ios_base::iostate __err = ios_base::goodbit;
919            if (__i != __eof)
920            {
921                __r = traits_type::to_int_type(*__i);
922                ++__c;
923                if (++__i == __eof)
924                    __err |= ios_base::eofbit;
925            }
926            else
927                __err |= ios_base::failbit | ios_base::eofbit;
928            this->setstate(__err);
929            __gc_ = __c;
930        }
931#ifndef _LIBCPP_NO_EXCEPTIONS
932    }
933    catch (...)
934    {
935        this->__set_badbit_and_consider_rethrow();
936    }
937#endif  // _LIBCPP_NO_EXCEPTIONS
938    return __r;
939}
940
941template<class _CharT, class _Traits>
942inline _LIBCPP_INLINE_VISIBILITY
943basic_istream<_CharT, _Traits>&
944basic_istream<_CharT, _Traits>::get(char_type& __c)
945{
946    int_type __ch = get();
947    if (__ch != traits_type::eof())
948        __c = traits_type::to_char_type(__ch);
949    return *this;
950}
951
952template<class _CharT, class _Traits>
953basic_istream<_CharT, _Traits>&
954basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n, char_type __dlm)
955{
956    __gc_ = 0;
957#ifndef _LIBCPP_NO_EXCEPTIONS
958    try
959    {
960#endif  // _LIBCPP_NO_EXCEPTIONS
961        sentry __sen(*this, true);
962        if (__sen)
963        {
964            streamsize __c = 0;
965            if (__n > 0)
966            {
967                typedef istreambuf_iterator<char_type, traits_type> _I;
968                _I __i(*this);
969                _I __eof;
970                for (; __i != __eof && __n > 1; ++__i, ++__s, ++__c)
971                {
972                    char_type __ch = *__i;
973                    if (traits_type::eq(__ch, __dlm))
974                        break;
975                    *__s = __ch;
976                }
977                *__s = char_type();
978                ios_base::iostate __err = ios_base::goodbit;
979                if (__i == __eof)
980                   __err |= ios_base::eofbit;
981                if (__c == 0)
982                   __err |= ios_base::failbit;
983                this->setstate(__err);
984            }
985            else
986                this->setstate(ios_base::failbit);
987            __gc_ = __c;
988        }
989#ifndef _LIBCPP_NO_EXCEPTIONS
990    }
991    catch (...)
992    {
993        this->__set_badbit_and_consider_rethrow();
994    }
995#endif  // _LIBCPP_NO_EXCEPTIONS
996    return *this;
997}
998
999template<class _CharT, class _Traits>
1000inline _LIBCPP_INLINE_VISIBILITY
1001basic_istream<_CharT, _Traits>&
1002basic_istream<_CharT, _Traits>::get(char_type* __s, streamsize __n)
1003{
1004    return get(__s, __n, this->widen('\n'));
1005}
1006
1007template<class _CharT, class _Traits>
1008basic_istream<_CharT, _Traits>&
1009basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb,
1010                                    char_type __dlm)
1011{
1012    __gc_ = 0;
1013#ifndef _LIBCPP_NO_EXCEPTIONS
1014    try
1015    {
1016#endif  // _LIBCPP_NO_EXCEPTIONS
1017        sentry __sen(*this, true);
1018        if (__sen)
1019        {
1020            streamsize __c = 0;
1021            ios_base::iostate __err = ios_base::goodbit;
1022#ifndef _LIBCPP_NO_EXCEPTIONS
1023            try
1024            {
1025#endif  // _LIBCPP_NO_EXCEPTIONS
1026                typedef istreambuf_iterator<char_type, traits_type> _I;
1027                typedef ostreambuf_iterator<char_type, traits_type> _O;
1028                _I __i(*this);
1029                _I __eof;
1030                _O __o(&__sb);
1031                for (; __i != __eof; ++__i, ++__o, ++__c)
1032                {
1033                    char_type __ch = *__i;
1034                    if (traits_type::eq(__ch, __dlm))
1035                        break;
1036                    *__o = __ch;
1037                    if (__o.failed())
1038                        break;
1039                }
1040                if (__i == __eof)
1041                   __err |= ios_base::eofbit;
1042#ifndef _LIBCPP_NO_EXCEPTIONS
1043            }
1044            catch (...)
1045            {
1046            }
1047#endif  // _LIBCPP_NO_EXCEPTIONS
1048            if (__c == 0)
1049               __err |= ios_base::failbit;
1050            this->setstate(__err);
1051            __gc_ = __c;
1052        }
1053#ifndef _LIBCPP_NO_EXCEPTIONS
1054    }
1055    catch (...)
1056    {
1057        this->__set_badbit_and_consider_rethrow();
1058    }
1059#endif  // _LIBCPP_NO_EXCEPTIONS
1060    return *this;
1061}
1062
1063template<class _CharT, class _Traits>
1064inline _LIBCPP_INLINE_VISIBILITY
1065basic_istream<_CharT, _Traits>&
1066basic_istream<_CharT, _Traits>::get(basic_streambuf<char_type, traits_type>& __sb)
1067{
1068    return get(__sb, this->widen('\n'));
1069}
1070
1071template<class _CharT, class _Traits>
1072basic_istream<_CharT, _Traits>&
1073basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n, char_type __dlm)
1074{
1075    __gc_ = 0;
1076#ifndef _LIBCPP_NO_EXCEPTIONS
1077    try
1078    {
1079#endif  // _LIBCPP_NO_EXCEPTIONS
1080        sentry __sen(*this, true);
1081        if (__sen)
1082        {
1083            streamsize __c = 0;
1084            typedef istreambuf_iterator<char_type, traits_type> _I;
1085            _I __i(*this);
1086            _I __eof;
1087            for (; __i != __eof; ++__s, --__n)
1088            {
1089                char_type __ch = *__i;
1090                ++__i;
1091                ++__c;
1092                if (traits_type::eq(__ch, __dlm))
1093                    break;
1094                if (__n < 2)
1095                {
1096                    this->setstate(ios_base::failbit);
1097                    break;
1098                }
1099                *__s = __ch;
1100            }
1101            if (__n)
1102                *__s = char_type();
1103            ios_base::iostate __err = ios_base::goodbit;
1104            if (__i == __eof)
1105               __err |= ios_base::eofbit;
1106            if (__c == 0)
1107               __err |= ios_base::failbit;
1108            this->setstate(__err);
1109            __gc_ = __c;
1110        }
1111#ifndef _LIBCPP_NO_EXCEPTIONS
1112    }
1113    catch (...)
1114    {
1115        this->__set_badbit_and_consider_rethrow();
1116    }
1117#endif  // _LIBCPP_NO_EXCEPTIONS
1118    return *this;
1119}
1120
1121template<class _CharT, class _Traits>
1122inline _LIBCPP_INLINE_VISIBILITY
1123basic_istream<_CharT, _Traits>&
1124basic_istream<_CharT, _Traits>::getline(char_type* __s, streamsize __n)
1125{
1126    return getline(__s, __n, this->widen('\n'));
1127}
1128
1129template<class _CharT, class _Traits>
1130basic_istream<_CharT, _Traits>&
1131basic_istream<_CharT, _Traits>::ignore(streamsize __n, int_type __dlm)
1132{
1133    __gc_ = 0;
1134#ifndef _LIBCPP_NO_EXCEPTIONS
1135    try
1136    {
1137#endif  // _LIBCPP_NO_EXCEPTIONS
1138        sentry __sen(*this, true);
1139        if (__sen)
1140        {
1141            streamsize __c = 0;
1142            typedef istreambuf_iterator<char_type, traits_type> _I;
1143            _I __i(*this);
1144            _I __eof;
1145            if (__n != numeric_limits<streamsize>::max())
1146            {
1147                for (; __n > 0 && __i != __eof; --__n)
1148                {
1149                    char_type __ch = *__i;
1150                    ++__i;
1151                    ++__c;
1152                    if (traits_type::eq(__ch, __dlm))
1153                        break;
1154                }
1155            }
1156            else
1157            {
1158                while (__i != __eof)
1159                {
1160                    char_type __ch = *__i;
1161                    ++__i;
1162                    ++__c;
1163                    if (traits_type::eq(__ch, __dlm))
1164                        break;
1165                }
1166            }
1167            if (__i == __eof)
1168                this->setstate(ios_base::eofbit);
1169            __gc_ = __c;
1170        }
1171#ifndef _LIBCPP_NO_EXCEPTIONS
1172    }
1173    catch (...)
1174    {
1175        this->__set_badbit_and_consider_rethrow();
1176    }
1177#endif  // _LIBCPP_NO_EXCEPTIONS
1178    return *this;
1179}
1180
1181template<class _CharT, class _Traits>
1182typename basic_istream<_CharT, _Traits>::int_type
1183basic_istream<_CharT, _Traits>::peek()
1184{
1185    __gc_ = 0;
1186    int_type __r = traits_type::eof();
1187#ifndef _LIBCPP_NO_EXCEPTIONS
1188    try
1189    {
1190#endif  // _LIBCPP_NO_EXCEPTIONS
1191        sentry __sen(*this, true);
1192        if (__sen)
1193            __r = this->rdbuf()->sgetc();
1194#ifndef _LIBCPP_NO_EXCEPTIONS
1195    }
1196    catch (...)
1197    {
1198        this->__set_badbit_and_consider_rethrow();
1199    }
1200#endif  // _LIBCPP_NO_EXCEPTIONS
1201    return __r;
1202}
1203
1204template<class _CharT, class _Traits>
1205basic_istream<_CharT, _Traits>&
1206basic_istream<_CharT, _Traits>::read(char_type* __s, streamsize __n)
1207{
1208    __gc_ = 0;
1209#ifndef _LIBCPP_NO_EXCEPTIONS
1210    try
1211    {
1212#endif  // _LIBCPP_NO_EXCEPTIONS
1213        sentry __sen(*this, true);
1214        if (__sen)
1215        {
1216            streamsize __c = 0;
1217            typedef istreambuf_iterator<char_type, traits_type> _I;
1218            _I __i(*this);
1219            _I __eof;
1220            for (; __i != __eof && __n > 0; ++__i, ++__s, ++__c, --__n)
1221                *__s = *__i;
1222            if (__i == __eof)
1223            {
1224                ios_base::iostate __err = ios_base::eofbit;
1225                if (__n > 0)
1226                    __err |= ios_base::failbit;
1227                this->setstate(__err);
1228            }
1229            __gc_ = __c;
1230        }
1231        else
1232            this->setstate(ios_base::failbit);
1233#ifndef _LIBCPP_NO_EXCEPTIONS
1234    }
1235    catch (...)
1236    {
1237        this->__set_badbit_and_consider_rethrow();
1238    }
1239#endif  // _LIBCPP_NO_EXCEPTIONS
1240    return *this;
1241}
1242
1243template<class _CharT, class _Traits>
1244streamsize
1245basic_istream<_CharT, _Traits>::readsome(char_type* __s, streamsize __n)
1246{
1247    __gc_ = 0;
1248    streamsize __c = 0;
1249#ifndef _LIBCPP_NO_EXCEPTIONS
1250    try
1251    {
1252#endif  // _LIBCPP_NO_EXCEPTIONS
1253        sentry __sen(*this, true);
1254        if (__sen)
1255        {
1256            typedef istreambuf_iterator<char_type, traits_type> _I;
1257            _I __i(*this);
1258            _I __eof;
1259            __c = this->rdbuf()->in_avail();
1260            switch (__c)
1261            {
1262            case -1:
1263                __i = __eof;
1264                break;
1265            case 0:
1266                break;
1267            default:
1268                __c = min(__c, __n);
1269                for (streamsize __k = 0; __k < __c; ++__k, ++__s, ++__i)
1270                    *__s = *__i;
1271            }
1272            if (__i == __eof)
1273                this->setstate(ios_base::eofbit);
1274            __gc_ = __c;
1275        }
1276        else
1277            this->setstate(ios_base::failbit);
1278#ifndef _LIBCPP_NO_EXCEPTIONS
1279    }
1280    catch (...)
1281    {
1282        this->__set_badbit_and_consider_rethrow();
1283    }
1284#endif  // _LIBCPP_NO_EXCEPTIONS
1285    return __c;
1286}
1287
1288template<class _CharT, class _Traits>
1289basic_istream<_CharT, _Traits>&
1290basic_istream<_CharT, _Traits>::putback(char_type __c)
1291{
1292    __gc_ = 0;
1293#ifndef _LIBCPP_NO_EXCEPTIONS
1294    try
1295    {
1296#endif  // _LIBCPP_NO_EXCEPTIONS
1297        sentry __sen(*this, true);
1298        if (__sen)
1299        {
1300            if (this->rdbuf() == 0 || this->rdbuf()->sputbackc(__c) == traits_type::eof())
1301                this->setstate(ios_base::badbit);
1302        }
1303        else
1304            this->setstate(ios_base::failbit);
1305#ifndef _LIBCPP_NO_EXCEPTIONS
1306    }
1307    catch (...)
1308    {
1309        this->__set_badbit_and_consider_rethrow();
1310    }
1311#endif  // _LIBCPP_NO_EXCEPTIONS
1312    return *this;
1313}
1314
1315template<class _CharT, class _Traits>
1316basic_istream<_CharT, _Traits>&
1317basic_istream<_CharT, _Traits>::unget()
1318{
1319    __gc_ = 0;
1320#ifndef _LIBCPP_NO_EXCEPTIONS
1321    try
1322    {
1323#endif  // _LIBCPP_NO_EXCEPTIONS
1324        sentry __sen(*this, true);
1325        if (__sen)
1326        {
1327            if (this->rdbuf() == 0 || this->rdbuf()->sungetc() == traits_type::eof())
1328                this->setstate(ios_base::badbit);
1329        }
1330        else
1331            this->setstate(ios_base::failbit);
1332#ifndef _LIBCPP_NO_EXCEPTIONS
1333    }
1334    catch (...)
1335    {
1336        this->__set_badbit_and_consider_rethrow();
1337    }
1338#endif  // _LIBCPP_NO_EXCEPTIONS
1339    return *this;
1340}
1341
1342template<class _CharT, class _Traits>
1343int
1344basic_istream<_CharT, _Traits>::sync()
1345{
1346    int __r = 0;
1347#ifndef _LIBCPP_NO_EXCEPTIONS
1348    try
1349    {
1350#endif  // _LIBCPP_NO_EXCEPTIONS
1351        sentry __sen(*this, true);
1352        if (__sen)
1353        {
1354            if (this->rdbuf() == 0)
1355                return -1;
1356            if (this->rdbuf()->pubsync() == -1)
1357            {
1358                this->setstate(ios_base::badbit);
1359                return -1;
1360            }
1361        }
1362#ifndef _LIBCPP_NO_EXCEPTIONS
1363    }
1364    catch (...)
1365    {
1366        this->__set_badbit_and_consider_rethrow();
1367    }
1368#endif  // _LIBCPP_NO_EXCEPTIONS
1369    return __r;
1370}
1371
1372template<class _CharT, class _Traits>
1373typename basic_istream<_CharT, _Traits>::pos_type
1374basic_istream<_CharT, _Traits>::tellg()
1375{
1376    pos_type __r(-1);
1377#ifndef _LIBCPP_NO_EXCEPTIONS
1378    try
1379    {
1380#endif  // _LIBCPP_NO_EXCEPTIONS
1381        sentry __sen(*this, true);
1382        if (__sen)
1383            __r = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::in);
1384#ifndef _LIBCPP_NO_EXCEPTIONS
1385    }
1386    catch (...)
1387    {
1388        this->__set_badbit_and_consider_rethrow();
1389    }
1390#endif  // _LIBCPP_NO_EXCEPTIONS
1391    return __r;
1392}
1393
1394template<class _CharT, class _Traits>
1395basic_istream<_CharT, _Traits>&
1396basic_istream<_CharT, _Traits>::seekg(pos_type __pos)
1397{
1398#ifndef _LIBCPP_NO_EXCEPTIONS
1399    try
1400    {
1401#endif  // _LIBCPP_NO_EXCEPTIONS
1402        sentry __sen(*this, true);
1403        if (__sen)
1404            if (this->rdbuf()->pubseekpos(__pos, ios_base::in) == pos_type(-1))
1405                this->setstate(ios_base::failbit);
1406#ifndef _LIBCPP_NO_EXCEPTIONS
1407    }
1408    catch (...)
1409    {
1410        this->__set_badbit_and_consider_rethrow();
1411    }
1412#endif  // _LIBCPP_NO_EXCEPTIONS
1413    return *this;
1414}
1415
1416template<class _CharT, class _Traits>
1417basic_istream<_CharT, _Traits>&
1418basic_istream<_CharT, _Traits>::seekg(off_type __off, ios_base::seekdir __dir)
1419{
1420#ifndef _LIBCPP_NO_EXCEPTIONS
1421    try
1422    {
1423#endif  // _LIBCPP_NO_EXCEPTIONS
1424        sentry __sen(*this, true);
1425        if (__sen)
1426            this->rdbuf()->pubseekoff(__off, __dir, ios_base::in);
1427#ifndef _LIBCPP_NO_EXCEPTIONS
1428    }
1429    catch (...)
1430    {
1431        this->__set_badbit_and_consider_rethrow();
1432    }
1433#endif  // _LIBCPP_NO_EXCEPTIONS
1434    return *this;
1435}
1436
1437template <class _CharT, class _Traits>
1438basic_istream<_CharT, _Traits>&
1439ws(basic_istream<_CharT, _Traits>& __is)
1440{
1441#ifndef _LIBCPP_NO_EXCEPTIONS
1442    try
1443    {
1444#endif  // _LIBCPP_NO_EXCEPTIONS
1445        typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1446        if (__sen)
1447        {
1448            typedef istreambuf_iterator<_CharT, _Traits> _I;
1449            const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
1450            _I __i(__is);
1451            _I __eof;
1452            for (; __i != __eof; ++__i)
1453                if (!__ct.is(__ct.space, *__i))
1454                    break;
1455            if (__i == __eof)
1456                __is.setstate(ios_base::failbit | ios_base::eofbit);
1457        }
1458#ifndef _LIBCPP_NO_EXCEPTIONS
1459    }
1460    catch (...)
1461    {
1462        __is.__set_badbit_and_consider_rethrow();
1463    }
1464#endif  // _LIBCPP_NO_EXCEPTIONS
1465    return __is;
1466}
1467
1468#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1469
1470template <class _CharT, class _Traits, class _Tp>
1471inline _LIBCPP_INLINE_VISIBILITY
1472basic_istream<_CharT, _Traits>&
1473operator>>(basic_istream<_CharT, _Traits>&& __is, _Tp& __x)
1474{
1475    __is >> __x;
1476    return __is;
1477}
1478
1479#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1480
1481template <class _CharT, class _Traits>
1482class _LIBCPP_VISIBLE basic_iostream
1483    : public basic_istream<_CharT, _Traits>,
1484      public basic_ostream<_CharT, _Traits>
1485{
1486public:
1487    // types:
1488    typedef _CharT                         char_type;
1489    typedef _Traits                        traits_type;
1490    typedef typename traits_type::int_type int_type;
1491    typedef typename traits_type::pos_type pos_type;
1492    typedef typename traits_type::off_type off_type;
1493
1494    // constructor/destructor
1495    explicit basic_iostream(basic_streambuf<char_type, traits_type>* __sb);
1496    virtual ~basic_iostream();
1497protected:
1498#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1499    basic_iostream(basic_iostream&& __rhs);
1500#endif
1501
1502    // assign/swap
1503#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1504    basic_iostream& operator=(basic_iostream&& __rhs);
1505#endif
1506    void swap(basic_iostream& __rhs);
1507public:
1508};
1509
1510template <class _CharT, class _Traits>
1511inline _LIBCPP_INLINE_VISIBILITY
1512basic_iostream<_CharT, _Traits>::basic_iostream(basic_streambuf<char_type, traits_type>* __sb)
1513    : basic_istream<_CharT, _Traits>(__sb)
1514{
1515}
1516
1517#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1518
1519template <class _CharT, class _Traits>
1520inline _LIBCPP_INLINE_VISIBILITY
1521basic_iostream<_CharT, _Traits>::basic_iostream(basic_iostream&& __rhs)
1522    : basic_istream<_CharT, _Traits>(_STD::move(__rhs))
1523{
1524}
1525
1526template <class _CharT, class _Traits>
1527inline _LIBCPP_INLINE_VISIBILITY
1528basic_iostream<_CharT, _Traits>&
1529basic_iostream<_CharT, _Traits>::operator=(basic_iostream&& __rhs)
1530{
1531    swap(__rhs);
1532    return *this;
1533}
1534
1535#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1536
1537template <class _CharT, class _Traits>
1538basic_iostream<_CharT, _Traits>::~basic_iostream()
1539{
1540}
1541
1542template <class _CharT, class _Traits>
1543inline _LIBCPP_INLINE_VISIBILITY
1544void
1545basic_iostream<_CharT, _Traits>::swap(basic_iostream& __rhs)
1546{
1547    basic_istream<char_type, traits_type>::swap(__rhs);
1548}
1549
1550template<class _CharT, class _Traits, class _Allocator>
1551basic_istream<_CharT, _Traits>&
1552operator>>(basic_istream<_CharT, _Traits>& __is,
1553           basic_string<_CharT, _Traits, _Allocator>& __str)
1554{
1555#ifndef _LIBCPP_NO_EXCEPTIONS
1556    try
1557    {
1558#endif  // _LIBCPP_NO_EXCEPTIONS
1559        typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1560        if (__sen)
1561        {
1562            __str.clear();
1563            typedef istreambuf_iterator<_CharT, _Traits> _I;
1564            streamsize __n = __is.width();
1565            if (__n == 0)
1566                __n = __str.max_size();
1567            if (__n < 0)
1568                __n = numeric_limits<streamsize>::max();
1569            streamsize __c = 0;
1570            const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
1571            _I __i(__is);
1572            _I __eof;
1573            for (; __i != __eof && __c < __n; ++__i, ++__c)
1574            {
1575                _CharT __ch = *__i;
1576                if (__ct.is(__ct.space, __ch))
1577                    break;
1578                __str.push_back(__ch);
1579            }
1580            __is.width(0);
1581            ios_base::iostate __err = ios_base::goodbit;
1582            if (__i == __eof)
1583               __err |= ios_base::eofbit;
1584            if (__c == 0)
1585               __err |= ios_base::failbit;
1586            __is.setstate(__err);
1587        }
1588        else
1589            __is.setstate(ios_base::failbit);
1590#ifndef _LIBCPP_NO_EXCEPTIONS
1591    }
1592    catch (...)
1593    {
1594        __is.__set_badbit_and_consider_rethrow();
1595    }
1596#endif  // _LIBCPP_NO_EXCEPTIONS
1597    return __is;
1598}
1599
1600template<class _CharT, class _Traits, class _Allocator>
1601basic_istream<_CharT, _Traits>&
1602getline(basic_istream<_CharT, _Traits>& __is,
1603        basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
1604{
1605#ifndef _LIBCPP_NO_EXCEPTIONS
1606    try
1607    {
1608#endif  // _LIBCPP_NO_EXCEPTIONS
1609        typename basic_istream<_CharT, _Traits>::sentry __sen(__is, true);
1610        if (__sen)
1611        {
1612            __str.clear();
1613            streamsize __c = 0;
1614            typedef istreambuf_iterator<_CharT, _Traits> _I;
1615            _I __i(__is);
1616            _I __eof;
1617            streamsize __n = __str.max_size();
1618            if (__n < 0)
1619                __n = numeric_limits<streamsize>::max();
1620            for (; __i != __eof;)
1621            {
1622                _CharT __ch = *__i;
1623                ++__i;
1624                ++__c;
1625                if (_Traits::eq(__ch, __dlm))
1626                    break;
1627                if (__c == __n)
1628                {
1629                    __is.setstate(ios_base::failbit);
1630                    break;
1631                }
1632                __str.push_back(__ch);
1633            }
1634            ios_base::iostate __err = ios_base::goodbit;
1635            if (__i == __eof)
1636               __err |= ios_base::eofbit;
1637            if (__c == 0)
1638               __err |= ios_base::failbit;
1639            __is.setstate(__err);
1640        }
1641#ifndef _LIBCPP_NO_EXCEPTIONS
1642    }
1643    catch (...)
1644    {
1645        __is.__set_badbit_and_consider_rethrow();
1646    }
1647#endif  // _LIBCPP_NO_EXCEPTIONS
1648    return __is;
1649}
1650
1651template<class _CharT, class _Traits, class _Allocator>
1652inline _LIBCPP_INLINE_VISIBILITY
1653basic_istream<_CharT, _Traits>&
1654getline(basic_istream<_CharT, _Traits>& __is,
1655        basic_string<_CharT, _Traits, _Allocator>& __str)
1656{
1657    return getline(__is, __str, __is.widen('\n'));
1658}
1659
1660#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1661
1662template<class _CharT, class _Traits, class _Allocator>
1663inline _LIBCPP_INLINE_VISIBILITY
1664basic_istream<_CharT, _Traits>&
1665getline(basic_istream<_CharT, _Traits>&& __is,
1666        basic_string<_CharT, _Traits, _Allocator>& __str, _CharT __dlm)
1667{
1668    return getline(__is, __str, __dlm);
1669}
1670
1671template<class _CharT, class _Traits, class _Allocator>
1672inline _LIBCPP_INLINE_VISIBILITY
1673basic_istream<_CharT, _Traits>&
1674getline(basic_istream<_CharT, _Traits>&& __is,
1675        basic_string<_CharT, _Traits, _Allocator>& __str)
1676{
1677    return getline(__is, __str, __is.widen('\n'));
1678}
1679
1680#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1681
1682template <class _CharT, class _Traits, size_t _Size>
1683basic_istream<_CharT, _Traits>&
1684operator>>(basic_istream<_CharT, _Traits>& __is, bitset<_Size>& __x)
1685{
1686#ifndef _LIBCPP_NO_EXCEPTIONS
1687    try
1688    {
1689#endif  // _LIBCPP_NO_EXCEPTIONS
1690        typename basic_istream<_CharT, _Traits>::sentry __sen(__is);
1691        if (__sen)
1692        {
1693            basic_string<_CharT, _Traits> __str;
1694            const ctype<_CharT>& __ct = use_facet<ctype<_CharT> >(__is.getloc());
1695            typedef istreambuf_iterator<_CharT, _Traits> _I;
1696            streamsize __c = 0;
1697            _CharT __zero = __ct.widen('0');
1698            _CharT __one = __ct.widen('1');
1699            _I __i(__is);
1700            _I __eof;
1701            for (; __i != __eof && __c < _Size; ++__i, ++__c)
1702            {
1703                _CharT __ch = *__i;
1704                if (__ch != __zero && __ch != __one)
1705                    break;
1706                __str.push_back(__ch);
1707            }
1708            __is.width(0);
1709            __x = bitset<_Size>(__str);
1710            ios_base::iostate __err = ios_base::goodbit;
1711            if (__i == __eof)
1712               __err |= ios_base::eofbit;
1713            if (__c == 0)
1714               __err |= ios_base::failbit;
1715            __is.setstate(__err);
1716        }
1717        else
1718            __is.setstate(ios_base::failbit);
1719#ifndef _LIBCPP_NO_EXCEPTIONS
1720    }
1721    catch (...)
1722    {
1723        __is.__set_badbit_and_consider_rethrow();
1724    }
1725#endif  // _LIBCPP_NO_EXCEPTIONS
1726    return __is;
1727}
1728
1729extern template class basic_istream<char>;
1730extern template class basic_istream<wchar_t>;
1731extern template class basic_iostream<char>;
1732
1733_LIBCPP_END_NAMESPACE_STD
1734
1735#endif  // _LIBCPP_ISTREAM
1736