1// -*- C++ -*-
2//===-------------------------- ostream -----------------------------------===//
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_OSTREAM
12#define _LIBCPP_OSTREAM
13
14/*
15    ostream synopsis
16
17template <class charT, class traits = char_traits<charT> >
18class basic_ostream
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.2.2 Constructor/destructor:
30    explicit basic_ostream(basic_streambuf<char_type,traits>* sb);
31    basic_ostream(basic_ostream&& rhs);
32    virtual ~basic_ostream();
33
34    // 27.7.2.3 Assign/swap
35    basic_ostream& operator=(const basic_ostream& rhs) = delete; // C++14
36    basic_ostream& operator=(basic_ostream&& rhs);
37    void swap(basic_ostream& rhs);
38
39    // 27.7.2.4 Prefix/suffix:
40    class sentry;
41
42    // 27.7.2.6 Formatted output:
43    basic_ostream& operator<<(basic_ostream& (*pf)(basic_ostream&));
44    basic_ostream& operator<<(basic_ios<charT, traits>& (*pf)(basic_ios<charT,traits>&));
45    basic_ostream& operator<<(ios_base& (*pf)(ios_base&));
46    basic_ostream& operator<<(bool n);
47    basic_ostream& operator<<(short n);
48    basic_ostream& operator<<(unsigned short n);
49    basic_ostream& operator<<(int n);
50    basic_ostream& operator<<(unsigned int n);
51    basic_ostream& operator<<(long n);
52    basic_ostream& operator<<(unsigned long n);
53    basic_ostream& operator<<(long long n);
54    basic_ostream& operator<<(unsigned long long n);
55    basic_ostream& operator<<(float f);
56    basic_ostream& operator<<(double f);
57    basic_ostream& operator<<(long double f);
58    basic_ostream& operator<<(const void* p);
59    basic_ostream& operator<<(basic_streambuf<char_type,traits>* sb);
60
61    // 27.7.2.7 Unformatted output:
62    basic_ostream& put(char_type c);
63    basic_ostream& write(const char_type* s, streamsize n);
64    basic_ostream& flush();
65
66    // 27.7.2.5 seeks:
67    pos_type tellp();
68    basic_ostream& seekp(pos_type);
69    basic_ostream& seekp(off_type, ios_base::seekdir);
70};
71
72// 27.7.2.6.4 character inserters
73
74template<class charT, class traits>
75  basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, charT);
76
77template<class charT, class traits>
78  basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, char);
79
80template<class traits>
81  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, char);
82
83// signed and unsigned
84
85template<class traits>
86  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, signed char);
87
88template<class traits>
89  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, unsigned char);
90
91// NTBS
92template<class charT, class traits>
93  basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const charT*);
94
95template<class charT, class traits>
96  basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const char*);
97
98template<class traits>
99  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const char*);
100
101// signed and unsigned
102template<class traits>
103basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const signed char*);
104
105template<class traits>
106  basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const unsigned char*);
107
108// swap:
109template <class charT, class traits>
110  void swap(basic_ostream<charT, traits>& x, basic_ostream<charT, traits>& y);
111
112template <class charT, class traits>
113  basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os);
114
115template <class charT, class traits>
116  basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os);
117
118template <class charT, class traits>
119  basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os);
120
121// rvalue stream insertion
122template <class charT, class traits, class T>
123  basic_ostream<charT, traits>&
124  operator<<(basic_ostream<charT, traits>&& os, const T& x);
125
126}  // std
127
128*/
129
130#include <__config>
131#include <ios>
132#include <streambuf>
133#include <locale>
134#include <iterator>
135#include <bitset>
136
137#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
138#pragma GCC system_header
139#endif
140
141_LIBCPP_BEGIN_NAMESPACE_STD
142
143template <class _CharT, class _Traits>
144class _LIBCPP_TYPE_VIS_ONLY basic_ostream
145    : virtual public basic_ios<_CharT, _Traits>
146{
147public:
148    // types (inherited from basic_ios (27.5.4)):
149    typedef _CharT                         char_type;
150    typedef _Traits                        traits_type;
151    typedef typename traits_type::int_type int_type;
152    typedef typename traits_type::pos_type pos_type;
153    typedef typename traits_type::off_type off_type;
154
155    // 27.7.2.2 Constructor/destructor:
156    explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb);
157    virtual ~basic_ostream();
158protected:
159#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
160    _LIBCPP_INLINE_VISIBILITY
161    basic_ostream(basic_ostream&& __rhs);
162#endif
163
164    // 27.7.2.3 Assign/swap
165#if _LIBCPP_STD_VER > 11
166    basic_ostream& operator=(const basic_ostream&) = delete;
167#endif
168#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
169    _LIBCPP_INLINE_VISIBILITY
170    basic_ostream& operator=(basic_ostream&& __rhs);
171#endif
172    void swap(basic_ostream& __rhs);
173public:
174
175    // 27.7.2.4 Prefix/suffix:
176    class _LIBCPP_TYPE_VIS_ONLY sentry;
177
178    // 27.7.2.6 Formatted output:
179    basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&));
180    basic_ostream& operator<<(basic_ios<char_type, traits_type>&
181                              (*__pf)(basic_ios<char_type,traits_type>&));
182    basic_ostream& operator<<(ios_base& (*__pf)(ios_base&));
183    basic_ostream& operator<<(bool __n);
184    basic_ostream& operator<<(short __n);
185    basic_ostream& operator<<(unsigned short __n);
186    basic_ostream& operator<<(int __n);
187    basic_ostream& operator<<(unsigned int __n);
188    basic_ostream& operator<<(long __n);
189    basic_ostream& operator<<(unsigned long __n);
190    basic_ostream& operator<<(long long __n);
191    basic_ostream& operator<<(unsigned long long __n);
192    basic_ostream& operator<<(float __f);
193    basic_ostream& operator<<(double __f);
194    basic_ostream& operator<<(long double __f);
195    basic_ostream& operator<<(const void* __p);
196    basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
197
198    // 27.7.2.7 Unformatted output:
199    basic_ostream& put(char_type __c);
200    basic_ostream& write(const char_type* __s, streamsize __n);
201    basic_ostream& flush();
202
203    // 27.7.2.5 seeks:
204    pos_type tellp();
205    basic_ostream& seekp(pos_type __pos);
206    basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
207
208protected:
209    _LIBCPP_ALWAYS_INLINE
210    basic_ostream() {}  // extension, intentially does not initialize
211};
212
213template <class _CharT, class _Traits>
214class _LIBCPP_TYPE_VIS_ONLY basic_ostream<_CharT, _Traits>::sentry
215{
216    bool __ok_;
217    basic_ostream<_CharT, _Traits>& __os_;
218
219    sentry(const sentry&); // = delete;
220    sentry& operator=(const sentry&); // = delete;
221
222public:
223    explicit sentry(basic_ostream<_CharT, _Traits>& __os);
224    ~sentry();
225
226    _LIBCPP_ALWAYS_INLINE
227        _LIBCPP_EXPLICIT
228        operator bool() const {return __ok_;}
229};
230
231template <class _CharT, class _Traits>
232basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os)
233    : __ok_(false),
234      __os_(__os)
235{
236    if (__os.good())
237    {
238        if (__os.tie())
239            __os.tie()->flush();
240        __ok_ = true;
241    }
242}
243
244template <class _CharT, class _Traits>
245basic_ostream<_CharT, _Traits>::sentry::~sentry()
246{
247    if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf)
248                      && !uncaught_exception())
249    {
250#ifndef _LIBCPP_NO_EXCEPTIONS
251        try
252        {
253#endif  // _LIBCPP_NO_EXCEPTIONS
254            if (__os_.rdbuf()->pubsync() == -1)
255                __os_.setstate(ios_base::badbit);
256#ifndef _LIBCPP_NO_EXCEPTIONS
257        }
258        catch (...)
259        {
260        }
261#endif  // _LIBCPP_NO_EXCEPTIONS
262    }
263}
264
265template <class _CharT, class _Traits>
266inline _LIBCPP_INLINE_VISIBILITY
267basic_ostream<_CharT, _Traits>::basic_ostream(basic_streambuf<char_type, traits_type>* __sb)
268{
269    this->init(__sb);
270}
271
272#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
273
274template <class _CharT, class _Traits>
275inline _LIBCPP_INLINE_VISIBILITY
276basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs)
277{
278    this->move(__rhs);
279}
280
281template <class _CharT, class _Traits>
282inline _LIBCPP_INLINE_VISIBILITY
283basic_ostream<_CharT, _Traits>&
284basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs)
285{
286    swap(__rhs);
287    return *this;
288}
289
290#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
291
292template <class _CharT, class _Traits>
293basic_ostream<_CharT, _Traits>::~basic_ostream()
294{
295}
296
297template <class _CharT, class _Traits>
298inline _LIBCPP_INLINE_VISIBILITY
299void
300basic_ostream<_CharT, _Traits>::swap(basic_ostream& __rhs)
301{
302    basic_ios<char_type, traits_type>::swap(__rhs);
303}
304
305template <class _CharT, class _Traits>
306inline _LIBCPP_INLINE_VISIBILITY
307basic_ostream<_CharT, _Traits>&
308basic_ostream<_CharT, _Traits>::operator<<(basic_ostream& (*__pf)(basic_ostream&))
309{
310    return __pf(*this);
311}
312
313template <class _CharT, class _Traits>
314inline _LIBCPP_INLINE_VISIBILITY
315basic_ostream<_CharT, _Traits>&
316basic_ostream<_CharT, _Traits>::operator<<(basic_ios<char_type, traits_type>&
317                                           (*__pf)(basic_ios<char_type,traits_type>&))
318{
319    __pf(*this);
320    return *this;
321}
322
323template <class _CharT, class _Traits>
324inline _LIBCPP_INLINE_VISIBILITY
325basic_ostream<_CharT, _Traits>&
326basic_ostream<_CharT, _Traits>::operator<<(ios_base& (*__pf)(ios_base&))
327{
328    __pf(*this);
329    return *this;
330}
331
332template <class _CharT, class _Traits>
333basic_ostream<_CharT, _Traits>&
334basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb)
335{
336#ifndef _LIBCPP_NO_EXCEPTIONS
337    try
338    {
339#endif  // _LIBCPP_NO_EXCEPTIONS
340        sentry __s(*this);
341        if (__s)
342        {
343            if (__sb)
344            {
345#ifndef _LIBCPP_NO_EXCEPTIONS
346                try
347                {
348#endif  // _LIBCPP_NO_EXCEPTIONS
349                    typedef istreambuf_iterator<_CharT, _Traits> _Ip;
350                    typedef ostreambuf_iterator<_CharT, _Traits> _Op;
351                    _Ip __i(__sb);
352                    _Ip __eof;
353                    _Op __o(*this);
354                    size_t __c = 0;
355                    for (; __i != __eof; ++__i, ++__o, ++__c)
356                    {
357                        *__o = *__i;
358                        if (__o.failed())
359                            break;
360                    }
361                    if (__c == 0)
362                        this->setstate(ios_base::failbit);
363#ifndef _LIBCPP_NO_EXCEPTIONS
364                }
365                catch (...)
366                {
367                    this->__set_failbit_and_consider_rethrow();
368                }
369#endif  // _LIBCPP_NO_EXCEPTIONS
370            }
371            else
372                this->setstate(ios_base::badbit);
373        }
374#ifndef _LIBCPP_NO_EXCEPTIONS
375    }
376    catch (...)
377    {
378        this->__set_badbit_and_consider_rethrow();
379    }
380#endif  // _LIBCPP_NO_EXCEPTIONS
381    return *this;
382}
383
384template <class _CharT, class _Traits>
385basic_ostream<_CharT, _Traits>&
386basic_ostream<_CharT, _Traits>::operator<<(bool __n)
387{
388#ifndef _LIBCPP_NO_EXCEPTIONS
389    try
390    {
391#endif  // _LIBCPP_NO_EXCEPTIONS
392        sentry __s(*this);
393        if (__s)
394        {
395            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
396            const _Fp& __f = use_facet<_Fp>(this->getloc());
397            if (__f.put(*this, *this, this->fill(), __n).failed())
398                this->setstate(ios_base::badbit | ios_base::failbit);
399        }
400#ifndef _LIBCPP_NO_EXCEPTIONS
401    }
402    catch (...)
403    {
404        this->__set_badbit_and_consider_rethrow();
405    }
406#endif  // _LIBCPP_NO_EXCEPTIONS
407    return *this;
408}
409
410template <class _CharT, class _Traits>
411basic_ostream<_CharT, _Traits>&
412basic_ostream<_CharT, _Traits>::operator<<(short __n)
413{
414#ifndef _LIBCPP_NO_EXCEPTIONS
415    try
416    {
417#endif  // _LIBCPP_NO_EXCEPTIONS
418        sentry __s(*this);
419        if (__s)
420        {
421            ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
422            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
423            const _Fp& __f = use_facet<_Fp>(this->getloc());
424            if (__f.put(*this, *this, this->fill(),
425                        __flags == ios_base::oct || __flags == ios_base::hex ?
426                        static_cast<long>(static_cast<unsigned short>(__n))  :
427                        static_cast<long>(__n)).failed())
428                this->setstate(ios_base::badbit | ios_base::failbit);
429        }
430#ifndef _LIBCPP_NO_EXCEPTIONS
431    }
432    catch (...)
433    {
434        this->__set_badbit_and_consider_rethrow();
435    }
436#endif  // _LIBCPP_NO_EXCEPTIONS
437    return *this;
438}
439
440template <class _CharT, class _Traits>
441basic_ostream<_CharT, _Traits>&
442basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n)
443{
444#ifndef _LIBCPP_NO_EXCEPTIONS
445    try
446    {
447#endif  // _LIBCPP_NO_EXCEPTIONS
448        sentry __s(*this);
449        if (__s)
450        {
451            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
452            const _Fp& __f = use_facet<_Fp>(this->getloc());
453            if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
454                this->setstate(ios_base::badbit | ios_base::failbit);
455        }
456#ifndef _LIBCPP_NO_EXCEPTIONS
457    }
458    catch (...)
459    {
460        this->__set_badbit_and_consider_rethrow();
461    }
462#endif  // _LIBCPP_NO_EXCEPTIONS
463    return *this;
464}
465
466template <class _CharT, class _Traits>
467basic_ostream<_CharT, _Traits>&
468basic_ostream<_CharT, _Traits>::operator<<(int __n)
469{
470#ifndef _LIBCPP_NO_EXCEPTIONS
471    try
472    {
473#endif  // _LIBCPP_NO_EXCEPTIONS
474        sentry __s(*this);
475        if (__s)
476        {
477            ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
478            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
479            const _Fp& __f = use_facet<_Fp>(this->getloc());
480            if (__f.put(*this, *this, this->fill(),
481                        __flags == ios_base::oct || __flags == ios_base::hex ?
482                        static_cast<long>(static_cast<unsigned int>(__n))  :
483                        static_cast<long>(__n)).failed())
484                this->setstate(ios_base::badbit | ios_base::failbit);
485        }
486#ifndef _LIBCPP_NO_EXCEPTIONS
487    }
488    catch (...)
489    {
490        this->__set_badbit_and_consider_rethrow();
491    }
492#endif  // _LIBCPP_NO_EXCEPTIONS
493    return *this;
494}
495
496template <class _CharT, class _Traits>
497basic_ostream<_CharT, _Traits>&
498basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n)
499{
500#ifndef _LIBCPP_NO_EXCEPTIONS
501    try
502    {
503#endif  // _LIBCPP_NO_EXCEPTIONS
504        sentry __s(*this);
505        if (__s)
506        {
507            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
508            const _Fp& __f = use_facet<_Fp>(this->getloc());
509            if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
510                this->setstate(ios_base::badbit | ios_base::failbit);
511        }
512#ifndef _LIBCPP_NO_EXCEPTIONS
513    }
514    catch (...)
515    {
516        this->__set_badbit_and_consider_rethrow();
517    }
518#endif  // _LIBCPP_NO_EXCEPTIONS
519    return *this;
520}
521
522template <class _CharT, class _Traits>
523basic_ostream<_CharT, _Traits>&
524basic_ostream<_CharT, _Traits>::operator<<(long __n)
525{
526#ifndef _LIBCPP_NO_EXCEPTIONS
527    try
528    {
529#endif  // _LIBCPP_NO_EXCEPTIONS
530        sentry __s(*this);
531        if (__s)
532        {
533            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
534            const _Fp& __f = use_facet<_Fp>(this->getloc());
535            if (__f.put(*this, *this, this->fill(), __n).failed())
536                this->setstate(ios_base::badbit | ios_base::failbit);
537        }
538#ifndef _LIBCPP_NO_EXCEPTIONS
539    }
540    catch (...)
541    {
542        this->__set_badbit_and_consider_rethrow();
543    }
544#endif  // _LIBCPP_NO_EXCEPTIONS
545    return *this;
546}
547
548template <class _CharT, class _Traits>
549basic_ostream<_CharT, _Traits>&
550basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
551{
552#ifndef _LIBCPP_NO_EXCEPTIONS
553    try
554    {
555#endif  // _LIBCPP_NO_EXCEPTIONS
556        sentry __s(*this);
557        if (__s)
558        {
559            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
560            const _Fp& __f = use_facet<_Fp>(this->getloc());
561            if (__f.put(*this, *this, this->fill(), __n).failed())
562                this->setstate(ios_base::badbit | ios_base::failbit);
563        }
564#ifndef _LIBCPP_NO_EXCEPTIONS
565    }
566    catch (...)
567    {
568        this->__set_badbit_and_consider_rethrow();
569    }
570#endif  // _LIBCPP_NO_EXCEPTIONS
571    return *this;
572}
573
574template <class _CharT, class _Traits>
575basic_ostream<_CharT, _Traits>&
576basic_ostream<_CharT, _Traits>::operator<<(long long __n)
577{
578#ifndef _LIBCPP_NO_EXCEPTIONS
579    try
580    {
581#endif  // _LIBCPP_NO_EXCEPTIONS
582        sentry __s(*this);
583        if (__s)
584        {
585            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
586            const _Fp& __f = use_facet<_Fp>(this->getloc());
587            if (__f.put(*this, *this, this->fill(), __n).failed())
588                this->setstate(ios_base::badbit | ios_base::failbit);
589        }
590#ifndef _LIBCPP_NO_EXCEPTIONS
591    }
592    catch (...)
593    {
594        this->__set_badbit_and_consider_rethrow();
595    }
596#endif  // _LIBCPP_NO_EXCEPTIONS
597    return *this;
598}
599
600template <class _CharT, class _Traits>
601basic_ostream<_CharT, _Traits>&
602basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
603{
604#ifndef _LIBCPP_NO_EXCEPTIONS
605    try
606    {
607#endif  // _LIBCPP_NO_EXCEPTIONS
608        sentry __s(*this);
609        if (__s)
610        {
611            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
612            const _Fp& __f = use_facet<_Fp>(this->getloc());
613            if (__f.put(*this, *this, this->fill(), __n).failed())
614                this->setstate(ios_base::badbit | ios_base::failbit);
615        }
616#ifndef _LIBCPP_NO_EXCEPTIONS
617    }
618    catch (...)
619    {
620        this->__set_badbit_and_consider_rethrow();
621    }
622#endif  // _LIBCPP_NO_EXCEPTIONS
623    return *this;
624}
625
626template <class _CharT, class _Traits>
627basic_ostream<_CharT, _Traits>&
628basic_ostream<_CharT, _Traits>::operator<<(float __n)
629{
630#ifndef _LIBCPP_NO_EXCEPTIONS
631    try
632    {
633#endif  // _LIBCPP_NO_EXCEPTIONS
634        sentry __s(*this);
635        if (__s)
636        {
637            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
638            const _Fp& __f = use_facet<_Fp>(this->getloc());
639            if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
640                this->setstate(ios_base::badbit | ios_base::failbit);
641        }
642#ifndef _LIBCPP_NO_EXCEPTIONS
643    }
644    catch (...)
645    {
646        this->__set_badbit_and_consider_rethrow();
647    }
648#endif  // _LIBCPP_NO_EXCEPTIONS
649    return *this;
650}
651
652template <class _CharT, class _Traits>
653basic_ostream<_CharT, _Traits>&
654basic_ostream<_CharT, _Traits>::operator<<(double __n)
655{
656#ifndef _LIBCPP_NO_EXCEPTIONS
657    try
658    {
659#endif  // _LIBCPP_NO_EXCEPTIONS
660        sentry __s(*this);
661        if (__s)
662        {
663            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
664            const _Fp& __f = use_facet<_Fp>(this->getloc());
665            if (__f.put(*this, *this, this->fill(), __n).failed())
666                this->setstate(ios_base::badbit | ios_base::failbit);
667        }
668#ifndef _LIBCPP_NO_EXCEPTIONS
669    }
670    catch (...)
671    {
672        this->__set_badbit_and_consider_rethrow();
673    }
674#endif  // _LIBCPP_NO_EXCEPTIONS
675    return *this;
676}
677
678template <class _CharT, class _Traits>
679basic_ostream<_CharT, _Traits>&
680basic_ostream<_CharT, _Traits>::operator<<(long double __n)
681{
682#ifndef _LIBCPP_NO_EXCEPTIONS
683    try
684    {
685#endif  // _LIBCPP_NO_EXCEPTIONS
686        sentry __s(*this);
687        if (__s)
688        {
689            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
690            const _Fp& __f = use_facet<_Fp>(this->getloc());
691            if (__f.put(*this, *this, this->fill(), __n).failed())
692                this->setstate(ios_base::badbit | ios_base::failbit);
693        }
694#ifndef _LIBCPP_NO_EXCEPTIONS
695    }
696    catch (...)
697    {
698        this->__set_badbit_and_consider_rethrow();
699    }
700#endif  // _LIBCPP_NO_EXCEPTIONS
701    return *this;
702}
703
704template <class _CharT, class _Traits>
705basic_ostream<_CharT, _Traits>&
706basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
707{
708#ifndef _LIBCPP_NO_EXCEPTIONS
709    try
710    {
711#endif  // _LIBCPP_NO_EXCEPTIONS
712        sentry __s(*this);
713        if (__s)
714        {
715            typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
716            const _Fp& __f = use_facet<_Fp>(this->getloc());
717            if (__f.put(*this, *this, this->fill(), __n).failed())
718                this->setstate(ios_base::badbit | ios_base::failbit);
719        }
720#ifndef _LIBCPP_NO_EXCEPTIONS
721    }
722    catch (...)
723    {
724        this->__set_badbit_and_consider_rethrow();
725    }
726#endif  // _LIBCPP_NO_EXCEPTIONS
727    return *this;
728}
729
730template<class _CharT, class _Traits>
731basic_ostream<_CharT, _Traits>&
732__put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
733                          const _CharT* __str, size_t __len)
734{
735#ifndef _LIBCPP_NO_EXCEPTIONS
736    try
737    {
738#endif  // _LIBCPP_NO_EXCEPTIONS
739        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
740        if (__s)
741        {
742            typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
743            if (__pad_and_output(_Ip(__os),
744                                 __str,
745                                 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
746                                     __str + __len :
747                                     __str,
748                                 __str + __len,
749                                 __os,
750                                 __os.fill()).failed())
751                __os.setstate(ios_base::badbit | ios_base::failbit);
752        }
753#ifndef _LIBCPP_NO_EXCEPTIONS
754    }
755    catch (...)
756    {
757        __os.__set_badbit_and_consider_rethrow();
758    }
759#endif  // _LIBCPP_NO_EXCEPTIONS
760    return __os;
761}
762
763
764template<class _CharT, class _Traits>
765basic_ostream<_CharT, _Traits>&
766operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
767{
768    return _VSTD::__put_character_sequence(__os, &__c, 1);
769}
770
771template<class _CharT, class _Traits>
772basic_ostream<_CharT, _Traits>&
773operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
774{
775#ifndef _LIBCPP_NO_EXCEPTIONS
776    try
777    {
778#endif  // _LIBCPP_NO_EXCEPTIONS
779        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
780        if (__s)
781        {
782            _CharT __c = __os.widen(__cn);
783            typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
784            if (__pad_and_output(_Ip(__os),
785                                 &__c,
786                                 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
787                                     &__c + 1 :
788                                     &__c,
789                                 &__c + 1,
790                                 __os,
791                                 __os.fill()).failed())
792                __os.setstate(ios_base::badbit | ios_base::failbit);
793        }
794#ifndef _LIBCPP_NO_EXCEPTIONS
795    }
796    catch (...)
797    {
798        __os.__set_badbit_and_consider_rethrow();
799    }
800#endif  // _LIBCPP_NO_EXCEPTIONS
801    return __os;
802}
803
804template<class _Traits>
805basic_ostream<char, _Traits>&
806operator<<(basic_ostream<char, _Traits>& __os, char __c)
807{
808    return _VSTD::__put_character_sequence(__os, &__c, 1);
809}
810
811template<class _Traits>
812basic_ostream<char, _Traits>&
813operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
814{
815    return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
816}
817
818template<class _Traits>
819basic_ostream<char, _Traits>&
820operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
821{
822    return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
823}
824
825template<class _CharT, class _Traits>
826basic_ostream<_CharT, _Traits>&
827operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
828{
829    return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
830}
831
832template<class _CharT, class _Traits>
833basic_ostream<_CharT, _Traits>&
834operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn)
835{
836#ifndef _LIBCPP_NO_EXCEPTIONS
837    try
838    {
839#endif  // _LIBCPP_NO_EXCEPTIONS
840        typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
841        if (__s)
842        {
843            typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
844            size_t __len = char_traits<char>::length(__strn);
845            const int __bs = 100;
846            _CharT __wbb[__bs];
847            _CharT* __wb = __wbb;
848            unique_ptr<_CharT, void(*)(void*)> __h(0, free);
849            if (__len > __bs)
850            {
851                __wb = (_CharT*)malloc(__len*sizeof(_CharT));
852                if (__wb == 0)
853                    __throw_bad_alloc();
854                __h.reset(__wb);
855            }
856            for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
857                *__p = __os.widen(*__strn);
858            if (__pad_and_output(_Ip(__os),
859                                 __wb,
860                                 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
861                                     __wb + __len :
862                                     __wb,
863                                 __wb + __len,
864                                 __os,
865                                 __os.fill()).failed())
866                __os.setstate(ios_base::badbit | ios_base::failbit);
867        }
868#ifndef _LIBCPP_NO_EXCEPTIONS
869    }
870    catch (...)
871    {
872        __os.__set_badbit_and_consider_rethrow();
873    }
874#endif  // _LIBCPP_NO_EXCEPTIONS
875    return __os;
876}
877
878template<class _Traits>
879basic_ostream<char, _Traits>&
880operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
881{
882    return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
883}
884
885template<class _Traits>
886basic_ostream<char, _Traits>&
887operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
888{
889    const char *__s = (const char *) __str;
890    return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
891}
892
893template<class _Traits>
894basic_ostream<char, _Traits>&
895operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
896{
897    const char *__s = (const char *) __str;
898    return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
899}
900
901template <class _CharT, class _Traits>
902basic_ostream<_CharT, _Traits>&
903basic_ostream<_CharT, _Traits>::put(char_type __c)
904{
905#ifndef _LIBCPP_NO_EXCEPTIONS
906    try
907    {
908#endif  // _LIBCPP_NO_EXCEPTIONS
909        sentry __s(*this);
910        if (__s)
911        {
912            typedef ostreambuf_iterator<_CharT, _Traits> _Op;
913            _Op __o(*this);
914            *__o = __c;
915            if (__o.failed())
916                this->setstate(ios_base::badbit);
917        }
918#ifndef _LIBCPP_NO_EXCEPTIONS
919    }
920    catch (...)
921    {
922        this->__set_badbit_and_consider_rethrow();
923    }
924#endif  // _LIBCPP_NO_EXCEPTIONS
925    return *this;
926}
927
928template <class _CharT, class _Traits>
929basic_ostream<_CharT, _Traits>&
930basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n)
931{
932#ifndef _LIBCPP_NO_EXCEPTIONS
933    try
934    {
935#endif  // _LIBCPP_NO_EXCEPTIONS
936        sentry __sen(*this);
937        if (__sen && __n)
938        {
939            if (this->rdbuf()->sputn(__s, __n) != __n)
940                this->setstate(ios_base::badbit);
941        }
942#ifndef _LIBCPP_NO_EXCEPTIONS
943    }
944    catch (...)
945    {
946        this->__set_badbit_and_consider_rethrow();
947    }
948#endif  // _LIBCPP_NO_EXCEPTIONS
949    return *this;
950}
951
952template <class _CharT, class _Traits>
953basic_ostream<_CharT, _Traits>&
954basic_ostream<_CharT, _Traits>::flush()
955{
956#ifndef _LIBCPP_NO_EXCEPTIONS
957    try
958    {
959#endif  // _LIBCPP_NO_EXCEPTIONS
960        if (this->rdbuf())
961        {
962            sentry __s(*this);
963            if (__s)
964            {
965                if (this->rdbuf()->pubsync() == -1)
966                    this->setstate(ios_base::badbit);
967            }
968        }
969#ifndef _LIBCPP_NO_EXCEPTIONS
970    }
971    catch (...)
972    {
973        this->__set_badbit_and_consider_rethrow();
974    }
975#endif  // _LIBCPP_NO_EXCEPTIONS
976    return *this;
977}
978
979template <class _CharT, class _Traits>
980inline _LIBCPP_INLINE_VISIBILITY
981typename basic_ostream<_CharT, _Traits>::pos_type
982basic_ostream<_CharT, _Traits>::tellp()
983{
984    if (this->fail())
985        return pos_type(-1);
986    return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
987}
988
989template <class _CharT, class _Traits>
990inline _LIBCPP_INLINE_VISIBILITY
991basic_ostream<_CharT, _Traits>&
992basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
993{
994    sentry __s(*this);
995    if (__s)
996    {
997        if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
998            this->setstate(ios_base::failbit);
999    }
1000    return *this;
1001}
1002
1003template <class _CharT, class _Traits>
1004inline _LIBCPP_INLINE_VISIBILITY
1005basic_ostream<_CharT, _Traits>&
1006basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir)
1007{
1008    sentry __s(*this);
1009    if (__s)
1010    {
1011        if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
1012            this->setstate(ios_base::failbit);
1013    }
1014    return *this;
1015}
1016
1017template <class _CharT, class _Traits>
1018inline _LIBCPP_INLINE_VISIBILITY
1019basic_ostream<_CharT, _Traits>&
1020endl(basic_ostream<_CharT, _Traits>& __os)
1021{
1022    __os.put(__os.widen('\n'));
1023    __os.flush();
1024    return __os;
1025}
1026
1027template <class _CharT, class _Traits>
1028inline _LIBCPP_INLINE_VISIBILITY
1029basic_ostream<_CharT, _Traits>&
1030ends(basic_ostream<_CharT, _Traits>& __os)
1031{
1032    __os.put(_CharT());
1033    return __os;
1034}
1035
1036template <class _CharT, class _Traits>
1037inline _LIBCPP_INLINE_VISIBILITY
1038basic_ostream<_CharT, _Traits>&
1039flush(basic_ostream<_CharT, _Traits>& __os)
1040{
1041    __os.flush();
1042    return __os;
1043}
1044
1045#ifndef _LIBCPP_HAS_NO_RVALUE_REFERENCES
1046
1047template <class _Stream, class _Tp>
1048inline _LIBCPP_INLINE_VISIBILITY
1049typename enable_if
1050<
1051    !is_lvalue_reference<_Stream>::value &&
1052    is_base_of<ios_base, _Stream>::value,
1053    _Stream&&
1054>::type
1055operator<<(_Stream&& __os, const _Tp& __x)
1056{
1057    __os << __x;
1058    return _VSTD::move(__os);
1059}
1060
1061#endif  // _LIBCPP_HAS_NO_RVALUE_REFERENCES
1062
1063template<class _CharT, class _Traits, class _Allocator>
1064basic_ostream<_CharT, _Traits>&
1065operator<<(basic_ostream<_CharT, _Traits>& __os,
1066           const basic_string<_CharT, _Traits, _Allocator>& __str)
1067{
1068    return _VSTD::__put_character_sequence(__os, __str.data(), __str.size());
1069}
1070
1071template <class _CharT, class _Traits>
1072inline _LIBCPP_INLINE_VISIBILITY
1073basic_ostream<_CharT, _Traits>&
1074operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
1075{
1076    return __os << __ec.category().name() << ':' << __ec.value();
1077}
1078
1079template<class _CharT, class _Traits, class _Yp>
1080inline _LIBCPP_INLINE_VISIBILITY
1081basic_ostream<_CharT, _Traits>&
1082operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
1083{
1084    return __os << __p.get();
1085}
1086
1087template <class _CharT, class _Traits, size_t _Size>
1088basic_ostream<_CharT, _Traits>&
1089operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
1090{
1091    return __os << __x.template to_string<_CharT, _Traits>
1092                        (use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
1093                         use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
1094}
1095
1096_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS basic_ostream<char>)
1097_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_TYPE_VIS basic_ostream<wchar_t>)
1098
1099_LIBCPP_END_NAMESPACE_STD
1100
1101#endif  // _LIBCPP_OSTREAM
1102