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