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