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// [ostream.formatted.print], print functions 163template<class... Args> // since C++23 164 void print(ostream& os, format_string<Args...> fmt, Args&&... args); 165template<class... Args> // since C++23 166 void println(ostream& os, format_string<Args...> fmt, Args&&... args); 167 168void vprint_unicode(ostream& os, string_view fmt, format_args args); // since C++23 169void vprint_nonunicode(ostream& os, string_view fmt, format_args args); // since C++23 170} // std 171 172*/ 173 174#include <__availability> 175#include <__config> 176#include <__exception/operations.h> 177#include <__fwd/ostream.h> 178#include <__memory/shared_ptr.h> 179#include <__memory/unique_ptr.h> 180#include <__system_error/error_code.h> 181#include <__type_traits/conjunction.h> 182#include <__type_traits/enable_if.h> 183#include <__type_traits/is_base_of.h> 184#include <__type_traits/void_t.h> 185#include <__utility/declval.h> 186#include <bitset> 187#include <cstdio> 188#include <format> 189#include <ios> 190#include <locale> 191#include <new> 192#include <print> 193#include <streambuf> 194#include <string_view> 195#include <version> 196 197#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 198# pragma GCC system_header 199#endif 200 201_LIBCPP_PUSH_MACROS 202#include <__undef_macros> 203 204_LIBCPP_BEGIN_NAMESPACE_STD 205 206template <class _CharT, class _Traits> 207class _LIBCPP_TEMPLATE_VIS basic_ostream : virtual public basic_ios<_CharT, _Traits> { 208public: 209 // types (inherited from basic_ios (27.5.4)): 210 typedef _CharT char_type; 211 typedef _Traits traits_type; 212 typedef typename traits_type::int_type int_type; 213 typedef typename traits_type::pos_type pos_type; 214 typedef typename traits_type::off_type off_type; 215 216 // 27.7.2.2 Constructor/destructor: 217 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb) { 218 this->init(__sb); 219 } 220 ~basic_ostream() override; 221 222protected: 223 inline _LIBCPP_HIDE_FROM_ABI basic_ostream(basic_ostream&& __rhs); 224 225 // 27.7.2.3 Assign/swap 226 inline _LIBCPP_HIDE_FROM_ABI basic_ostream& operator=(basic_ostream&& __rhs); 227 228 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_ostream& __rhs) { 229 basic_ios<char_type, traits_type>::swap(__rhs); 230 } 231 232 basic_ostream(const basic_ostream& __rhs) = delete; 233 basic_ostream& operator=(const basic_ostream& __rhs) = delete; 234 235public: 236 // 27.7.2.4 Prefix/suffix: 237 class _LIBCPP_TEMPLATE_VIS sentry; 238 239 // 27.7.2.6 Formatted output: 240 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&)) { 241 return __pf(*this); 242 } 243 244 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& 245 operator<<(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) { 246 __pf(*this); 247 return *this; 248 } 249 250 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&)) { 251 __pf(*this); 252 return *this; 253 } 254 255 basic_ostream& operator<<(bool __n); 256 basic_ostream& operator<<(short __n); 257 basic_ostream& operator<<(unsigned short __n); 258 basic_ostream& operator<<(int __n); 259 basic_ostream& operator<<(unsigned int __n); 260 basic_ostream& operator<<(long __n); 261 basic_ostream& operator<<(unsigned long __n); 262 basic_ostream& operator<<(long long __n); 263 basic_ostream& operator<<(unsigned long long __n); 264 basic_ostream& operator<<(float __f); 265 basic_ostream& operator<<(double __f); 266 basic_ostream& operator<<(long double __f); 267 basic_ostream& operator<<(const void* __p); 268 269#if _LIBCPP_STD_VER >= 23 270 _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(const volatile void* __p) { 271 return operator<<(const_cast<const void*>(__p)); 272 } 273#endif 274 275 basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb); 276 277#if _LIBCPP_STD_VER >= 17 278 // LWG 2221 - nullptr. This is not backported to older standards modes. 279 // See https://reviews.llvm.org/D127033 for more info on the rationale. 280 _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(nullptr_t) { return *this << "nullptr"; } 281#endif 282 283 // 27.7.2.7 Unformatted output: 284 basic_ostream& put(char_type __c); 285 basic_ostream& write(const char_type* __s, streamsize __n); 286 basic_ostream& flush(); 287 288 // 27.7.2.5 seeks: 289 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type tellp(); 290 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(pos_type __pos); 291 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir); 292 293protected: 294 _LIBCPP_HIDE_FROM_ABI basic_ostream() {} // extension, intentially does not initialize 295}; 296 297template <class _CharT, class _Traits> 298class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry { 299 bool __ok_; 300 basic_ostream<_CharT, _Traits>& __os_; 301 302public: 303 explicit sentry(basic_ostream<_CharT, _Traits>& __os); 304 ~sentry(); 305 sentry(const sentry&) = delete; 306 sentry& operator=(const sentry&) = delete; 307 308 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; } 309}; 310 311template <class _CharT, class _Traits> 312basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os) : __ok_(false), __os_(__os) { 313 if (__os.good()) { 314 if (__os.tie()) 315 __os.tie()->flush(); 316 __ok_ = true; 317 } 318} 319 320template <class _CharT, class _Traits> 321basic_ostream<_CharT, _Traits>::sentry::~sentry() { 322 if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && !uncaught_exception()) { 323#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 324 try { 325#endif // _LIBCPP_HAS_NO_EXCEPTIONS 326 if (__os_.rdbuf()->pubsync() == -1) 327 __os_.setstate(ios_base::badbit); 328#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 329 } catch (...) { 330 } 331#endif // _LIBCPP_HAS_NO_EXCEPTIONS 332 } 333} 334 335template <class _CharT, class _Traits> 336basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs) { 337 this->move(__rhs); 338} 339 340template <class _CharT, class _Traits> 341basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs) { 342 swap(__rhs); 343 return *this; 344} 345 346template <class _CharT, class _Traits> 347basic_ostream<_CharT, _Traits>::~basic_ostream() {} 348 349template <class _CharT, class _Traits> 350basic_ostream<_CharT, _Traits>& 351basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb) { 352#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 353 try { 354#endif // _LIBCPP_HAS_NO_EXCEPTIONS 355 sentry __s(*this); 356 if (__s) { 357 if (__sb) { 358#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 359 try { 360#endif // _LIBCPP_HAS_NO_EXCEPTIONS 361 typedef istreambuf_iterator<_CharT, _Traits> _Ip; 362 typedef ostreambuf_iterator<_CharT, _Traits> _Op; 363 _Ip __i(__sb); 364 _Ip __eof; 365 _Op __o(*this); 366 size_t __c = 0; 367 for (; __i != __eof; ++__i, ++__o, ++__c) { 368 *__o = *__i; 369 if (__o.failed()) 370 break; 371 } 372 if (__c == 0) 373 this->setstate(ios_base::failbit); 374#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 375 } catch (...) { 376 this->__set_failbit_and_consider_rethrow(); 377 } 378#endif // _LIBCPP_HAS_NO_EXCEPTIONS 379 } else 380 this->setstate(ios_base::badbit); 381 } 382#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 383 } catch (...) { 384 this->__set_badbit_and_consider_rethrow(); 385 } 386#endif // _LIBCPP_HAS_NO_EXCEPTIONS 387 return *this; 388} 389 390template <class _CharT, class _Traits> 391basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __n) { 392#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 393 try { 394#endif // _LIBCPP_HAS_NO_EXCEPTIONS 395 sentry __s(*this); 396 if (__s) { 397 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 398 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 399 if (__f.put(*this, *this, this->fill(), __n).failed()) 400 this->setstate(ios_base::badbit | ios_base::failbit); 401 } 402#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 403 } catch (...) { 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>& basic_ostream<_CharT, _Traits>::operator<<(short __n) { 412#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 413 try { 414#endif // _LIBCPP_HAS_NO_EXCEPTIONS 415 sentry __s(*this); 416 if (__s) { 417 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield; 418 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 419 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 420 if (__f.put(*this, 421 *this, 422 this->fill(), 423 __flags == ios_base::oct || __flags == ios_base::hex 424 ? static_cast<long>(static_cast<unsigned short>(__n)) 425 : static_cast<long>(__n)) 426 .failed()) 427 this->setstate(ios_base::badbit | ios_base::failbit); 428 } 429#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 430 } catch (...) { 431 this->__set_badbit_and_consider_rethrow(); 432 } 433#endif // _LIBCPP_HAS_NO_EXCEPTIONS 434 return *this; 435} 436 437template <class _CharT, class _Traits> 438basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __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(), static_cast<unsigned long>(__n)).failed()) 447 this->setstate(ios_base::badbit | ios_base::failbit); 448 } 449#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 450 } catch (...) { 451 this->__set_badbit_and_consider_rethrow(); 452 } 453#endif // _LIBCPP_HAS_NO_EXCEPTIONS 454 return *this; 455} 456 457template <class _CharT, class _Traits> 458basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __n) { 459#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 460 try { 461#endif // _LIBCPP_HAS_NO_EXCEPTIONS 462 sentry __s(*this); 463 if (__s) { 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 = std::use_facet<_Fp>(this->getloc()); 467 if (__f.put(*this, 468 *this, 469 this->fill(), 470 __flags == ios_base::oct || __flags == ios_base::hex 471 ? static_cast<long>(static_cast<unsigned int>(__n)) 472 : static_cast<long>(__n)) 473 .failed()) 474 this->setstate(ios_base::badbit | ios_base::failbit); 475 } 476#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 477 } catch (...) { 478 this->__set_badbit_and_consider_rethrow(); 479 } 480#endif // _LIBCPP_HAS_NO_EXCEPTIONS 481 return *this; 482} 483 484template <class _CharT, class _Traits> 485basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n) { 486#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 487 try { 488#endif // _LIBCPP_HAS_NO_EXCEPTIONS 489 sentry __s(*this); 490 if (__s) { 491 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 492 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 493 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed()) 494 this->setstate(ios_base::badbit | ios_base::failbit); 495 } 496#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 497 } catch (...) { 498 this->__set_badbit_and_consider_rethrow(); 499 } 500#endif // _LIBCPP_HAS_NO_EXCEPTIONS 501 return *this; 502} 503 504template <class _CharT, class _Traits> 505basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) { 506#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 507 try { 508#endif // _LIBCPP_HAS_NO_EXCEPTIONS 509 sentry __s(*this); 510 if (__s) { 511 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 512 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 513 if (__f.put(*this, *this, this->fill(), __n).failed()) 514 this->setstate(ios_base::badbit | ios_base::failbit); 515 } 516#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 517 } catch (...) { 518 this->__set_badbit_and_consider_rethrow(); 519 } 520#endif // _LIBCPP_HAS_NO_EXCEPTIONS 521 return *this; 522} 523 524template <class _CharT, class _Traits> 525basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n) { 526#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 527 try { 528#endif // _LIBCPP_HAS_NO_EXCEPTIONS 529 sentry __s(*this); 530 if (__s) { 531 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 532 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 533 if (__f.put(*this, *this, this->fill(), __n).failed()) 534 this->setstate(ios_base::badbit | ios_base::failbit); 535 } 536#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 537 } catch (...) { 538 this->__set_badbit_and_consider_rethrow(); 539 } 540#endif // _LIBCPP_HAS_NO_EXCEPTIONS 541 return *this; 542} 543 544template <class _CharT, class _Traits> 545basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long long __n) { 546#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 547 try { 548#endif // _LIBCPP_HAS_NO_EXCEPTIONS 549 sentry __s(*this); 550 if (__s) { 551 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 552 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 553 if (__f.put(*this, *this, this->fill(), __n).failed()) 554 this->setstate(ios_base::badbit | ios_base::failbit); 555 } 556#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 557 } catch (...) { 558 this->__set_badbit_and_consider_rethrow(); 559 } 560#endif // _LIBCPP_HAS_NO_EXCEPTIONS 561 return *this; 562} 563 564template <class _CharT, class _Traits> 565basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n) { 566#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 567 try { 568#endif // _LIBCPP_HAS_NO_EXCEPTIONS 569 sentry __s(*this); 570 if (__s) { 571 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 572 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 573 if (__f.put(*this, *this, this->fill(), __n).failed()) 574 this->setstate(ios_base::badbit | ios_base::failbit); 575 } 576#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 577 } catch (...) { 578 this->__set_badbit_and_consider_rethrow(); 579 } 580#endif // _LIBCPP_HAS_NO_EXCEPTIONS 581 return *this; 582} 583 584template <class _CharT, class _Traits> 585basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __n) { 586#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 587 try { 588#endif // _LIBCPP_HAS_NO_EXCEPTIONS 589 sentry __s(*this); 590 if (__s) { 591 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 592 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 593 if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed()) 594 this->setstate(ios_base::badbit | ios_base::failbit); 595 } 596#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 597 } catch (...) { 598 this->__set_badbit_and_consider_rethrow(); 599 } 600#endif // _LIBCPP_HAS_NO_EXCEPTIONS 601 return *this; 602} 603 604template <class _CharT, class _Traits> 605basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __n) { 606#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 607 try { 608#endif // _LIBCPP_HAS_NO_EXCEPTIONS 609 sentry __s(*this); 610 if (__s) { 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 } catch (...) { 618 this->__set_badbit_and_consider_rethrow(); 619 } 620#endif // _LIBCPP_HAS_NO_EXCEPTIONS 621 return *this; 622} 623 624template <class _CharT, class _Traits> 625basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __n) { 626#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 627 try { 628#endif // _LIBCPP_HAS_NO_EXCEPTIONS 629 sentry __s(*this); 630 if (__s) { 631 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 632 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 633 if (__f.put(*this, *this, this->fill(), __n).failed()) 634 this->setstate(ios_base::badbit | ios_base::failbit); 635 } 636#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 637 } catch (...) { 638 this->__set_badbit_and_consider_rethrow(); 639 } 640#endif // _LIBCPP_HAS_NO_EXCEPTIONS 641 return *this; 642} 643 644template <class _CharT, class _Traits> 645basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __n) { 646#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 647 try { 648#endif // _LIBCPP_HAS_NO_EXCEPTIONS 649 sentry __s(*this); 650 if (__s) { 651 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 652 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 653 if (__f.put(*this, *this, this->fill(), __n).failed()) 654 this->setstate(ios_base::badbit | ios_base::failbit); 655 } 656#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 657 } catch (...) { 658 this->__set_badbit_and_consider_rethrow(); 659 } 660#endif // _LIBCPP_HAS_NO_EXCEPTIONS 661 return *this; 662} 663 664template <class _CharT, class _Traits> 665_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 666__put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) { 667#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 668 try { 669#endif // _LIBCPP_HAS_NO_EXCEPTIONS 670 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 671 if (__s) { 672 typedef ostreambuf_iterator<_CharT, _Traits> _Ip; 673 if (std::__pad_and_output( 674 _Ip(__os), 675 __str, 676 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str, 677 __str + __len, 678 __os, 679 __os.fill()) 680 .failed()) 681 __os.setstate(ios_base::badbit | ios_base::failbit); 682 } 683#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 684 } catch (...) { 685 __os.__set_badbit_and_consider_rethrow(); 686 } 687#endif // _LIBCPP_HAS_NO_EXCEPTIONS 688 return __os; 689} 690 691template <class _CharT, class _Traits> 692_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) { 693 return std::__put_character_sequence(__os, &__c, 1); 694} 695 696template <class _CharT, class _Traits> 697_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn) { 698#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 699 try { 700#endif // _LIBCPP_HAS_NO_EXCEPTIONS 701 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 702 if (__s) { 703 _CharT __c = __os.widen(__cn); 704 typedef ostreambuf_iterator<_CharT, _Traits> _Ip; 705 if (std::__pad_and_output( 706 _Ip(__os), 707 &__c, 708 (__os.flags() & ios_base::adjustfield) == ios_base::left ? &__c + 1 : &__c, 709 &__c + 1, 710 __os, 711 __os.fill()) 712 .failed()) 713 __os.setstate(ios_base::badbit | ios_base::failbit); 714 } 715#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 716 } catch (...) { 717 __os.__set_badbit_and_consider_rethrow(); 718 } 719#endif // _LIBCPP_HAS_NO_EXCEPTIONS 720 return __os; 721} 722 723template <class _Traits> 724_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, char __c) { 725 return std::__put_character_sequence(__os, &__c, 1); 726} 727 728template <class _Traits> 729_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, signed char __c) { 730 return std::__put_character_sequence(__os, (char*)&__c, 1); 731} 732 733template <class _Traits> 734_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) { 735 return std::__put_character_sequence(__os, (char*)&__c, 1); 736} 737 738template <class _CharT, class _Traits> 739_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 740operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str) { 741 return std::__put_character_sequence(__os, __str, _Traits::length(__str)); 742} 743 744template <class _CharT, class _Traits> 745_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 746operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn) { 747#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 748 try { 749#endif // _LIBCPP_HAS_NO_EXCEPTIONS 750 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 751 if (__s) { 752 typedef ostreambuf_iterator<_CharT, _Traits> _Ip; 753 size_t __len = char_traits<char>::length(__strn); 754 const int __bs = 100; 755 _CharT __wbb[__bs]; 756 _CharT* __wb = __wbb; 757 unique_ptr<_CharT, void (*)(void*)> __h(0, free); 758 if (__len > __bs) { 759 __wb = (_CharT*)malloc(__len * sizeof(_CharT)); 760 if (__wb == 0) 761 __throw_bad_alloc(); 762 __h.reset(__wb); 763 } 764 for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p) 765 *__p = __os.widen(*__strn); 766 if (std::__pad_and_output( 767 _Ip(__os), 768 __wb, 769 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __wb + __len : __wb, 770 __wb + __len, 771 __os, 772 __os.fill()) 773 .failed()) 774 __os.setstate(ios_base::badbit | ios_base::failbit); 775 } 776#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 777 } catch (...) { 778 __os.__set_badbit_and_consider_rethrow(); 779 } 780#endif // _LIBCPP_HAS_NO_EXCEPTIONS 781 return __os; 782} 783 784template <class _Traits> 785_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, const char* __str) { 786 return std::__put_character_sequence(__os, __str, _Traits::length(__str)); 787} 788 789template <class _Traits> 790_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& 791operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str) { 792 const char* __s = (const char*)__str; 793 return std::__put_character_sequence(__os, __s, _Traits::length(__s)); 794} 795 796template <class _Traits> 797_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& 798operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str) { 799 const char* __s = (const char*)__str; 800 return std::__put_character_sequence(__os, __s, _Traits::length(__s)); 801} 802 803template <class _CharT, class _Traits> 804basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::put(char_type __c) { 805#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 806 try { 807#endif // _LIBCPP_HAS_NO_EXCEPTIONS 808 sentry __s(*this); 809 if (__s) { 810 typedef ostreambuf_iterator<_CharT, _Traits> _Op; 811 _Op __o(*this); 812 *__o = __c; 813 if (__o.failed()) 814 this->setstate(ios_base::badbit); 815 } 816#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 817 } catch (...) { 818 this->__set_badbit_and_consider_rethrow(); 819 } 820#endif // _LIBCPP_HAS_NO_EXCEPTIONS 821 return *this; 822} 823 824template <class _CharT, class _Traits> 825basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) { 826#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 827 try { 828#endif // _LIBCPP_HAS_NO_EXCEPTIONS 829 sentry __sen(*this); 830 if (__sen && __n) { 831 if (this->rdbuf()->sputn(__s, __n) != __n) 832 this->setstate(ios_base::badbit); 833 } 834#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 835 } catch (...) { 836 this->__set_badbit_and_consider_rethrow(); 837 } 838#endif // _LIBCPP_HAS_NO_EXCEPTIONS 839 return *this; 840} 841 842template <class _CharT, class _Traits> 843basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::flush() { 844#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 845 try { 846#endif // _LIBCPP_HAS_NO_EXCEPTIONS 847 if (this->rdbuf()) { 848 sentry __s(*this); 849 if (__s) { 850 if (this->rdbuf()->pubsync() == -1) 851 this->setstate(ios_base::badbit); 852 } 853 } 854#ifndef _LIBCPP_HAS_NO_EXCEPTIONS 855 } catch (...) { 856 this->__set_badbit_and_consider_rethrow(); 857 } 858#endif // _LIBCPP_HAS_NO_EXCEPTIONS 859 return *this; 860} 861 862template <class _CharT, class _Traits> 863typename basic_ostream<_CharT, _Traits>::pos_type basic_ostream<_CharT, _Traits>::tellp() { 864 if (this->fail()) 865 return pos_type(-1); 866 return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out); 867} 868 869template <class _CharT, class _Traits> 870basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) { 871 sentry __s(*this); 872 if (!this->fail()) { 873 if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1)) 874 this->setstate(ios_base::failbit); 875 } 876 return *this; 877} 878 879template <class _CharT, class _Traits> 880basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) { 881 sentry __s(*this); 882 if (!this->fail()) { 883 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1)) 884 this->setstate(ios_base::failbit); 885 } 886 return *this; 887} 888 889template <class _CharT, class _Traits> 890_LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& endl(basic_ostream<_CharT, _Traits>& __os) { 891 __os.put(__os.widen('\n')); 892 __os.flush(); 893 return __os; 894} 895 896template <class _CharT, class _Traits> 897_LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& ends(basic_ostream<_CharT, _Traits>& __os) { 898 __os.put(_CharT()); 899 return __os; 900} 901 902template <class _CharT, class _Traits> 903_LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& flush(basic_ostream<_CharT, _Traits>& __os) { 904 __os.flush(); 905 return __os; 906} 907 908template <class _Stream, class _Tp, class = void> 909struct __is_ostreamable : false_type {}; 910 911template <class _Stream, class _Tp> 912struct __is_ostreamable<_Stream, _Tp, decltype(std::declval<_Stream>() << std::declval<_Tp>(), void())> : true_type {}; 913 914template <class _Stream, 915 class _Tp, 916 __enable_if_t<_And<is_base_of<ios_base, _Stream>, __is_ostreamable<_Stream&, const _Tp&> >::value, int> = 0> 917_LIBCPP_HIDE_FROM_ABI _Stream&& operator<<(_Stream&& __os, const _Tp& __x) { 918 __os << __x; 919 return std::move(__os); 920} 921 922template <class _CharT, class _Traits, class _Allocator> 923basic_ostream<_CharT, _Traits>& 924operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str) { 925 return std::__put_character_sequence(__os, __str.data(), __str.size()); 926} 927 928template <class _CharT, class _Traits> 929_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 930operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv) { 931 return std::__put_character_sequence(__os, __sv.data(), __sv.size()); 932} 933 934template <class _CharT, class _Traits> 935inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 936operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec) { 937 return __os << __ec.category().name() << ':' << __ec.value(); 938} 939 940template <class _CharT, class _Traits, class _Yp> 941inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 942operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p) { 943 return __os << __p.get(); 944} 945 946template < 947 class _CharT, 948 class _Traits, 949 class _Yp, 950 class _Dp, 951 __enable_if_t<is_same<void, 952 __void_t<decltype((std::declval<basic_ostream<_CharT, _Traits>&>() 953 << std::declval<typename unique_ptr<_Yp, _Dp>::pointer>()))> >::value, 954 int> = 0> 955inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 956operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p) { 957 return __os << __p.get(); 958} 959 960template <class _CharT, class _Traits, size_t _Size> 961_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 962operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) { 963 return __os << __x.template to_string<_CharT, _Traits>(std::use_facet<ctype<_CharT> >(__os.getloc()).widen('0'), 964 std::use_facet<ctype<_CharT> >(__os.getloc()).widen('1')); 965} 966 967#if _LIBCPP_STD_VER >= 20 968 969# ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 970template <class _Traits> 971basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete; 972 973template <class _Traits> 974basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete; 975 976template <class _Traits> 977basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete; 978 979template <class _Traits> 980basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete; 981 982template <class _Traits> 983basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete; 984 985template <class _Traits> 986basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete; 987 988# endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 989 990# ifndef _LIBCPP_HAS_NO_CHAR8_T 991template <class _Traits> 992basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char8_t) = delete; 993 994template <class _Traits> 995basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete; 996 997template <class _Traits> 998basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete; 999 1000template <class _Traits> 1001basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete; 1002# endif 1003 1004template <class _Traits> 1005basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char16_t) = delete; 1006 1007template <class _Traits> 1008basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char32_t) = delete; 1009 1010template <class _Traits> 1011basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete; 1012 1013template <class _Traits> 1014basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete; 1015 1016#endif // _LIBCPP_STD_VER >= 20 1017 1018extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>; 1019#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 1020extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>; 1021#endif 1022 1023#if _LIBCPP_STD_VER >= 23 1024 1025template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 1026_LIBCPP_HIDE_FROM_ABI inline void 1027__vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) { 1028 // [ostream.formatted.print]/3 1029 // Effects: Behaves as a formatted output function 1030 // ([ostream.formatted.reqmts]) of os, except that: 1031 // - failure to generate output is reported as specified below, and 1032 // - any exception thrown by the call to vformat is propagated without regard 1033 // to the value of os.exceptions() and without turning on ios_base::badbit 1034 // in the error state of os. 1035 // After constructing a sentry object, the function initializes an automatic 1036 // variable via 1037 // string out = vformat(os.getloc(), fmt, args); 1038 1039 ostream::sentry __s(__os); 1040 if (__s) { 1041 string __o = std::vformat(__os.getloc(), __fmt, __args); 1042 if (__write_nl) 1043 __o += '\n'; 1044 1045 const char* __str = __o.data(); 1046 size_t __len = __o.size(); 1047 1048# ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1049 try { 1050# endif // _LIBCPP_HAS_NO_EXCEPTIONS 1051 typedef ostreambuf_iterator<char> _Ip; 1052 if (std::__pad_and_output( 1053 _Ip(__os), 1054 __str, 1055 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str, 1056 __str + __len, 1057 __os, 1058 __os.fill()) 1059 .failed()) 1060 __os.setstate(ios_base::badbit | ios_base::failbit); 1061 1062# ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1063 } catch (...) { 1064 __os.__set_badbit_and_consider_rethrow(); 1065 } 1066# endif // _LIBCPP_HAS_NO_EXCEPTIONS 1067 } 1068} 1069 1070template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 1071_LIBCPP_HIDE_FROM_ABI inline void vprint_nonunicode(ostream& __os, string_view __fmt, format_args __args) { 1072 std::__vprint_nonunicode(__os, __fmt, __args, false); 1073} 1074 1075// Returns the FILE* associated with the __os. 1076// Returns a nullptr when no FILE* is associated with __os. 1077// This function is in the dylib since the type of the buffer associated 1078// with std::cout, std::cerr, and std::clog is only known in the dylib. 1079// 1080// This function implements part of the implementation-defined behavior 1081// of [ostream.formatted.print]/3 1082// If the function is vprint_unicode and os is a stream that refers to 1083// a terminal capable of displaying Unicode which is determined in an 1084// implementation-defined manner, writes out to the terminal using the 1085// native Unicode API; 1086// Whether the returned FILE* is "a terminal capable of displaying Unicode" 1087// is determined in the same way as the print(FILE*, ...) overloads. 1088_LIBCPP_EXPORTED_FROM_ABI FILE* __get_ostream_file(ostream& __os); 1089 1090# ifndef _LIBCPP_HAS_NO_UNICODE 1091template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 1092_LIBCPP_HIDE_FROM_ABI void __vprint_unicode(ostream& __os, string_view __fmt, format_args __args, bool __write_nl) { 1093# if _LIBCPP_AVAILABILITY_HAS_PRINT == 0 1094 return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl); 1095# else 1096 FILE* __file = std::__get_ostream_file(__os); 1097 if (!__file || !__print::__is_terminal(__file)) 1098 return std::__vprint_nonunicode(__os, __fmt, __args, __write_nl); 1099 1100 // [ostream.formatted.print]/3 1101 // If the function is vprint_unicode and os is a stream that refers to a 1102 // terminal capable of displaying Unicode which is determined in an 1103 // implementation-defined manner, writes out to the terminal using the 1104 // native Unicode API; if out contains invalid code units, the behavior is 1105 // undefined and implementations are encouraged to diagnose it. If the 1106 // native Unicode API is used, the function flushes os before writing out. 1107 // 1108 // This is the path for the native API, start with flushing. 1109 __os.flush(); 1110 1111# ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1112 try { 1113# endif // _LIBCPP_HAS_NO_EXCEPTIONS 1114 ostream::sentry __s(__os); 1115 if (__s) { 1116# ifndef _LIBCPP_WIN32API 1117 __print::__vprint_unicode_posix(__file, __fmt, __args, __write_nl, true); 1118# elif !defined(_LIBCPP_HAS_NO_WIDE_CHARACTERS) 1119 __print::__vprint_unicode_windows(__file, __fmt, __args, __write_nl, true); 1120# else 1121# error "Windows builds with wchar_t disabled are not supported." 1122# endif 1123 } 1124 1125# ifndef _LIBCPP_HAS_NO_EXCEPTIONS 1126 } catch (...) { 1127 __os.__set_badbit_and_consider_rethrow(); 1128 } 1129# endif // _LIBCPP_HAS_NO_EXCEPTIONS 1130# endif // _LIBCPP_AVAILABILITY_HAS_PRINT 1131} 1132 1133template <class = void> // TODO PRINT template or availability markup fires too eagerly (http://llvm.org/PR61563). 1134_LIBCPP_HIDE_FROM_ABI inline void vprint_unicode(ostream& __os, string_view __fmt, format_args __args) { 1135 std::__vprint_unicode(__os, __fmt, __args, false); 1136} 1137# endif // _LIBCPP_HAS_NO_UNICODE 1138 1139template <class... _Args> 1140_LIBCPP_HIDE_FROM_ABI void print(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) { 1141# ifndef _LIBCPP_HAS_NO_UNICODE 1142 if constexpr (__print::__use_unicode_execution_charset) 1143 std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), false); 1144 else 1145 std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false); 1146# else // _LIBCPP_HAS_NO_UNICODE 1147 std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), false); 1148# endif // _LIBCPP_HAS_NO_UNICODE 1149} 1150 1151template <class... _Args> 1152_LIBCPP_HIDE_FROM_ABI void println(ostream& __os, format_string<_Args...> __fmt, _Args&&... __args) { 1153# ifndef _LIBCPP_HAS_NO_UNICODE 1154 // Note the wording in the Standard is inefficient. The output of 1155 // std::format is a std::string which is then copied. This solution 1156 // just appends a newline at the end of the output. 1157 if constexpr (__print::__use_unicode_execution_charset) 1158 std::__vprint_unicode(__os, __fmt.get(), std::make_format_args(__args...), true); 1159 else 1160 std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true); 1161# else // _LIBCPP_HAS_NO_UNICODE 1162 std::__vprint_nonunicode(__os, __fmt.get(), std::make_format_args(__args...), true); 1163# endif // _LIBCPP_HAS_NO_UNICODE 1164} 1165 1166#endif // _LIBCPP_STD_VER >= 23 1167 1168_LIBCPP_END_NAMESPACE_STD 1169 1170_LIBCPP_POP_MACROS 1171 1172#if !defined(_LIBCPP_REMOVE_TRANSITIVE_INCLUDES) && _LIBCPP_STD_VER <= 20 1173# include <atomic> 1174# include <concepts> 1175# include <cstdlib> 1176# include <iosfwd> 1177# include <iterator> 1178# include <stdexcept> 1179# include <type_traits> 1180#endif 1181 1182#endif // _LIBCPP_OSTREAM 1183