blob: 697732d54e6d806bdae5bbf9d3425648d9ed2195 [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
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>
Marshall Clow8732fed2018-12-11 04:35:44 +0000143#include <version>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000144
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000145#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000146#pragma GCC system_header
Howard Hinnantaaaa52b2011-10-17 20:05:10 +0000147#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000148
149_LIBCPP_BEGIN_NAMESPACE_STD
150
151template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000152class _LIBCPP_TEMPLATE_VIS basic_ostream
Howard Hinnantc51e1022010-05-11 19:42:16 +0000153 : 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:
Louis Dionneb4d05d72018-10-16 19:26:23 +0000164 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Eric Fiselier815ed732016-09-16 00:00:48 +0000165 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb)
166 { this->init(__sb); }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000167 virtual ~basic_ostream();
168protected:
Eric Fiselier78ccf772017-04-18 23:38:41 +0000169#ifndef _LIBCPP_CXX03_LANG
Eric Fiselier815ed732016-09-16 00:00:48 +0000170 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000171 basic_ostream(basic_ostream&& __rhs);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000172
173 // 27.7.2.3 Assign/swap
Eric Fiselier815ed732016-09-16 00:00:48 +0000174 inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000175 basic_ostream& operator=(basic_ostream&& __rhs);
176#endif
Louis Dionneb4d05d72018-10-16 19:26:23 +0000177 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Eric Fiselier815ed732016-09-16 00:00:48 +0000178 void swap(basic_ostream& __rhs)
179 { basic_ios<char_type, traits_type>::swap(__rhs); }
Marshall Clow27d29872014-09-16 15:27:01 +0000180
Eric Fiselier2d8515f2017-01-06 20:58:25 +0000181#ifndef _LIBCPP_CXX03_LANG
Marshall Clow242d9c12014-09-16 15:33:53 +0000182 basic_ostream (const basic_ostream& __rhs) = delete;
183 basic_ostream& operator=(const basic_ostream& __rhs) = delete;
Marshall Clow27d29872014-09-16 15:27:01 +0000184#else
Marshall Clowd37f25c2014-09-17 01:58:15 +0000185 basic_ostream (const basic_ostream& __rhs); // not defined
186 basic_ostream& operator=(const basic_ostream& __rhs); // not defined
Marshall Clow27d29872014-09-16 15:27:01 +0000187#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +0000188public:
189
190 // 27.7.2.4 Prefix/suffix:
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000191 class _LIBCPP_TEMPLATE_VIS sentry;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000192
193 // 27.7.2.6 Formatted output:
Louis Dionneb4d05d72018-10-16 19:26:23 +0000194 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Eric Fiselier815ed732016-09-16 00:00:48 +0000195 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&))
196 { return __pf(*this); }
197
Louis Dionneb4d05d72018-10-16 19:26:23 +0000198 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000199 basic_ostream& operator<<(basic_ios<char_type, traits_type>&
Eric Fiselier815ed732016-09-16 00:00:48 +0000200 (*__pf)(basic_ios<char_type,traits_type>&))
201 { __pf(*this); return *this; }
202
Louis Dionneb4d05d72018-10-16 19:26:23 +0000203 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Eric Fiselier815ed732016-09-16 00:00:48 +0000204 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&))
205 { __pf(*this); return *this; }
206
Howard Hinnantc51e1022010-05-11 19:42:16 +0000207 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
Marshall Clow706862d2019-07-01 16:20:25 +0000222 _LIBCPP_INLINE_VISIBILITY
223 basic_ostream& operator<<(nullptr_t)
224 { return *this << "nullptr"; }
225
Howard Hinnantc51e1022010-05-11 19:42:16 +0000226 // 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:
Louis Dionneb4d05d72018-10-16 19:26:23 +0000232 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000233 pos_type tellp();
Louis Dionneb4d05d72018-10-16 19:26:23 +0000234 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000235 basic_ostream& seekp(pos_type __pos);
Louis Dionneb4d05d72018-10-16 19:26:23 +0000236 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1
Howard Hinnantc51e1022010-05-11 19:42:16 +0000237 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
238
239protected:
Louis Dionne16fe2952018-07-11 23:14:33 +0000240 _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +0000241 basic_ostream() {} // extension, intentially does not initialize
242};
243
244template <class _CharT, class _Traits>
Eric Fiselierb5eb1bf2017-01-04 23:56:00 +0000245class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry
Howard Hinnantc51e1022010-05-11 19:42:16 +0000246{
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
Louis Dionne16fe2952018-07-11 23:14:33 +0000257 _LIBCPP_INLINE_VISIBILITY
Howard Hinnant86a291f2012-02-21 21:46:43 +0000258 _LIBCPP_EXPLICIT
Howard Hinnantc51e1022010-05-11 19:42:16 +0000259 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000284#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000285 if (__os_.rdbuf()->pubsync() == -1)
286 __os_.setstate(ios_base::badbit);
287#ifndef _LIBCPP_NO_EXCEPTIONS
288 }
289 catch (...)
290 {
291 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000292#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000293 }
294}
295
Eric Fiselier78ccf772017-04-18 23:38:41 +0000296#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000297
298template <class _CharT, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000299basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs)
300{
301 this->move(__rhs);
302}
303
304template <class _CharT, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000305basic_ostream<_CharT, _Traits>&
306basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs)
307{
308 swap(__rhs);
309 return *this;
310}
311
Eric Fiselier78ccf772017-04-18 23:38:41 +0000312#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +0000313
314template <class _CharT, class _Traits>
315basic_ostream<_CharT, _Traits>::~basic_ostream()
316{
317}
318
319template <class _CharT, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000320basic_ostream<_CharT, _Traits>&
321basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb)
322{
323#ifndef _LIBCPP_NO_EXCEPTIONS
324 try
325 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000326#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000327 sentry __s(*this);
328 if (__s)
329 {
330 if (__sb)
331 {
332#ifndef _LIBCPP_NO_EXCEPTIONS
333 try
334 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000335#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc834c512011-11-29 18:15:50 +0000336 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
337 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
338 _Ip __i(__sb);
339 _Ip __eof;
340 _Op __o(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000341 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000356#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000357 }
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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000367#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000368 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000378#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000379 sentry __s(*this);
380 if (__s)
381 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000382 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
383 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000384 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000393#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000394 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000404#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000405 sentry __s(*this);
406 if (__s)
407 {
408 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
Howard Hinnantc834c512011-11-29 18:15:50 +0000409 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
410 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000411 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000423#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000424 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000434#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000435 sentry __s(*this);
436 if (__s)
437 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000438 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
439 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000440 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000449#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000450 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000460#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000461 sentry __s(*this);
462 if (__s)
463 {
464 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
Howard Hinnantc834c512011-11-29 18:15:50 +0000465 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
466 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000467 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000479#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000480 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000490#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000491 sentry __s(*this);
492 if (__s)
493 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000494 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
495 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000496 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000505#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000506 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000516#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000517 sentry __s(*this);
518 if (__s)
519 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000520 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
521 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000522 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000531#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000532 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000542#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000543 sentry __s(*this);
544 if (__s)
545 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000546 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
547 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000548 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000557#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000558 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000568#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000569 sentry __s(*this);
570 if (__s)
571 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000572 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
573 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000574 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000583#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000584 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000594#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000595 sentry __s(*this);
596 if (__s)
597 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000598 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
599 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000600 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000609#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000610 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000620#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000621 sentry __s(*this);
622 if (__s)
623 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000624 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
625 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000626 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000635#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000636 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000646#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000647 sentry __s(*this);
648 if (__s)
649 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000650 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
651 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000652 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000661#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000662 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000672#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000673 sentry __s(*this);
674 if (__s)
675 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000676 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
677 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000678 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000687#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000688 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000698#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000699 sentry __s(*this);
700 if (__s)
701 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000702 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
703 const _Fp& __f = use_facet<_Fp>(this->getloc());
Howard Hinnantc51e1022010-05-11 19:42:16 +0000704 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000713#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000714 return *this;
715}
716
717template<class _CharT, class _Traits>
718basic_ostream<_CharT, _Traits>&
Marshall Clow00995642013-12-10 19:25:49 +0000719__put_character_sequence(basic_ostream<_CharT, _Traits>& __os,
720 const _CharT* __str, size_t __len)
Howard Hinnantc51e1022010-05-11 19:42:16 +0000721{
722#ifndef _LIBCPP_NO_EXCEPTIONS
723 try
724 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000725#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000726 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
727 if (__s)
728 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000729 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
730 if (__pad_and_output(_Ip(__os),
Marshall Clow00995642013-12-10 19:25:49 +0000731 __str,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000732 (__os.flags() & ios_base::adjustfield) == ios_base::left ?
Marshall Clow00995642013-12-10 19:25:49 +0000733 __str + __len :
734 __str,
735 __str + __len,
Howard Hinnantc51e1022010-05-11 19:42:16 +0000736 __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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000746#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000747 return __os;
748}
749
Volodymyr Sapsai4c14a922018-09-19 23:31:34 +0000750
Marshall Clow00995642013-12-10 19:25:49 +0000751template<class _CharT, class _Traits>
752basic_ostream<_CharT, _Traits>&
753operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c)
754{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000755 return _VSTD::__put_character_sequence(__os, &__c, 1);
Marshall Clow00995642013-12-10 19:25:49 +0000756}
757
Howard Hinnantc51e1022010-05-11 19:42:16 +0000758template<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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000765#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000766 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
767 if (__s)
768 {
769 _CharT __c = __os.widen(__cn);
Howard Hinnantc834c512011-11-29 18:15:50 +0000770 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
771 if (__pad_and_output(_Ip(__os),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000772 &__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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000787#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000788 return __os;
789}
790
791template<class _Traits>
792basic_ostream<char, _Traits>&
793operator<<(basic_ostream<char, _Traits>& __os, char __c)
794{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000795 return _VSTD::__put_character_sequence(__os, &__c, 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000796}
797
798template<class _Traits>
799basic_ostream<char, _Traits>&
800operator<<(basic_ostream<char, _Traits>& __os, signed char __c)
801{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000802 return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000803}
804
805template<class _Traits>
806basic_ostream<char, _Traits>&
807operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c)
808{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000809 return _VSTD::__put_character_sequence(__os, (char *) &__c, 1);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000810}
811
812template<class _CharT, class _Traits>
813basic_ostream<_CharT, _Traits>&
814operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str)
815{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000816 return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000817}
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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000826#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000827 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
828 if (__s)
829 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000830 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
Howard Hinnantc51e1022010-05-11 19:42:16 +0000831 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);
Howard Hinnantc834c512011-11-29 18:15:50 +0000845 if (__pad_and_output(_Ip(__os),
Howard Hinnantc51e1022010-05-11 19:42:16 +0000846 __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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000861#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000862 return __os;
863}
864
865template<class _Traits>
866basic_ostream<char, _Traits>&
867operator<<(basic_ostream<char, _Traits>& __os, const char* __str)
868{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000869 return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000870}
871
872template<class _Traits>
873basic_ostream<char, _Traits>&
874operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str)
875{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000876 const char *__s = (const char *) __str;
877 return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000878}
879
880template<class _Traits>
881basic_ostream<char, _Traits>&
882operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str)
883{
Marshall Clow4bc2be22014-02-16 01:57:26 +0000884 const char *__s = (const char *) __str;
885 return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s));
Howard Hinnantc51e1022010-05-11 19:42:16 +0000886}
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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000895#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000896 sentry __s(*this);
897 if (__s)
898 {
Howard Hinnantc834c512011-11-29 18:15:50 +0000899 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
900 _Op __o(*this);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000901 *__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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000911#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000912 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000922#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000923 sentry __sen(*this);
924 if (__sen && __n)
925 {
Howard Hinnant79cef932013-01-15 17:22:03 +0000926 if (this->rdbuf()->sputn(__s, __n) != __n)
927 this->setstate(ios_base::badbit);
Howard Hinnantc51e1022010-05-11 19:42:16 +0000928 }
929#ifndef _LIBCPP_NO_EXCEPTIONS
930 }
931 catch (...)
932 {
933 this->__set_badbit_and_consider_rethrow();
934 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000935#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000936 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 {
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000946#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000947 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 }
Howard Hinnant3b6579a2010-08-22 00:02:43 +0000962#endif // _LIBCPP_NO_EXCEPTIONS
Howard Hinnantc51e1022010-05-11 19:42:16 +0000963 return *this;
964}
965
966template <class _CharT, class _Traits>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000967typename 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>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000976basic_ostream<_CharT, _Traits>&
977basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
978{
Marshall Clow29c4daa2013-10-31 22:20:45 +0000979 sentry __s(*this);
Marshall Clow8aa079a2015-06-22 15:01:21 +0000980 if (!this->fail())
Howard Hinnantc51e1022010-05-11 19:42:16 +0000981 {
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>
Howard Hinnantc51e1022010-05-11 19:42:16 +0000989basic_ostream<_CharT, _Traits>&
990basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir)
991{
Marshall Clow29c4daa2013-10-31 22:20:45 +0000992 sentry __s(*this);
Marshall Clow8aa079a2015-06-22 15:01:21 +0000993 if (!this->fail())
Marshall Clow29c4daa2013-10-31 22:20:45 +0000994 {
995 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
996 this->setstate(ios_base::failbit);
997 }
Howard Hinnantc51e1022010-05-11 19:42:16 +0000998 return *this;
999}
1000
1001template <class _CharT, class _Traits>
Louis Dionnee29136f2020-07-13 11:53:48 -04001002inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001003basic_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>
Louis Dionnee29136f2020-07-13 11:53:48 -04001012inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001013basic_ostream<_CharT, _Traits>&
1014ends(basic_ostream<_CharT, _Traits>& __os)
1015{
1016 __os.put(_CharT());
1017 return __os;
1018}
1019
1020template <class _CharT, class _Traits>
Louis Dionnee29136f2020-07-13 11:53:48 -04001021inline
Howard Hinnantc51e1022010-05-11 19:42:16 +00001022basic_ostream<_CharT, _Traits>&
1023flush(basic_ostream<_CharT, _Traits>& __os)
1024{
1025 __os.flush();
1026 return __os;
1027}
1028
Eric Fiselier78ccf772017-04-18 23:38:41 +00001029#ifndef _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001030
1031template <class _Stream, class _Tp>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +00001032inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001033typename enable_if
1034<
1035 !is_lvalue_reference<_Stream>::value &&
1036 is_base_of<ios_base, _Stream>::value,
Howard Hinnantc8697b62012-01-12 23:37:51 +00001037 _Stream&&
Howard Hinnantc51e1022010-05-11 19:42:16 +00001038>::type
1039operator<<(_Stream&& __os, const _Tp& __x)
1040{
1041 __os << __x;
Howard Hinnantc8697b62012-01-12 23:37:51 +00001042 return _VSTD::move(__os);
Howard Hinnantc51e1022010-05-11 19:42:16 +00001043}
1044
Eric Fiselier78ccf772017-04-18 23:38:41 +00001045#endif // _LIBCPP_CXX03_LANG
Howard Hinnantc51e1022010-05-11 19:42:16 +00001046
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{
Marshall Clow4bc2be22014-02-16 01:57:26 +00001052 return _VSTD::__put_character_sequence(__os, __str.data(), __str.size());
Howard Hinnantc51e1022010-05-11 19:42:16 +00001053}
1054
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001055template<class _CharT, class _Traits>
1056basic_ostream<_CharT, _Traits>&
1057operator<<(basic_ostream<_CharT, _Traits>& __os,
Eric Fiselier14116922019-09-25 18:56:54 +00001058 basic_string_view<_CharT, _Traits> __sv)
Marshall Clowdf63a6d2016-07-21 05:31:24 +00001059{
1060 return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size());
1061}
1062
Howard Hinnantc51e1022010-05-11 19:42:16 +00001063template <class _CharT, class _Traits>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +00001064inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001065basic_ostream<_CharT, _Traits>&
1066operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec)
1067{
1068 return __os << __ec.category().name() << ':' << __ec.value();
1069}
1070
Howard Hinnantc834c512011-11-29 18:15:50 +00001071template<class _CharT, class _Traits, class _Yp>
Evgeniy Stepanovb9254262016-01-08 19:21:02 +00001072inline _LIBCPP_INLINE_VISIBILITY
Howard Hinnantc51e1022010-05-11 19:42:16 +00001073basic_ostream<_CharT, _Traits>&
Howard Hinnantc834c512011-11-29 18:15:50 +00001074operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001075{
1076 return __os << __p.get();
1077}
1078
Marshall Clowe52a3242017-11-27 15:51:36 +00001079template<class _CharT, class _Traits, class _Yp, class _Dp>
1080inline _LIBCPP_INLINE_VISIBILITY
1081typename enable_if
1082<
Marshall Cloweea0a1d2018-03-22 18:27:28 +00001083 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 +00001084 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
Howard Hinnantc51e1022010-05-11 19:42:16 +00001091template <class _CharT, class _Traits, size_t _Size>
1092basic_ostream<_CharT, _Traits>&
Howard Hinnantfa65ab62011-04-05 14:55:28 +00001093operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x)
Howard Hinnantc51e1022010-05-11 19:42:16 +00001094{
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
Louis Dionne207a9102018-12-06 00:24:58 +00001100#ifndef _LIBCPP_DO_NOT_ASSUME_STREAMS_EXPLICIT_INSTANTIATION_IN_DYLIB
Eric Fiselier1b57fa82016-09-15 22:27:07 +00001101_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>)
Mehdi Amini228053d2017-05-04 17:08:54 +00001103#endif
Howard Hinnantc51e1022010-05-11 19:42:16 +00001104
1105_LIBCPP_END_NAMESPACE_STD
1106
1107#endif // _LIBCPP_OSTREAM