798 lines · c
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-exception6//7//===---------------------------------------------------------------------===//8 9#ifndef _LIBCPP___CXX03___OSTREAM_BASIC_OSTREAM_H10#define _LIBCPP___CXX03___OSTREAM_BASIC_OSTREAM_H11 12#include <__cxx03/__config>13#include <__cxx03/__exception/operations.h>14#include <__cxx03/__memory/shared_ptr.h>15#include <__cxx03/__memory/unique_ptr.h>16#include <__cxx03/__system_error/error_code.h>17#include <__cxx03/__type_traits/conjunction.h>18#include <__cxx03/__type_traits/enable_if.h>19#include <__cxx03/__type_traits/is_base_of.h>20#include <__cxx03/__type_traits/void_t.h>21#include <__cxx03/__utility/declval.h>22#include <__cxx03/bitset>23#include <__cxx03/cstddef>24#include <__cxx03/ios>25#include <__cxx03/locale>26#include <__cxx03/new> // for __throw_bad_alloc27#include <__cxx03/streambuf>28#include <__cxx03/string_view>29 30#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)31# pragma GCC system_header32#endif33 34_LIBCPP_PUSH_MACROS35#include <__cxx03/__undef_macros>36 37_LIBCPP_BEGIN_NAMESPACE_STD38 39template <class _CharT, class _Traits>40class _LIBCPP_TEMPLATE_VIS basic_ostream : virtual public basic_ios<_CharT, _Traits> {41public: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 58protected:59 inline _LIBCPP_HIDE_FROM_ABI basic_ostream(basic_ostream&& __rhs);60 61 // 27.7.2.3 Assign/swap62 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 68public: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 basic_ostream& operator<<(basic_streambuf<char_type, traits_type>* __sb);103 104 // 27.7.2.7 Unformatted output:105 basic_ostream& put(char_type __c);106 basic_ostream& write(const char_type* __s, streamsize __n);107 basic_ostream& flush();108 109 // 27.7.2.5 seeks:110 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 pos_type tellp();111 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(pos_type __pos);112 inline _LIBCPP_HIDE_FROM_ABI_AFTER_V1 basic_ostream& seekp(off_type __off, ios_base::seekdir __dir);113 114protected:115 _LIBCPP_HIDE_FROM_ABI basic_ostream() {} // extension, intentially does not initialize116};117 118template <class _CharT, class _Traits>119class _LIBCPP_TEMPLATE_VIS basic_ostream<_CharT, _Traits>::sentry {120 bool __ok_;121 basic_ostream<_CharT, _Traits>& __os_;122 123public:124 explicit sentry(basic_ostream<_CharT, _Traits>& __os);125 ~sentry();126 sentry(const sentry&) = delete;127 sentry& operator=(const sentry&) = delete;128 129 _LIBCPP_HIDE_FROM_ABI explicit operator bool() const { return __ok_; }130};131 132template <class _CharT, class _Traits>133basic_ostream<_CharT, _Traits>::sentry::sentry(basic_ostream<_CharT, _Traits>& __os) : __ok_(false), __os_(__os) {134 if (__os.good()) {135 if (__os.tie())136 __os.tie()->flush();137 __ok_ = true;138 }139}140 141template <class _CharT, class _Traits>142basic_ostream<_CharT, _Traits>::sentry::~sentry() {143 if (__os_.rdbuf() && __os_.good() && (__os_.flags() & ios_base::unitbuf) && !uncaught_exception()) {144#ifndef _LIBCPP_HAS_NO_EXCEPTIONS145 try {146#endif // _LIBCPP_HAS_NO_EXCEPTIONS147 if (__os_.rdbuf()->pubsync() == -1)148 __os_.setstate(ios_base::badbit);149#ifndef _LIBCPP_HAS_NO_EXCEPTIONS150 } catch (...) {151 }152#endif // _LIBCPP_HAS_NO_EXCEPTIONS153 }154}155 156template <class _CharT, class _Traits>157basic_ostream<_CharT, _Traits>::basic_ostream(basic_ostream&& __rhs) {158 this->move(__rhs);159}160 161template <class _CharT, class _Traits>162basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator=(basic_ostream&& __rhs) {163 swap(__rhs);164 return *this;165}166 167template <class _CharT, class _Traits>168basic_ostream<_CharT, _Traits>::~basic_ostream() {}169 170template <class _CharT, class _Traits>171basic_ostream<_CharT, _Traits>&172basic_ostream<_CharT, _Traits>::operator<<(basic_streambuf<char_type, traits_type>* __sb) {173#ifndef _LIBCPP_HAS_NO_EXCEPTIONS174 try {175#endif // _LIBCPP_HAS_NO_EXCEPTIONS176 sentry __s(*this);177 if (__s) {178 if (__sb) {179#ifndef _LIBCPP_HAS_NO_EXCEPTIONS180 try {181#endif // _LIBCPP_HAS_NO_EXCEPTIONS182 typedef istreambuf_iterator<_CharT, _Traits> _Ip;183 typedef ostreambuf_iterator<_CharT, _Traits> _Op;184 _Ip __i(__sb);185 _Ip __eof;186 _Op __o(*this);187 size_t __c = 0;188 for (; __i != __eof; ++__i, ++__o, ++__c) {189 *__o = *__i;190 if (__o.failed())191 break;192 }193 if (__c == 0)194 this->setstate(ios_base::failbit);195#ifndef _LIBCPP_HAS_NO_EXCEPTIONS196 } catch (...) {197 this->__set_failbit_and_consider_rethrow();198 }199#endif // _LIBCPP_HAS_NO_EXCEPTIONS200 } else201 this->setstate(ios_base::badbit);202 }203#ifndef _LIBCPP_HAS_NO_EXCEPTIONS204 } catch (...) {205 this->__set_badbit_and_consider_rethrow();206 }207#endif // _LIBCPP_HAS_NO_EXCEPTIONS208 return *this;209}210 211template <class _CharT, class _Traits>212basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(bool __n) {213#ifndef _LIBCPP_HAS_NO_EXCEPTIONS214 try {215#endif // _LIBCPP_HAS_NO_EXCEPTIONS216 sentry __s(*this);217 if (__s) {218 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;219 const _Fp& __f = std::use_facet<_Fp>(this->getloc());220 if (__f.put(*this, *this, this->fill(), __n).failed())221 this->setstate(ios_base::badbit | ios_base::failbit);222 }223#ifndef _LIBCPP_HAS_NO_EXCEPTIONS224 } catch (...) {225 this->__set_badbit_and_consider_rethrow();226 }227#endif // _LIBCPP_HAS_NO_EXCEPTIONS228 return *this;229}230 231template <class _CharT, class _Traits>232basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(short __n) {233#ifndef _LIBCPP_HAS_NO_EXCEPTIONS234 try {235#endif // _LIBCPP_HAS_NO_EXCEPTIONS236 sentry __s(*this);237 if (__s) {238 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;239 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;240 const _Fp& __f = std::use_facet<_Fp>(this->getloc());241 if (__f.put(*this,242 *this,243 this->fill(),244 __flags == ios_base::oct || __flags == ios_base::hex245 ? static_cast<long>(static_cast<unsigned short>(__n))246 : static_cast<long>(__n))247 .failed())248 this->setstate(ios_base::badbit | ios_base::failbit);249 }250#ifndef _LIBCPP_HAS_NO_EXCEPTIONS251 } catch (...) {252 this->__set_badbit_and_consider_rethrow();253 }254#endif // _LIBCPP_HAS_NO_EXCEPTIONS255 return *this;256}257 258template <class _CharT, class _Traits>259basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned short __n) {260#ifndef _LIBCPP_HAS_NO_EXCEPTIONS261 try {262#endif // _LIBCPP_HAS_NO_EXCEPTIONS263 sentry __s(*this);264 if (__s) {265 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;266 const _Fp& __f = std::use_facet<_Fp>(this->getloc());267 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())268 this->setstate(ios_base::badbit | ios_base::failbit);269 }270#ifndef _LIBCPP_HAS_NO_EXCEPTIONS271 } catch (...) {272 this->__set_badbit_and_consider_rethrow();273 }274#endif // _LIBCPP_HAS_NO_EXCEPTIONS275 return *this;276}277 278template <class _CharT, class _Traits>279basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(int __n) {280#ifndef _LIBCPP_HAS_NO_EXCEPTIONS281 try {282#endif // _LIBCPP_HAS_NO_EXCEPTIONS283 sentry __s(*this);284 if (__s) {285 ios_base::fmtflags __flags = ios_base::flags() & ios_base::basefield;286 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;287 const _Fp& __f = std::use_facet<_Fp>(this->getloc());288 if (__f.put(*this,289 *this,290 this->fill(),291 __flags == ios_base::oct || __flags == ios_base::hex292 ? static_cast<long>(static_cast<unsigned int>(__n))293 : static_cast<long>(__n))294 .failed())295 this->setstate(ios_base::badbit | ios_base::failbit);296 }297#ifndef _LIBCPP_HAS_NO_EXCEPTIONS298 } catch (...) {299 this->__set_badbit_and_consider_rethrow();300 }301#endif // _LIBCPP_HAS_NO_EXCEPTIONS302 return *this;303}304 305template <class _CharT, class _Traits>306basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned int __n) {307#ifndef _LIBCPP_HAS_NO_EXCEPTIONS308 try {309#endif // _LIBCPP_HAS_NO_EXCEPTIONS310 sentry __s(*this);311 if (__s) {312 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;313 const _Fp& __f = std::use_facet<_Fp>(this->getloc());314 if (__f.put(*this, *this, this->fill(), static_cast<unsigned long>(__n)).failed())315 this->setstate(ios_base::badbit | ios_base::failbit);316 }317#ifndef _LIBCPP_HAS_NO_EXCEPTIONS318 } catch (...) {319 this->__set_badbit_and_consider_rethrow();320 }321#endif // _LIBCPP_HAS_NO_EXCEPTIONS322 return *this;323}324 325template <class _CharT, class _Traits>326basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long __n) {327#ifndef _LIBCPP_HAS_NO_EXCEPTIONS328 try {329#endif // _LIBCPP_HAS_NO_EXCEPTIONS330 sentry __s(*this);331 if (__s) {332 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;333 const _Fp& __f = std::use_facet<_Fp>(this->getloc());334 if (__f.put(*this, *this, this->fill(), __n).failed())335 this->setstate(ios_base::badbit | ios_base::failbit);336 }337#ifndef _LIBCPP_HAS_NO_EXCEPTIONS338 } catch (...) {339 this->__set_badbit_and_consider_rethrow();340 }341#endif // _LIBCPP_HAS_NO_EXCEPTIONS342 return *this;343}344 345template <class _CharT, class _Traits>346basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n) {347#ifndef _LIBCPP_HAS_NO_EXCEPTIONS348 try {349#endif // _LIBCPP_HAS_NO_EXCEPTIONS350 sentry __s(*this);351 if (__s) {352 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;353 const _Fp& __f = std::use_facet<_Fp>(this->getloc());354 if (__f.put(*this, *this, this->fill(), __n).failed())355 this->setstate(ios_base::badbit | ios_base::failbit);356 }357#ifndef _LIBCPP_HAS_NO_EXCEPTIONS358 } catch (...) {359 this->__set_badbit_and_consider_rethrow();360 }361#endif // _LIBCPP_HAS_NO_EXCEPTIONS362 return *this;363}364 365template <class _CharT, class _Traits>366basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long long __n) {367#ifndef _LIBCPP_HAS_NO_EXCEPTIONS368 try {369#endif // _LIBCPP_HAS_NO_EXCEPTIONS370 sentry __s(*this);371 if (__s) {372 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;373 const _Fp& __f = std::use_facet<_Fp>(this->getloc());374 if (__f.put(*this, *this, this->fill(), __n).failed())375 this->setstate(ios_base::badbit | ios_base::failbit);376 }377#ifndef _LIBCPP_HAS_NO_EXCEPTIONS378 } catch (...) {379 this->__set_badbit_and_consider_rethrow();380 }381#endif // _LIBCPP_HAS_NO_EXCEPTIONS382 return *this;383}384 385template <class _CharT, class _Traits>386basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n) {387#ifndef _LIBCPP_HAS_NO_EXCEPTIONS388 try {389#endif // _LIBCPP_HAS_NO_EXCEPTIONS390 sentry __s(*this);391 if (__s) {392 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;393 const _Fp& __f = std::use_facet<_Fp>(this->getloc());394 if (__f.put(*this, *this, this->fill(), __n).failed())395 this->setstate(ios_base::badbit | ios_base::failbit);396 }397#ifndef _LIBCPP_HAS_NO_EXCEPTIONS398 } catch (...) {399 this->__set_badbit_and_consider_rethrow();400 }401#endif // _LIBCPP_HAS_NO_EXCEPTIONS402 return *this;403}404 405template <class _CharT, class _Traits>406basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(float __n) {407#ifndef _LIBCPP_HAS_NO_EXCEPTIONS408 try {409#endif // _LIBCPP_HAS_NO_EXCEPTIONS410 sentry __s(*this);411 if (__s) {412 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;413 const _Fp& __f = std::use_facet<_Fp>(this->getloc());414 if (__f.put(*this, *this, this->fill(), static_cast<double>(__n)).failed())415 this->setstate(ios_base::badbit | ios_base::failbit);416 }417#ifndef _LIBCPP_HAS_NO_EXCEPTIONS418 } catch (...) {419 this->__set_badbit_and_consider_rethrow();420 }421#endif // _LIBCPP_HAS_NO_EXCEPTIONS422 return *this;423}424 425template <class _CharT, class _Traits>426basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(double __n) {427#ifndef _LIBCPP_HAS_NO_EXCEPTIONS428 try {429#endif // _LIBCPP_HAS_NO_EXCEPTIONS430 sentry __s(*this);431 if (__s) {432 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;433 const _Fp& __f = std::use_facet<_Fp>(this->getloc());434 if (__f.put(*this, *this, this->fill(), __n).failed())435 this->setstate(ios_base::badbit | ios_base::failbit);436 }437#ifndef _LIBCPP_HAS_NO_EXCEPTIONS438 } catch (...) {439 this->__set_badbit_and_consider_rethrow();440 }441#endif // _LIBCPP_HAS_NO_EXCEPTIONS442 return *this;443}444 445template <class _CharT, class _Traits>446basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(long double __n) {447#ifndef _LIBCPP_HAS_NO_EXCEPTIONS448 try {449#endif // _LIBCPP_HAS_NO_EXCEPTIONS450 sentry __s(*this);451 if (__s) {452 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;453 const _Fp& __f = std::use_facet<_Fp>(this->getloc());454 if (__f.put(*this, *this, this->fill(), __n).failed())455 this->setstate(ios_base::badbit | ios_base::failbit);456 }457#ifndef _LIBCPP_HAS_NO_EXCEPTIONS458 } catch (...) {459 this->__set_badbit_and_consider_rethrow();460 }461#endif // _LIBCPP_HAS_NO_EXCEPTIONS462 return *this;463}464 465template <class _CharT, class _Traits>466basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::operator<<(const void* __n) {467#ifndef _LIBCPP_HAS_NO_EXCEPTIONS468 try {469#endif // _LIBCPP_HAS_NO_EXCEPTIONS470 sentry __s(*this);471 if (__s) {472 typedef num_put<char_type, ostreambuf_iterator<char_type, traits_type> > _Fp;473 const _Fp& __f = std::use_facet<_Fp>(this->getloc());474 if (__f.put(*this, *this, this->fill(), __n).failed())475 this->setstate(ios_base::badbit | ios_base::failbit);476 }477#ifndef _LIBCPP_HAS_NO_EXCEPTIONS478 } catch (...) {479 this->__set_badbit_and_consider_rethrow();480 }481#endif // _LIBCPP_HAS_NO_EXCEPTIONS482 return *this;483}484 485template <class _CharT, class _Traits>486_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&487__put_character_sequence(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str, size_t __len) {488#ifndef _LIBCPP_HAS_NO_EXCEPTIONS489 try {490#endif // _LIBCPP_HAS_NO_EXCEPTIONS491 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);492 if (__s) {493 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;494 if (std::__pad_and_output(495 _Ip(__os),496 __str,497 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __str + __len : __str,498 __str + __len,499 __os,500 __os.fill())501 .failed())502 __os.setstate(ios_base::badbit | ios_base::failbit);503 }504#ifndef _LIBCPP_HAS_NO_EXCEPTIONS505 } catch (...) {506 __os.__set_badbit_and_consider_rethrow();507 }508#endif // _LIBCPP_HAS_NO_EXCEPTIONS509 return __os;510}511 512template <class _CharT, class _Traits>513_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c) {514 return std::__put_character_sequence(__os, &__c, 1);515}516 517template <class _CharT, class _Traits>518_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>& operator<<(basic_ostream<_CharT, _Traits>& __os, char __cn) {519#ifndef _LIBCPP_HAS_NO_EXCEPTIONS520 try {521#endif // _LIBCPP_HAS_NO_EXCEPTIONS522 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);523 if (__s) {524 _CharT __c = __os.widen(__cn);525 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;526 if (std::__pad_and_output(527 _Ip(__os),528 &__c,529 (__os.flags() & ios_base::adjustfield) == ios_base::left ? &__c + 1 : &__c,530 &__c + 1,531 __os,532 __os.fill())533 .failed())534 __os.setstate(ios_base::badbit | ios_base::failbit);535 }536#ifndef _LIBCPP_HAS_NO_EXCEPTIONS537 } catch (...) {538 __os.__set_badbit_and_consider_rethrow();539 }540#endif // _LIBCPP_HAS_NO_EXCEPTIONS541 return __os;542}543 544template <class _Traits>545_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, char __c) {546 return std::__put_character_sequence(__os, &__c, 1);547}548 549template <class _Traits>550_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, signed char __c) {551 return std::__put_character_sequence(__os, (char*)&__c, 1);552}553 554template <class _Traits>555_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) {556 return std::__put_character_sequence(__os, (char*)&__c, 1);557}558 559template <class _CharT, class _Traits>560_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&561operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __str) {562 return std::__put_character_sequence(__os, __str, _Traits::length(__str));563}564 565template <class _CharT, class _Traits>566_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&567operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __strn) {568#ifndef _LIBCPP_HAS_NO_EXCEPTIONS569 try {570#endif // _LIBCPP_HAS_NO_EXCEPTIONS571 typename basic_ostream<_CharT, _Traits>::sentry __s(__os);572 if (__s) {573 typedef ostreambuf_iterator<_CharT, _Traits> _Ip;574 size_t __len = char_traits<char>::length(__strn);575 const int __bs = 100;576 _CharT __wbb[__bs];577 _CharT* __wb = __wbb;578 unique_ptr<_CharT, void (*)(void*)> __h(0, free);579 if (__len > __bs) {580 __wb = (_CharT*)malloc(__len * sizeof(_CharT));581 if (__wb == 0)582 __throw_bad_alloc();583 __h.reset(__wb);584 }585 for (_CharT* __p = __wb; *__strn != '\0'; ++__strn, ++__p)586 *__p = __os.widen(*__strn);587 if (std::__pad_and_output(588 _Ip(__os),589 __wb,590 (__os.flags() & ios_base::adjustfield) == ios_base::left ? __wb + __len : __wb,591 __wb + __len,592 __os,593 __os.fill())594 .failed())595 __os.setstate(ios_base::badbit | ios_base::failbit);596 }597#ifndef _LIBCPP_HAS_NO_EXCEPTIONS598 } catch (...) {599 __os.__set_badbit_and_consider_rethrow();600 }601#endif // _LIBCPP_HAS_NO_EXCEPTIONS602 return __os;603}604 605template <class _Traits>606_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>& operator<<(basic_ostream<char, _Traits>& __os, const char* __str) {607 return std::__put_character_sequence(__os, __str, _Traits::length(__str));608}609 610template <class _Traits>611_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&612operator<<(basic_ostream<char, _Traits>& __os, const signed char* __str) {613 const char* __s = (const char*)__str;614 return std::__put_character_sequence(__os, __s, _Traits::length(__s));615}616 617template <class _Traits>618_LIBCPP_HIDE_FROM_ABI basic_ostream<char, _Traits>&619operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __str) {620 const char* __s = (const char*)__str;621 return std::__put_character_sequence(__os, __s, _Traits::length(__s));622}623 624template <class _CharT, class _Traits>625basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::put(char_type __c) {626#ifndef _LIBCPP_HAS_NO_EXCEPTIONS627 try {628#endif // _LIBCPP_HAS_NO_EXCEPTIONS629 sentry __s(*this);630 if (__s) {631 typedef ostreambuf_iterator<_CharT, _Traits> _Op;632 _Op __o(*this);633 *__o = __c;634 if (__o.failed())635 this->setstate(ios_base::badbit);636 }637#ifndef _LIBCPP_HAS_NO_EXCEPTIONS638 } catch (...) {639 this->__set_badbit_and_consider_rethrow();640 }641#endif // _LIBCPP_HAS_NO_EXCEPTIONS642 return *this;643}644 645template <class _CharT, class _Traits>646basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::write(const char_type* __s, streamsize __n) {647#ifndef _LIBCPP_HAS_NO_EXCEPTIONS648 try {649#endif // _LIBCPP_HAS_NO_EXCEPTIONS650 sentry __sen(*this);651 if (__sen && __n) {652 if (this->rdbuf()->sputn(__s, __n) != __n)653 this->setstate(ios_base::badbit);654 }655#ifndef _LIBCPP_HAS_NO_EXCEPTIONS656 } catch (...) {657 this->__set_badbit_and_consider_rethrow();658 }659#endif // _LIBCPP_HAS_NO_EXCEPTIONS660 return *this;661}662 663template <class _CharT, class _Traits>664basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::flush() {665#ifndef _LIBCPP_HAS_NO_EXCEPTIONS666 try {667#endif // _LIBCPP_HAS_NO_EXCEPTIONS668 if (this->rdbuf()) {669 sentry __s(*this);670 if (__s) {671 if (this->rdbuf()->pubsync() == -1)672 this->setstate(ios_base::badbit);673 }674 }675#ifndef _LIBCPP_HAS_NO_EXCEPTIONS676 } catch (...) {677 this->__set_badbit_and_consider_rethrow();678 }679#endif // _LIBCPP_HAS_NO_EXCEPTIONS680 return *this;681}682 683template <class _CharT, class _Traits>684typename basic_ostream<_CharT, _Traits>::pos_type basic_ostream<_CharT, _Traits>::tellp() {685 if (this->fail())686 return pos_type(-1);687 return this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);688}689 690template <class _CharT, class _Traits>691basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(pos_type __pos) {692 sentry __s(*this);693 if (!this->fail()) {694 if (this->rdbuf()->pubseekpos(__pos, ios_base::out) == pos_type(-1))695 this->setstate(ios_base::failbit);696 }697 return *this;698}699 700template <class _CharT, class _Traits>701basic_ostream<_CharT, _Traits>& basic_ostream<_CharT, _Traits>::seekp(off_type __off, ios_base::seekdir __dir) {702 sentry __s(*this);703 if (!this->fail()) {704 if (this->rdbuf()->pubseekoff(__off, __dir, ios_base::out) == pos_type(-1))705 this->setstate(ios_base::failbit);706 }707 return *this;708}709 710template <class _CharT, class _Traits>711_LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& endl(basic_ostream<_CharT, _Traits>& __os) {712 __os.put(__os.widen('\n'));713 __os.flush();714 return __os;715}716 717template <class _CharT, class _Traits>718_LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& ends(basic_ostream<_CharT, _Traits>& __os) {719 __os.put(_CharT());720 return __os;721}722 723template <class _CharT, class _Traits>724_LIBCPP_HIDE_FROM_ABI inline basic_ostream<_CharT, _Traits>& flush(basic_ostream<_CharT, _Traits>& __os) {725 __os.flush();726 return __os;727}728 729template <class _Stream, class _Tp, class = void>730struct __is_ostreamable : false_type {};731 732template <class _Stream, class _Tp>733struct __is_ostreamable<_Stream, _Tp, decltype(std::declval<_Stream>() << std::declval<_Tp>(), void())> : true_type {};734 735template <class _Stream,736 class _Tp,737 __enable_if_t<_And<is_base_of<ios_base, _Stream>, __is_ostreamable<_Stream&, const _Tp&> >::value, int> = 0>738_LIBCPP_HIDE_FROM_ABI _Stream&& operator<<(_Stream&& __os, const _Tp& __x) {739 __os << __x;740 return std::move(__os);741}742 743template <class _CharT, class _Traits, class _Allocator>744basic_ostream<_CharT, _Traits>&745operator<<(basic_ostream<_CharT, _Traits>& __os, const basic_string<_CharT, _Traits, _Allocator>& __str) {746 return std::__put_character_sequence(__os, __str.data(), __str.size());747}748 749template <class _CharT, class _Traits>750_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&751operator<<(basic_ostream<_CharT, _Traits>& __os, basic_string_view<_CharT, _Traits> __sv) {752 return std::__put_character_sequence(__os, __sv.data(), __sv.size());753}754 755template <class _CharT, class _Traits>756inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&757operator<<(basic_ostream<_CharT, _Traits>& __os, const error_code& __ec) {758 return __os << __ec.category().name() << ':' << __ec.value();759}760 761template <class _CharT, class _Traits, class _Yp>762inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&763operator<<(basic_ostream<_CharT, _Traits>& __os, shared_ptr<_Yp> const& __p) {764 return __os << __p.get();765}766 767template <768 class _CharT,769 class _Traits,770 class _Yp,771 class _Dp,772 __enable_if_t<is_same<void,773 __void_t<decltype((std::declval<basic_ostream<_CharT, _Traits>&>()774 << std::declval<typename unique_ptr<_Yp, _Dp>::pointer>()))> >::value,775 int> = 0>776inline _LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&777operator<<(basic_ostream<_CharT, _Traits>& __os, unique_ptr<_Yp, _Dp> const& __p) {778 return __os << __p.get();779}780 781template <class _CharT, class _Traits, size_t _Size>782_LIBCPP_HIDE_FROM_ABI basic_ostream<_CharT, _Traits>&783operator<<(basic_ostream<_CharT, _Traits>& __os, const bitset<_Size>& __x) {784 return __os << __x.template to_string<_CharT, _Traits>(std::use_facet<ctype<_CharT> >(__os.getloc()).widen('0'),785 std::use_facet<ctype<_CharT> >(__os.getloc()).widen('1'));786}787 788extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<char>;789#ifndef _LIBCPP_HAS_NO_WIDE_CHARACTERS790extern template class _LIBCPP_EXTERN_TEMPLATE_TYPE_VIS basic_ostream<wchar_t>;791#endif792 793_LIBCPP_END_NAMESPACE_STD794 795_LIBCPP_POP_MACROS796 797#endif // _LIBCPP___CXX03___OSTREAM_BASIC_OSTREAM_H798