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