1 //===---------------------------------------------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===---------------------------------------------------------------------===// 8 9 #ifndef _LIBCPP___OSTREAM_BASIC_OSTREAM_H 10 #define _LIBCPP___OSTREAM_BASIC_OSTREAM_H 11 12 #include <__config> 13 #include <__exception/operations.h> 14 #include <__memory/shared_ptr.h> 15 #include <__memory/unique_ptr.h> 16 #include <__system_error/error_code.h> 17 #include <__type_traits/conjunction.h> 18 #include <__type_traits/enable_if.h> 19 #include <__type_traits/is_base_of.h> 20 #include <__type_traits/void_t.h> 21 #include <__utility/declval.h> 22 #include <bitset> 23 #include <cstddef> 24 #include <ios> 25 #include <locale> 26 #include <new> // for __throw_bad_alloc 27 #include <streambuf> 28 #include <string_view> 29 30 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 31 # pragma GCC system_header 32 #endif 33 34 _LIBCPP_PUSH_MACROS 35 #include <__undef_macros> 36 37 _LIBCPP_BEGIN_NAMESPACE_STD 38 39 template <class _CharT, class _Traits> 40 class _LIBCPP_TEMPLATE_VIS basic_ostream : virtual public basic_ios<_CharT, _Traits> { 41 public: 42 // types (inherited from basic_ios (27.5.4)): 43 typedef _CharT char_type; 44 typedef _Traits traits_type; 45 typedef typename traits_type::int_type int_type; 46 typedef typename traits_type::pos_type pos_type; 47 typedef typename traits_type::off_type off_type; 48 49 // 27.7.2.2 Constructor/destructor: 50 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 explicit basic_ostream(basic_streambuf<char_type, traits_type>* __sb) { 51 this->init(__sb); 52 } 53 ~basic_ostream() override; 54 55 basic_ostream(const basic_ostream& __rhs) = delete; 56 basic_ostream& operator=(const basic_ostream& __rhs) = delete; 57 58 protected: 59 inline _LIBCPP_HIDE_FROM_ABI basic_ostream(basic_ostream&& __rhs); 60 61 // 27.7.2.3 Assign/swap 62 inline _LIBCPP_HIDE_FROM_ABI basic_ostream& operator=(basic_ostream&& __rhs); 63 64 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 void swap(basic_ostream& __rhs) { 65 basic_ios<char_type, traits_type>::swap(__rhs); 66 } 67 68 public: 69 // 27.7.2.4 Prefix/suffix: 70 class _LIBCPP_TEMPLATE_VIS sentry; 71 72 // 27.7.2.6 Formatted output: 73 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(basic_ostream& (*__pf)(basic_ostream&)) { 74 return __pf(*this); 75 } 76 77 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& 78 operator<<(basic_ios<char_type, traits_type>& (*__pf)(basic_ios<char_type, traits_type>&)) { 79 __pf(*this); 80 return *this; 81 } 82 83 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& operator<<(ios_base& (*__pf)(ios_base&)) { 84 __pf(*this); 85 return *this; 86 } 87 88 basic_ostream& operator<<(bool __n); 89 basic_ostream& operator<<(short __n); 90 basic_ostream& operator<<(unsigned short __n); 91 basic_ostream& operator<<(int __n); 92 basic_ostream& operator<<(unsigned int __n); 93 basic_ostream& operator<<(long __n); 94 basic_ostream& operator<<(unsigned long __n); 95 basic_ostream& operator<<(long long __n); 96 basic_ostream& operator<<(unsigned long long __n); 97 basic_ostream& operator<<(float __f); 98 basic_ostream& operator<<(double __f); 99 basic_ostream& operator<<(long double __f); 100 basic_ostream& operator<<(const void* __p); 101 102 #if _LIBCPP_STD_VER >= 23 103 _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(const volatile void* __p) { 104 return operator<<(const_cast<const void*>(__p)); 105 } 106 #endif 107 108 basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb); 109 110 #if _LIBCPP_STD_VER >= 17 111 // LWG 2221 - nullptr. This is not backported to older standards modes. 112 // See https://reviews.llvm.org/D127033 for more info on the rationale. 113 _LIBCPP_HIDE_FROM_ABI basic_ostream& operator<<(nullptr_t) { return *this << "nullptr"; } 114 #endif 115 116 // 27.7.2.7 Unformatted output: 117 basic_ostream& put(char_type __c); 118 basic_ostream& write(const char_type* __s, streamsize __n); 119 basic_ostream& flush(); 120 121 // 27.7.2.5 seeks: 122 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type tellp(); 123 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(pos_type __pos); 124 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir); 125 126 protected: 127 _LIBCPP_HIDE_FROM_ABI basic_ostream() {} // extension, intentially does not initialize 128 }; 129 130 template <class _CharT, class _Traits> 131 class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry { 132 bool __ok_; 133 basic_ostream<_CharT, _Traits>& __os_; 134 135 public: 136 explicit sentry(basic_ostream<_CharT, _Traits>& __os); 137 ~sentry(); 138 sentry(const sentry&) = delete; 139 sentry& operator=(const sentry&) = delete; 140 141 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; } 142 }; 143 144 template <class _CharT, class _Traits> 145 basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os) : __ok_(false), __os_(__os) { 146 if (__os.good()) { 147 if (__os.tie()) 148 __os.tie()->flush(); 149 __ok_ = true; 150 } 151 } 152 153 template <class _CharT, class _Traits> 154 basic_ostream<_CharT, _Traits>::sentry::~sentry() { 155 if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && !uncaught_exception()) { 156 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 157 try { 158 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 159 if (__os_.rdbuf()->pubsync() == -1) 160 __os_.setstate(ios_base::badbit); 161 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 162 } catch (...) { 163 } 164 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 165 } 166 } 167 168 template <class _CharT, class _Traits> 169 basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs) { 170 this->move(__rhs); 171 } 172 173 template <class _CharT, class _Traits> 174 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs) { 175 swap(__rhs); 176 return *this; 177 } 178 179 template <class _CharT, class _Traits> 180 basic_ostream<_CharT, _Traits>::~basic_ostream() {} 181 182 template <class _CharT, class _Traits> 183 basic_ostream<_CharT, _Traits>& 184 basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb) { 185 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 186 try { 187 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 188 sentry __s(*this); 189 if (__s) { 190 if (__sb) { 191 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 192 try { 193 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 194 typedef istreambuf_iterator<_CharT, _Traits> _Ip; 195 typedef ostreambuf_iterator<_CharT, _Traits> _Op; 196 _Ip __i(__sb); 197 _Ip __eof; 198 _Op __o(*this); 199 size_t __c = 0; 200 for (; __i != __eof; ++__i, ++__o, ++__c) { 201 *__o = *__i; 202 if (__o.failed()) 203 break; 204 } 205 if (__c == 0) 206 this->setstate(ios_base::failbit); 207 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 208 } catch (...) { 209 this->__set_failbit_and_consider_rethrow(); 210 } 211 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 212 } else 213 this->setstate(ios_base::badbit); 214 } 215 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 216 } catch (...) { 217 this->__set_badbit_and_consider_rethrow(); 218 } 219 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 220 return *this; 221 } 222 223 template <class _CharT, class _Traits> 224 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __n) { 225 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 226 try { 227 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 228 sentry __s(*this); 229 if (__s) { 230 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 231 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 232 if (__f.put(*this, *this, this->fill(), __n).failed()) 233 this->setstate(ios_base::badbit | ios_base::failbit); 234 } 235 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 236 } catch (...) { 237 this->__set_badbit_and_consider_rethrow(); 238 } 239 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 240 return *this; 241 } 242 243 template <class _CharT, class _Traits> 244 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(short __n) { 245 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 246 try { 247 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 248 sentry __s(*this); 249 if (__s) { 250 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield; 251 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 252 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 253 if (__f.put(*this, 254 *this, 255 this->fill(), 256 __flags == ios_base::oct || __flags == ios_base::hex 257 ? static_cast<long>(static_cast<unsigned short>(__n)) 258 : static_cast<long>(__n)) 259 .failed()) 260 this->setstate(ios_base::badbit | ios_base::failbit); 261 } 262 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 263 } catch (...) { 264 this->__set_badbit_and_consider_rethrow(); 265 } 266 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 267 return *this; 268 } 269 270 template <class _CharT, class _Traits> 271 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n) { 272 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 273 try { 274 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 275 sentry __s(*this); 276 if (__s) { 277 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 278 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 279 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed()) 280 this->setstate(ios_base::badbit | ios_base::failbit); 281 } 282 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 283 } catch (...) { 284 this->__set_badbit_and_consider_rethrow(); 285 } 286 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 287 return *this; 288 } 289 290 template <class _CharT, class _Traits> 291 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __n) { 292 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 293 try { 294 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 295 sentry __s(*this); 296 if (__s) { 297 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield; 298 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 299 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 300 if (__f.put(*this, 301 *this, 302 this->fill(), 303 __flags == ios_base::oct || __flags == ios_base::hex 304 ? static_cast<long>(static_cast<unsigned int>(__n)) 305 : static_cast<long>(__n)) 306 .failed()) 307 this->setstate(ios_base::badbit | ios_base::failbit); 308 } 309 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 310 } catch (...) { 311 this->__set_badbit_and_consider_rethrow(); 312 } 313 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 314 return *this; 315 } 316 317 template <class _CharT, class _Traits> 318 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n) { 319 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 320 try { 321 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 322 sentry __s(*this); 323 if (__s) { 324 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 325 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 326 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed()) 327 this->setstate(ios_base::badbit | ios_base::failbit); 328 } 329 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 330 } catch (...) { 331 this->__set_badbit_and_consider_rethrow(); 332 } 333 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 334 return *this; 335 } 336 337 template <class _CharT, class _Traits> 338 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) { 339 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 340 try { 341 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 342 sentry __s(*this); 343 if (__s) { 344 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 345 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 346 if (__f.put(*this, *this, this->fill(), __n).failed()) 347 this->setstate(ios_base::badbit | ios_base::failbit); 348 } 349 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 350 } catch (...) { 351 this->__set_badbit_and_consider_rethrow(); 352 } 353 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 354 return *this; 355 } 356 357 template <class _CharT, class _Traits> 358 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n) { 359 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 360 try { 361 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 362 sentry __s(*this); 363 if (__s) { 364 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 365 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 366 if (__f.put(*this, *this, this->fill(), __n).failed()) 367 this->setstate(ios_base::badbit | ios_base::failbit); 368 } 369 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 370 } catch (...) { 371 this->__set_badbit_and_consider_rethrow(); 372 } 373 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 374 return *this; 375 } 376 377 template <class _CharT, class _Traits> 378 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long long __n) { 379 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 380 try { 381 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 382 sentry __s(*this); 383 if (__s) { 384 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 385 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 386 if (__f.put(*this, *this, this->fill(), __n).failed()) 387 this->setstate(ios_base::badbit | ios_base::failbit); 388 } 389 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 390 } catch (...) { 391 this->__set_badbit_and_consider_rethrow(); 392 } 393 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 394 return *this; 395 } 396 397 template <class _CharT, class _Traits> 398 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n) { 399 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 400 try { 401 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 402 sentry __s(*this); 403 if (__s) { 404 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 405 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 406 if (__f.put(*this, *this, this->fill(), __n).failed()) 407 this->setstate(ios_base::badbit | ios_base::failbit); 408 } 409 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 410 } catch (...) { 411 this->__set_badbit_and_consider_rethrow(); 412 } 413 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 414 return *this; 415 } 416 417 template <class _CharT, class _Traits> 418 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __n) { 419 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 420 try { 421 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 422 sentry __s(*this); 423 if (__s) { 424 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 425 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 426 if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed()) 427 this->setstate(ios_base::badbit | ios_base::failbit); 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 437 template <class _CharT, class _Traits> 438 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __n) { 439 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 440 try { 441 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 442 sentry __s(*this); 443 if (__s) { 444 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 445 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 446 if (__f.put(*this, *this, this->fill(), __n).failed()) 447 this->setstate(ios_base::badbit | ios_base::failbit); 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 457 template <class _CharT, class _Traits> 458 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __n) { 459 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 460 try { 461 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 462 sentry __s(*this); 463 if (__s) { 464 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 465 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 466 if (__f.put(*this, *this, this->fill(), __n).failed()) 467 this->setstate(ios_base::badbit | ios_base::failbit); 468 } 469 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 470 } catch (...) { 471 this->__set_badbit_and_consider_rethrow(); 472 } 473 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 474 return *this; 475 } 476 477 template <class _CharT, class _Traits> 478 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __n) { 479 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 480 try { 481 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 482 sentry __s(*this); 483 if (__s) { 484 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp; 485 const _Fp& __f = std::use_facet<_Fp>(this->getloc()); 486 if (__f.put(*this, *this, this->fill(), __n).failed()) 487 this->setstate(ios_base::badbit | ios_base::failbit); 488 } 489 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 490 } catch (...) { 491 this->__set_badbit_and_consider_rethrow(); 492 } 493 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 494 return *this; 495 } 496 497 template <class _CharT, class _Traits> 498 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 499 __put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) { 500 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 501 try { 502 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 503 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 504 if (__s) { 505 typedef ostreambuf_iterator<_CharT, _Traits> _Ip; 506 if (std::__pad_and_output( 507 _Ip(__os), 508 __str, 509 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str, 510 __str + __len, 511 __os, 512 __os.fill()) 513 .failed()) 514 __os.setstate(ios_base::badbit | ios_base::failbit); 515 } 516 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 517 } catch (...) { 518 __os.__set_badbit_and_consider_rethrow(); 519 } 520 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 521 return __os; 522 } 523 524 template <class _CharT, class _Traits> 525 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) { 526 return std::__put_character_sequence(__os, &__c, 1); 527 } 528 529 template <class _CharT, class _Traits> 530 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn) { 531 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 532 try { 533 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 534 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 535 if (__s) { 536 _CharT __c = __os.widen(__cn); 537 typedef ostreambuf_iterator<_CharT, _Traits> _Ip; 538 if (std::__pad_and_output( 539 _Ip(__os), 540 &__c, 541 (__os.flags() & ios_base::adjustfield) == ios_base::left ? &__c + 1 : &__c, 542 &__c + 1, 543 __os, 544 __os.fill()) 545 .failed()) 546 __os.setstate(ios_base::badbit | ios_base::failbit); 547 } 548 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 549 } catch (...) { 550 __os.__set_badbit_and_consider_rethrow(); 551 } 552 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 553 return __os; 554 } 555 556 template <class _Traits> 557 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, char __c) { 558 return std::__put_character_sequence(__os, &__c, 1); 559 } 560 561 template <class _Traits> 562 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, signed char __c) { 563 return std::__put_character_sequence(__os, (char*)&__c, 1); 564 } 565 566 template <class _Traits> 567 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) { 568 return std::__put_character_sequence(__os, (char*)&__c, 1); 569 } 570 571 template <class _CharT, class _Traits> 572 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 573 operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str) { 574 return std::__put_character_sequence(__os, __str, _Traits::length(__str)); 575 } 576 577 template <class _CharT, class _Traits> 578 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 579 operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn) { 580 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 581 try { 582 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 583 typename basic_ostream<_CharT, _Traits>::sentry __s(__os); 584 if (__s) { 585 typedef ostreambuf_iterator<_CharT, _Traits> _Ip; 586 size_t __len = char_traits<char>::length(__strn); 587 const int __bs = 100; 588 _CharT __wbb[__bs]; 589 _CharT* __wb = __wbb; 590 unique_ptr<_CharT, void (*)(void*)> __h(0, free); 591 if (__len > __bs) { 592 __wb = (_CharT*)malloc(__len * sizeof(_CharT)); 593 if (__wb == 0) 594 __throw_bad_alloc(); 595 __h.reset(__wb); 596 } 597 for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p) 598 *__p = __os.widen(*__strn); 599 if (std::__pad_and_output( 600 _Ip(__os), 601 __wb, 602 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __wb + __len : __wb, 603 __wb + __len, 604 __os, 605 __os.fill()) 606 .failed()) 607 __os.setstate(ios_base::badbit | ios_base::failbit); 608 } 609 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 610 } catch (...) { 611 __os.__set_badbit_and_consider_rethrow(); 612 } 613 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 614 return __os; 615 } 616 617 template <class _Traits> 618 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, const char* __str) { 619 return std::__put_character_sequence(__os, __str, _Traits::length(__str)); 620 } 621 622 template <class _Traits> 623 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& 624 operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str) { 625 const char* __s = (const char*)__str; 626 return std::__put_character_sequence(__os, __s, _Traits::length(__s)); 627 } 628 629 template <class _Traits> 630 _LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& 631 operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str) { 632 const char* __s = (const char*)__str; 633 return std::__put_character_sequence(__os, __s, _Traits::length(__s)); 634 } 635 636 template <class _CharT, class _Traits> 637 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::put(char_type __c) { 638 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 639 try { 640 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 641 sentry __s(*this); 642 if (__s) { 643 typedef ostreambuf_iterator<_CharT, _Traits> _Op; 644 _Op __o(*this); 645 *__o = __c; 646 if (__o.failed()) 647 this->setstate(ios_base::badbit); 648 } 649 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 650 } catch (...) { 651 this->__set_badbit_and_consider_rethrow(); 652 } 653 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 654 return *this; 655 } 656 657 template <class _CharT, class _Traits> 658 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) { 659 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 660 try { 661 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 662 sentry __sen(*this); 663 if (__sen && __n) { 664 if (this->rdbuf()->sputn(__s, __n) != __n) 665 this->setstate(ios_base::badbit); 666 } 667 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 668 } catch (...) { 669 this->__set_badbit_and_consider_rethrow(); 670 } 671 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 672 return *this; 673 } 674 675 template <class _CharT, class _Traits> 676 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::flush() { 677 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 678 try { 679 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 680 if (this->rdbuf()) { 681 sentry __s(*this); 682 if (__s) { 683 if (this->rdbuf()->pubsync() == -1) 684 this->setstate(ios_base::badbit); 685 } 686 } 687 #ifndef _LIBCPP_HAS_NO_EXCEPTIONS 688 } catch (...) { 689 this->__set_badbit_and_consider_rethrow(); 690 } 691 #endif // _LIBCPP_HAS_NO_EXCEPTIONS 692 return *this; 693 } 694 695 template <class _CharT, class _Traits> 696 typename basic_ostream<_CharT, _Traits>::pos_type basic_ostream<_CharT, _Traits>::tellp() { 697 if (this->fail()) 698 return pos_type(-1); 699 return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out); 700 } 701 702 template <class _CharT, class _Traits> 703 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) { 704 sentry __s(*this); 705 if (!this->fail()) { 706 if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1)) 707 this->setstate(ios_base::failbit); 708 } 709 return *this; 710 } 711 712 template <class _CharT, class _Traits> 713 basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) { 714 sentry __s(*this); 715 if (!this->fail()) { 716 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1)) 717 this->setstate(ios_base::failbit); 718 } 719 return *this; 720 } 721 722 template <class _CharT, class _Traits> 723 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& endl(basic_ostream<_CharT, _Traits>& __os) { 724 __os.put(__os.widen('\n')); 725 __os.flush(); 726 return __os; 727 } 728 729 template <class _CharT, class _Traits> 730 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& ends(basic_ostream<_CharT, _Traits>& __os) { 731 __os.put(_CharT()); 732 return __os; 733 } 734 735 template <class _CharT, class _Traits> 736 _LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& flush(basic_ostream<_CharT, _Traits>& __os) { 737 __os.flush(); 738 return __os; 739 } 740 741 template <class _Stream, class _Tp, class = void> 742 struct __is_ostreamable : false_type {}; 743 744 template <class _Stream, class _Tp> 745 struct __is_ostreamable<_Stream, _Tp, decltype(std::declval<_Stream>() << std::declval<_Tp>(), void())> : true_type {}; 746 747 template <class _Stream, 748 class _Tp, 749 __enable_if_t<_And<is_base_of<ios_base, _Stream>, __is_ostreamable<_Stream&, const _Tp&> >::value, int> = 0> 750 _LIBCPP_HIDE_FROM_ABI _Stream&& operator<<(_Stream&& __os, const _Tp& __x) { 751 __os << __x; 752 return std::move(__os); 753 } 754 755 template <class _CharT, class _Traits, class _Allocator> 756 basic_ostream<_CharT, _Traits>& 757 operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str) { 758 return std::__put_character_sequence(__os, __str.data(), __str.size()); 759 } 760 761 template <class _CharT, class _Traits> 762 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 763 operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv) { 764 return std::__put_character_sequence(__os, __sv.data(), __sv.size()); 765 } 766 767 template <class _CharT, class _Traits> 768 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 769 operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec) { 770 return __os << __ec.category().name() << ':' << __ec.value(); 771 } 772 773 template <class _CharT, class _Traits, class _Yp> 774 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 775 operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p) { 776 return __os << __p.get(); 777 } 778 779 template < 780 class _CharT, 781 class _Traits, 782 class _Yp, 783 class _Dp, 784 __enable_if_t<is_same<void, 785 __void_t<decltype((std::declval<basic_ostream<_CharT, _Traits>&>() 786 << std::declval<typename unique_ptr<_Yp, _Dp>::pointer>()))> >::value, 787 int> = 0> 788 inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 789 operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p) { 790 return __os << __p.get(); 791 } 792 793 template <class _CharT, class _Traits, size_t _Size> 794 _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& 795 operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) { 796 return __os << __x.template to_string<_CharT, _Traits>(std::use_facet<ctype<_CharT> >(__os.getloc()).widen('0'), 797 std::use_facet<ctype<_CharT> >(__os.getloc()).widen('1')); 798 } 799 800 #if _LIBCPP_STD_VER >= 20 801 802 # ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 803 template <class _Traits> 804 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, wchar_t) = delete; 805 806 template <class _Traits> 807 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const wchar_t*) = delete; 808 809 template <class _Traits> 810 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char16_t) = delete; 811 812 template <class _Traits> 813 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char32_t) = delete; 814 815 template <class _Traits> 816 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char16_t*) = delete; 817 818 template <class _Traits> 819 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char32_t*) = delete; 820 821 # endif // _LIBCPP_HAS_NO_WIDE_CHARACTERS 822 823 # ifndef _LIBCPP_HAS_NO_CHAR8_T 824 template <class _Traits> 825 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char8_t) = delete; 826 827 template <class _Traits> 828 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, char8_t) = delete; 829 830 template <class _Traits> 831 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char8_t*) = delete; 832 833 template <class _Traits> 834 basic_ostream<wchar_t, _Traits>& operator<<(basic_ostream<wchar_t, _Traits>&, const char8_t*) = delete; 835 # endif 836 837 template <class _Traits> 838 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char16_t) = delete; 839 840 template <class _Traits> 841 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, char32_t) = delete; 842 843 template <class _Traits> 844 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char16_t*) = delete; 845 846 template <class _Traits> 847 basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>&, const char32_t*) = delete; 848 849 #endif // _LIBCPP_STD_VER >= 20 850 851 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>; 852 #ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS 853 extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>; 854 #endif 855 856 _LIBCPP_END_NAMESPACE_STD 857 858 _LIBCPP_POP_MACROS 859 860 #endif // _LIBCPP___OSTREAM_BASIC_OSTREAM_H 861