# ostream

Go to the documentation of this file.

    00001 // Output streams -*- C++ -*-
    00002
    00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
    00004 // Free Software Foundation, Inc.
    00005 //
    00006 // This file is part of the GNU ISO C++ Library.  This library is free
    00007 // software; you can redistribute it and/or modify it under the
    00008 // terms of the GNU General Public License as published by the
    00009 // Free Software Foundation; either version 2, or (at your option)
    00010 // any later version.
    00011
    00012 // This library is distributed in the hope that it will be useful,
    00013 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00014 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00015 // GNU General Public License for more details.
    00016
    00017 // You should have received a copy of the GNU General Public License along
    00018 // with this library; see the file COPYING.  If not, write to the Free
    00019 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00020 // USA.
    00021
    00022 // As a special exception, you may use this file as part of a free software
    00023 // library without restriction.  Specifically, if other files instantiate
    00024 // templates or use macros or inline functions from this file, or you compile
    00025 // this file and link it with other files to produce an executable, this
    00026 // file does not by itself cause the resulting executable to be covered by
    00027 // the GNU General Public License.  This exception does not however
    00028 // invalidate any other reasons why the executable file might be covered by
    00029 // the GNU General Public License.
    00030
    00031 //
    00032 // ISO C++ 14882: 27.6.2  Output streams
    00033 //
    00034
    00040 #ifndef _CPP_OSTREAM
    00041 #define _CPP_OSTREAM    1
    00042
    00043 #pragma GCC system_header
    00044
    00045 #include <ios>
    00046
    00047 namespace std
    00048 {
    00049   // [27.6.2.1] Template class basic_ostream
    00057   template<typename _CharT, typename _Traits>
    00058     class basic_ostream : virtual public basic_ios<_CharT, _Traits>
    00059     {
    00060     public:
    00061       // Types (inherited from basic_ios (27.4.4)):
    00062       typedef _CharT                            char_type;
    00063       typedef typename _Traits::int_type        int_type;
    00064       typedef typename _Traits::pos_type        pos_type;
    00065       typedef typename _Traits::off_type        off_type;
    00066       typedef _Traits                           traits_type;
    00067
    00068       // Non-standard Types:
    00069       typedef basic_streambuf<_CharT, _Traits>      __streambuf_type;
    00070       typedef basic_ios<_CharT, _Traits>        __ios_type;
    00071       typedef basic_ostream<_CharT, _Traits>        __ostream_type;
    00072       typedef ostreambuf_iterator<_CharT, _Traits>  __ostreambuf_iter;
    00073       typedef num_put<_CharT, __ostreambuf_iter>        __numput_type;
    00074       typedef ctype<_CharT>                     __ctype_type;
    00075
    00076       // [27.6.2.2] constructor/destructor
    00084       explicit
    00085       basic_ostream(__streambuf_type* __sb)
    00086       { this->init(__sb); }
    00087
    00093       virtual
    00094       ~basic_ostream() { }
    00095
    00096       // [27.6.2.3] prefix/suffix
    00097       class sentry;
    00098       friend class sentry;
    00099
    00100       // [27.6.2.5] formatted output
    00101       // [27.6.2.5.3]  basic_ostream::operator<<
    00103
    00110       __ostream_type&
    00111       operator<<(__ostream_type& (*__pf)(__ostream_type&));
    00112
    00113       __ostream_type&
    00114       operator<<(__ios_type& (*__pf)(__ios_type&));
    00115
    00116       __ostream_type&
    00117       operator<<(ios_base& (*__pf) (ios_base&));
    00119
    00120       // [27.6.2.5.2] arithmetic inserters
    00147       __ostream_type&
    00148       operator<<(long __n);
    00149
    00150       __ostream_type&
    00151       operator<<(unsigned long __n);
    00152
    00153       __ostream_type&
    00154       operator<<(bool __n);
    00155
    00156       __ostream_type&
    00157       operator<<(short __n)
    00158       {
    00159     ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
    00160     if (__fmt & ios_base::oct || __fmt & ios_base::hex)
    00161       return this->operator<<(static_cast<unsigned long>
    00162                   (static_cast<unsigned short>(__n)));
    00163     else
    00164       return this->operator<<(static_cast<long>(__n));
    00165       }
    00166
    00167       __ostream_type&
    00168       operator<<(unsigned short __n)
    00169       { return this->operator<<(static_cast<unsigned long>(__n)); }
    00170
    00171       __ostream_type&
    00172       operator<<(int __n)
    00173       {
    00174     ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
    00175     if (__fmt & ios_base::oct || __fmt & ios_base::hex)
    00176       return this->operator<<(static_cast<unsigned long>
    00177                   (static_cast<unsigned int>(__n)));
    00178     else
    00179       return this->operator<<(static_cast<long>(__n));
    00180       }
    00181
    00182       __ostream_type&
    00183       operator<<(unsigned int __n)
    00184       { return this->operator<<(static_cast<unsigned long>(__n)); }
    00185
    00186 #ifdef _GLIBCPP_USE_LONG_LONG
    00187       __ostream_type&
    00188       operator<<(long long __n);
    00189
    00190       __ostream_type&
    00191       operator<<(unsigned long long __n);
    00192 #endif
    00193
    00194       __ostream_type&
    00195       operator<<(double __f);
    00196
    00197       __ostream_type&
    00198       operator<<(float __f)
    00199       { return this->operator<<(static_cast<double>(__f)); }
    00200
    00201       __ostream_type&
    00202       operator<<(long double __f);
    00203
    00204       __ostream_type&
    00205       operator<<(const void* __p);
    00206
    00228       __ostream_type&
    00229       operator<<(__streambuf_type* __sb);
    00231
    00232       // [27.6.2.6] unformatted output functions
    00261       __ostream_type&
    00262       put(char_type __c);
    00263
    00280       __ostream_type&
    00281       write(const char_type* __s, streamsize __n);
    00283
    00293       __ostream_type&
    00294       flush();
    00295
    00296       // [27.6.2.4] seek members
    00304       pos_type
    00305       tellp();
    00306
    00315       __ostream_type&
    00316       seekp(pos_type);
    00317
    00327        __ostream_type&
    00328       seekp(off_type, ios_base::seekdir);
    00329     };
    00330
    00341   template <typename _CharT, typename _Traits>
    00342     class basic_ostream<_CharT, _Traits>::sentry
    00343     {
    00344       // Data Members:
    00345       bool              _M_ok;
    00346       basic_ostream<_CharT,_Traits>&    _M_os;
    00347
    00348     public:
    00360       explicit
    00361       sentry(basic_ostream<_CharT,_Traits>& __os);
    00362
    00370       ~sentry()
    00371       {
    00372     // XXX MT
    00373     if (_M_os.flags() & ios_base::unitbuf && !uncaught_exception())
    00374       {
    00375         // Can't call flush directly or else will get into recursive lock.
    00376         if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
    00377           _M_os.setstate(ios_base::badbit);
    00378       }
    00379       }
    00380
    00388       operator bool()
    00389       { return _M_ok; }
    00390     };
    00391
    00392   // [27.6.2.5.4] character insertion templates
    00394
    00409   template<typename _CharT, typename _Traits>
    00410     basic_ostream<_CharT, _Traits>&
    00411     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c);
    00412
    00413   template<typename _CharT, typename _Traits>
    00414     basic_ostream<_CharT, _Traits>&
    00415     operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
    00416     { return (__out << __out.widen(__c)); }
    00417
    00418   // Specialization
    00419   template <class _Traits>
    00420     basic_ostream<char, _Traits>&
    00421     operator<<(basic_ostream<char, _Traits>& __out, char __c);
    00422
    00423   // Signed and unsigned
    00424   template<class _Traits>
    00425     basic_ostream<char, _Traits>&
    00426     operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
    00427     { return (__out << static_cast<char>(__c)); }
    00428
    00429   template<class _Traits>
    00430     basic_ostream<char, _Traits>&
    00431     operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
    00432     { return (__out << static_cast<char>(__c)); }
    00434
    00436
    00449   template<typename _CharT, typename _Traits>
    00450     basic_ostream<_CharT, _Traits>&
    00451     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s);
    00452
    00453   template<typename _CharT, typename _Traits>
    00454     basic_ostream<_CharT, _Traits> &
    00455     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
    00456
    00457   // Partial specializationss
    00458   template<class _Traits>
    00459     basic_ostream<char, _Traits>&
    00460     operator<<(basic_ostream<char, _Traits>& __out, const char* __s);
    00461
    00462   // Signed and unsigned
    00463   template<class _Traits>
    00464     basic_ostream<char, _Traits>&
    00465     operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
    00466     { return (__out << reinterpret_cast<const char*>(__s)); }
    00467
    00468   template<class _Traits>
    00469     basic_ostream<char, _Traits> &
    00470     operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
    00471     { return (__out << reinterpret_cast<const char*>(__s)); }
    00473
    00474   // [27.6.2.7] standard basic_ostream manipulators
    00483   template<typename _CharT, typename _Traits>
    00484     basic_ostream<_CharT, _Traits>&
    00485     endl(basic_ostream<_CharT, _Traits>& __os)
    00486     { return flush(__os.put(__os.widen('\n'))); }
    00487
    00494   template<typename _CharT, typename _Traits>
    00495     basic_ostream<_CharT, _Traits>&
    00496     ends(basic_ostream<_CharT, _Traits>& __os)
    00497     { return __os.put(_CharT()); }
    00498
    00504   template<typename _CharT, typename _Traits>
    00505     basic_ostream<_CharT, _Traits>&
    00506     flush(basic_ostream<_CharT, _Traits>& __os)
    00507     { return __os.flush(); }
    00508
    00509 } // namespace std
    00510
    00511 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
    00512 # define export
    00513 #endif
    00514 #ifdef  _GLIBCPP_FULLY_COMPLIANT_HEADERS
    00515 # include <bits/ostream.tcc>
    00516 #endif
    00517
    00518 #endif  /* _CPP_OSTREAM */