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