[rtsan] Remove mkfifoat interceptor (#116997)
[llvm-project.git] / libcxx / include / __cxx03 / __ostream / basic_ostream.h
blob1f0fb9acd3871d803675ba7414bfc19fe66882e5
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 <__cxx03/__config>
13 #include <__cxx03/__exception/operations.h>
14 #include <__cxx03/__memory/shared_ptr.h>
15 #include <__cxx03/__memory/unique_ptr.h>
16 #include <__cxx03/__system_error/error_code.h>
17 #include <__cxx03/__type_traits/conjunction.h>
18 #include <__cxx03/__type_traits/enable_if.h>
19 #include <__cxx03/__type_traits/is_base_of.h>
20 #include <__cxx03/__type_traits/void_t.h>
21 #include <__cxx03/__utility/declval.h>
22 #include <__cxx03/bitset>
23 #include <__cxx03/cstddef>
24 #include <__cxx03/ios>
25 #include <__cxx03/locale>
26 #include <__cxx03/new> // for __throw_bad_alloc
27 #include <__cxx03/streambuf>
28 #include <__cxx03/string_view>
30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
31 # pragma GCC system_header
32 #endif
34 _LIBCPP_PUSH_MACROS
35 #include <__cxx03/__undef_macros>
37 _LIBCPP_BEGIN_NAMESPACE_STD
39 template <class _CharT, class _Traits>
40 class _LIBCPP_TEMPLATE_VIS basic_ostream : virtual public basic_ios<_CharT, _Traits> {
41 public:
42 // types (inherited from basic_ios (27.5.4)):
43 typedef _CharT char_type;
44 typedef _Traits traits_type;
45 typedef typename traits_type::int_type int_type;
46 typedef typename traits_type::pos_type pos_type;
47 typedef typename traits_type::off_type off_type;
49 // 27.7.2.2 Constructor/destructor:
50 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb) {
51 this->init(__sb);
53 ~basic_ostream() override;
55 basic_ostream(const basic_ostream& __rhs) = delete;
56 basic_ostream& operator=(const basic_ostream& __rhs) = delete;
58 protected:
59 inline _LIBCPP_HIDE_FROM_ABI basic_ostream(basic_ostream&& __rhs);
61 // 27.7.2.3 Assign/swap
62 inline _LIBCPP_HIDE_FROM_ABI basic_ostream& operator=(basic_ostream&& __rhs);
64 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_ostream& __rhs) {
65 basic_ios<char_type, traits_type>::swap(__rhs);
68 public:
69 // 27.7.2.4 Prefix/suffix:
70 class _LIBCPP_TEMPLATE_VIS sentry;
72 // 27.7.2.6 Formatted output:
73 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&)) {
74 return __pf(*this);
77 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream&
78 operator<<(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) {
79 __pf(*this);
80 return *this;
83 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&)) {
84 __pf(*this);
85 return *this;
88 basic_ostream& operator<<(bool __n);
89 basic_ostream& operator<<(short __n);
90 basic_ostream& operator<<(unsigned short __n);
91 basic_ostream& operator<<(int __n);
92 basic_ostream& operator<<(unsigned int __n);
93 basic_ostream& operator<<(long __n);
94 basic_ostream& operator<<(unsigned long __n);
95 basic_ostream& operator<<(long long __n);
96 basic_ostream& operator<<(unsigned long long __n);
97 basic_ostream& operator<<(float __f);
98 basic_ostream& operator<<(double __f);
99 basic_ostream& operator<<(long double __f);
100 basic_ostream& operator<<(const void* __p);
102 #if _LIBCPP_STD_VER >= 23
103 _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(const volatile void* __p) {
104 return operator<<(const_cast<const void*>(__p));
106 #endif
108 basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);
110 #if _LIBCPP_STD_VER >= 17
111 // LWG 2221 - nullptr. This is not backported to older standards modes.
112 // See https://reviews.llvm.org/D127033 for more info on the rationale.
113 _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(nullptr_t) { return *this << "nullptr"; }
114 #endif
116 // 27.7.2.7 Unformatted output:
117 basic_ostream& put(char_type __c);
118 basic_ostream& write(const char_type* __s, streamsize __n);
119 basic_ostream& flush();
121 // 27.7.2.5 seeks:
122 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type tellp();
123 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(pos_type __pos);
124 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);
126 protected:
127 _LIBCPP_HIDE_FROM_ABI basic_ostream() {} // extension, intentially does not initialize
130 template <class _CharT, class _Traits>
131 class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry {
132 bool __ok_;
133 basic_ostream<_CharT, _Traits>& __os_;
135 public:
136 explicit sentry(basic_ostream<_CharT, _Traits>& __os);
137 ~sentry();
138 sentry(const sentry&) = delete;
139 sentry& operator=(const sentry&) = delete;
141 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }
144 template <class _CharT, class _Traits>
145 basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os) : __ok_(false), __os_(__os) {
146 if (__os.good()) {
147 if (__os.tie())
148 __os.tie()->flush();
149 __ok_ = true;
153 template <class _CharT, class _Traits>
154 basic_ostream<_CharT, _Traits>::sentry::~sentry() {
155 if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && !uncaught_exception()) {
156 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
157 try {
158 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
159 if (__os_.rdbuf()->pubsync() == -1)
160 __os_.setstate(ios_base::badbit);
161 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
162 } catch (...) {
164 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
168 template <class _CharT, class _Traits>
169 basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs) {
170 this->move(__rhs);
173 template <class _CharT, class _Traits>
174 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs) {
175 swap(__rhs);
176 return *this;
179 template <class _CharT, class _Traits>
180 basic_ostream<_CharT, _Traits>::~basic_ostream() {}
182 template <class _CharT, class _Traits>
183 basic_ostream<_CharT, _Traits>&
184 basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb) {
185 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
186 try {
187 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
188 sentry __s(*this);
189 if (__s) {
190 if (__sb) {
191 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
192 try {
193 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
194 typedef istreambuf_iterator<_CharT, _Traits> _Ip;
195 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
196 _Ip __i(__sb);
197 _Ip __eof;
198 _Op __o(*this);
199 size_t __c = 0;
200 for (; __i != __eof; ++__i, ++__o, ++__c) {
201 *__o = *__i;
202 if (__o.failed())
203 break;
205 if (__c == 0)
206 this->setstate(ios_base::failbit);
207 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
208 } catch (...) {
209 this->__set_failbit_and_consider_rethrow();
211 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
212 } else
213 this->setstate(ios_base::badbit);
215 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
216 } catch (...) {
217 this->__set_badbit_and_consider_rethrow();
219 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
220 return *this;
223 template <class _CharT, class _Traits>
224 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __n) {
225 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
226 try {
227 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
228 sentry __s(*this);
229 if (__s) {
230 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
231 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
232 if (__f.put(*this, *this, this->fill(), __n).failed())
233 this->setstate(ios_base::badbit | ios_base::failbit);
235 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
236 } catch (...) {
237 this->__set_badbit_and_consider_rethrow();
239 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
240 return *this;
243 template <class _CharT, class _Traits>
244 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(short __n) {
245 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
246 try {
247 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
248 sentry __s(*this);
249 if (__s) {
250 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
251 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
252 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
253 if (__f.put(*this,
254 *this,
255 this->fill(),
256 __flags == ios_base::oct || __flags == ios_base::hex
257 ? static_cast<long>(static_cast<unsigned short>(__n))
258 : static_cast<long>(__n))
259 .failed())
260 this->setstate(ios_base::badbit | ios_base::failbit);
262 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
263 } catch (...) {
264 this->__set_badbit_and_consider_rethrow();
266 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
267 return *this;
270 template <class _CharT, class _Traits>
271 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n) {
272 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
273 try {
274 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
275 sentry __s(*this);
276 if (__s) {
277 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
278 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
279 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
280 this->setstate(ios_base::badbit | ios_base::failbit);
282 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
283 } catch (...) {
284 this->__set_badbit_and_consider_rethrow();
286 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
287 return *this;
290 template <class _CharT, class _Traits>
291 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __n) {
292 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
293 try {
294 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
295 sentry __s(*this);
296 if (__s) {
297 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;
298 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
299 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
300 if (__f.put(*this,
301 *this,
302 this->fill(),
303 __flags == ios_base::oct || __flags == ios_base::hex
304 ? static_cast<long>(static_cast<unsigned int>(__n))
305 : static_cast<long>(__n))
306 .failed())
307 this->setstate(ios_base::badbit | ios_base::failbit);
309 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
310 } catch (...) {
311 this->__set_badbit_and_consider_rethrow();
313 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
314 return *this;
317 template <class _CharT, class _Traits>
318 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n) {
319 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
320 try {
321 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
322 sentry __s(*this);
323 if (__s) {
324 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
325 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
326 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())
327 this->setstate(ios_base::badbit | ios_base::failbit);
329 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
330 } catch (...) {
331 this->__set_badbit_and_consider_rethrow();
333 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
334 return *this;
337 template <class _CharT, class _Traits>
338 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) {
339 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
340 try {
341 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
342 sentry __s(*this);
343 if (__s) {
344 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
345 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
346 if (__f.put(*this, *this, this->fill(), __n).failed())
347 this->setstate(ios_base::badbit | ios_base::failbit);
349 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
350 } catch (...) {
351 this->__set_badbit_and_consider_rethrow();
353 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
354 return *this;
357 template <class _CharT, class _Traits>
358 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n) {
359 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
360 try {
361 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
362 sentry __s(*this);
363 if (__s) {
364 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
365 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
366 if (__f.put(*this, *this, this->fill(), __n).failed())
367 this->setstate(ios_base::badbit | ios_base::failbit);
369 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
370 } catch (...) {
371 this->__set_badbit_and_consider_rethrow();
373 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
374 return *this;
377 template <class _CharT, class _Traits>
378 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long long __n) {
379 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
380 try {
381 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
382 sentry __s(*this);
383 if (__s) {
384 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
385 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
386 if (__f.put(*this, *this, this->fill(), __n).failed())
387 this->setstate(ios_base::badbit | ios_base::failbit);
389 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
390 } catch (...) {
391 this->__set_badbit_and_consider_rethrow();
393 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
394 return *this;
397 template <class _CharT, class _Traits>
398 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n) {
399 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
400 try {
401 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
402 sentry __s(*this);
403 if (__s) {
404 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
405 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
406 if (__f.put(*this, *this, this->fill(), __n).failed())
407 this->setstate(ios_base::badbit | ios_base::failbit);
409 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
410 } catch (...) {
411 this->__set_badbit_and_consider_rethrow();
413 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
414 return *this;
417 template <class _CharT, class _Traits>
418 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __n) {
419 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
420 try {
421 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
422 sentry __s(*this);
423 if (__s) {
424 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
425 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
426 if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())
427 this->setstate(ios_base::badbit | ios_base::failbit);
429 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
430 } catch (...) {
431 this->__set_badbit_and_consider_rethrow();
433 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
434 return *this;
437 template <class _CharT, class _Traits>
438 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __n) {
439 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
440 try {
441 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
442 sentry __s(*this);
443 if (__s) {
444 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
445 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
446 if (__f.put(*this, *this, this->fill(), __n).failed())
447 this->setstate(ios_base::badbit | ios_base::failbit);
449 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
450 } catch (...) {
451 this->__set_badbit_and_consider_rethrow();
453 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
454 return *this;
457 template <class _CharT, class _Traits>
458 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __n) {
459 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
460 try {
461 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
462 sentry __s(*this);
463 if (__s) {
464 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
465 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
466 if (__f.put(*this, *this, this->fill(), __n).failed())
467 this->setstate(ios_base::badbit | ios_base::failbit);
469 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
470 } catch (...) {
471 this->__set_badbit_and_consider_rethrow();
473 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
474 return *this;
477 template <class _CharT, class _Traits>
478 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __n) {
479 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
480 try {
481 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
482 sentry __s(*this);
483 if (__s) {
484 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;
485 const _Fp& __f = std::use_facet<_Fp>(this->getloc());
486 if (__f.put(*this, *this, this->fill(), __n).failed())
487 this->setstate(ios_base::badbit | ios_base::failbit);
489 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
490 } catch (...) {
491 this->__set_badbit_and_consider_rethrow();
493 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
494 return *this;
497 template <class _CharT, class _Traits>
498 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
499 __put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) {
500 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
501 try {
502 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
503 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
504 if (__s) {
505 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
506 if (std::__pad_and_output(
507 _Ip(__os),
508 __str,
509 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,
510 __str + __len,
511 __os,
512 __os.fill())
513 .failed())
514 __os.setstate(ios_base::badbit | ios_base::failbit);
516 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
517 } catch (...) {
518 __os.__set_badbit_and_consider_rethrow();
520 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
521 return __os;
524 template <class _CharT, class _Traits>
525 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) {
526 return std::__put_character_sequence(__os, &__c, 1);
529 template <class _CharT, class _Traits>
530 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn) {
531 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
532 try {
533 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
534 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
535 if (__s) {
536 _CharT __c = __os.widen(__cn);
537 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
538 if (std::__pad_and_output(
539 _Ip(__os),
540 &__c,
541 (__os.flags() & ios_base::adjustfield) == ios_base::left ? &__c + 1 : &__c,
542 &__c + 1,
543 __os,
544 __os.fill())
545 .failed())
546 __os.setstate(ios_base::badbit | ios_base::failbit);
548 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
549 } catch (...) {
550 __os.__set_badbit_and_consider_rethrow();
552 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
553 return __os;
556 template <class _Traits>
557 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, char __c) {
558 return std::__put_character_sequence(__os, &__c, 1);
561 template <class _Traits>
562 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, signed char __c) {
563 return std::__put_character_sequence(__os, (char*)&__c, 1);
566 template <class _Traits>
567 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) {
568 return std::__put_character_sequence(__os, (char*)&__c, 1);
571 template <class _CharT, class _Traits>
572 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
573 operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str) {
574 return std::__put_character_sequence(__os, __str, _Traits::length(__str));
577 template <class _CharT, class _Traits>
578 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
579 operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn) {
580 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
581 try {
582 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
583 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);
584 if (__s) {
585 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;
586 size_t __len = char_traits<char>::length(__strn);
587 const int __bs = 100;
588 _CharT __wbb[__bs];
589 _CharT* __wb = __wbb;
590 unique_ptr<_CharT, void (*)(void*)> __h(0, free);
591 if (__len > __bs) {
592 __wb = (_CharT*)malloc(__len * sizeof(_CharT));
593 if (__wb == 0)
594 __throw_bad_alloc();
595 __h.reset(__wb);
597 for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)
598 *__p = __os.widen(*__strn);
599 if (std::__pad_and_output(
600 _Ip(__os),
601 __wb,
602 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __wb + __len : __wb,
603 __wb + __len,
604 __os,
605 __os.fill())
606 .failed())
607 __os.setstate(ios_base::badbit | ios_base::failbit);
609 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
610 } catch (...) {
611 __os.__set_badbit_and_consider_rethrow();
613 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
614 return __os;
617 template <class _Traits>
618 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, const char* __str) {
619 return std::__put_character_sequence(__os, __str, _Traits::length(__str));
622 template <class _Traits>
623 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
624 operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str) {
625 const char* __s = (const char*)__str;
626 return std::__put_character_sequence(__os, __s, _Traits::length(__s));
629 template <class _Traits>
630 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&
631 operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str) {
632 const char* __s = (const char*)__str;
633 return std::__put_character_sequence(__os, __s, _Traits::length(__s));
636 template <class _CharT, class _Traits>
637 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::put(char_type __c) {
638 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
639 try {
640 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
641 sentry __s(*this);
642 if (__s) {
643 typedef ostreambuf_iterator<_CharT, _Traits> _Op;
644 _Op __o(*this);
645 *__o = __c;
646 if (__o.failed())
647 this->setstate(ios_base::badbit);
649 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
650 } catch (...) {
651 this->__set_badbit_and_consider_rethrow();
653 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
654 return *this;
657 template <class _CharT, class _Traits>
658 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) {
659 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
660 try {
661 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
662 sentry __sen(*this);
663 if (__sen && __n) {
664 if (this->rdbuf()->sputn(__s, __n) != __n)
665 this->setstate(ios_base::badbit);
667 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
668 } catch (...) {
669 this->__set_badbit_and_consider_rethrow();
671 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
672 return *this;
675 template <class _CharT, class _Traits>
676 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::flush() {
677 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
678 try {
679 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
680 if (this->rdbuf()) {
681 sentry __s(*this);
682 if (__s) {
683 if (this->rdbuf()->pubsync() == -1)
684 this->setstate(ios_base::badbit);
687 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS
688 } catch (...) {
689 this->__set_badbit_and_consider_rethrow();
691 #endif // _LIBCPP_HAS_NO_EXCEPTIONS
692 return *this;
695 template <class _CharT, class _Traits>
696 typename basic_ostream<_CharT, _Traits>::pos_type basic_ostream<_CharT, _Traits>::tellp() {
697 if (this->fail())
698 return pos_type(-1);
699 return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
702 template <class _CharT, class _Traits>
703 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) {
704 sentry __s(*this);
705 if (!this->fail()) {
706 if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))
707 this->setstate(ios_base::failbit);
709 return *this;
712 template <class _CharT, class _Traits>
713 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) {
714 sentry __s(*this);
715 if (!this->fail()) {
716 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))
717 this->setstate(ios_base::failbit);
719 return *this;
722 template <class _CharT, class _Traits>
723 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& endl(basic_ostream<_CharT, _Traits>& __os) {
724 __os.put(__os.widen('\n'));
725 __os.flush();
726 return __os;
729 template <class _CharT, class _Traits>
730 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& ends(basic_ostream<_CharT, _Traits>& __os) {
731 __os.put(_CharT());
732 return __os;
735 template <class _CharT, class _Traits>
736 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& flush(basic_ostream<_CharT, _Traits>& __os) {
737 __os.flush();
738 return __os;
741 template <class _Stream, class _Tp, class = void>
742 struct __is_ostreamable : false_type {};
744 template <class _Stream, class _Tp>
745 struct __is_ostreamable<_Stream, _Tp, decltype(std::declval<_Stream>() << std::declval<_Tp>(), void())> : true_type {};
747 template <class _Stream,
748 class _Tp,
749 __enable_if_t<_And<is_base_of<ios_base, _Stream>, __is_ostreamable<_Stream&, const _Tp&> >::value, int> = 0>
750 _LIBCPP_HIDE_FROM_ABI _Stream&& operator<<(_Stream&& __os, const _Tp& __x) {
751 __os << __x;
752 return std::move(__os);
755 template <class _CharT, class _Traits, class _Allocator>
756 basic_ostream<_CharT, _Traits>&
757 operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str) {
758 return std::__put_character_sequence(__os, __str.data(), __str.size());
761 template <class _CharT, class _Traits>
762 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
763 operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv) {
764 return std::__put_character_sequence(__os, __sv.data(), __sv.size());
767 template <class _CharT, class _Traits>
768 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
769 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec) {
770 return __os << __ec.category().name() << ':' << __ec.value();
773 template <class _CharT, class _Traits, class _Yp>
774 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
775 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p) {
776 return __os << __p.get();
779 template <
780 class _CharT,
781 class _Traits,
782 class _Yp,
783 class _Dp,
784 __enable_if_t<is_same<void,
785 __void_t<decltype((std::declval<basic_ostream<_CharT, _Traits>&>()
786 << std::declval<typename unique_ptr<_Yp, _Dp>::pointer>()))> >::value,
787 int> = 0>
788 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
789 operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p) {
790 return __os << __p.get();
793 template <class _CharT, class _Traits, size_t _Size>
794 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&
795 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) {
796 return __os << __x.template to_string<_CharT, _Traits>(std::use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),
797 std::use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));
800 #if _LIBCPP_STD_VER >= 20
802 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
803 template <class _Traits>
804 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete;
806 template <class _Traits>
807 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete;
809 template <class _Traits>
810 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete;
812 template <class _Traits>
813 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete;
815 template <class _Traits>
816 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete;
818 template <class _Traits>
819 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete;
821 # endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS
823 # ifndef _LIBCPP_HAS_NO_CHAR8_T
824 template <class _Traits>
825 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char8_t) = delete;
827 template <class _Traits>
828 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete;
830 template <class _Traits>
831 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete;
833 template <class _Traits>
834 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete;
835 # endif
837 template <class _Traits>
838 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char16_t) = delete;
840 template <class _Traits>
841 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char32_t) = delete;
843 template <class _Traits>
844 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete;
846 template <class _Traits>
847 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete;
849 #endif // _LIBCPP_STD_VER >= 20
851 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>;
852 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS
853 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>;
854 #endif
856 _LIBCPP_END_NAMESPACE_STD
858 _LIBCPP_POP_MACROS
860 #endif // _LIBCPP___OSTREAM_BASIC_OSTREAM_H