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