1// -*- C++ -*- 2//===----------------------------------------------------------------------===// 3// 4// 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 7// 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 34 basic_ostream& operator=(const basic_ostream& rhs) = delete; // C++14 35 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<<(const volatile void* val); // C++23 59 basic_ostream& operator<<(basic_streambuf<char_type,traits>* sb); 60 basic_ostream& operator<<(nullptr_t); 61 62 // 27.7.2.7 Unformatted output: 63 basic_ostream& put(char_type c); 64 basic_ostream& write(const char_type* s, streamsize n); 65 basic_ostream& flush(); 66 67 // 27.7.2.5 seeks: 68 pos_type tellp(); 69 basic_ostream& seekp(pos_type); 70 basic_ostream& seekp(off_type, ios_base::seekdir); 71protected: 72 basic_ostream(const basic_ostream& rhs) = delete; 73 basic_ostream(basic_ostream&& rhs); 74 // 27.7.3.3 Assign/swap 75 basic_ostream& operator=(basic_ostream& rhs) = delete; 76 basic_ostream& operator=(const basic_ostream&& rhs); 77 void swap(basic_ostream& rhs); 78}; 79 80// 27.7.2.6.4 character inserters 81 82template<class charT, class traits> 83 basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, charT); 84 85template<class charT, class traits> 86 basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, char); 87 88template<class traits> 89 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, char); 90 91// signed and unsigned 92 93template<class traits> 94 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, signed char); 95 96template<class traits> 97 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, unsigned char); 98 99// NTBS 100template<class charT, class traits> 101 basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const charT*); 102 103template<class charT, class traits> 104 basic_ostream<charT,traits>& operator<<(basic_ostream<charT,traits>&, const char*); 105 106template<class traits> 107 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const char*); 108 109// signed and unsigned 110template<class traits> 111basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const signed char*); 112 113template<class traits> 114 basic_ostream<char,traits>& operator<<(basic_ostream<char,traits>&, const unsigned char*); 115 116// swap: 117template <class charT, class traits> 118 void swap(basic_ostream<charT, traits>& x, basic_ostream<charT, traits>& y); 119 120template <class charT, class traits> 121 basic_ostream<charT,traits>& endl(basic_ostream<charT,traits>& os); 122 123template <class charT, class traits> 124 basic_ostream<charT,traits>& ends(basic_ostream<charT,traits>& os); 125 126template <class charT, class traits> 127 basic_ostream<charT,traits>& flush(basic_ostream<charT,traits>& os); 128 129// rvalue stream insertion 130template <class Stream, class T> 131 Stream&& operator<<(Stream&& os, const T& x); 132 133template<class traits> 134basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, wchar_t) = delete; // since C++20 135template<class traits> 136basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char8_t) = delete; // since C++20 137template<class traits> 138basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char16_t) = delete; // since C++20 139template<class traits> 140basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, char32_t) = delete; // since C++20 141template<class traits> 142basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, char8_t) = delete; // since C++20 143template<class traits> 144basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, char16_t) = delete; // since C++20 145template<class traits> 146basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, char32_t) = delete; // since C++20 147template<class traits> 148basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const wchar_t*) = delete; // since C++20 149template<class traits> 150basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char8_t*) = delete; // since C++20 151template<class traits> 152basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char16_t*) = delete; // since C++20 153template<class traits> 154basic_ostream<char, traits>& operator<<(basic_ostream<char, traits>&, const char32_t*) = delete; // since C++20 155template<class traits> 156basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, const char8_t*) = delete; // since C++20 157template<class traits> 158basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, const char16_t*) = delete; // since C++20 159template<class traits> 160basic_ostream<wchar_t, traits>& operator<<(basic_ostream<wchar_t, traits>&, const char32_t*) = delete; // since C++20 161 162} // std 163 164*/ 165 166#include <__assert> // all public C++ headers provide the assertion handler 167#include <__config> 168#include <__exception/operations.h> 169#include <__memory/shared_ptr.h> 170#include <__memory/unique_ptr.h> 171#include <__system_error/error_code.h> 172#include <__type_traits/conjunction.h> 173#include <__type_traits/enable_if.h> 174#include <__type_traits/is_base_of.h> 175#include <__type_traits/void_t.h> 176#include <__utility/declval.h> 177#include <bitset> 178#include <ios> 179#include <locale> 180#include <new> 181#include <streambuf> 182#include <version> 183 184#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 185# pragma GCC system_header 186#endif 187 188_LIBCPP_BEGIN_NAMESPACE_STD 189 190template <class _CharT, class _Traits> 191class _LIBCPP_TEMPLATE_VIS basic_ostream 192 : virtual public basic_ios<_CharT, _Traits> 193{ 194public: 195 // types (inherited from basic_ios (27.5.4)): 196 typedef _CharT char_type; 197 typedef _Traits traits_type; 198 typedef typename traits_type::int_type int_type; 199 typedef typename traits_type::pos_type pos_type; 200 typedef typename traits_type::off_type off_type; 201 202 // 27.7.2.2 Constructor/destructor: 203 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 204 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb) 205 { this->init(__sb); } 206 ~basic_ostream() override; 207protected: 208 inline _LIBCPP_INLINE_VISIBILITY 209 basic_ostream(basic_ostream&& __rhs); 210 211 // 27.7.2.3 Assign/swap 212 inline _LIBCPP_INLINE_VISIBILITY 213 basic_ostream& operator=(basic_ostream&& __rhs); 214 215 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 216 void swap(basic_ostream& __rhs) 217 { basic_ios<char_type, traits_type>::swap(__rhs); } 218 219 basic_ostream (const basic_ostream& __rhs) = delete; 220 basic_ostream& operator=(const basic_ostream& __rhs) = delete; 221 222public: 223 // 27.7.2.4 Prefix/suffix: 224 class _LIBCPP_TEMPLATE_VIS sentry; 225 226 // 27.7.2.6 Formatted output: 227 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 228 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&)) 229 { return __pf(*this); } 230 231 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 232 basic_ostream& operator<<(basic_ios<char_type, traits_type>& 233 (*__pf)(basic_ios<char_type,traits_type>&)) 234 { __pf(*this); return *this; } 235 236 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 237 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&)) 238 { __pf(*this); return *this; } 239 240 basic_ostream& operator<<(bool __n); 241 basic_ostream& operator<<(short __n); 242 basic_ostream& operator<<(unsigned short __n); 243 basic_ostream& operator<<(int __n); 244 basic_ostream& operator<<(unsigned int __n); 245 basic_ostream& operator<<(long __n); 246 basic_ostream& operator<<(unsigned long __n); 247 basic_ostream& operator<<(long long __n); 248 basic_ostream& operator<<(unsigned long long __n); 249 basic_ostream& operator<<(float __f); 250 basic_ostream& operator<<(double __f); 251 basic_ostream& operator<<(long double __f); 252 basic_ostream& operator<<(const void* __p); 253 254#if _LIBCPP_STD_VER >= 23 255 _LIBCPP_HIDE_FROM_ABI 256 basic_ostream& operator<<(const volatile void* __p) { 257 return operator<<(const_cast<const void*>(__p)); 258 } 259#endif 260 261 basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb); 262 263#if _LIBCPP_STD_VER >= 17 264// LWG 2221 - nullptr. This is not backported to older standards modes. 265// See https://reviews.llvm.org/D127033 for more info on the rationale. 266 _LIBCPP_INLINE_VISIBILITY 267 basic_ostream& operator<<(nullptr_t) 268 { return *this << "nullptr"; } 269#endif 270 271 // 27.7.2.7 Unformatted output: 272 basic_ostream& put(char_type __c); 273 basic_ostream& write(const char_type* __s, streamsize __n); 274 basic_ostream& flush(); 275 276 // 27.7.2.5 seeks: 277 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 278 pos_type tellp(); 279 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 280 basic_ostream& seekp(pos_type __pos); 281 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 282 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir); 283 284protected: 285 _LIBCPP_INLINE_VISIBILITY 286 basic_ostream() {} // extension, intentially does not initialize 287}; 288 289template <class _CharT, class _Traits> 290class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry 291{ 292 bool __ok_; 293 basic_ostream<_CharT, _Traits>& __os_; 294 295public: 296 explicit sentry(basic_ostream<_CharT, _Traits>& __os); 297 ~sentry(); 298 sentry(const sentry&) = delete; 299 sentry& operator=(const sentry&) = delete; 300 301 _LIBCPP_INLINE_VISIBILITY 302 explicit operator bool() const {return __ok_;} 303}; 304 305template <class _CharT, class _Traits> 306basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os) 307 : __ok_(false), 308 __os_(__os) 309{ 310 if (__os.good()) 311 { 312 if (__os.tie()) 313 __os.tie()->flush(); 314 __ok_ = true; 315 } 316} 317 318template <class _CharT, class _Traits> 319basic_ostream<_CharT, _Traits>::sentry::~sentry() 320{ 321 if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) 322 && !uncaught_exception()) 323 { 324#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 325 try 326 { 327#endif // _LIBCPP_HAS_NO_EXCEPTIONS 328 if (__os_.rdbuf()->pubsync() == -1) 329 __os_.setstate(ios_base::badbit); 330#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 331 } 332 catch (...) 333 { 334 } 335#endif // _LIBCPP_HAS_NO_EXCEPTIONS 336 } 337} 338 339template <class _CharT, class _Traits> 340basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs) 341{ 342 this->move(__rhs); 343} 344 345template <class _CharT, class _Traits> 346basic_ostream<_CharT, _Traits>& 347basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs) 348{ 349 swap(__rhs); 350 return *this; 351} 352 353template <class _CharT, class _Traits> 354basic_ostream<_CharT, _Traits>::~basic_ostream() 355{ 356} 357 358template <class _CharT, class _Traits> 359basic_ostream<_CharT, _Traits>& 360basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb) 361{ 362#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 363 try 364 { 365#endif // _LIBCPP_HAS_NO_EXCEPTIONS 366 sentry __s(*this); 367 if (__s) 368 { 369 if (__sb) 370 { 371#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 372 try 373 { 374#endif // _LIBCPP_HAS_NO_EXCEPTIONS 375 typedef istreambuf_iterator<_CharT, _Traits> _Ip; 376 typedef ostreambuf_iterator<_CharT, _Traits> _Op; 377 _Ip __i(__sb); 378 _Ip __eof; 379 _Op __o(*this); 380 size_t __c = 0; 381 for (; __i != __eof; ++__i, ++__o, ++__c) 382 { 383 *__o = *__i; 384 if (__o.failed()) 385 break; 386 } 387 if (__c == 0) 388 this->setstate(ios_base::failbit); 389#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 390 } 391 catch (...) 392 { 393 this->__set_failbit_and_consider_rethrow(); 394 } 395#endif // _LIBCPP_HAS_NO_EXCEPTIONS 396 } 397 else 398 this->setstate(ios_base::badbit); 399 } 400#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 401 } 402 catch (...) 403 { 404 this->__set_badbit_and_consider_rethrow(); 405 } 406#endif // _LIBCPP_HAS_NO_EXCEPTIONS 407 return *this; 408} 409 410template <class _CharT, class _Traits> 411basic_ostream<_CharT, _Traits>& 412basic_ostream<_CharT, _Traits>::operator<<(bool __n) 413{ 414#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 415 try 416 { 417#endif // _LIBCPP_HAS_NO_EXCEPTIONS 418 sentry __s(*this); 419 if (__s) 420 { 421 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 422 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 423 if (__f.put(*this, *this, this->fill(), __n).failed()) 424 this->setstate(ios_base::badbit | ios_base::failbit); 425 } 426#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 427 } 428 catch (...) 429 { 430 this->__set_badbit_and_consider_rethrow(); 431 } 432#endif // _LIBCPP_HAS_NO_EXCEPTIONS 433 return *this; 434} 435 436template <class _CharT, class _Traits> 437basic_ostream<_CharT, _Traits>& 438basic_ostream<_CharT, _Traits>::operator<<(short __n) 439{ 440#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 441 try 442 { 443#endif // _LIBCPP_HAS_NO_EXCEPTIONS 444 sentry __s(*this); 445 if (__s) 446 { 447 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield; 448 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 449 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 450 if (__f.put(*this, *this, this->fill(), 451 __flags == ios_base::oct || __flags == ios_base::hex ? 452 static_cast<long>(static_cast<unsigned short>(__n)) : 453 static_cast<long>(__n)).failed()) 454 this->setstate(ios_base::badbit | ios_base::failbit); 455 } 456#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 457 } 458 catch (...) 459 { 460 this->__set_badbit_and_consider_rethrow(); 461 } 462#endif // _LIBCPP_HAS_NO_EXCEPTIONS 463 return *this; 464} 465 466template <class _CharT, class _Traits> 467basic_ostream<_CharT, _Traits>& 468basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n) 469{ 470#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 471 try 472 { 473#endif // _LIBCPP_HAS_NO_EXCEPTIONS 474 sentry __s(*this); 475 if (__s) 476 { 477 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 478 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 479 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed()) 480 this->setstate(ios_base::badbit | ios_base::failbit); 481 } 482#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 483 } 484 catch (...) 485 { 486 this->__set_badbit_and_consider_rethrow(); 487 } 488#endif // _LIBCPP_HAS_NO_EXCEPTIONS 489 return *this; 490} 491 492template <class _CharT, class _Traits> 493basic_ostream<_CharT, _Traits>& 494basic_ostream<_CharT, _Traits>::operator<<(int __n) 495{ 496#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 497 try 498 { 499#endif // _LIBCPP_HAS_NO_EXCEPTIONS 500 sentry __s(*this); 501 if (__s) 502 { 503 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield; 504 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 505 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 506 if (__f.put(*this, *this, this->fill(), 507 __flags == ios_base::oct || __flags == ios_base::hex ? 508 static_cast<long>(static_cast<unsigned int>(__n)) : 509 static_cast<long>(__n)).failed()) 510 this->setstate(ios_base::badbit | ios_base::failbit); 511 } 512#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 513 } 514 catch (...) 515 { 516 this->__set_badbit_and_consider_rethrow(); 517 } 518#endif // _LIBCPP_HAS_NO_EXCEPTIONS 519 return *this; 520} 521 522template <class _CharT, class _Traits> 523basic_ostream<_CharT, _Traits>& 524basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n) 525{ 526#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 527 try 528 { 529#endif // _LIBCPP_HAS_NO_EXCEPTIONS 530 sentry __s(*this); 531 if (__s) 532 { 533 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 534 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 535 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed()) 536 this->setstate(ios_base::badbit | ios_base::failbit); 537 } 538#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 539 } 540 catch (...) 541 { 542 this->__set_badbit_and_consider_rethrow(); 543 } 544#endif // _LIBCPP_HAS_NO_EXCEPTIONS 545 return *this; 546} 547 548template <class _CharT, class _Traits> 549basic_ostream<_CharT, _Traits>& 550basic_ostream<_CharT, _Traits>::operator<<(long __n) 551{ 552#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 553 try 554 { 555#endif // _LIBCPP_HAS_NO_EXCEPTIONS 556 sentry __s(*this); 557 if (__s) 558 { 559 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 560 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 561 if (__f.put(*this, *this, this->fill(), __n).failed()) 562 this->setstate(ios_base::badbit | ios_base::failbit); 563 } 564#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 565 } 566 catch (...) 567 { 568 this->__set_badbit_and_consider_rethrow(); 569 } 570#endif // _LIBCPP_HAS_NO_EXCEPTIONS 571 return *this; 572} 573 574template <class _CharT, class _Traits> 575basic_ostream<_CharT, _Traits>& 576basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n) 577{ 578#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 579 try 580 { 581#endif // _LIBCPP_HAS_NO_EXCEPTIONS 582 sentry __s(*this); 583 if (__s) 584 { 585 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 586 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 587 if (__f.put(*this, *this, this->fill(), __n).failed()) 588 this->setstate(ios_base::badbit | ios_base::failbit); 589 } 590#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 591 } 592 catch (...) 593 { 594 this->__set_badbit_and_consider_rethrow(); 595 } 596#endif // _LIBCPP_HAS_NO_EXCEPTIONS 597 return *this; 598} 599 600template <class _CharT, class _Traits> 601basic_ostream<_CharT, _Traits>& 602basic_ostream<_CharT, _Traits>::operator<<(long long __n) 603{ 604#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 605 try 606 { 607#endif // _LIBCPP_HAS_NO_EXCEPTIONS 608 sentry __s(*this); 609 if (__s) 610 { 611 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 612 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 613 if (__f.put(*this, *this, this->fill(), __n).failed()) 614 this->setstate(ios_base::badbit | ios_base::failbit); 615 } 616#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 617 } 618 catch (...) 619 { 620 this->__set_badbit_and_consider_rethrow(); 621 } 622#endif // _LIBCPP_HAS_NO_EXCEPTIONS 623 return *this; 624} 625 626template <class _CharT, class _Traits> 627basic_ostream<_CharT, _Traits>& 628basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n) 629{ 630#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 631 try 632 { 633#endif // _LIBCPP_HAS_NO_EXCEPTIONS 634 sentry __s(*this); 635 if (__s) 636 { 637 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 638 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 639 if (__f.put(*this, *this, this->fill(), __n).failed()) 640 this->setstate(ios_base::badbit | ios_base::failbit); 641 } 642#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 643 } 644 catch (...) 645 { 646 this->__set_badbit_and_consider_rethrow(); 647 } 648#endif // _LIBCPP_HAS_NO_EXCEPTIONS 649 return *this; 650} 651 652template <class _CharT, class _Traits> 653basic_ostream<_CharT, _Traits>& 654basic_ostream<_CharT, _Traits>::operator<<(float __n) 655{ 656#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 657 try 658 { 659#endif // _LIBCPP_HAS_NO_EXCEPTIONS 660 sentry __s(*this); 661 if (__s) 662 { 663 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 664 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 665 if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed()) 666 this->setstate(ios_base::badbit | ios_base::failbit); 667 } 668#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 669 } 670 catch (...) 671 { 672 this->__set_badbit_and_consider_rethrow(); 673 } 674#endif // _LIBCPP_HAS_NO_EXCEPTIONS 675 return *this; 676} 677 678template <class _CharT, class _Traits> 679basic_ostream<_CharT, _Traits>& 680basic_ostream<_CharT, _Traits>::operator<<(double __n) 681{ 682#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 683 try 684 { 685#endif // _LIBCPP_HAS_NO_EXCEPTIONS 686 sentry __s(*this); 687 if (__s) 688 { 689 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 690 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 691 if (__f.put(*this, *this, this->fill(), __n).failed()) 692 this->setstate(ios_base::badbit | ios_base::failbit); 693 } 694#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 695 } 696 catch (...) 697 { 698 this->__set_badbit_and_consider_rethrow(); 699 } 700#endif // _LIBCPP_HAS_NO_EXCEPTIONS 701 return *this; 702} 703 704template <class _CharT, class _Traits> 705basic_ostream<_CharT, _Traits>& 706basic_ostream<_CharT, _Traits>::operator<<(long double __n) 707{ 708#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 709 try 710 { 711#endif // _LIBCPP_HAS_NO_EXCEPTIONS 712 sentry __s(*this); 713 if (__s) 714 { 715 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 716 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 717 if (__f.put(*this, *this, this->fill(), __n).failed()) 718 this->setstate(ios_base::badbit | ios_base::failbit); 719 } 720#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 721 } 722 catch (...) 723 { 724 this->__set_badbit_and_consider_rethrow(); 725 } 726#endif // _LIBCPP_HAS_NO_EXCEPTIONS 727 return *this; 728} 729 730template <class _CharT, class _Traits> 731basic_ostream<_CharT, _Traits>& 732basic_ostream<_CharT, _Traits>::operator<<(const void* __n) 733{ 734#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 735 try 736 { 737#endif // _LIBCPP_HAS_NO_EXCEPTIONS 738 sentry __s(*this); 739 if (__s) 740 { 741 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 742 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 743 if (__f.put(*this, *this, this->fill(), __n).failed()) 744 this->setstate(ios_base::badbit | ios_base::failbit); 745 } 746#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 747 } 748 catch (...) 749 { 750 this->__set_badbit_and_consider_rethrow(); 751 } 752#endif // _LIBCPP_HAS_NO_EXCEPTIONS 753 return *this; 754} 755 756template<class _CharT, class _Traits> 757_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 758__put_character_sequence(basic_ostream<_CharT, _Traits>& __os, 759 const _CharT* __str, size_t __len) 760{ 761#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 762 try 763 { 764#endif // _LIBCPP_HAS_NO_EXCEPTIONS 765 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 766 if (__s) 767 { 768 typedef ostreambuf_iterator<_CharT, _Traits> _Ip; 769 if (std::__pad_and_output(_Ip(__os), 770 __str, 771 (__os.flags() & ios_base::adjustfield) == ios_base::left ? 772 __str + __len : 773 __str, 774 __str + __len, 775 __os, 776 __os.fill()).failed()) 777 __os.setstate(ios_base::badbit | ios_base::failbit); 778 } 779#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 780 } 781 catch (...) 782 { 783 __os.__set_badbit_and_consider_rethrow(); 784 } 785#endif // _LIBCPP_HAS_NO_EXCEPTIONS 786 return __os; 787} 788 789 790template<class _CharT, class _Traits> 791_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 792operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) 793{ 794 return _VSTD::__put_character_sequence(__os, &__c, 1); 795} 796 797template<class _CharT, class _Traits> 798_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 799operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn) 800{ 801#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 802 try 803 { 804#endif // _LIBCPP_HAS_NO_EXCEPTIONS 805 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 806 if (__s) 807 { 808 _CharT __c = __os.widen(__cn); 809 typedef ostreambuf_iterator<_CharT, _Traits> _Ip; 810 if (std::__pad_and_output(_Ip(__os), 811 &__c, 812 (__os.flags() & ios_base::adjustfield) == ios_base::left ? 813 &__c + 1 : 814 &__c, 815 &__c + 1, 816 __os, 817 __os.fill()).failed()) 818 __os.setstate(ios_base::badbit | ios_base::failbit); 819 } 820#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 821 } 822 catch (...) 823 { 824 __os.__set_badbit_and_consider_rethrow(); 825 } 826#endif // _LIBCPP_HAS_NO_EXCEPTIONS 827 return __os; 828} 829 830template<class _Traits> 831_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& 832operator<<(basic_ostream<char, _Traits>& __os, char __c) 833{ 834 return _VSTD::__put_character_sequence(__os, &__c, 1); 835} 836 837template<class _Traits> 838_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& 839operator<<(basic_ostream<char, _Traits>& __os, signed char __c) 840{ 841 return _VSTD::__put_character_sequence(__os, (char *) &__c, 1); 842} 843 844template<class _Traits> 845_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& 846operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) 847{ 848 return _VSTD::__put_character_sequence(__os, (char *) &__c, 1); 849} 850 851template<class _CharT, class _Traits> 852_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 853operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str) 854{ 855 return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str)); 856} 857 858template<class _CharT, class _Traits> 859_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 860operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn) 861{ 862#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 863 try 864 { 865#endif // _LIBCPP_HAS_NO_EXCEPTIONS 866 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 867 if (__s) 868 { 869 typedef ostreambuf_iterator<_CharT, _Traits> _Ip; 870 size_t __len = char_traits<char>::length(__strn); 871 const int __bs = 100; 872 _CharT __wbb[__bs]; 873 _CharT* __wb = __wbb; 874 unique_ptr<_CharT, void(*)(void*)> __h(0, free); 875 if (__len > __bs) 876 { 877 __wb = (_CharT*)malloc(__len*sizeof(_CharT)); 878 if (__wb == 0) 879 __throw_bad_alloc(); 880 __h.reset(__wb); 881 } 882 for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p) 883 *__p = __os.widen(*__strn); 884 if (std::__pad_and_output(_Ip(__os), 885 __wb, 886 (__os.flags() & ios_base::adjustfield) == ios_base::left ? 887 __wb + __len : 888 __wb, 889 __wb + __len, 890 __os, 891 __os.fill()).failed()) 892 __os.setstate(ios_base::badbit | ios_base::failbit); 893 } 894#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 895 } 896 catch (...) 897 { 898 __os.__set_badbit_and_consider_rethrow(); 899 } 900#endif // _LIBCPP_HAS_NO_EXCEPTIONS 901 return __os; 902} 903 904template<class _Traits> 905_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& 906operator<<(basic_ostream<char, _Traits>& __os, const char* __str) 907{ 908 return _VSTD::__put_character_sequence(__os, __str, _Traits::length(__str)); 909} 910 911template<class _Traits> 912_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& 913operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str) 914{ 915 const char *__s = (const char *) __str; 916 return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s)); 917} 918 919template<class _Traits> 920_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& 921operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str) 922{ 923 const char *__s = (const char *) __str; 924 return _VSTD::__put_character_sequence(__os, __s, _Traits::length(__s)); 925} 926 927template <class _CharT, class _Traits> 928basic_ostream<_CharT, _Traits>& 929basic_ostream<_CharT, _Traits>::put(char_type __c) 930{ 931#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 932 try 933 { 934#endif // _LIBCPP_HAS_NO_EXCEPTIONS 935 sentry __s(*this); 936 if (__s) 937 { 938 typedef ostreambuf_iterator<_CharT, _Traits> _Op; 939 _Op __o(*this); 940 *__o = __c; 941 if (__o.failed()) 942 this->setstate(ios_base::badbit); 943 } 944#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 945 } 946 catch (...) 947 { 948 this->__set_badbit_and_consider_rethrow(); 949 } 950#endif // _LIBCPP_HAS_NO_EXCEPTIONS 951 return *this; 952} 953 954template <class _CharT, class _Traits> 955basic_ostream<_CharT, _Traits>& 956basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) 957{ 958#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 959 try 960 { 961#endif // _LIBCPP_HAS_NO_EXCEPTIONS 962 sentry __sen(*this); 963 if (__sen && __n) 964 { 965 if (this->rdbuf()->sputn(__s, __n) != __n) 966 this->setstate(ios_base::badbit); 967 } 968#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 969 } 970 catch (...) 971 { 972 this->__set_badbit_and_consider_rethrow(); 973 } 974#endif // _LIBCPP_HAS_NO_EXCEPTIONS 975 return *this; 976} 977 978template <class _CharT, class _Traits> 979basic_ostream<_CharT, _Traits>& 980basic_ostream<_CharT, _Traits>::flush() 981{ 982#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 983 try 984 { 985#endif // _LIBCPP_HAS_NO_EXCEPTIONS 986 if (this->rdbuf()) 987 { 988 sentry __s(*this); 989 if (__s) 990 { 991 if (this->rdbuf()->pubsync() == -1) 992 this->setstate(ios_base::badbit); 993 } 994 } 995#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 996 } 997 catch (...) 998 { 999 this->__set_badbit_and_consider_rethrow(); 1000 } 1001#endif // _LIBCPP_HAS_NO_EXCEPTIONS 1002 return *this; 1003} 1004 1005template <class _CharT, class _Traits> 1006typename basic_ostream<_CharT, _Traits>::pos_type 1007basic_ostream<_CharT, _Traits>::tellp() 1008{ 1009 if (this->fail()) 1010 return pos_type(-1); 1011 return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out); 1012} 1013 1014template <class _CharT, class _Traits> 1015basic_ostream<_CharT, _Traits>& 1016basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) 1017{ 1018 sentry __s(*this); 1019 if (!this->fail()) 1020 { 1021 if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1)) 1022 this->setstate(ios_base::failbit); 1023 } 1024 return *this; 1025} 1026 1027template <class _CharT, class _Traits> 1028basic_ostream<_CharT, _Traits>& 1029basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) 1030{ 1031 sentry __s(*this); 1032 if (!this->fail()) 1033 { 1034 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1)) 1035 this->setstate(ios_base::failbit); 1036 } 1037 return *this; 1038} 1039 1040template <class _CharT, class _Traits> 1041_LIBCPP_HIDE_FROM_ABI inline 1042basic_ostream<_CharT, _Traits>& 1043endl(basic_ostream<_CharT, _Traits>& __os) 1044{ 1045 __os.put(__os.widen('\n')); 1046 __os.flush(); 1047 return __os; 1048} 1049 1050template <class _CharT, class _Traits> 1051_LIBCPP_HIDE_FROM_ABI inline 1052basic_ostream<_CharT, _Traits>& 1053ends(basic_ostream<_CharT, _Traits>& __os) 1054{ 1055 __os.put(_CharT()); 1056 return __os; 1057} 1058 1059template <class _CharT, class _Traits> 1060_LIBCPP_HIDE_FROM_ABI inline 1061basic_ostream<_CharT, _Traits>& 1062flush(basic_ostream<_CharT, _Traits>& __os) 1063{ 1064 __os.flush(); 1065 return __os; 1066} 1067 1068template <class _Stream, class _Tp, class = void> 1069struct __is_ostreamable : false_type { }; 1070 1071template <class _Stream, class _Tp> 1072struct __is_ostreamable<_Stream, _Tp, decltype( 1073 std::declval<_Stream>() << std::declval<_Tp>(), void() 1074)> : true_type { }; 1075 1076template <class _Stream, class _Tp, class = typename enable_if< 1077 _And<is_base_of<ios_base, _Stream>, 1078 __is_ostreamable<_Stream&, const _Tp&> >::value 1079>::type> 1080_LIBCPP_INLINE_VISIBILITY 1081_Stream&& operator<<(_Stream&& __os, const _Tp& __x) 1082{ 1083 __os << __x; 1084 return _VSTD::move(__os); 1085} 1086 1087template<class _CharT, class _Traits, class _Allocator> 1088basic_ostream<_CharT, _Traits>& 1089operator<<(basic_ostream<_CharT, _Traits>& __os, 1090 const basic_string<_CharT, _Traits, _Allocator>& __str) 1091{ 1092 return _VSTD::__put_character_sequence(__os, __str.data(), __str.size()); 1093} 1094 1095template<class _CharT, class _Traits> 1096_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 1097operator<<(basic_ostream<_CharT, _Traits>& __os, 1098 basic_string_view<_CharT, _Traits> __sv) 1099{ 1100 return _VSTD::__put_character_sequence(__os, __sv.data(), __sv.size()); 1101} 1102 1103template <class _CharT, class _Traits> 1104inline _LIBCPP_INLINE_VISIBILITY 1105basic_ostream<_CharT, _Traits>& 1106operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec) 1107{ 1108 return __os << __ec.category().name() << ':' << __ec.value(); 1109} 1110 1111template<class _CharT, class _Traits, class _Yp> 1112inline _LIBCPP_INLINE_VISIBILITY 1113basic_ostream<_CharT, _Traits>& 1114operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p) 1115{ 1116 return __os << __p.get(); 1117} 1118 1119template<class _CharT, class _Traits, class _Yp, class _Dp> 1120inline _LIBCPP_INLINE_VISIBILITY 1121typename enable_if 1122< 1123 is_same<void, __void_t<decltype((std::declval<basic_ostream<_CharT, _Traits>&>() << std::declval<typename unique_ptr<_Yp, _Dp>::pointer>()))> >::value, 1124 basic_ostream<_CharT, _Traits>& 1125>::type 1126operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p) 1127{ 1128 return __os << __p.get(); 1129} 1130 1131template <class _CharT, class _Traits, size_t _Size> 1132_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 1133operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) 1134{ 1135 return __os << __x.template to_string<_CharT, _Traits> 1136 (std::use_facet<ctype<_CharT> >(__os.getloc()).widen('0'), 1137 std::use_facet<ctype<_CharT> >(__os.getloc()).widen('1')); 1138} 1139 1140#if _LIBCPP_STD_VER >= 20 1141 1142#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 1143template <class _Traits> 1144basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete; 1145 1146template <class _Traits> 1147basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete; 1148 1149template <class _Traits> 1150basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete; 1151 1152template <class _Traits> 1153basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete; 1154 1155template <class _Traits> 1156basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete; 1157 1158template <class _Traits> 1159basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete; 1160 1161#endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 1162 1163#ifndef _LIBCPP_HAS_NO_CHAR8_T 1164template <class _Traits> 1165basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char8_t) = delete; 1166 1167template <class _Traits> 1168basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete; 1169 1170template <class _Traits> 1171basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete; 1172 1173template <class _Traits> 1174basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete; 1175#endif 1176 1177template <class _Traits> 1178basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char16_t) = delete; 1179 1180template <class _Traits> 1181basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char32_t) = delete; 1182 1183template <class _Traits> 1184basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete; 1185 1186template <class _Traits> 1187basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete; 1188 1189#endif // _LIBCPP_STD_VER >= 20 1190 1191extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>; 1192#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 1193extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>; 1194#endif 1195 1196_LIBCPP_END_NAMESPACE_STD 1197 1198#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 1199# include <atomic> 1200# include <concepts> 1201# include <cstdlib> 1202# include <iterator> 1203# include <type_traits> 1204#endif 1205 1206#endif // _LIBCPP_OSTREAM 1207