blob: a8faa08a18e3fb8c6769c252ddfa59d12f95fbc0 [file] [log] [blame]
Howard Hinnantc51e1022010-05-11 19:42:16 +00001// -*- C++ -*-
2//===-------------------------- ostream -----------------------------------===//
3//
Chandler Carruthd2012102019-01-19 10:56:40 +00004// 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00007//
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
Marshall Clowe9ca0272013-08-14 15:15:28 +000034 basic_ostream& operator=(const basic_ostream& rhs) = delete; // C++14
Howard Hinnantc51e1022010-05-11 19:42:16 +000035 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);
Marshall Clow706862d2019-07-01 16:20:25 +000059 basic_ostream& operator<<(nullptr_t);
Howard Hinnantc51e1022010-05-11 19:42:16 +000060
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);
Marshall Clow27d29872014-09-16 15:27:01 +000070protected:
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);
Howard Hinnantc51e1022010-05-11 19:42:16 +000077};
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
Louis Dionned9f73b12020-09-23 08:49:00 -0400129template <class Stream, class T>
130 Stream&& operator<<(Stream&& os, const T& x);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000131
132} // std
133
134*/
135
136#include <__config>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000137#include <bitset>
Arthur O'Dwyeref181602021-05-19 11:57:04 -0400138#include <ios>
139#include <iterator>
140#include <locale>
141#include <streambuf>
Marshall Clow8732fed2018-12-11 04:35:44 +0000142#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000143
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000144#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000145#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000146#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000147
148_LIBCPP_BEGIN_NAMESPACE_STD
149
150template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000151class _LIBCPP_TEMPLATE_VIS basic_ostream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000152 : 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:
Louis Dionneb4d05d72018-10-16 19:26:23 +0000163 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Eric Fiselier815ed732016-09-16 00:00:48 +0000164 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb)
165 { this->init(__sb); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000166 virtual ~basic_ostream();
167protected:
Eric Fiselier78ccf772017-04-18 23:38:41 +0000168#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier815ed732016-09-16 00:00:48 +0000169 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000170 basic_ostream(basic_ostream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171
172 // 27.7.2.3 Assign/swap
Eric Fiselier815ed732016-09-16 00:00:48 +0000173 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000174 basic_ostream& operator=(basic_ostream&& __rhs);
175#endif
Louis Dionneb4d05d72018-10-16 19:26:23 +0000176 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Eric Fiselier815ed732016-09-16 00:00:48 +0000177 void swap(basic_ostream& __rhs)
178 { basic_ios<char_type, traits_type>::swap(__rhs); }
Marshall Clow27d29872014-09-16 15:27:01 +0000179
Eric Fiselier2d8515f2017-01-06 20:58:25 +0000180#ifndef _LIBCPP_CXX03_LANG
Marshall Clow242d9c12014-09-16 15:33:53 +0000181 basic_ostream (const basic_ostream& __rhs) = delete;
182 basic_ostream& operator=(const basic_ostream& __rhs) = delete;
Marshall Clow27d29872014-09-16 15:27:01 +0000183#else
Marshall Clowd37f25c2014-09-17 01:58:15 +0000184 basic_ostream (const basic_ostream& __rhs); // not defined
185 basic_ostream& operator=(const basic_ostream& __rhs); // not defined
Marshall Clow27d29872014-09-16 15:27:01 +0000186#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000187public:
188
189 // 27.7.2.4 Prefix/suffix:
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000190 class _LIBCPP_TEMPLATE_VIS sentry;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000191
192 // 27.7.2.6 Formatted output:
Louis Dionneb4d05d72018-10-16 19:26:23 +0000193 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Eric Fiselier815ed732016-09-16 00:00:48 +0000194 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&))
195 { return __pf(*this); }
196
Louis Dionneb4d05d72018-10-16 19:26:23 +0000197 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000198 basic_ostream& operator<<(basic_ios<char_type, traits_type>&
Eric Fiselier815ed732016-09-16 00:00:48 +0000199 (*__pf)(basic_ios<char_type,traits_type>&))
200 { __pf(*this); return *this; }
201
Louis Dionneb4d05d72018-10-16 19:26:23 +0000202 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Eric Fiselier815ed732016-09-16 00:00:48 +0000203 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&))
204 { __pf(*this); return *this; }
205
Howard Hinnantc51e1022010-05-11 19:42:16 +0000206 basic_ostream& operator<<(bool __n);
207 basic_ostream& operator<<(short __n);
208 basic_ostream& operator<<(unsigned short __n);
209 basic_ostream& operator<<(int __n);
210 basic_ostream& operator<<(unsigned int __n);
211 basic_ostream& operator<<(long __n);
212 basic_ostream& operator<<(unsigned long __n);
213 basic_ostream& operator<<(long long __n);
214 basic_ostream& operator<<(unsigned long long __n);
215 basic_ostream& operator<<(float __f);
216 basic_ostream& operator<<(double __f);
217 basic_ostream& operator<<(long double __f);
218 basic_ostream& operator<<(const void* __p);
219 basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
220
Marshall Clow706862d2019-07-01 16:20:25 +0000221 _LIBCPP_INLINE_VISIBILITY
222 basic_ostream& operator<<(nullptr_t)
223 { return *this << "nullptr"; }
224
Howard Hinnantc51e1022010-05-11 19:42:16 +0000225 // 27.7.2.7 Unformatted output:
226 basic_ostream& put(char_type __c);
227 basic_ostream& write(const char_type* __s, streamsize __n);
228 basic_ostream& flush();
229
230 // 27.7.2.5 seeks:
Louis Dionneb4d05d72018-10-16 19:26:23 +0000231 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000232 pos_type tellp();
Louis Dionneb4d05d72018-10-16 19:26:23 +0000233 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000234 basic_ostream& seekp(pos_type __pos);
Louis Dionneb4d05d72018-10-16 19:26:23 +0000235 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000236 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
237
238protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000239 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000240 basic_ostream() {} // extension, intentially does not initialize
241};
242
243template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000244class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry
Howard Hinnantc51e1022010-05-11 19:42:16 +0000245{
246 bool __ok_;
247 basic_ostream<_CharT, _Traits>& __os_;
248
249 sentry(const sentry&); // = delete;
250 sentry& operator=(const sentry&); // = delete;
251
252public:
253 explicit sentry(basic_ostream<_CharT, _Traits>& __os);
254 ~sentry();
255
Louis Dionne16fe2952018-07-11 23:14:33 +0000256 _LIBCPP_INLINE_VISIBILITY
Arthur O'Dwyer6c9c9a72021-06-15 12:57:54 -0400257 explicit operator bool() const {return __ok_;}
Howard Hinnantc51e1022010-05-11 19:42:16 +0000258};
259
260template <class _CharT, class _Traits>
261basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os)
262 : __ok_(false),
263 __os_(__os)
264{
265 if (__os.good())
266 {
267 if (__os.tie())
268 __os.tie()->flush();
269 __ok_ = true;
270 }
271}
272
273template <class _CharT, class _Traits>
274basic_ostream<_CharT, _Traits>::sentry::~sentry()
275{
276 if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf)
277 && !uncaught_exception())
278 {
279#ifndef _LIBCPP_NO_EXCEPTIONS
280 try
281 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400282#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000283 if (__os_.rdbuf()->pubsync() == -1)
284 __os_.setstate(ios_base::badbit);
285#ifndef _LIBCPP_NO_EXCEPTIONS
286 }
287 catch (...)
288 {
289 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400290#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000291 }
292}
293
Eric Fiselier78ccf772017-04-18 23:38:41 +0000294#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000295
296template <class _CharT, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs)
298{
299 this->move(__rhs);
300}
301
302template <class _CharT, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000303basic_ostream<_CharT, _Traits>&
304basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs)
305{
306 swap(__rhs);
307 return *this;
308}
309
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400310#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000311
312template <class _CharT, class _Traits>
313basic_ostream<_CharT, _Traits>::~basic_ostream()
314{
315}
316
317template <class _CharT, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000318basic_ostream<_CharT, _Traits>&
319basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb)
320{
321#ifndef _LIBCPP_NO_EXCEPTIONS
322 try
323 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400324#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000325 sentry __s(*this);
326 if (__s)
327 {
328 if (__sb)
329 {
330#ifndef _LIBCPP_NO_EXCEPTIONS
331 try
332 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400333#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc834c512011-11-29 18:15:50 +0000334 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
335 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
336 _Ip __i(__sb);
337 _Ip __eof;
338 _Op __o(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000339 size_t __c = 0;
340 for (; __i != __eof; ++__i, ++__o, ++__c)
341 {
342 *__o = *__i;
343 if (__o.failed())
344 break;
345 }
346 if (__c == 0)
347 this->setstate(ios_base::failbit);
348#ifndef _LIBCPP_NO_EXCEPTIONS
349 }
350 catch (...)
351 {
352 this->__set_failbit_and_consider_rethrow();
353 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400354#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000355 }
356 else
357 this->setstate(ios_base::badbit);
358 }
359#ifndef _LIBCPP_NO_EXCEPTIONS
360 }
361 catch (...)
362 {
363 this->__set_badbit_and_consider_rethrow();
364 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400365#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000366 return *this;
367}
368
369template <class _CharT, class _Traits>
370basic_ostream<_CharT, _Traits>&
371basic_ostream<_CharT, _Traits>::operator<<(bool __n)
372{
373#ifndef _LIBCPP_NO_EXCEPTIONS
374 try
375 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400376#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000377 sentry __s(*this);
378 if (__s)
379 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000380 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
381 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000382 if (__f.put(*this, *this, this->fill(), __n).failed())
383 this->setstate(ios_base::badbit | ios_base::failbit);
384 }
385#ifndef _LIBCPP_NO_EXCEPTIONS
386 }
387 catch (...)
388 {
389 this->__set_badbit_and_consider_rethrow();
390 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400391#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000392 return *this;
393}
394
395template <class _CharT, class _Traits>
396basic_ostream<_CharT, _Traits>&
397basic_ostream<_CharT, _Traits>::operator<<(short __n)
398{
399#ifndef _LIBCPP_NO_EXCEPTIONS
400 try
401 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400402#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000403 sentry __s(*this);
404 if (__s)
405 {
406 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
Howard Hinnantc834c512011-11-29 18:15:50 +0000407 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
408 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000409 if (__f.put(*this, *this, this->fill(),
410 __flags == ios_base::oct || __flags == ios_base::hex ?
411 static_cast<long>(static_cast<unsigned short>(__n)) :
412 static_cast<long>(__n)).failed())
413 this->setstate(ios_base::badbit | ios_base::failbit);
414 }
415#ifndef _LIBCPP_NO_EXCEPTIONS
416 }
417 catch (...)
418 {
419 this->__set_badbit_and_consider_rethrow();
420 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400421#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000422 return *this;
423}
424
425template <class _CharT, class _Traits>
426basic_ostream<_CharT, _Traits>&
427basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n)
428{
429#ifndef _LIBCPP_NO_EXCEPTIONS
430 try
431 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400432#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000433 sentry __s(*this);
434 if (__s)
435 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000436 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
437 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000438 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
439 this->setstate(ios_base::badbit | ios_base::failbit);
440 }
441#ifndef _LIBCPP_NO_EXCEPTIONS
442 }
443 catch (...)
444 {
445 this->__set_badbit_and_consider_rethrow();
446 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400447#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000448 return *this;
449}
450
451template <class _CharT, class _Traits>
452basic_ostream<_CharT, _Traits>&
453basic_ostream<_CharT, _Traits>::operator<<(int __n)
454{
455#ifndef _LIBCPP_NO_EXCEPTIONS
456 try
457 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400458#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000459 sentry __s(*this);
460 if (__s)
461 {
462 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
Howard Hinnantc834c512011-11-29 18:15:50 +0000463 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
464 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000465 if (__f.put(*this, *this, this->fill(),
466 __flags == ios_base::oct || __flags == ios_base::hex ?
467 static_cast<long>(static_cast<unsigned int>(__n)) :
468 static_cast<long>(__n)).failed())
469 this->setstate(ios_base::badbit | ios_base::failbit);
470 }
471#ifndef _LIBCPP_NO_EXCEPTIONS
472 }
473 catch (...)
474 {
475 this->__set_badbit_and_consider_rethrow();
476 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400477#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000478 return *this;
479}
480
481template <class _CharT, class _Traits>
482basic_ostream<_CharT, _Traits>&
483basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n)
484{
485#ifndef _LIBCPP_NO_EXCEPTIONS
486 try
487 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400488#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000489 sentry __s(*this);
490 if (__s)
491 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000492 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
493 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000494 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
495 this->setstate(ios_base::badbit | ios_base::failbit);
496 }
497#ifndef _LIBCPP_NO_EXCEPTIONS
498 }
499 catch (...)
500 {
501 this->__set_badbit_and_consider_rethrow();
502 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400503#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000504 return *this;
505}
506
507template <class _CharT, class _Traits>
508basic_ostream<_CharT, _Traits>&
509basic_ostream<_CharT, _Traits>::operator<<(long __n)
510{
511#ifndef _LIBCPP_NO_EXCEPTIONS
512 try
513 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400514#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000515 sentry __s(*this);
516 if (__s)
517 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000518 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
519 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000520 if (__f.put(*this, *this, this->fill(), __n).failed())
521 this->setstate(ios_base::badbit | ios_base::failbit);
522 }
523#ifndef _LIBCPP_NO_EXCEPTIONS
524 }
525 catch (...)
526 {
527 this->__set_badbit_and_consider_rethrow();
528 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400529#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000530 return *this;
531}
532
533template <class _CharT, class _Traits>
534basic_ostream<_CharT, _Traits>&
535basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
536{
537#ifndef _LIBCPP_NO_EXCEPTIONS
538 try
539 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400540#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000541 sentry __s(*this);
542 if (__s)
543 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000544 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
545 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000546 if (__f.put(*this, *this, this->fill(), __n).failed())
547 this->setstate(ios_base::badbit | ios_base::failbit);
548 }
549#ifndef _LIBCPP_NO_EXCEPTIONS
550 }
551 catch (...)
552 {
553 this->__set_badbit_and_consider_rethrow();
554 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400555#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000556 return *this;
557}
558
559template <class _CharT, class _Traits>
560basic_ostream<_CharT, _Traits>&
561basic_ostream<_CharT, _Traits>::operator<<(long long __n)
562{
563#ifndef _LIBCPP_NO_EXCEPTIONS
564 try
565 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400566#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000567 sentry __s(*this);
568 if (__s)
569 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000570 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
571 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000572 if (__f.put(*this, *this, this->fill(), __n).failed())
573 this->setstate(ios_base::badbit | ios_base::failbit);
574 }
575#ifndef _LIBCPP_NO_EXCEPTIONS
576 }
577 catch (...)
578 {
579 this->__set_badbit_and_consider_rethrow();
580 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400581#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000582 return *this;
583}
584
585template <class _CharT, class _Traits>
586basic_ostream<_CharT, _Traits>&
587basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
588{
589#ifndef _LIBCPP_NO_EXCEPTIONS
590 try
591 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400592#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000593 sentry __s(*this);
594 if (__s)
595 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000596 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
597 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000598 if (__f.put(*this, *this, this->fill(), __n).failed())
599 this->setstate(ios_base::badbit | ios_base::failbit);
600 }
601#ifndef _LIBCPP_NO_EXCEPTIONS
602 }
603 catch (...)
604 {
605 this->__set_badbit_and_consider_rethrow();
606 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400607#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000608 return *this;
609}
610
611template <class _CharT, class _Traits>
612basic_ostream<_CharT, _Traits>&
613basic_ostream<_CharT, _Traits>::operator<<(float __n)
614{
615#ifndef _LIBCPP_NO_EXCEPTIONS
616 try
617 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400618#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000619 sentry __s(*this);
620 if (__s)
621 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000622 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
623 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000624 if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
625 this->setstate(ios_base::badbit | ios_base::failbit);
626 }
627#ifndef _LIBCPP_NO_EXCEPTIONS
628 }
629 catch (...)
630 {
631 this->__set_badbit_and_consider_rethrow();
632 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400633#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000634 return *this;
635}
636
637template <class _CharT, class _Traits>
638basic_ostream<_CharT, _Traits>&
639basic_ostream<_CharT, _Traits>::operator<<(double __n)
640{
641#ifndef _LIBCPP_NO_EXCEPTIONS
642 try
643 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400644#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000645 sentry __s(*this);
646 if (__s)
647 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000648 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
649 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000650 if (__f.put(*this, *this, this->fill(), __n).failed())
651 this->setstate(ios_base::badbit | ios_base::failbit);
652 }
653#ifndef _LIBCPP_NO_EXCEPTIONS
654 }
655 catch (...)
656 {
657 this->__set_badbit_and_consider_rethrow();
658 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400659#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000660 return *this;
661}
662
663template <class _CharT, class _Traits>
664basic_ostream<_CharT, _Traits>&
665basic_ostream<_CharT, _Traits>::operator<<(long double __n)
666{
667#ifndef _LIBCPP_NO_EXCEPTIONS
668 try
669 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400670#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000671 sentry __s(*this);
672 if (__s)
673 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000674 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
675 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000676 if (__f.put(*this, *this, this->fill(), __n).failed())
677 this->setstate(ios_base::badbit | ios_base::failbit);
678 }
679#ifndef _LIBCPP_NO_EXCEPTIONS
680 }
681 catch (...)
682 {
683 this->__set_badbit_and_consider_rethrow();
684 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400685#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000686 return *this;
687}
688
689template <class _CharT, class _Traits>
690basic_ostream<_CharT, _Traits>&
691basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
692{
693#ifndef _LIBCPP_NO_EXCEPTIONS
694 try
695 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400696#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000697 sentry __s(*this);
698 if (__s)
699 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000700 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
701 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000702 if (__f.put(*this, *this, this->fill(), __n).failed())
703 this->setstate(ios_base::badbit | ios_base::failbit);
704 }
705#ifndef _LIBCPP_NO_EXCEPTIONS
706 }
707 catch (...)
708 {
709 this->__set_badbit_and_consider_rethrow();
710 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400711#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000712 return *this;
713}
714
715template<class _CharT, class _Traits>
716basic_ostream<_CharT, _Traits>&
Marshall Clow00995642013-12-10 19:25:49 +0000717__put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
718 const _CharT* __str, size_t __len)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000719{
720#ifndef _LIBCPP_NO_EXCEPTIONS
721 try
722 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400723#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000724 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
725 if (__s)
726 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000727 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
728 if (__pad_and_output(_Ip(__os),
Marshall Clow00995642013-12-10 19:25:49 +0000729 __str,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000730 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
Marshall Clow00995642013-12-10 19:25:49 +0000731 __str + __len :
732 __str,
733 __str + __len,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000734 __os,
735 __os.fill()).failed())
736 __os.setstate(ios_base::badbit | ios_base::failbit);
737 }
738#ifndef _LIBCPP_NO_EXCEPTIONS
739 }
740 catch (...)
741 {
742 __os.__set_badbit_and_consider_rethrow();
743 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400744#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000745 return __os;
746}
747
Volodymyr Sapsai4c14a922018-09-19 23:31:34 +0000748
Marshall Clow00995642013-12-10 19:25:49 +0000749template<class _CharT, class _Traits>
750basic_ostream<_CharT, _Traits>&
751operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
752{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000753 return _VSTD::__put_character_sequence(__os, &__c, 1);
Marshall Clow00995642013-12-10 19:25:49 +0000754}
755
Howard Hinnantc51e1022010-05-11 19:42:16 +0000756template<class _CharT, class _Traits>
757basic_ostream<_CharT, _Traits>&
758operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn)
759{
760#ifndef _LIBCPP_NO_EXCEPTIONS
761 try
762 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400763#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000764 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
765 if (__s)
766 {
767 _CharT __c = __os.widen(__cn);
Howard Hinnantc834c512011-11-29 18:15:50 +0000768 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
769 if (__pad_and_output(_Ip(__os),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000770 &__c,
771 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
772 &__c + 1 :
773 &__c,
774 &__c + 1,
775 __os,
776 __os.fill()).failed())
777 __os.setstate(ios_base::badbit | ios_base::failbit);
778 }
779#ifndef _LIBCPP_NO_EXCEPTIONS
780 }
781 catch (...)
782 {
783 __os.__set_badbit_and_consider_rethrow();
784 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400785#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000786 return __os;
787}
788
789template<class _Traits>
790basic_ostream<char, _Traits>&
791operator<<(basic_ostream<char, _Traits>& __os, char __c)
792{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000793 return _VSTD::__put_character_sequence(__os, &__c, 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000794}
795
796template<class _Traits>
797basic_ostream<char, _Traits>&
798operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
799{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000800 return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000801}
802
803template<class _Traits>
804basic_ostream<char, _Traits>&
805operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
806{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000807 return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000808}
809
810template<class _CharT, class _Traits>
811basic_ostream<_CharT, _Traits>&
812operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
813{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000814 return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000815}
816
817template<class _CharT, class _Traits>
818basic_ostream<_CharT, _Traits>&
819operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn)
820{
821#ifndef _LIBCPP_NO_EXCEPTIONS
822 try
823 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400824#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000825 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
826 if (__s)
827 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000828 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000829 size_t __len = char_traits<char>::length(__strn);
830 const int __bs = 100;
831 _CharT __wbb[__bs];
832 _CharT* __wb = __wbb;
833 unique_ptr<_CharT, void(*)(void*)> __h(0, free);
834 if (__len > __bs)
835 {
836 __wb = (_CharT*)malloc(__len*sizeof(_CharT));
837 if (__wb == 0)
838 __throw_bad_alloc();
839 __h.reset(__wb);
840 }
841 for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
842 *__p = __os.widen(*__strn);
Howard Hinnantc834c512011-11-29 18:15:50 +0000843 if (__pad_and_output(_Ip(__os),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000844 __wb,
845 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
846 __wb + __len :
847 __wb,
848 __wb + __len,
849 __os,
850 __os.fill()).failed())
851 __os.setstate(ios_base::badbit | ios_base::failbit);
852 }
853#ifndef _LIBCPP_NO_EXCEPTIONS
854 }
855 catch (...)
856 {
857 __os.__set_badbit_and_consider_rethrow();
858 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400859#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000860 return __os;
861}
862
863template<class _Traits>
864basic_ostream<char, _Traits>&
865operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
866{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000867 return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000868}
869
870template<class _Traits>
871basic_ostream<char, _Traits>&
872operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
873{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000874 const char *__s = (const char *) __str;
875 return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000876}
877
878template<class _Traits>
879basic_ostream<char, _Traits>&
880operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
881{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000882 const char *__s = (const char *) __str;
883 return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000884}
885
886template <class _CharT, class _Traits>
887basic_ostream<_CharT, _Traits>&
888basic_ostream<_CharT, _Traits>::put(char_type __c)
889{
890#ifndef _LIBCPP_NO_EXCEPTIONS
891 try
892 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400893#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000894 sentry __s(*this);
895 if (__s)
896 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000897 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
898 _Op __o(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000899 *__o = __c;
900 if (__o.failed())
901 this->setstate(ios_base::badbit);
902 }
903#ifndef _LIBCPP_NO_EXCEPTIONS
904 }
905 catch (...)
906 {
907 this->__set_badbit_and_consider_rethrow();
908 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400909#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000910 return *this;
911}
912
913template <class _CharT, class _Traits>
914basic_ostream<_CharT, _Traits>&
915basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n)
916{
917#ifndef _LIBCPP_NO_EXCEPTIONS
918 try
919 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400920#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000921 sentry __sen(*this);
922 if (__sen && __n)
923 {
Howard Hinnant79cef932013-01-15 17:22:03 +0000924 if (this->rdbuf()->sputn(__s, __n) != __n)
925 this->setstate(ios_base::badbit);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000926 }
927#ifndef _LIBCPP_NO_EXCEPTIONS
928 }
929 catch (...)
930 {
931 this->__set_badbit_and_consider_rethrow();
932 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400933#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000934 return *this;
935}
936
937template <class _CharT, class _Traits>
938basic_ostream<_CharT, _Traits>&
939basic_ostream<_CharT, _Traits>::flush()
940{
941#ifndef _LIBCPP_NO_EXCEPTIONS
942 try
943 {
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400944#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000945 if (this->rdbuf())
946 {
947 sentry __s(*this);
948 if (__s)
949 {
950 if (this->rdbuf()->pubsync() == -1)
951 this->setstate(ios_base::badbit);
952 }
953 }
954#ifndef _LIBCPP_NO_EXCEPTIONS
955 }
956 catch (...)
957 {
958 this->__set_badbit_and_consider_rethrow();
959 }
Louis Dionne2b1ceaa2021-04-20 12:03:32 -0400960#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000961 return *this;
962}
963
964template <class _CharT, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000965typename basic_ostream<_CharT, _Traits>::pos_type
966basic_ostream<_CharT, _Traits>::tellp()
967{
968 if (this->fail())
969 return pos_type(-1);
970 return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
971}
972
973template <class _CharT, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000974basic_ostream<_CharT, _Traits>&
975basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
976{
Marshall Clow29c4daa2013-10-31 22:20:45 +0000977 sentry __s(*this);
Marshall Clow8aa079a2015-06-22 15:01:21 +0000978 if (!this->fail())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000979 {
980 if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
981 this->setstate(ios_base::failbit);
982 }
983 return *this;
984}
985
986template <class _CharT, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000987basic_ostream<_CharT, _Traits>&
988basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir)
989{
Marshall Clow29c4daa2013-10-31 22:20:45 +0000990 sentry __s(*this);
Marshall Clow8aa079a2015-06-22 15:01:21 +0000991 if (!this->fail())
Marshall Clow29c4daa2013-10-31 22:20:45 +0000992 {
993 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
994 this->setstate(ios_base::failbit);
995 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000996 return *this;
997}
998
999template <class _CharT, class _Traits>
Louis Dionnee29136f2020-07-13 11:53:48 -04001000inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001001basic_ostream<_CharT, _Traits>&
1002endl(basic_ostream<_CharT, _Traits>& __os)
1003{
1004 __os.put(__os.widen('\n'));
1005 __os.flush();
1006 return __os;
1007}
1008
1009template <class _CharT, class _Traits>
Louis Dionnee29136f2020-07-13 11:53:48 -04001010inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001011basic_ostream<_CharT, _Traits>&
1012ends(basic_ostream<_CharT, _Traits>& __os)
1013{
1014 __os.put(_CharT());
1015 return __os;
1016}
1017
1018template <class _CharT, class _Traits>
Louis Dionnee29136f2020-07-13 11:53:48 -04001019inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001020basic_ostream<_CharT, _Traits>&
1021flush(basic_ostream<_CharT, _Traits>& __os)
1022{
1023 __os.flush();
1024 return __os;
1025}
1026
Eric Fiselier78ccf772017-04-18 23:38:41 +00001027#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001028
Louis Dionned9f73b12020-09-23 08:49:00 -04001029template <class _Stream, class _Tp, class = void>
1030struct __is_ostreamable : false_type { };
1031
Howard Hinnantc51e1022010-05-11 19:42:16 +00001032template <class _Stream, class _Tp>
Louis Dionned9f73b12020-09-23 08:49:00 -04001033struct __is_ostreamable<_Stream, _Tp, decltype(
Arthur O'Dwyer3285c342021-05-10 13:04:16 -04001034 declval<_Stream>() << declval<_Tp>(), void()
Louis Dionned9f73b12020-09-23 08:49:00 -04001035)> : true_type { };
1036
1037template <class _Stream, class _Tp, class = typename enable_if<
1038 _And<is_base_of<ios_base, _Stream>,
1039 __is_ostreamable<_Stream&, const _Tp&>>::value
1040>::type>
1041_LIBCPP_INLINE_VISIBILITY
1042_Stream&& operator<<(_Stream&& __os, const _Tp& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043{
1044 __os << __x;
Howard Hinnantc8697b62012-01-12 23:37:51 +00001045 return _VSTD::move(__os);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046}
1047
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001048#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001049
1050template<class _CharT, class _Traits, class _Allocator>
1051basic_ostream<_CharT, _Traits>&
1052operator<<(basic_ostream<_CharT, _Traits>& __os,
1053 const basic_string<_CharT, _Traits, _Allocator>& __str)
1054{
Marshall Clow4bc2be22014-02-16 01:57:26 +00001055 return _VSTD::__put_character_sequence(__os, __str.data(), __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001056}
1057
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001058template<class _CharT, class _Traits>
1059basic_ostream<_CharT, _Traits>&
1060operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier14116922019-09-25 18:56:54 +00001061 basic_string_view<_CharT, _Traits> __sv)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001062{
1063 return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
1064}
1065
Howard Hinnantc51e1022010-05-11 19:42:16 +00001066template <class _CharT, class _Traits>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +00001067inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001068basic_ostream<_CharT, _Traits>&
1069operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
1070{
1071 return __os << __ec.category().name() << ':' << __ec.value();
1072}
1073
Howard Hinnantc834c512011-11-29 18:15:50 +00001074template<class _CharT, class _Traits, class _Yp>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +00001075inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001076basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00001077operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001078{
1079 return __os << __p.get();
1080}
1081
Marshall Clowe52a3242017-11-27 15:51:36 +00001082template<class _CharT, class _Traits, class _Yp, class _Dp>
1083inline _LIBCPP_INLINE_VISIBILITY
1084typename enable_if
1085<
Marshall Cloweea0a1d2018-03-22 18:27:28 +00001086 is_same<void, typename __void_t<decltype((declval<basic_ostream<_CharT, _Traits>&>() << declval<typename unique_ptr<_Yp, _Dp>::pointer>()))>::type>::value,
Marshall Clowe52a3242017-11-27 15:51:36 +00001087 basic_ostream<_CharT, _Traits>&
1088>::type
1089operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p)
1090{
1091 return __os << __p.get();
1092}
1093
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094template <class _CharT, class _Traits, size_t _Size>
1095basic_ostream<_CharT, _Traits>&
Howard Hinnantfa65ab62011-04-05 14:55:28 +00001096operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001097{
1098 return __os << __x.template to_string<_CharT, _Traits>
1099 (use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
1100 use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
1101}
1102
Eric Fiselier1b57fa82016-09-15 22:27:07 +00001103_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>)
1104_LIBCPP_EXTERN_TEMPLATE(class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001105
1106_LIBCPP_END_NAMESPACE_STD
1107
Louis Dionne2b1ceaa2021-04-20 12:03:32 -04001108#endif // _LIBCPP_OSTREAM