xref: /freebsd/contrib/llvm-project/libcxx/include/__cxx03/__ostream/basic_ostream.h (revision 700637cbb5e582861067a11aaca4d053546871d2)
1 //===---------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===---------------------------------------------------------------------===//
8 
9 #ifndef _LIBCPP___CXX03___OSTREAM_BASIC_OSTREAM_H
10 #define _LIBCPP___CXX03___OSTREAM_BASIC_OSTREAM_H
11 
12 #include <__cxx03/__config>
13 #include <__cxx03/__exception/operations.h>
14 #include <__cxx03/__memory/shared_ptr.h>
15 #include <__cxx03/__memory/unique_ptr.h>
16 #include <__cxx03/__system_error/error_code.h>
17 #include <__cxx03/__type_traits/conjunction.h>
18 #include <__cxx03/__type_traits/enable_if.h>
19 #include <__cxx03/__type_traits/is_base_of.h>
20 #include <__cxx03/__type_traits/void_t.h>
21 #include <__cxx03/__utility/declval.h>
22 #include <__cxx03/bitset>
23 #include <__cxx03/cstddef>
24 #include <__cxx03/ios>
25 #include <__cxx03/locale>
26 #include <__cxx03/new> // for __throw_bad_alloc
27 #include <__cxx03/streambuf>
28 #include <__cxx03/string_view>
29 
30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31 #  pragma GCC system_header
32 #endif
33 
34 _LIBCPP_PUSH_MACROS
35 #include <__cxx03/__undef_macros>
36 
37 _LIBCPP_BEGIN_NAMESPACE_STD
38 
39 template <class _CharT, class _Traits>
40 class _LIBCPP_TEMPLATE_VIS basic_ostream : virtual public basic_ios<_CharT, _Traits> {
41 public:
42   // types (inherited from basic_ios (27.5.4)):
43   typedef _CharT char_type;
44   typedef _Traits traits_type;
45   typedef typename traits_type::int_type int_type;
46   typedef typename traits_type::pos_type pos_type;
47   typedef typename traits_type::off_type off_type;
48 
49   // 27.7.2.2 Constructor/destructor:
basic_ostream(basic_streambuf<char_type,traits_type> * __sb)50   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb) {
51     this->init(__sb);
52   }
53   ~basic_ostream() override;
54 
55   basic_ostream(const basic_ostream& __rhs)            = delete;
56   basic_ostream& operator=(const basic_ostream& __rhs) = delete;
57 
58 protected:
59   inline _LIBCPP_HIDE_FROM_ABI basic_ostream(basic_ostream&& __rhs);
60 
61   // 27.7.2.3 Assign/swap
62   inline _LIBCPP_HIDE_FROM_ABI basic_ostream& operator=(basic_ostream&& __rhs);
63 
swap(basic_ostream & __rhs)64   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_ostream& __rhs) {
65     basic_ios<char_type, traits_type>::swap(__rhs);
66   }
67 
68 public:
69   // 27.7.2.4 Prefix/suffix:
70   class _LIBCPP_TEMPLATE_VIS sentry;
71 
72   // 27.7.2.6 Formatted output:
73   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&)) {
74     return __pf(*this);
75   }
76 
77   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream&
78   operator<<(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) {
79     __pf(*this);
80     return *this;
81   }
82 
83   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&)) {
84     __pf(*this);
85     return *this;
86   }
87 
88   basic_ostream& operator<<(bool __n);
89   basic_ostream& operator<<(short __n);
90   basic_ostream& operator<<(unsigned short __n);
91   basic_ostream& operator<<(int __n);
92   basic_ostream& operator<<(unsigned int __n);
93   basic_ostream& operator<<(long __n);
94   basic_ostream& operator<<(unsigned long __n);
95   basic_ostream& operator<<(long long __n);
96   basic_ostream& operator<<(unsigned long long __n);
97   basic_ostream& operator<<(float __f);
98   basic_ostream& operator<<(double __f);
99   basic_ostream& operator<<(long double __f);
100   basic_ostream& operator<<(const void* __p);
101 
102   basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
103 
104   // 27.7.2.7 Unformatted output:
105   basic_ostream& put(char_type __c);
106   basic_ostream& write(const char_type* __s, streamsize __n);
107   basic_ostream& flush();
108 
109   // 27.7.2.5 seeks:
110   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type tellp();
111   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(pos_type __pos);
112   inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
113 
114 protected:
basic_ostream()115   _LIBCPP_HIDE_FROM_ABI basic_ostream() {} // extension, intentially does not initialize
116 };
117 
118 template <class _CharT, class _Traits>
119 class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry {
120   bool __ok_;
121   basic_ostream<_CharT, _Traits>& __os_;
122 
123 public:
124   explicit sentry(basic_ostream<_CharT, _Traits>& __os);
125   ~sentry();
126   sentry(const sentry&)            = delete;
127   sentry& operator=(const sentry&) = delete;
128 
129   _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }
130 };
131 
132 template <class _CharT, class _Traits>
sentry(basic_ostream<_CharT,_Traits> & __os)133 basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os) : __ok_(false), __os_(__os) {
134   if (__os.good()) {
135     if (__os.tie())
136       __os.tie()->flush();
137     __ok_ = true;
138   }
139 }
140 
141 template <class _CharT, class _Traits>
~sentry()142 basic_ostream<_CharT, _Traits>::sentry::~sentry() {
143   if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && !uncaught_exception()) {
144 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
145     try {
146 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
147       if (__os_.rdbuf()->pubsync() == -1)
148         __os_.setstate(ios_base::badbit);
149 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
150     } catch (...) {
151     }
152 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
153   }
154 }
155 
156 template <class _CharT, class _Traits>
basic_ostream(basic_ostream && __rhs)157 basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs) {
158   this->move(__rhs);
159 }
160 
161 template <class _CharT, class _Traits>
162 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs) {
163   swap(__rhs);
164   return *this;
165 }
166 
167 template <class _CharT, class _Traits>
~basic_ostream()168 basic_ostream<_CharT, _Traits>::~basic_ostream() {}
169 
170 template <class _CharT, class _Traits>
171 basic_ostream<_CharT, _Traits>&
172 basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb) {
173 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
174   try {
175 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
176     sentry __s(*this);
177     if (__s) {
178       if (__sb) {
179 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
180         try {
181 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
182           typedef istreambuf_iterator<_CharT, _Traits> _Ip;
183           typedef ostreambuf_iterator<_CharT, _Traits> _Op;
184           _Ip __i(__sb);
185           _Ip __eof;
186           _Op __o(*this);
187           size_t __c = 0;
188           for (; __i != __eof; ++__i, ++__o, ++__c) {
189             *__o = *__i;
190             if (__o.failed())
191               break;
192           }
193           if (__c == 0)
194             this->setstate(ios_base::failbit);
195 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)196         } catch (...) {
197           this->__set_failbit_and_consider_rethrow();
198         }
199 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
200       } else
201         this->setstate(ios_base::badbit);
202     }
203 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)204   } catch (...) {
205     this->__set_badbit_and_consider_rethrow();
206   }
207 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
208   return *this;
209 }
210 
211 template <class _CharT, class _Traits>
212 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __n) {
213 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
214   try {
215 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
216     sentry __s(*this);
217     if (__s) {
218       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
219       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
220       if (__f.put(*this, *this, this->fill(), __n).failed())
221         this->setstate(ios_base::badbit | ios_base::failbit);
222     }
223 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)224   } catch (...) {
225     this->__set_badbit_and_consider_rethrow();
226   }
227 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
228   return *this;
229 }
230 
231 template <class _CharT, class _Traits>
232 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(short __n) {
233 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
234   try {
235 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
236     sentry __s(*this);
237     if (__s) {
238       ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
239       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
240       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
241       if (__f.put(*this,
242                   *this,
243                   this->fill(),
244                   __flags == ios_base::oct || __flags == ios_base::hex
245                       ? static_cast<long>(static_cast<unsigned short>(__n))
246                       : static_cast<long>(__n))
247               .failed())
248         this->setstate(ios_base::badbit | ios_base::failbit);
249     }
250 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)251   } catch (...) {
252     this->__set_badbit_and_consider_rethrow();
253   }
254 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
255   return *this;
256 }
257 
258 template <class _CharT, class _Traits>
259 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n) {
260 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
261   try {
262 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
263     sentry __s(*this);
264     if (__s) {
265       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
266       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
267       if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
268         this->setstate(ios_base::badbit | ios_base::failbit);
269     }
270 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)271   } catch (...) {
272     this->__set_badbit_and_consider_rethrow();
273   }
274 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
275   return *this;
276 }
277 
278 template <class _CharT, class _Traits>
279 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __n) {
280 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
281   try {
282 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
283     sentry __s(*this);
284     if (__s) {
285       ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
286       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
287       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
288       if (__f.put(*this,
289                   *this,
290                   this->fill(),
291                   __flags == ios_base::oct || __flags == ios_base::hex
292                       ? static_cast<long>(static_cast<unsigned int>(__n))
293                       : static_cast<long>(__n))
294               .failed())
295         this->setstate(ios_base::badbit | ios_base::failbit);
296     }
297 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)298   } catch (...) {
299     this->__set_badbit_and_consider_rethrow();
300   }
301 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
302   return *this;
303 }
304 
305 template <class _CharT, class _Traits>
306 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n) {
307 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
308   try {
309 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
310     sentry __s(*this);
311     if (__s) {
312       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
313       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
314       if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
315         this->setstate(ios_base::badbit | ios_base::failbit);
316     }
317 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)318   } catch (...) {
319     this->__set_badbit_and_consider_rethrow();
320   }
321 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
322   return *this;
323 }
324 
325 template <class _CharT, class _Traits>
326 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) {
327 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
328   try {
329 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
330     sentry __s(*this);
331     if (__s) {
332       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
333       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
334       if (__f.put(*this, *this, this->fill(), __n).failed())
335         this->setstate(ios_base::badbit | ios_base::failbit);
336     }
337 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)338   } catch (...) {
339     this->__set_badbit_and_consider_rethrow();
340   }
341 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
342   return *this;
343 }
344 
345 template <class _CharT, class _Traits>
346 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n) {
347 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
348   try {
349 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
350     sentry __s(*this);
351     if (__s) {
352       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
353       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
354       if (__f.put(*this, *this, this->fill(), __n).failed())
355         this->setstate(ios_base::badbit | ios_base::failbit);
356     }
357 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)358   } catch (...) {
359     this->__set_badbit_and_consider_rethrow();
360   }
361 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
362   return *this;
363 }
364 
365 template <class _CharT, class _Traits>
366 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long long __n) {
367 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
368   try {
369 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
370     sentry __s(*this);
371     if (__s) {
372       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
373       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
374       if (__f.put(*this, *this, this->fill(), __n).failed())
375         this->setstate(ios_base::badbit | ios_base::failbit);
376     }
377 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)378   } catch (...) {
379     this->__set_badbit_and_consider_rethrow();
380   }
381 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
382   return *this;
383 }
384 
385 template <class _CharT, class _Traits>
386 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n) {
387 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
388   try {
389 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
390     sentry __s(*this);
391     if (__s) {
392       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
393       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
394       if (__f.put(*this, *this, this->fill(), __n).failed())
395         this->setstate(ios_base::badbit | ios_base::failbit);
396     }
397 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)398   } catch (...) {
399     this->__set_badbit_and_consider_rethrow();
400   }
401 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
402   return *this;
403 }
404 
405 template <class _CharT, class _Traits>
406 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __n) {
407 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
408   try {
409 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
410     sentry __s(*this);
411     if (__s) {
412       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
413       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
414       if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
415         this->setstate(ios_base::badbit | ios_base::failbit);
416     }
417 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)418   } catch (...) {
419     this->__set_badbit_and_consider_rethrow();
420   }
421 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
422   return *this;
423 }
424 
425 template <class _CharT, class _Traits>
426 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __n) {
427 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
428   try {
429 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
430     sentry __s(*this);
431     if (__s) {
432       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
433       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
434       if (__f.put(*this, *this, this->fill(), __n).failed())
435         this->setstate(ios_base::badbit | ios_base::failbit);
436     }
437 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)438   } catch (...) {
439     this->__set_badbit_and_consider_rethrow();
440   }
441 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
442   return *this;
443 }
444 
445 template <class _CharT, class _Traits>
446 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __n) {
447 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
448   try {
449 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
450     sentry __s(*this);
451     if (__s) {
452       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
453       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
454       if (__f.put(*this, *this, this->fill(), __n).failed())
455         this->setstate(ios_base::badbit | ios_base::failbit);
456     }
457 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)458   } catch (...) {
459     this->__set_badbit_and_consider_rethrow();
460   }
461 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
462   return *this;
463 }
464 
465 template <class _CharT, class _Traits>
466 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __n) {
467 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
468   try {
469 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
470     sentry __s(*this);
471     if (__s) {
472       typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
473       const _Fp& __f = std::use_facet<_Fp>(this->getloc());
474       if (__f.put(*this, *this, this->fill(), __n).failed())
475         this->setstate(ios_base::badbit | ios_base::failbit);
476     }
477 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)478   } catch (...) {
479     this->__set_badbit_and_consider_rethrow();
480   }
481 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
482   return *this;
483 }
484 
485 template <class _CharT, class _Traits>
486 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
__put_character_sequence(basic_ostream<_CharT,_Traits> & __os,const _CharT * __str,size_t __len)487 __put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) {
488 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
489   try {
490 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
491     typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
492     if (__s) {
493       typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
494       if (std::__pad_and_output(
495               _Ip(__os),
496               __str,
497               (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
498               __str + __len,
499               __os,
500               __os.fill())
501               .failed())
502         __os.setstate(ios_base::badbit | ios_base::failbit);
503     }
504 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
505   } catch (...) {
506     __os.__set_badbit_and_consider_rethrow();
507   }
508 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
509   return __os;
510 }
511 
512 template <class _CharT, class _Traits>
513 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) {
514   return std::__put_character_sequence(__os, &__c, 1);
515 }
516 
517 template <class _CharT, class _Traits>
518 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn) {
519 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
520   try {
521 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
522     typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
523     if (__s) {
524       _CharT __c = __os.widen(__cn);
525       typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
526       if (std::__pad_and_output(
527               _Ip(__os),
528               &__c,
529               (__os.flags() & ios_base::adjustfield) == ios_base::left ? &__c + 1 : &__c,
530               &__c + 1,
531               __os,
532               __os.fill())
533               .failed())
534         __os.setstate(ios_base::badbit | ios_base::failbit);
535     }
536 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)537   } catch (...) {
538     __os.__set_badbit_and_consider_rethrow();
539   }
540 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
541   return __os;
542 }
543 
544 template <class _Traits>
545 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, char __c) {
546   return std::__put_character_sequence(__os, &__c, 1);
547 }
548 
549 template <class _Traits>
550 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, signed char __c) {
551   return std::__put_character_sequence(__os, (char*)&__c, 1);
552 }
553 
554 template <class _Traits>
555 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) {
556   return std::__put_character_sequence(__os, (char*)&__c, 1);
557 }
558 
559 template <class _CharT, class _Traits>
560 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
561 operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str) {
562   return std::__put_character_sequence(__os, __str, _Traits::length(__str));
563 }
564 
565 template <class _CharT, class _Traits>
566 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
567 operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn) {
568 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
569   try {
570 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
571     typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
572     if (__s) {
573       typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
574       size_t __len   = char_traits<char>::length(__strn);
575       const int __bs = 100;
576       _CharT __wbb[__bs];
577       _CharT* __wb = __wbb;
578       unique_ptr<_CharT, void (*)(void*)> __h(0, free);
579       if (__len > __bs) {
580         __wb = (_CharT*)malloc(__len * sizeof(_CharT));
581         if (__wb == 0)
582           __throw_bad_alloc();
583         __h.reset(__wb);
584       }
585       for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
586         *__p = __os.widen(*__strn);
587       if (std::__pad_and_output(
588               _Ip(__os),
589               __wb,
590               (__os.flags() & ios_base::adjustfield) == ios_base::left ? __wb + __len : __wb,
591               __wb + __len,
592               __os,
593               __os.fill())
594               .failed())
595         __os.setstate(ios_base::badbit | ios_base::failbit);
596     }
597 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
catch(...)598   } catch (...) {
599     __os.__set_badbit_and_consider_rethrow();
600   }
601 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
602   return __os;
603 }
604 
605 template <class _Traits>
606 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, const char* __str) {
607   return std::__put_character_sequence(__os, __str, _Traits::length(__str));
608 }
609 
610 template <class _Traits>
611 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
612 operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str) {
613   const char* __s = (const char*)__str;
614   return std::__put_character_sequence(__os, __s, _Traits::length(__s));
615 }
616 
617 template <class _Traits>
618 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
619 operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str) {
620   const char* __s = (const char*)__str;
621   return std::__put_character_sequence(__os, __s, _Traits::length(__s));
622 }
623 
624 template <class _CharT, class _Traits>
put(char_type __c)625 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::put(char_type __c) {
626 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
627   try {
628 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
629     sentry __s(*this);
630     if (__s) {
631       typedef ostreambuf_iterator<_CharT, _Traits> _Op;
632       _Op __o(*this);
633       *__o = __c;
634       if (__o.failed())
635         this->setstate(ios_base::badbit);
636     }
637 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
638   } catch (...) {
639     this->__set_badbit_and_consider_rethrow();
640   }
641 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
642   return *this;
643 }
644 
645 template <class _CharT, class _Traits>
write(const char_type * __s,streamsize __n)646 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) {
647 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
648   try {
649 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
650     sentry __sen(*this);
651     if (__sen && __n) {
652       if (this->rdbuf()->sputn(__s, __n) != __n)
653         this->setstate(ios_base::badbit);
654     }
655 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
656   } catch (...) {
657     this->__set_badbit_and_consider_rethrow();
658   }
659 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
660   return *this;
661 }
662 
663 template <class _CharT, class _Traits>
flush()664 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::flush() {
665 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
666   try {
667 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
668     if (this->rdbuf()) {
669       sentry __s(*this);
670       if (__s) {
671         if (this->rdbuf()->pubsync() == -1)
672           this->setstate(ios_base::badbit);
673       }
674     }
675 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
676   } catch (...) {
677     this->__set_badbit_and_consider_rethrow();
678   }
679 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
680   return *this;
681 }
682 
683 template <class _CharT, class _Traits>
tellp()684 typename basic_ostream<_CharT, _Traits>::pos_type basic_ostream<_CharT, _Traits>::tellp() {
685   if (this->fail())
686     return pos_type(-1);
687   return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
688 }
689 
690 template <class _CharT, class _Traits>
seekp(pos_type __pos)691 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) {
692   sentry __s(*this);
693   if (!this->fail()) {
694     if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
695       this->setstate(ios_base::failbit);
696   }
697   return *this;
698 }
699 
700 template <class _CharT, class _Traits>
seekp(off_type __off,ios_base::seekdir __dir)701 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) {
702   sentry __s(*this);
703   if (!this->fail()) {
704     if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
705       this->setstate(ios_base::failbit);
706   }
707   return *this;
708 }
709 
710 template <class _CharT, class _Traits>
endl(basic_ostream<_CharT,_Traits> & __os)711 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& endl(basic_ostream<_CharT, _Traits>& __os) {
712   __os.put(__os.widen('\n'));
713   __os.flush();
714   return __os;
715 }
716 
717 template <class _CharT, class _Traits>
ends(basic_ostream<_CharT,_Traits> & __os)718 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& ends(basic_ostream<_CharT, _Traits>& __os) {
719   __os.put(_CharT());
720   return __os;
721 }
722 
723 template <class _CharT, class _Traits>
flush(basic_ostream<_CharT,_Traits> & __os)724 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& flush(basic_ostream<_CharT, _Traits>& __os) {
725   __os.flush();
726   return __os;
727 }
728 
729 template <class _Stream, class _Tp, class = void>
730 struct __is_ostreamable : false_type {};
731 
732 template <class _Stream, class _Tp>
733 struct __is_ostreamable<_Stream, _Tp, decltype(std::declval<_Stream>() << std::declval<_Tp>(), void())> : true_type {};
734 
735 template <class _Stream,
736           class _Tp,
737           __enable_if_t<_And<is_base_of<ios_base, _Stream>, __is_ostreamable<_Stream&, const _Tp&> >::value, int> = 0>
738 _LIBCPP_HIDE_FROM_ABI _Stream&& operator<<(_Stream&& __os, const _Tp& __x) {
739   __os << __x;
740   return std::move(__os);
741 }
742 
743 template <class _CharT, class _Traits, class _Allocator>
744 basic_ostream<_CharT, _Traits>&
745 operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str) {
746   return std::__put_character_sequence(__os, __str.data(), __str.size());
747 }
748 
749 template <class _CharT, class _Traits>
750 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
751 operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv) {
752   return std::__put_character_sequence(__os, __sv.data(), __sv.size());
753 }
754 
755 template <class _CharT, class _Traits>
756 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
757 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec) {
758   return __os << __ec.category().name() << ':' << __ec.value();
759 }
760 
761 template <class _CharT, class _Traits, class _Yp>
762 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
763 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p) {
764   return __os << __p.get();
765 }
766 
767 template <
768     class _CharT,
769     class _Traits,
770     class _Yp,
771     class _Dp,
772     __enable_if_t<is_same<void,
773                           __void_t<decltype((std::declval<basic_ostream<_CharT, _Traits>&>()
774                                              << std::declval<typename unique_ptr<_Yp, _Dp>::pointer>()))> >::value,
775                   int> = 0>
776 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
777 operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p) {
778   return __os << __p.get();
779 }
780 
781 template <class _CharT, class _Traits, size_t _Size>
782 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
783 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) {
784   return __os << __x.template to_string<_CharT, _Traits>(std::use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
785                                                          std::use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
786 }
787 
788 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>;
789 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
790 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>;
791 #endif
792 
793 _LIBCPP_END_NAMESPACE_STD
794 
795 _LIBCPP_POP_MACROS
796 
797 #endif // _LIBCPP___CXX03___OSTREAM_BASIC_OSTREAM_H
798