1*38fd1498Szrj // ostream classes -*- C++ -*- 2*38fd1498Szrj 3*38fd1498Szrj // Copyright (C) 1997-2018 Free Software Foundation, Inc. 4*38fd1498Szrj // 5*38fd1498Szrj // This file is part of the GNU ISO C++ Library. This library is free 6*38fd1498Szrj // software; you can redistribute it and/or modify it under the 7*38fd1498Szrj // terms of the GNU General Public License as published by the 8*38fd1498Szrj // Free Software Foundation; either version 3, or (at your option) 9*38fd1498Szrj // any later version. 10*38fd1498Szrj 11*38fd1498Szrj // This library is distributed in the hope that it will be useful, 12*38fd1498Szrj // but WITHOUT ANY WARRANTY; without even the implied warranty of 13*38fd1498Szrj // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14*38fd1498Szrj // GNU General Public License for more details. 15*38fd1498Szrj 16*38fd1498Szrj // Under Section 7 of GPL version 3, you are granted additional 17*38fd1498Szrj // permissions described in the GCC Runtime Library Exception, version 18*38fd1498Szrj // 3.1, as published by the Free Software Foundation. 19*38fd1498Szrj 20*38fd1498Szrj // You should have received a copy of the GNU General Public License and 21*38fd1498Szrj // a copy of the GCC Runtime Library Exception along with this program; 22*38fd1498Szrj // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see 23*38fd1498Szrj // <http://www.gnu.org/licenses/>. 24*38fd1498Szrj 25*38fd1498Szrj /** @file bits/ostream.tcc 26*38fd1498Szrj * This is an internal header file, included by other library headers. 27*38fd1498Szrj * Do not attempt to use it directly. @headername{ostream} 28*38fd1498Szrj */ 29*38fd1498Szrj 30*38fd1498Szrj // 31*38fd1498Szrj // ISO C++ 14882: 27.6.2 Output streams 32*38fd1498Szrj // 33*38fd1498Szrj 34*38fd1498Szrj #ifndef _OSTREAM_TCC 35*38fd1498Szrj #define _OSTREAM_TCC 1 36*38fd1498Szrj 37*38fd1498Szrj #pragma GCC system_header 38*38fd1498Szrj 39*38fd1498Szrj #include <bits/cxxabi_forced.h> 40*38fd1498Szrj 41*38fd1498Szrj namespace std _GLIBCXX_VISIBILITY(default) 42*38fd1498Szrj { 43*38fd1498Szrj _GLIBCXX_BEGIN_NAMESPACE_VERSION 44*38fd1498Szrj 45*38fd1498Szrj template<typename _CharT, typename _Traits> 46*38fd1498Szrj basic_ostream<_CharT, _Traits>::sentry:: sentry(basic_ostream<_CharT,_Traits> & __os)47*38fd1498Szrj sentry(basic_ostream<_CharT, _Traits>& __os) 48*38fd1498Szrj : _M_ok(false), _M_os(__os) 49*38fd1498Szrj { 50*38fd1498Szrj // XXX MT 51*38fd1498Szrj if (__os.tie() && __os.good()) 52*38fd1498Szrj __os.tie()->flush(); 53*38fd1498Szrj 54*38fd1498Szrj if (__os.good()) 55*38fd1498Szrj _M_ok = true; 56*38fd1498Szrj else 57*38fd1498Szrj __os.setstate(ios_base::failbit); 58*38fd1498Szrj } 59*38fd1498Szrj 60*38fd1498Szrj template<typename _CharT, typename _Traits> 61*38fd1498Szrj template<typename _ValueT> 62*38fd1498Szrj basic_ostream<_CharT, _Traits>& 63*38fd1498Szrj basic_ostream<_CharT, _Traits>:: _M_insert(_ValueT __v)64*38fd1498Szrj _M_insert(_ValueT __v) 65*38fd1498Szrj { 66*38fd1498Szrj sentry __cerb(*this); 67*38fd1498Szrj if (__cerb) 68*38fd1498Szrj { 69*38fd1498Szrj ios_base::iostate __err = ios_base::goodbit; 70*38fd1498Szrj __try 71*38fd1498Szrj { 72*38fd1498Szrj const __num_put_type& __np = __check_facet(this->_M_num_put); 73*38fd1498Szrj if (__np.put(*this, *this, this->fill(), __v).failed()) 74*38fd1498Szrj __err |= ios_base::badbit; 75*38fd1498Szrj } 76*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&) 77*38fd1498Szrj { 78*38fd1498Szrj this->_M_setstate(ios_base::badbit); 79*38fd1498Szrj __throw_exception_again; 80*38fd1498Szrj } 81*38fd1498Szrj __catch(...) 82*38fd1498Szrj { this->_M_setstate(ios_base::badbit); } 83*38fd1498Szrj if (__err) 84*38fd1498Szrj this->setstate(__err); 85*38fd1498Szrj } 86*38fd1498Szrj return *this; 87*38fd1498Szrj } 88*38fd1498Szrj 89*38fd1498Szrj template<typename _CharT, typename _Traits> 90*38fd1498Szrj basic_ostream<_CharT, _Traits>& 91*38fd1498Szrj basic_ostream<_CharT, _Traits>:: operator <<(short __n)92*38fd1498Szrj operator<<(short __n) 93*38fd1498Szrj { 94*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 95*38fd1498Szrj // 117. basic_ostream uses nonexistent num_put member functions. 96*38fd1498Szrj const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield; 97*38fd1498Szrj if (__fmt == ios_base::oct || __fmt == ios_base::hex) 98*38fd1498Szrj return _M_insert(static_cast<long>(static_cast<unsigned short>(__n))); 99*38fd1498Szrj else 100*38fd1498Szrj return _M_insert(static_cast<long>(__n)); 101*38fd1498Szrj } 102*38fd1498Szrj 103*38fd1498Szrj template<typename _CharT, typename _Traits> 104*38fd1498Szrj basic_ostream<_CharT, _Traits>& 105*38fd1498Szrj basic_ostream<_CharT, _Traits>:: operator <<(int __n)106*38fd1498Szrj operator<<(int __n) 107*38fd1498Szrj { 108*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 109*38fd1498Szrj // 117. basic_ostream uses nonexistent num_put member functions. 110*38fd1498Szrj const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield; 111*38fd1498Szrj if (__fmt == ios_base::oct || __fmt == ios_base::hex) 112*38fd1498Szrj return _M_insert(static_cast<long>(static_cast<unsigned int>(__n))); 113*38fd1498Szrj else 114*38fd1498Szrj return _M_insert(static_cast<long>(__n)); 115*38fd1498Szrj } 116*38fd1498Szrj 117*38fd1498Szrj template<typename _CharT, typename _Traits> 118*38fd1498Szrj basic_ostream<_CharT, _Traits>& 119*38fd1498Szrj basic_ostream<_CharT, _Traits>:: operator <<(__streambuf_type * __sbin)120*38fd1498Szrj operator<<(__streambuf_type* __sbin) 121*38fd1498Szrj { 122*38fd1498Szrj ios_base::iostate __err = ios_base::goodbit; 123*38fd1498Szrj sentry __cerb(*this); 124*38fd1498Szrj if (__cerb && __sbin) 125*38fd1498Szrj { 126*38fd1498Szrj __try 127*38fd1498Szrj { 128*38fd1498Szrj if (!__copy_streambufs(__sbin, this->rdbuf())) 129*38fd1498Szrj __err |= ios_base::failbit; 130*38fd1498Szrj } 131*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&) 132*38fd1498Szrj { 133*38fd1498Szrj this->_M_setstate(ios_base::badbit); 134*38fd1498Szrj __throw_exception_again; 135*38fd1498Szrj } 136*38fd1498Szrj __catch(...) 137*38fd1498Szrj { this->_M_setstate(ios_base::failbit); } 138*38fd1498Szrj } 139*38fd1498Szrj else if (!__sbin) 140*38fd1498Szrj __err |= ios_base::badbit; 141*38fd1498Szrj if (__err) 142*38fd1498Szrj this->setstate(__err); 143*38fd1498Szrj return *this; 144*38fd1498Szrj } 145*38fd1498Szrj 146*38fd1498Szrj template<typename _CharT, typename _Traits> 147*38fd1498Szrj basic_ostream<_CharT, _Traits>& 148*38fd1498Szrj basic_ostream<_CharT, _Traits>:: put(char_type __c)149*38fd1498Szrj put(char_type __c) 150*38fd1498Szrj { 151*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 152*38fd1498Szrj // DR 60. What is a formatted input function? 153*38fd1498Szrj // basic_ostream::put(char_type) is an unformatted output function. 154*38fd1498Szrj // DR 63. Exception-handling policy for unformatted output. 155*38fd1498Szrj // Unformatted output functions should catch exceptions thrown 156*38fd1498Szrj // from streambuf members. 157*38fd1498Szrj sentry __cerb(*this); 158*38fd1498Szrj if (__cerb) 159*38fd1498Szrj { 160*38fd1498Szrj ios_base::iostate __err = ios_base::goodbit; 161*38fd1498Szrj __try 162*38fd1498Szrj { 163*38fd1498Szrj const int_type __put = this->rdbuf()->sputc(__c); 164*38fd1498Szrj if (traits_type::eq_int_type(__put, traits_type::eof())) 165*38fd1498Szrj __err |= ios_base::badbit; 166*38fd1498Szrj } 167*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&) 168*38fd1498Szrj { 169*38fd1498Szrj this->_M_setstate(ios_base::badbit); 170*38fd1498Szrj __throw_exception_again; 171*38fd1498Szrj } 172*38fd1498Szrj __catch(...) 173*38fd1498Szrj { this->_M_setstate(ios_base::badbit); } 174*38fd1498Szrj if (__err) 175*38fd1498Szrj this->setstate(__err); 176*38fd1498Szrj } 177*38fd1498Szrj return *this; 178*38fd1498Szrj } 179*38fd1498Szrj 180*38fd1498Szrj template<typename _CharT, typename _Traits> 181*38fd1498Szrj basic_ostream<_CharT, _Traits>& 182*38fd1498Szrj basic_ostream<_CharT, _Traits>:: write(const _CharT * __s,streamsize __n)183*38fd1498Szrj write(const _CharT* __s, streamsize __n) 184*38fd1498Szrj { 185*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 186*38fd1498Szrj // DR 60. What is a formatted input function? 187*38fd1498Szrj // basic_ostream::write(const char_type*, streamsize) is an 188*38fd1498Szrj // unformatted output function. 189*38fd1498Szrj // DR 63. Exception-handling policy for unformatted output. 190*38fd1498Szrj // Unformatted output functions should catch exceptions thrown 191*38fd1498Szrj // from streambuf members. 192*38fd1498Szrj sentry __cerb(*this); 193*38fd1498Szrj if (__cerb) 194*38fd1498Szrj { 195*38fd1498Szrj __try 196*38fd1498Szrj { _M_write(__s, __n); } 197*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&) 198*38fd1498Szrj { 199*38fd1498Szrj this->_M_setstate(ios_base::badbit); 200*38fd1498Szrj __throw_exception_again; 201*38fd1498Szrj } 202*38fd1498Szrj __catch(...) 203*38fd1498Szrj { this->_M_setstate(ios_base::badbit); } 204*38fd1498Szrj } 205*38fd1498Szrj return *this; 206*38fd1498Szrj } 207*38fd1498Szrj 208*38fd1498Szrj template<typename _CharT, typename _Traits> 209*38fd1498Szrj basic_ostream<_CharT, _Traits>& 210*38fd1498Szrj basic_ostream<_CharT, _Traits>:: flush()211*38fd1498Szrj flush() 212*38fd1498Szrj { 213*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 214*38fd1498Szrj // DR 60. What is a formatted input function? 215*38fd1498Szrj // basic_ostream::flush() is *not* an unformatted output function. 216*38fd1498Szrj ios_base::iostate __err = ios_base::goodbit; 217*38fd1498Szrj __try 218*38fd1498Szrj { 219*38fd1498Szrj if (this->rdbuf() && this->rdbuf()->pubsync() == -1) 220*38fd1498Szrj __err |= ios_base::badbit; 221*38fd1498Szrj } 222*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&) 223*38fd1498Szrj { 224*38fd1498Szrj this->_M_setstate(ios_base::badbit); 225*38fd1498Szrj __throw_exception_again; 226*38fd1498Szrj } 227*38fd1498Szrj __catch(...) 228*38fd1498Szrj { this->_M_setstate(ios_base::badbit); } 229*38fd1498Szrj if (__err) 230*38fd1498Szrj this->setstate(__err); 231*38fd1498Szrj return *this; 232*38fd1498Szrj } 233*38fd1498Szrj 234*38fd1498Szrj template<typename _CharT, typename _Traits> 235*38fd1498Szrj typename basic_ostream<_CharT, _Traits>::pos_type 236*38fd1498Szrj basic_ostream<_CharT, _Traits>:: tellp()237*38fd1498Szrj tellp() 238*38fd1498Szrj { 239*38fd1498Szrj pos_type __ret = pos_type(-1); 240*38fd1498Szrj __try 241*38fd1498Szrj { 242*38fd1498Szrj if (!this->fail()) 243*38fd1498Szrj __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out); 244*38fd1498Szrj } 245*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&) 246*38fd1498Szrj { 247*38fd1498Szrj this->_M_setstate(ios_base::badbit); 248*38fd1498Szrj __throw_exception_again; 249*38fd1498Szrj } 250*38fd1498Szrj __catch(...) 251*38fd1498Szrj { this->_M_setstate(ios_base::badbit); } 252*38fd1498Szrj return __ret; 253*38fd1498Szrj } 254*38fd1498Szrj 255*38fd1498Szrj template<typename _CharT, typename _Traits> 256*38fd1498Szrj basic_ostream<_CharT, _Traits>& 257*38fd1498Szrj basic_ostream<_CharT, _Traits>:: seekp(pos_type __pos)258*38fd1498Szrj seekp(pos_type __pos) 259*38fd1498Szrj { 260*38fd1498Szrj ios_base::iostate __err = ios_base::goodbit; 261*38fd1498Szrj __try 262*38fd1498Szrj { 263*38fd1498Szrj if (!this->fail()) 264*38fd1498Szrj { 265*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 266*38fd1498Szrj // 136. seekp, seekg setting wrong streams? 267*38fd1498Szrj const pos_type __p = this->rdbuf()->pubseekpos(__pos, 268*38fd1498Szrj ios_base::out); 269*38fd1498Szrj 270*38fd1498Szrj // 129. Need error indication from seekp() and seekg() 271*38fd1498Szrj if (__p == pos_type(off_type(-1))) 272*38fd1498Szrj __err |= ios_base::failbit; 273*38fd1498Szrj } 274*38fd1498Szrj } 275*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&) 276*38fd1498Szrj { 277*38fd1498Szrj this->_M_setstate(ios_base::badbit); 278*38fd1498Szrj __throw_exception_again; 279*38fd1498Szrj } 280*38fd1498Szrj __catch(...) 281*38fd1498Szrj { this->_M_setstate(ios_base::badbit); } 282*38fd1498Szrj if (__err) 283*38fd1498Szrj this->setstate(__err); 284*38fd1498Szrj return *this; 285*38fd1498Szrj } 286*38fd1498Szrj 287*38fd1498Szrj template<typename _CharT, typename _Traits> 288*38fd1498Szrj basic_ostream<_CharT, _Traits>& 289*38fd1498Szrj basic_ostream<_CharT, _Traits>:: seekp(off_type __off,ios_base::seekdir __dir)290*38fd1498Szrj seekp(off_type __off, ios_base::seekdir __dir) 291*38fd1498Szrj { 292*38fd1498Szrj ios_base::iostate __err = ios_base::goodbit; 293*38fd1498Szrj __try 294*38fd1498Szrj { 295*38fd1498Szrj if (!this->fail()) 296*38fd1498Szrj { 297*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 298*38fd1498Szrj // 136. seekp, seekg setting wrong streams? 299*38fd1498Szrj const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir, 300*38fd1498Szrj ios_base::out); 301*38fd1498Szrj 302*38fd1498Szrj // 129. Need error indication from seekp() and seekg() 303*38fd1498Szrj if (__p == pos_type(off_type(-1))) 304*38fd1498Szrj __err |= ios_base::failbit; 305*38fd1498Szrj } 306*38fd1498Szrj } 307*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&) 308*38fd1498Szrj { 309*38fd1498Szrj this->_M_setstate(ios_base::badbit); 310*38fd1498Szrj __throw_exception_again; 311*38fd1498Szrj } 312*38fd1498Szrj __catch(...) 313*38fd1498Szrj { this->_M_setstate(ios_base::badbit); } 314*38fd1498Szrj if (__err) 315*38fd1498Szrj this->setstate(__err); 316*38fd1498Szrj return *this; 317*38fd1498Szrj } 318*38fd1498Szrj 319*38fd1498Szrj template<typename _CharT, typename _Traits> 320*38fd1498Szrj basic_ostream<_CharT, _Traits>& operator <<(basic_ostream<_CharT,_Traits> & __out,const char * __s)321*38fd1498Szrj operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s) 322*38fd1498Szrj { 323*38fd1498Szrj if (!__s) 324*38fd1498Szrj __out.setstate(ios_base::badbit); 325*38fd1498Szrj else 326*38fd1498Szrj { 327*38fd1498Szrj // _GLIBCXX_RESOLVE_LIB_DEFECTS 328*38fd1498Szrj // 167. Improper use of traits_type::length() 329*38fd1498Szrj const size_t __clen = char_traits<char>::length(__s); 330*38fd1498Szrj __try 331*38fd1498Szrj { 332*38fd1498Szrj struct __ptr_guard 333*38fd1498Szrj { 334*38fd1498Szrj _CharT *__p; 335*38fd1498Szrj __ptr_guard (_CharT *__ip): __p(__ip) { } 336*38fd1498Szrj ~__ptr_guard() { delete[] __p; } 337*38fd1498Szrj _CharT* __get() { return __p; } 338*38fd1498Szrj } __pg (new _CharT[__clen]); 339*38fd1498Szrj 340*38fd1498Szrj _CharT *__ws = __pg.__get(); 341*38fd1498Szrj for (size_t __i = 0; __i < __clen; ++__i) 342*38fd1498Szrj __ws[__i] = __out.widen(__s[__i]); 343*38fd1498Szrj __ostream_insert(__out, __ws, __clen); 344*38fd1498Szrj } 345*38fd1498Szrj __catch(__cxxabiv1::__forced_unwind&) 346*38fd1498Szrj { 347*38fd1498Szrj __out._M_setstate(ios_base::badbit); 348*38fd1498Szrj __throw_exception_again; 349*38fd1498Szrj } 350*38fd1498Szrj __catch(...) 351*38fd1498Szrj { __out._M_setstate(ios_base::badbit); } 352*38fd1498Szrj } 353*38fd1498Szrj return __out; 354*38fd1498Szrj } 355*38fd1498Szrj 356*38fd1498Szrj // Inhibit implicit instantiations for required instantiations, 357*38fd1498Szrj // which are defined via explicit instantiations elsewhere. 358*38fd1498Szrj #if _GLIBCXX_EXTERN_TEMPLATE 359*38fd1498Szrj extern template class basic_ostream<char>; 360*38fd1498Szrj extern template ostream& endl(ostream&); 361*38fd1498Szrj extern template ostream& ends(ostream&); 362*38fd1498Szrj extern template ostream& flush(ostream&); 363*38fd1498Szrj extern template ostream& operator<<(ostream&, char); 364*38fd1498Szrj extern template ostream& operator<<(ostream&, unsigned char); 365*38fd1498Szrj extern template ostream& operator<<(ostream&, signed char); 366*38fd1498Szrj extern template ostream& operator<<(ostream&, const char*); 367*38fd1498Szrj extern template ostream& operator<<(ostream&, const unsigned char*); 368*38fd1498Szrj extern template ostream& operator<<(ostream&, const signed char*); 369*38fd1498Szrj 370*38fd1498Szrj extern template ostream& ostream::_M_insert(long); 371*38fd1498Szrj extern template ostream& ostream::_M_insert(unsigned long); 372*38fd1498Szrj extern template ostream& ostream::_M_insert(bool); 373*38fd1498Szrj #ifdef _GLIBCXX_USE_LONG_LONG 374*38fd1498Szrj extern template ostream& ostream::_M_insert(long long); 375*38fd1498Szrj extern template ostream& ostream::_M_insert(unsigned long long); 376*38fd1498Szrj #endif 377*38fd1498Szrj extern template ostream& ostream::_M_insert(double); 378*38fd1498Szrj extern template ostream& ostream::_M_insert(long double); 379*38fd1498Szrj extern template ostream& ostream::_M_insert(const void*); 380*38fd1498Szrj 381*38fd1498Szrj #ifdef _GLIBCXX_USE_WCHAR_T 382*38fd1498Szrj extern template class basic_ostream<wchar_t>; 383*38fd1498Szrj extern template wostream& endl(wostream&); 384*38fd1498Szrj extern template wostream& ends(wostream&); 385*38fd1498Szrj extern template wostream& flush(wostream&); 386*38fd1498Szrj extern template wostream& operator<<(wostream&, wchar_t); 387*38fd1498Szrj extern template wostream& operator<<(wostream&, char); 388*38fd1498Szrj extern template wostream& operator<<(wostream&, const wchar_t*); 389*38fd1498Szrj extern template wostream& operator<<(wostream&, const char*); 390*38fd1498Szrj 391*38fd1498Szrj extern template wostream& wostream::_M_insert(long); 392*38fd1498Szrj extern template wostream& wostream::_M_insert(unsigned long); 393*38fd1498Szrj extern template wostream& wostream::_M_insert(bool); 394*38fd1498Szrj #ifdef _GLIBCXX_USE_LONG_LONG 395*38fd1498Szrj extern template wostream& wostream::_M_insert(long long); 396*38fd1498Szrj extern template wostream& wostream::_M_insert(unsigned long long); 397*38fd1498Szrj #endif 398*38fd1498Szrj extern template wostream& wostream::_M_insert(double); 399*38fd1498Szrj extern template wostream& wostream::_M_insert(long double); 400*38fd1498Szrj extern template wostream& wostream::_M_insert(const void*); 401*38fd1498Szrj #endif 402*38fd1498Szrj #endif 403*38fd1498Szrj 404*38fd1498Szrj _GLIBCXX_END_NAMESPACE_VERSION 405*38fd1498Szrj } // namespace std 406*38fd1498Szrj 407*38fd1498Szrj #endif 408