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