ostream

Go to the documentation of this file.
00001 // Output streams -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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 
00039 #ifndef _GLIBCXX_OSTREAM
00040 #define _GLIBCXX_OSTREAM 1
00041 
00042 #pragma GCC system_header
00043 
00044 #include <ios>
00045 
00046 namespace std
00047 {
00048   // [27.6.2.1] Template class basic_ostream
00056   template<typename _CharT, typename _Traits>
00057     class basic_ostream : virtual public basic_ios<_CharT, _Traits>
00058     {
00059     public:
00060       // Types (inherited from basic_ios (27.4.4)):
00061       typedef _CharT                            char_type;
00062       typedef typename _Traits::int_type        int_type;
00063       typedef typename _Traits::pos_type        pos_type;
00064       typedef typename _Traits::off_type        off_type;
00065       typedef _Traits                           traits_type;
00066       
00067       // Non-standard Types:
00068       typedef basic_streambuf<_CharT, _Traits>      __streambuf_type;
00069       typedef basic_ios<_CharT, _Traits>        __ios_type;
00070       typedef basic_ostream<_CharT, _Traits>        __ostream_type;
00071       typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >        
00072                                 __num_put_type;
00073       typedef ctype<_CharT>                     __ctype_type;
00074 
00075       template<typename _CharT2, typename _Traits2>
00076         friend basic_ostream<_CharT2, _Traits2>&
00077         operator<<(basic_ostream<_CharT2, _Traits2>&, _CharT2);
00078  
00079       template<typename _Traits2>
00080         friend basic_ostream<char, _Traits2>&
00081         operator<<(basic_ostream<char, _Traits2>&, char);
00082  
00083       template<typename _CharT2, typename _Traits2>
00084         friend basic_ostream<_CharT2, _Traits2>&
00085         operator<<(basic_ostream<_CharT2, _Traits2>&, const _CharT2*);
00086  
00087       template<typename _Traits2>
00088         friend basic_ostream<char, _Traits2>&
00089         operator<<(basic_ostream<char, _Traits2>&, const char*);
00090  
00091       template<typename _CharT2, typename _Traits2>
00092         friend basic_ostream<_CharT2, _Traits2>&
00093         operator<<(basic_ostream<_CharT2, _Traits2>&, const char*);
00094 
00095       // [27.6.2.2] constructor/destructor
00103       explicit 
00104       basic_ostream(__streambuf_type* __sb)
00105       { this->init(__sb); }
00106 
00112       virtual 
00113       ~basic_ostream() { }
00114 
00115       // [27.6.2.3] prefix/suffix
00116       class sentry;
00117       friend class sentry;
00118       
00119       // [27.6.2.5] formatted output
00120       // [27.6.2.5.3]  basic_ostream::operator<<
00122 
00129       inline __ostream_type&
00130       operator<<(__ostream_type& (*__pf)(__ostream_type&));
00131       
00132       inline __ostream_type&
00133       operator<<(__ios_type& (*__pf)(__ios_type&));
00134       
00135       inline __ostream_type&
00136       operator<<(ios_base& (*__pf) (ios_base&));
00138 
00139       // [27.6.2.5.2] arithmetic inserters
00166       __ostream_type& 
00167       operator<<(long __n);
00168       
00169       __ostream_type& 
00170       operator<<(unsigned long __n);
00171 
00172       __ostream_type& 
00173       operator<<(bool __n);
00174 
00175       __ostream_type& 
00176       operator<<(short __n);
00177 
00178       __ostream_type& 
00179       operator<<(unsigned short __n);
00180 
00181       __ostream_type& 
00182       operator<<(int __n);
00183 
00184       __ostream_type& 
00185       operator<<(unsigned int __n);
00186 
00187 #ifdef _GLIBCXX_USE_LONG_LONG
00188       __ostream_type& 
00189       operator<<(long long __n);
00190 
00191       __ostream_type& 
00192       operator<<(unsigned long long __n);
00193 #endif
00194 
00195       __ostream_type& 
00196       operator<<(double __f);
00197 
00198       __ostream_type& 
00199       operator<<(float __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 
00264       // Core write functionality, without sentry.
00265       void
00266       _M_write(const char_type* __s, streamsize __n)
00267       {
00268     streamsize __put = this->rdbuf()->sputn(__s, __n);
00269     if (__put != __n)
00270       this->setstate(ios_base::badbit);
00271       }
00272 
00289       __ostream_type& 
00290       write(const char_type* __s, streamsize __n);
00292 
00302       __ostream_type& 
00303       flush();
00304 
00305       // [27.6.2.4] seek members
00313       pos_type 
00314       tellp();
00315 
00324       __ostream_type& 
00325       seekp(pos_type);
00326 
00336        __ostream_type& 
00337       seekp(off_type, ios_base::seekdir);
00338       
00339     protected:
00340       explicit 
00341       basic_ostream() { }
00342     };
00343 
00354   template <typename _CharT, typename _Traits>
00355     class basic_ostream<_CharT, _Traits>::sentry
00356     {
00357       // Data Members:
00358       bool              _M_ok;
00359       basic_ostream<_CharT,_Traits>&    _M_os;
00360       
00361     public:
00373       explicit
00374       sentry(basic_ostream<_CharT,_Traits>& __os);
00375 
00383       ~sentry()
00384       {
00385     // XXX MT
00386     if (_M_os.flags() & ios_base::unitbuf && !uncaught_exception())
00387       {
00388         // Can't call flush directly or else will get into recursive lock.
00389         if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
00390           _M_os.setstate(ios_base::badbit);
00391       }
00392       }
00393 
00401       operator bool() const
00402       { return _M_ok; }
00403     };
00404 
00405   // [27.6.2.5.4] character insertion templates
00407 
00422   template<typename _CharT, typename _Traits>
00423     basic_ostream<_CharT, _Traits>&
00424     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c);
00425 
00426   template<typename _CharT, typename _Traits>
00427     basic_ostream<_CharT, _Traits>&
00428     operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
00429     { return (__out << __out.widen(__c)); }
00430 
00431   // Specialization
00432   template <class _Traits> 
00433     basic_ostream<char, _Traits>&
00434     operator<<(basic_ostream<char, _Traits>& __out, char __c);
00435 
00436   // Signed and unsigned
00437   template<class _Traits>
00438     basic_ostream<char, _Traits>&
00439     operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
00440     { return (__out << static_cast<char>(__c)); }
00441   
00442   template<class _Traits>
00443     basic_ostream<char, _Traits>&
00444     operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
00445     { return (__out << static_cast<char>(__c)); }
00447   
00449 
00462   template<typename _CharT, typename _Traits>
00463     basic_ostream<_CharT, _Traits>&
00464     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s);
00465 
00466   template<typename _CharT, typename _Traits>
00467     basic_ostream<_CharT, _Traits> &
00468     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
00469 
00470   // Partial specializationss
00471   template<class _Traits>
00472     basic_ostream<char, _Traits>&
00473     operator<<(basic_ostream<char, _Traits>& __out, const char* __s);
00474  
00475   // Signed and unsigned
00476   template<class _Traits>
00477     basic_ostream<char, _Traits>&
00478     operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
00479     { return (__out << reinterpret_cast<const char*>(__s)); }
00480 
00481   template<class _Traits>
00482     basic_ostream<char, _Traits> &
00483     operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
00484     { return (__out << reinterpret_cast<const char*>(__s)); }
00486 
00487   // [27.6.2.7] standard basic_ostream manipulators
00496   template<typename _CharT, typename _Traits>
00497     basic_ostream<_CharT, _Traits>& 
00498     endl(basic_ostream<_CharT, _Traits>& __os)
00499     { return flush(__os.put(__os.widen('\n'))); }
00500 
00507   template<typename _CharT, typename _Traits>
00508     basic_ostream<_CharT, _Traits>& 
00509     ends(basic_ostream<_CharT, _Traits>& __os)
00510     { return __os.put(_CharT()); }
00511   
00517   template<typename _CharT, typename _Traits>
00518     basic_ostream<_CharT, _Traits>& 
00519     flush(basic_ostream<_CharT, _Traits>& __os)
00520     { return __os.flush(); }
00521 
00522 } // namespace std
00523 
00524 #ifndef _GLIBCXX_EXPORT_TEMPLATE
00525 # include <bits/ostream.tcc>
00526 #endif
00527 
00528 #endif  /* _GLIBCXX_OSTREAM */

Generated on Tue Feb 2 16:56:20 2010 for GNU C++ STL by  doxygen 1.4.7