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
00050   template<typename _CharT, typename _Traits>
00051     class basic_ostream : virtual public basic_ios<_CharT, _Traits>
00052     {
00053     public:
00054       // Types (inherited from basic_ios (27.4.4)):
00055       typedef _CharT                            char_type;
00056       typedef typename _Traits::int_type        int_type;
00057       typedef typename _Traits::pos_type        pos_type;
00058       typedef typename _Traits::off_type        off_type;
00059       typedef _Traits                           traits_type;
00060       
00061       // Non-standard Types:
00062       typedef basic_streambuf<_CharT, _Traits>      __streambuf_type;
00063       typedef basic_ios<_CharT, _Traits>        __ios_type;
00064       typedef basic_ostream<_CharT, _Traits>        __ostream_type;
00065       typedef ostreambuf_iterator<_CharT, _Traits>  __ostreambuf_iter;
00066       typedef num_put<_CharT, __ostreambuf_iter>        __numput_type;
00067       typedef ctype<_CharT>                     __ctype_type;
00068 
00069       // 27.6.2.2 Constructor/destructor:
00070       explicit 
00071       basic_ostream(__streambuf_type* __sb)
00072       { this->init(__sb); }
00073 
00074       virtual 
00075       ~basic_ostream() { }
00076 
00077       // 27.6.2.3 Prefix/suffix:
00078       class sentry;
00079       friend class sentry;
00080       
00081       // 27.6.2.5 Formatted output:
00082       // 27.6.2.5.3  basic_ostream::operator<<
00083       __ostream_type&
00084       operator<<(__ostream_type& (*__pf)(__ostream_type&));
00085       
00086       __ostream_type&
00087       operator<<(__ios_type& (*__pf)(__ios_type&));
00088       
00089       __ostream_type&
00090       operator<<(ios_base& (*__pf) (ios_base&));
00091 
00092       // 27.6.2.5.2 Arithmetic Inserters
00093       __ostream_type& 
00094       operator<<(long __n);
00095       
00096       __ostream_type& 
00097       operator<<(unsigned long __n);
00098 
00099       __ostream_type& 
00100       operator<<(bool __n);
00101 
00102       __ostream_type& 
00103       operator<<(short __n)
00104       { 
00105     ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
00106     if (__fmt & ios_base::oct || __fmt & ios_base::hex)
00107       return this->operator<<(static_cast<unsigned long>
00108                   (static_cast<unsigned short>(__n)));
00109     else
00110       return this->operator<<(static_cast<long>(__n));
00111       }
00112 
00113       __ostream_type& 
00114       operator<<(unsigned short __n)
00115       { return this->operator<<(static_cast<unsigned long>(__n)); }
00116 
00117       __ostream_type& 
00118       operator<<(int __n)
00119       { 
00120     ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
00121     if (__fmt & ios_base::oct || __fmt & ios_base::hex)
00122       return this->operator<<(static_cast<unsigned long>
00123                   (static_cast<unsigned int>(__n)));
00124     else
00125       return this->operator<<(static_cast<long>(__n));
00126       }
00127 
00128       __ostream_type& 
00129       operator<<(unsigned int __n)
00130       { return this->operator<<(static_cast<unsigned long>(__n)); }
00131 
00132 #ifdef _GLIBCPP_USE_LONG_LONG
00133       __ostream_type& 
00134       operator<<(long long __n);
00135 
00136       __ostream_type& 
00137       operator<<(unsigned long long __n);
00138 #endif
00139 
00140       __ostream_type& 
00141       operator<<(double __f);
00142 
00143       __ostream_type& 
00144       operator<<(float __f)
00145       { return this->operator<<(static_cast<double>(__f)); }
00146 
00147       __ostream_type& 
00148       operator<<(long double __f);
00149 
00150       __ostream_type& 
00151       operator<<(const void* __p);
00152 
00153       __ostream_type& 
00154       operator<<(__streambuf_type* __sb);
00155 
00156       // Unformatted output:
00157       __ostream_type& 
00158       put(char_type __c);
00159 
00160       __ostream_type& 
00161       write(const char_type* __s, streamsize __n);
00162 
00163       __ostream_type& 
00164       flush();
00165 
00166       // Seeks:
00167       pos_type 
00168       tellp();
00169 
00170       __ostream_type& 
00171       seekp(pos_type);
00172 
00173       __ostream_type& 
00174       seekp(off_type, ios_base::seekdir);
00175     };
00176 
00177   // 27.6.2.3  Class basic_ostream::sentry
00178   template <typename _CharT, typename _Traits>
00179     class basic_ostream<_CharT, _Traits>::sentry
00180     {
00181       // Data Members:
00182       bool              _M_ok;
00183       basic_ostream<_CharT,_Traits>&    _M_os;
00184       
00185     public:
00186       explicit
00187       sentry(basic_ostream<_CharT,_Traits>& __os);
00188 
00189       ~sentry()
00190       {
00191     // XXX MT
00192     if (_M_os.flags() & ios_base::unitbuf && !uncaught_exception())
00193       {
00194         // Can't call flush directly or else will get into recursive lock.
00195         if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
00196           _M_os.setstate(ios_base::badbit);
00197       }
00198       }
00199 
00200       operator bool() 
00201       { return _M_ok; }
00202     };
00203 
00204   template<typename _CharT, typename _Traits>
00205     basic_ostream<_CharT, _Traits>&
00206     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c);
00207 
00208   template<typename _CharT, typename _Traits>
00209     basic_ostream<_CharT, _Traits>&
00210     operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
00211     { return (__out << __out.widen(__c)); }
00212 
00213   // Specialization
00214   template <class _Traits> 
00215     basic_ostream<char, _Traits>&
00216     operator<<(basic_ostream<char, _Traits>& __out, char __c);
00217 
00218   // Signed and unsigned
00219   template<class _Traits>
00220     basic_ostream<char, _Traits>&
00221     operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
00222     { return (__out << static_cast<char>(__c)); }
00223   
00224   template<class _Traits>
00225     basic_ostream<char, _Traits>&
00226     operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
00227     { return (__out << static_cast<char>(__c)); }
00228   
00229   template<typename _CharT, typename _Traits>
00230     basic_ostream<_CharT, _Traits>&
00231     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s);
00232 
00233   template<typename _CharT, typename _Traits>
00234     basic_ostream<_CharT, _Traits> &
00235     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
00236 
00237   // Partial specializationss
00238   template<class _Traits>
00239     basic_ostream<char, _Traits>&
00240     operator<<(basic_ostream<char, _Traits>& __out, const char* __s);
00241  
00242   // Signed and unsigned
00243   template<class _Traits>
00244     basic_ostream<char, _Traits>&
00245     operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
00246     { return (__out << reinterpret_cast<const char*>(__s)); }
00247 
00248   template<class _Traits>
00249     basic_ostream<char, _Traits> &
00250     operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
00251     { return (__out << reinterpret_cast<const char*>(__s)); }
00252 
00253   // 27.6.2.7 Standard basic_ostream manipulators
00254   template<typename _CharT, typename _Traits>
00255     basic_ostream<_CharT, _Traits>& 
00256     endl(basic_ostream<_CharT, _Traits>& __os)
00257     { return flush(__os.put(__os.widen('\n'))); }
00258 
00259   template<typename _CharT, typename _Traits>
00260     basic_ostream<_CharT, _Traits>& 
00261     ends(basic_ostream<_CharT, _Traits>& __os)
00262     { return __os.put(_CharT()); }
00263   
00264   template<typename _CharT, typename _Traits>
00265     basic_ostream<_CharT, _Traits>& 
00266     flush(basic_ostream<_CharT, _Traits>& __os)
00267     { return __os.flush(); }
00268 
00269 } // namespace std
00270 
00271 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
00272 # define export
00273 #endif
00274 #ifdef  _GLIBCPP_FULLY_COMPLIANT_HEADERS
00275 # include <bits/ostream.tcc>
00276 #endif
00277 
00278 #endif  /* _CPP_OSTREAM */

Generated on Wed May 1 19:19:33 2002 for libstdc++-v3 Source by doxygen1.2.15