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