[NFC] Maintainers.rst: align email address formatting
[llvm-project.git] / libcxx / include / __ostream / basic_ostream.h
blobad43c72a3c2eae52c98606f8127fda288c8b7417
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 //===---------------------------------------------------------------------===//
9 #ifndef _LIBCPP___OSTREAM_BASIC_OSTREAM_H
10 #define _LIBCPP___OSTREAM_BASIC_OSTREAM_H
12 #include <__config>
14 #if _LIBCPP_HAS_LOCALIZATION
16 # include <__exception/operations.h>
17 # include <__memory/shared_ptr.h>
18 # include <__memory/unique_ptr.h>
19 # include <__ostream/put_character_sequence.h>
20 # include <__system_error/error_code.h>
21 # include <__type_traits/conjunction.h>
22 # include <__type_traits/enable_if.h>
23 # include <__type_traits/is_base_of.h>
24 # include <__type_traits/void_t.h>
25 # include <__utility/declval.h>
26 # include <bitset>
27 # include <ios>
28 # include <locale>
29 # include <new> // for __throw_bad_alloc
30 # include <streambuf>
31 # include <string_view>
33 # if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
34 # pragma GCC system_header
35 # endif
37 _LIBCPP_PUSH_MACROS
38 # include <__undef_macros>
40 _LIBCPP_BEGIN_NAMESPACE_STD
42 template <class _CharT, class _Traits>
43 class _LIBCPP_TEMPLATE_VIS basic_ostream : virtual public basic_ios<_CharT, _Traits> {
44 public:
45 // types (inherited from basic_ios (27.5.4)):
46 typedef _CharT char_type;
47 typedef _Traits traits_type;
48 typedef typename traits_type::int_type int_type;
49 typedef typename traits_type::pos_type pos_type;
50 typedef typename traits_type::off_type off_type;
52 // 27.7.2.2 Constructor/destructor:
53 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb) {
54 this->init(__sb);
56 ~basic_ostream() override;
58 basic_ostream(const basic_ostream& __rhs) = delete;
59 basic_ostream& operator=(const basic_ostream& __rhs) = delete;
61 protected:
62 inline _LIBCPP_HIDE_FROM_ABI basic_ostream(basic_ostream&& __rhs);
64 // 27.7.2.3 Assign/swap
65 inline _LIBCPP_HIDE_FROM_ABI basic_ostream& operator=(basic_ostream&& __rhs);
67 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_ostream& __rhs) {
68 basic_ios<char_type, traits_type>::swap(__rhs);
71 public:
72 // 27.7.2.4 Prefix/suffix:
73 class _LIBCPP_TEMPLATE_VIS sentry;
75 // 27.7.2.6 Formatted output:
76 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&)) {
77 return __pf(*this);
80 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream&
81 operator<<(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) {
82 __pf(*this);
83 return *this;
86 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&)) {
87 __pf(*this);
88 return *this;
91 basic_ostream& operator<<(bool __n);
92 basic_ostream& operator<<(short __n);
93 basic_ostream& operator<<(unsigned short __n);
94 basic_ostream& operator<<(int __n);
95 basic_ostream& operator<<(unsigned int __n);
96 basic_ostream& operator<<(long __n);
97 basic_ostream& operator<<(unsigned long __n);
98 basic_ostream& operator<<(long long __n);
99 basic_ostream& operator<<(unsigned long long __n);
100 basic_ostream& operator<<(float __f);
101 basic_ostream& operator<<(double __f);
102 basic_ostream& operator<<(long double __f);
103 basic_ostream& operator<<(const void* __p);
105 # if _LIBCPP_STD_VER >= 23
106 _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(const volatile void* __p) {
107 return operator<<(const_cast<const void*>(__p));
109 # endif
111 basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
113 # if _LIBCPP_STD_VER >= 17
114 // LWG 2221 - nullptr. This is not backported to older standards modes.
115 // See https://reviews.llvm.org/D127033 for more info on the rationale.
116 _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(nullptr_t) { return *this << "nullptr"; }
117 # endif
119 // 27.7.2.7 Unformatted output:
120 basic_ostream& put(char_type __c);
121 basic_ostream& write(const char_type* __s, streamsize __n);
122 basic_ostream& flush();
124 // 27.7.2.5 seeks:
125 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type tellp();
126 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(pos_type __pos);
127 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
129 protected:
130 _LIBCPP_HIDE_FROM_ABI basic_ostream() {} // extension, intentially does not initialize
133 template <class _CharT, class _Traits>
134 class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry {
135 bool __ok_;
136 basic_ostream<_CharT, _Traits>& __os_;
138 public:
139 explicit sentry(basic_ostream<_CharT, _Traits>& __os);
140 ~sentry();
141 sentry(const sentry&) = delete;
142 sentry& operator=(const sentry&) = delete;
144 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }
147 template <class _CharT, class _Traits>
148 basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os) : __ok_(false), __os_(__os) {
149 if (__os.good()) {
150 if (__os.tie())
151 __os.tie()->flush();
152 __ok_ = true;
156 template <class _CharT, class _Traits>
157 basic_ostream<_CharT, _Traits>::sentry::~sentry() {
158 if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && uncaught_exceptions() == 0) {
159 # if _LIBCPP_HAS_EXCEPTIONS
160 try {
161 # endif // _LIBCPP_HAS_EXCEPTIONS
162 if (__os_.rdbuf()->pubsync() == -1)
163 __os_.setstate(ios_base::badbit);
164 # if _LIBCPP_HAS_EXCEPTIONS
165 } catch (...) {
167 # endif // _LIBCPP_HAS_EXCEPTIONS
171 template <class _CharT, class _Traits>
172 basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs) {
173 this->move(__rhs);
176 template <class _CharT, class _Traits>
177 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs) {
178 swap(__rhs);
179 return *this;
182 template <class _CharT, class _Traits>
183 basic_ostream<_CharT, _Traits>::~basic_ostream() {}
185 template <class _CharT, class _Traits>
186 basic_ostream<_CharT, _Traits>&
187 basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb) {
188 # if _LIBCPP_HAS_EXCEPTIONS
189 try {
190 # endif // _LIBCPP_HAS_EXCEPTIONS
191 sentry __s(*this);
192 if (__s) {
193 if (__sb) {
194 # if _LIBCPP_HAS_EXCEPTIONS
195 try {
196 # endif // _LIBCPP_HAS_EXCEPTIONS
197 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
198 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
199 _Ip __i(__sb);
200 _Ip __eof;
201 _Op __o(*this);
202 size_t __c = 0;
203 for (; __i != __eof; ++__i, ++__o, ++__c) {
204 *__o = *__i;
205 if (__o.failed())
206 break;
208 if (__c == 0)
209 this->setstate(ios_base::failbit);
210 # if _LIBCPP_HAS_EXCEPTIONS
211 } catch (...) {
212 this->__set_failbit_and_consider_rethrow();
214 # endif // _LIBCPP_HAS_EXCEPTIONS
215 } else
216 this->setstate(ios_base::badbit);
218 # if _LIBCPP_HAS_EXCEPTIONS
219 } catch (...) {
220 this->__set_badbit_and_consider_rethrow();
222 # endif // _LIBCPP_HAS_EXCEPTIONS
223 return *this;
226 template <class _CharT, class _Traits>
227 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __n) {
228 # if _LIBCPP_HAS_EXCEPTIONS
229 try {
230 # endif // _LIBCPP_HAS_EXCEPTIONS
231 sentry __s(*this);
232 if (__s) {
233 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
234 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
235 if (__f.put(*this, *this, this->fill(), __n).failed())
236 this->setstate(ios_base::badbit | ios_base::failbit);
238 # if _LIBCPP_HAS_EXCEPTIONS
239 } catch (...) {
240 this->__set_badbit_and_consider_rethrow();
242 # endif // _LIBCPP_HAS_EXCEPTIONS
243 return *this;
246 template <class _CharT, class _Traits>
247 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(short __n) {
248 # if _LIBCPP_HAS_EXCEPTIONS
249 try {
250 # endif // _LIBCPP_HAS_EXCEPTIONS
251 sentry __s(*this);
252 if (__s) {
253 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
254 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
255 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
256 if (__f.put(*this,
257 *this,
258 this->fill(),
259 __flags == ios_base::oct || __flags == ios_base::hex
260 ? static_cast<long>(static_cast<unsigned short>(__n))
261 : static_cast<long>(__n))
262 .failed())
263 this->setstate(ios_base::badbit | ios_base::failbit);
265 # if _LIBCPP_HAS_EXCEPTIONS
266 } catch (...) {
267 this->__set_badbit_and_consider_rethrow();
269 # endif // _LIBCPP_HAS_EXCEPTIONS
270 return *this;
273 template <class _CharT, class _Traits>
274 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n) {
275 # if _LIBCPP_HAS_EXCEPTIONS
276 try {
277 # endif // _LIBCPP_HAS_EXCEPTIONS
278 sentry __s(*this);
279 if (__s) {
280 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
281 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
282 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
283 this->setstate(ios_base::badbit | ios_base::failbit);
285 # if _LIBCPP_HAS_EXCEPTIONS
286 } catch (...) {
287 this->__set_badbit_and_consider_rethrow();
289 # endif // _LIBCPP_HAS_EXCEPTIONS
290 return *this;
293 template <class _CharT, class _Traits>
294 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __n) {
295 # if _LIBCPP_HAS_EXCEPTIONS
296 try {
297 # endif // _LIBCPP_HAS_EXCEPTIONS
298 sentry __s(*this);
299 if (__s) {
300 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
301 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
302 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
303 if (__f.put(*this,
304 *this,
305 this->fill(),
306 __flags == ios_base::oct || __flags == ios_base::hex
307 ? static_cast<long>(static_cast<unsigned int>(__n))
308 : static_cast<long>(__n))
309 .failed())
310 this->setstate(ios_base::badbit | ios_base::failbit);
312 # if _LIBCPP_HAS_EXCEPTIONS
313 } catch (...) {
314 this->__set_badbit_and_consider_rethrow();
316 # endif // _LIBCPP_HAS_EXCEPTIONS
317 return *this;
320 template <class _CharT, class _Traits>
321 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n) {
322 # if _LIBCPP_HAS_EXCEPTIONS
323 try {
324 # endif // _LIBCPP_HAS_EXCEPTIONS
325 sentry __s(*this);
326 if (__s) {
327 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
328 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
329 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
330 this->setstate(ios_base::badbit | ios_base::failbit);
332 # if _LIBCPP_HAS_EXCEPTIONS
333 } catch (...) {
334 this->__set_badbit_and_consider_rethrow();
336 # endif // _LIBCPP_HAS_EXCEPTIONS
337 return *this;
340 template <class _CharT, class _Traits>
341 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) {
342 # if _LIBCPP_HAS_EXCEPTIONS
343 try {
344 # endif // _LIBCPP_HAS_EXCEPTIONS
345 sentry __s(*this);
346 if (__s) {
347 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
348 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
349 if (__f.put(*this, *this, this->fill(), __n).failed())
350 this->setstate(ios_base::badbit | ios_base::failbit);
352 # if _LIBCPP_HAS_EXCEPTIONS
353 } catch (...) {
354 this->__set_badbit_and_consider_rethrow();
356 # endif // _LIBCPP_HAS_EXCEPTIONS
357 return *this;
360 template <class _CharT, class _Traits>
361 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n) {
362 # if _LIBCPP_HAS_EXCEPTIONS
363 try {
364 # endif // _LIBCPP_HAS_EXCEPTIONS
365 sentry __s(*this);
366 if (__s) {
367 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
368 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
369 if (__f.put(*this, *this, this->fill(), __n).failed())
370 this->setstate(ios_base::badbit | ios_base::failbit);
372 # if _LIBCPP_HAS_EXCEPTIONS
373 } catch (...) {
374 this->__set_badbit_and_consider_rethrow();
376 # endif // _LIBCPP_HAS_EXCEPTIONS
377 return *this;
380 template <class _CharT, class _Traits>
381 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long long __n) {
382 # if _LIBCPP_HAS_EXCEPTIONS
383 try {
384 # endif // _LIBCPP_HAS_EXCEPTIONS
385 sentry __s(*this);
386 if (__s) {
387 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
388 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
389 if (__f.put(*this, *this, this->fill(), __n).failed())
390 this->setstate(ios_base::badbit | ios_base::failbit);
392 # if _LIBCPP_HAS_EXCEPTIONS
393 } catch (...) {
394 this->__set_badbit_and_consider_rethrow();
396 # endif // _LIBCPP_HAS_EXCEPTIONS
397 return *this;
400 template <class _CharT, class _Traits>
401 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n) {
402 # if _LIBCPP_HAS_EXCEPTIONS
403 try {
404 # endif // _LIBCPP_HAS_EXCEPTIONS
405 sentry __s(*this);
406 if (__s) {
407 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
408 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
409 if (__f.put(*this, *this, this->fill(), __n).failed())
410 this->setstate(ios_base::badbit | ios_base::failbit);
412 # if _LIBCPP_HAS_EXCEPTIONS
413 } catch (...) {
414 this->__set_badbit_and_consider_rethrow();
416 # endif // _LIBCPP_HAS_EXCEPTIONS
417 return *this;
420 template <class _CharT, class _Traits>
421 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __n) {
422 # if _LIBCPP_HAS_EXCEPTIONS
423 try {
424 # endif // _LIBCPP_HAS_EXCEPTIONS
425 sentry __s(*this);
426 if (__s) {
427 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
428 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
429 if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
430 this->setstate(ios_base::badbit | ios_base::failbit);
432 # if _LIBCPP_HAS_EXCEPTIONS
433 } catch (...) {
434 this->__set_badbit_and_consider_rethrow();
436 # endif // _LIBCPP_HAS_EXCEPTIONS
437 return *this;
440 template <class _CharT, class _Traits>
441 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __n) {
442 # if _LIBCPP_HAS_EXCEPTIONS
443 try {
444 # endif // _LIBCPP_HAS_EXCEPTIONS
445 sentry __s(*this);
446 if (__s) {
447 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
448 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
449 if (__f.put(*this, *this, this->fill(), __n).failed())
450 this->setstate(ios_base::badbit | ios_base::failbit);
452 # if _LIBCPP_HAS_EXCEPTIONS
453 } catch (...) {
454 this->__set_badbit_and_consider_rethrow();
456 # endif // _LIBCPP_HAS_EXCEPTIONS
457 return *this;
460 template <class _CharT, class _Traits>
461 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __n) {
462 # if _LIBCPP_HAS_EXCEPTIONS
463 try {
464 # endif // _LIBCPP_HAS_EXCEPTIONS
465 sentry __s(*this);
466 if (__s) {
467 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
468 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
469 if (__f.put(*this, *this, this->fill(), __n).failed())
470 this->setstate(ios_base::badbit | ios_base::failbit);
472 # if _LIBCPP_HAS_EXCEPTIONS
473 } catch (...) {
474 this->__set_badbit_and_consider_rethrow();
476 # endif // _LIBCPP_HAS_EXCEPTIONS
477 return *this;
480 template <class _CharT, class _Traits>
481 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __n) {
482 # if _LIBCPP_HAS_EXCEPTIONS
483 try {
484 # endif // _LIBCPP_HAS_EXCEPTIONS
485 sentry __s(*this);
486 if (__s) {
487 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
488 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
489 if (__f.put(*this, *this, this->fill(), __n).failed())
490 this->setstate(ios_base::badbit | ios_base::failbit);
492 # if _LIBCPP_HAS_EXCEPTIONS
493 } catch (...) {
494 this->__set_badbit_and_consider_rethrow();
496 # endif // _LIBCPP_HAS_EXCEPTIONS
497 return *this;
500 template <class _CharT, class _Traits>
501 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) {
502 return std::__put_character_sequence(__os, &__c, 1);
505 template <class _CharT, class _Traits>
506 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn) {
507 # if _LIBCPP_HAS_EXCEPTIONS
508 try {
509 # endif // _LIBCPP_HAS_EXCEPTIONS
510 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
511 if (__s) {
512 _CharT __c = __os.widen(__cn);
513 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
514 if (std::__pad_and_output(
515 _Ip(__os),
516 &__c,
517 (__os.flags() & ios_base::adjustfield) == ios_base::left ? &__c + 1 : &__c,
518 &__c + 1,
519 __os,
520 __os.fill())
521 .failed())
522 __os.setstate(ios_base::badbit | ios_base::failbit);
524 # if _LIBCPP_HAS_EXCEPTIONS
525 } catch (...) {
526 __os.__set_badbit_and_consider_rethrow();
528 # endif // _LIBCPP_HAS_EXCEPTIONS
529 return __os;
532 template <class _Traits>
533 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, char __c) {
534 return std::__put_character_sequence(__os, &__c, 1);
537 template <class _Traits>
538 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, signed char __c) {
539 return std::__put_character_sequence(__os, (char*)&__c, 1);
542 template <class _Traits>
543 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) {
544 return std::__put_character_sequence(__os, (char*)&__c, 1);
547 template <class _CharT, class _Traits>
548 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
549 operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str) {
550 return std::__put_character_sequence(__os, __str, _Traits::length(__str));
553 template <class _CharT, class _Traits>
554 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
555 operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn) {
556 # if _LIBCPP_HAS_EXCEPTIONS
557 try {
558 # endif // _LIBCPP_HAS_EXCEPTIONS
559 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
560 if (__s) {
561 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
562 size_t __len = char_traits<char>::length(__strn);
563 const int __bs = 100;
564 _CharT __wbb[__bs];
565 _CharT* __wb = __wbb;
566 unique_ptr<_CharT, void (*)(void*)> __h(0, free);
567 if (__len > __bs) {
568 __wb = (_CharT*)malloc(__len * sizeof(_CharT));
569 if (__wb == 0)
570 __throw_bad_alloc();
571 __h.reset(__wb);
573 for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
574 *__p = __os.widen(*__strn);
575 if (std::__pad_and_output(
576 _Ip(__os),
577 __wb,
578 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __wb + __len : __wb,
579 __wb + __len,
580 __os,
581 __os.fill())
582 .failed())
583 __os.setstate(ios_base::badbit | ios_base::failbit);
585 # if _LIBCPP_HAS_EXCEPTIONS
586 } catch (...) {
587 __os.__set_badbit_and_consider_rethrow();
589 # endif // _LIBCPP_HAS_EXCEPTIONS
590 return __os;
593 template <class _Traits>
594 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, const char* __str) {
595 return std::__put_character_sequence(__os, __str, _Traits::length(__str));
598 template <class _Traits>
599 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
600 operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str) {
601 const char* __s = (const char*)__str;
602 return std::__put_character_sequence(__os, __s, _Traits::length(__s));
605 template <class _Traits>
606 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
607 operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str) {
608 const char* __s = (const char*)__str;
609 return std::__put_character_sequence(__os, __s, _Traits::length(__s));
612 template <class _CharT, class _Traits>
613 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::put(char_type __c) {
614 # if _LIBCPP_HAS_EXCEPTIONS
615 try {
616 # endif // _LIBCPP_HAS_EXCEPTIONS
617 sentry __s(*this);
618 if (__s) {
619 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
620 _Op __o(*this);
621 *__o = __c;
622 if (__o.failed())
623 this->setstate(ios_base::badbit);
625 # if _LIBCPP_HAS_EXCEPTIONS
626 } catch (...) {
627 this->__set_badbit_and_consider_rethrow();
629 # endif // _LIBCPP_HAS_EXCEPTIONS
630 return *this;
633 template <class _CharT, class _Traits>
634 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) {
635 # if _LIBCPP_HAS_EXCEPTIONS
636 try {
637 # endif // _LIBCPP_HAS_EXCEPTIONS
638 sentry __sen(*this);
639 if (__sen && __n) {
640 if (this->rdbuf()->sputn(__s, __n) != __n)
641 this->setstate(ios_base::badbit);
643 # if _LIBCPP_HAS_EXCEPTIONS
644 } catch (...) {
645 this->__set_badbit_and_consider_rethrow();
647 # endif // _LIBCPP_HAS_EXCEPTIONS
648 return *this;
651 template <class _CharT, class _Traits>
652 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::flush() {
653 # if _LIBCPP_HAS_EXCEPTIONS
654 try {
655 # endif // _LIBCPP_HAS_EXCEPTIONS
656 if (this->rdbuf()) {
657 sentry __s(*this);
658 if (__s) {
659 if (this->rdbuf()->pubsync() == -1)
660 this->setstate(ios_base::badbit);
663 # if _LIBCPP_HAS_EXCEPTIONS
664 } catch (...) {
665 this->__set_badbit_and_consider_rethrow();
667 # endif // _LIBCPP_HAS_EXCEPTIONS
668 return *this;
671 template <class _CharT, class _Traits>
672 typename basic_ostream<_CharT, _Traits>::pos_type basic_ostream<_CharT, _Traits>::tellp() {
673 if (this->fail())
674 return pos_type(-1);
675 return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
678 template <class _CharT, class _Traits>
679 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) {
680 sentry __s(*this);
681 if (!this->fail()) {
682 if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
683 this->setstate(ios_base::failbit);
685 return *this;
688 template <class _CharT, class _Traits>
689 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) {
690 sentry __s(*this);
691 if (!this->fail()) {
692 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
693 this->setstate(ios_base::failbit);
695 return *this;
698 template <class _CharT, class _Traits>
699 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& endl(basic_ostream<_CharT, _Traits>& __os) {
700 __os.put(__os.widen('\n'));
701 __os.flush();
702 return __os;
705 template <class _CharT, class _Traits>
706 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& ends(basic_ostream<_CharT, _Traits>& __os) {
707 __os.put(_CharT());
708 return __os;
711 template <class _CharT, class _Traits>
712 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& flush(basic_ostream<_CharT, _Traits>& __os) {
713 __os.flush();
714 return __os;
717 template <class _Stream, class _Tp, class = void>
718 struct __is_ostreamable : false_type {};
720 template <class _Stream, class _Tp>
721 struct __is_ostreamable<_Stream, _Tp, decltype(std::declval<_Stream>() << std::declval<_Tp>(), void())> : true_type {};
723 template <class _Stream,
724 class _Tp,
725 __enable_if_t<_And<is_base_of<ios_base, _Stream>, __is_ostreamable<_Stream&, const _Tp&> >::value, int> = 0>
726 _LIBCPP_HIDE_FROM_ABI _Stream&& operator<<(_Stream&& __os, const _Tp& __x) {
727 __os << __x;
728 return std::move(__os);
731 template <class _CharT, class _Traits, class _Allocator>
732 basic_ostream<_CharT, _Traits>&
733 operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str) {
734 return std::__put_character_sequence(__os, __str.data(), __str.size());
737 template <class _CharT, class _Traits>
738 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
739 operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv) {
740 return std::__put_character_sequence(__os, __sv.data(), __sv.size());
743 template <class _CharT, class _Traits>
744 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
745 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec) {
746 return __os << __ec.category().name() << ':' << __ec.value();
749 template <class _CharT, class _Traits, class _Yp>
750 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
751 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p) {
752 return __os << __p.get();
755 template <
756 class _CharT,
757 class _Traits,
758 class _Yp,
759 class _Dp,
760 __enable_if_t<is_same<void,
761 __void_t<decltype((std::declval<basic_ostream<_CharT, _Traits>&>()
762 << std::declval<typename unique_ptr<_Yp, _Dp>::pointer>()))> >::value,
763 int> = 0>
764 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
765 operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p) {
766 return __os << __p.get();
769 template <class _CharT, class _Traits, size_t _Size>
770 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
771 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) {
772 return __os << __x.template to_string<_CharT, _Traits>(std::use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
773 std::use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
776 # if _LIBCPP_STD_VER >= 20
778 # if _LIBCPP_HAS_WIDE_CHARACTERS
779 template <class _Traits>
780 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
782 template <class _Traits>
783 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
785 template <class _Traits>
786 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
788 template <class _Traits>
789 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
791 template <class _Traits>
792 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
794 template <class _Traits>
795 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
797 # endif // _LIBCPP_HAS_WIDE_CHARACTERS
799 # if _LIBCPP_HAS_CHAR8_T
800 template <class _Traits>
801 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
803 template <class _Traits>
804 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
806 template <class _Traits>
807 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
809 template <class _Traits>
810 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
811 # endif
813 template <class _Traits>
814 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
816 template <class _Traits>
817 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
819 template <class _Traits>
820 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
822 template <class _Traits>
823 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
825 # endif // _LIBCPP_STD_VER >= 20
827 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>;
828 # if _LIBCPP_HAS_WIDE_CHARACTERS
829 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>;
830 # endif
832 _LIBCPP_END_NAMESPACE_STD
834 _LIBCPP_POP_MACROS
836 #endif // _LIBCPP_HAS_LOCALIZATION
838 #endif // _LIBCPP___OSTREAM_BASIC_OSTREAM_H