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 
00035 /** @file ostream
00036  *  This is a Standard C++ Library header.
00037  */
00038 
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
00049   /**
00050    *  @brief  Controlling output.
00051    *
00052    *  This is the base class for all output streams.  It provides text
00053    *  formatting of all builtin types, and communicates with any class
00054    *  derived from basic_streambuf to do the actual output.
00055   */
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
00096       /**
00097        *  @brief  Base constructor.
00098        *
00099        *  This ctor is almost never called by the user directly, rather from
00100        *  derived classes' initialization lists, which pass a pointer to
00101        *  their own stream buffer.
00102       */
00103       explicit 
00104       basic_ostream(__streambuf_type* __sb)
00105       { this->init(__sb); }
00106 
00107       /**
00108        *  @brief  Base destructor.
00109        *
00110        *  This does very little apart from providing a virtual base dtor.
00111       */
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<<
00121       //@{
00122       /**
00123        *  @brief  Interface for manipulators.
00124        *
00125        *  Manuipulators such as @c std::endl and @c std::hex use these
00126        *  functions in constructs like "std::cout << std::endl".  For more
00127        *  information, see the iomanip header.
00128       */
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&));
00137       //@}
00138 
00139       // [27.6.2.5.2] arithmetic inserters
00140       /**
00141        *  @name Arithmetic Inserters
00142        *
00143        *  All the @c operator<< functions (aka <em>formatted output
00144        *  functions</em>) have some common behavior.  Each starts by
00145        *  constructing a temporary object of type std::basic_ostream::sentry.
00146        *  This can have several effects, concluding with the setting of a
00147        *  status flag; see the sentry documentation for more.
00148        *
00149        *  If the sentry status is good, the function tries to generate
00150        *  whatever data is appropriate for the type of the argument.
00151        *
00152        *  If an exception is thrown during insertion, ios_base::badbit
00153        *  will be turned on in the stream's error state without causing an
00154        *  ios_base::failure to be thrown.  The original exception will then
00155        *  be rethrown.
00156       */
00157       //@{
00158       /**
00159        *  @brief  Basic arithmetic inserters
00160        *  @param  A variable of builtin type.
00161        *  @return  @c *this if successful
00162        *
00163        *  These functions use the stream's current locale (specifically, the
00164        *  @c num_get facet) to perform numeric formatting.
00165       */
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 
00207       /**
00208        *  @brief  Extracting from another streambuf.
00209        *  @param  sb  A pointer to a streambuf
00210        *
00211        *  This function behaves like one of the basic arithmetic extractors,
00212        *  in that it also constructs a sentry object and has the same error
00213        *  handling behavior.
00214        *
00215        *  If @a sb is NULL, the stream will set failbit in its error state.
00216        *
00217        *  Characters are extracted from @a sb and inserted into @c *this
00218        *  until one of the following occurs:
00219        *
00220        *  - the input stream reaches end-of-file,
00221        *  - insertion into the output sequence fails (in this case, the
00222        *    character that would have been inserted is not extracted), or
00223        *  - an exception occurs while getting a character from @a sb, which
00224        *    sets failbit in the error state
00225        *
00226        *  If the function inserts no characters, failbit is set.
00227       */
00228       __ostream_type& 
00229       operator<<(__streambuf_type* __sb);
00230       //@}
00231 
00232       // [27.6.2.6] unformatted output functions
00233       /**
00234        *  @name Unformatted Output Functions
00235        *
00236        *  All the unformatted output functions have some common behavior.
00237        *  Each starts by constructing a temporary object of type
00238        *  std::basic_ostream::sentry.  This has several effects, concluding
00239        *  with the setting of a status flag; see the sentry documentation
00240        *  for more.
00241        *
00242        *  If the sentry status is good, the function tries to generate
00243        *  whatever data is appropriate for the type of the argument.
00244        *
00245        *  If an exception is thrown during insertion, ios_base::badbit
00246        *  will be turned on in the stream's error state.  If badbit is on in
00247        *  the stream's exceptions mask, the exception will be rethrown
00248        *  without completing its actions.
00249       */
00250       //@{
00251       /**
00252        *  @brief  Simple insertion.
00253        *  @param  c  The character to insert.
00254        *  @return  *this
00255        *
00256        *  Tries to insert @a c.
00257        *
00258        *  @note  This function is not overloaded on signed char and
00259        *         unsigned char.
00260       */
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 
00273       /**
00274        *  @brief  Character string insertion.
00275        *  @param  s  The array to insert.
00276        *  @param  n  Maximum number of characters to insert.
00277        *  @return  *this
00278        *
00279        *  Characters are copied from @a s and inserted into the stream until
00280        *  one of the following happens:
00281        *
00282        *  - @a n characters are inserted
00283        *  - inserting into the output sequence fails (in this case, badbit
00284        *    will be set in the stream's error state)
00285        *
00286        *  @note  This function is not overloaded on signed char and
00287        *         unsigned char.
00288       */
00289       __ostream_type& 
00290       write(const char_type* __s, streamsize __n);
00291       //@}
00292 
00293       /**
00294        *  @brief  Synchronizing the stream buffer.
00295        *  @return  *this
00296        *
00297        *  If @c rdbuf() is a null pointer, changes nothing.
00298        *
00299        *  Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
00300        *  sets badbit.
00301       */
00302       __ostream_type& 
00303       flush();
00304 
00305       // [27.6.2.4] seek members
00306       /**
00307        *  @brief  Getting the current write position.
00308        *  @return  A file position object.
00309        *
00310        *  If @c fail() is not false, returns @c pos_type(-1) to indicate
00311        *  failure.  Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
00312       */
00313       pos_type 
00314       tellp();
00315 
00316       /**
00317        *  @brief  Changing the current write position.
00318        *  @param  pos  A file position object.
00319        *  @return  *this
00320        *
00321        *  If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos).  If
00322        *  that function fails, sets failbit.
00323       */
00324       __ostream_type& 
00325       seekp(pos_type);
00326 
00327       /**
00328        *  @brief  Changing the current write position.
00329        *  @param  off  A file offset object.
00330        *  @param  dir  The direction in which to seek.
00331        *  @return  *this
00332        *
00333        *  If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
00334        *  If that function fails, sets failbit.
00335       */
00336        __ostream_type& 
00337       seekp(off_type, ios_base::seekdir);
00338       
00339     protected:
00340       explicit 
00341       basic_ostream() { }
00342     };
00343 
00344   /**
00345    *  @brief  Performs setup work for output streams.
00346    *
00347    *  Objects of this class are created before all of the standard
00348    *  inserters are run.  It is responsible for "exception-safe prefix and
00349    *  suffix operations."  Additional actions may be added by the
00350    *  implementation, and we list them in
00351    *  http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
00352    *  under [27.6] notes.
00353   */
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:
00362       /**
00363        *  @brief  The constructor performs preparatory work.
00364        *  @param  os  The output stream to guard.
00365        *
00366        *  If the stream state is good (@a os.good() is true), then if the
00367        *  stream is tied to another output stream, @c is.tie()->flush()
00368        *  is called to synchronize the output sequences.
00369        *
00370        *  If the stream state is still good, then the sentry state becomes
00371        *  true ("okay").
00372       */
00373       explicit
00374       sentry(basic_ostream<_CharT,_Traits>& __os);
00375 
00376       /**
00377        *  @brief  Possibly flushes the stream.
00378        *
00379        *  If @c ios_base::unitbuf is set in @c os.flags(), and
00380        *  @c std::uncaught_exception() is true, the sentry destructor calls
00381        *  @c flush() on the output stream.
00382       */
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 
00394       /**
00395        *  @brief  Quick status checking.
00396        *  @return  The sentry state.
00397        *
00398        *  For ease of use, sentries may be converted to booleans.  The
00399        *  return value is that of the sentry state (true == okay).
00400       */
00401       operator bool() const
00402       { return _M_ok; }
00403     };
00404 
00405   // [27.6.2.5.4] character insertion templates
00406   //@{
00407   /**
00408    *  @brief  Character inserters
00409    *  @param  out  An output stream.
00410    *  @param  c  A character.
00411    *  @return  out
00412    *
00413    *  Behaves like one of the formatted arithmetic inserters described in
00414    *  std::basic_ostream.  After constructing a sentry object with good
00415    *  status, this function inserts a single character and any required
00416    *  padding (as determined by [22.2.2.2.2]).  @c out.width(0) is then
00417    *  called.
00418    *
00419    *  If @a c is of type @c char and the character type of the stream is not
00420    *  @c char, the character is widened before insertion.
00421   */
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)); }
00446   //@}
00447   
00448   //@{
00449   /**
00450    *  @brief  String inserters
00451    *  @param  out  An output stream.
00452    *  @param  s  A character string.
00453    *  @return  out
00454    *  @pre  @a s must be a non-NULL pointer
00455    *
00456    *  Behaves like one of the formatted arithmetic inserters described in
00457    *  std::basic_ostream.  After constructing a sentry object with good
00458    *  status, this function inserts @c traits::length(s) characters starting
00459    *  at @a s, widened if necessary, followed by any required padding (as
00460    *  determined by [22.2.2.2.2]).  @c out.width(0) is then called.
00461   */
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)); }
00485   //@}
00486 
00487   // [27.6.2.7] standard basic_ostream manipulators
00488   /**
00489    *  @brief  Write a newline and flush the stream.
00490    *
00491    *  This manipulator is often mistakenly used when a simple newline is
00492    *  desired, leading to poor buffering performance.  See
00493    *  http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
00494    *  on this subject.
00495   */
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 
00501   /**
00502    *  @brief  Write a null character into the output sequence.
00503    *
00504    *  "Null character" is @c CharT() by definition.  For CharT of @c char,
00505    *  this correctly writes the ASCII @c NUL character string terminator.
00506   */
00507   template<typename _CharT, typename _Traits>
00508     basic_ostream<_CharT, _Traits>& 
00509     ends(basic_ostream<_CharT, _Traits>& __os)
00510     { return __os.put(_CharT()); }
00511   
00512   /**
00513    *  @brief  Flushes the output stream.
00514    *
00515    *  This manipulator simply calls the stream's @c flush() member function.
00516   */
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 Dec 2 03:59:26 2008 for libstdc++ by  doxygen 1.5.7.1