libstdc++
basic_ios.h
Go to the documentation of this file.
00001 // Iostreams base classes -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
00004 // 2006, 2007, 2008, 2009, 2010, 2011, 2012
00005 // Free Software Foundation, Inc.
00006 //
00007 // This file is part of the GNU ISO C++ Library.  This library is free
00008 // software; you can redistribute it and/or modify it under the
00009 // terms of the GNU General Public License as published by the
00010 // Free Software Foundation; either version 3, or (at your option)
00011 // any later version.
00012 
00013 // This library is distributed in the hope that it will be useful,
00014 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00015 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00016 // GNU General Public License for more details.
00017 
00018 // Under Section 7 of GPL version 3, you are granted additional
00019 // permissions described in the GCC Runtime Library Exception, version
00020 // 3.1, as published by the Free Software Foundation.
00021 
00022 // You should have received a copy of the GNU General Public License and
00023 // a copy of the GCC Runtime Library Exception along with this program;
00024 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00025 // <http://www.gnu.org/licenses/>.
00026 
00027 /** @file bits/basic_ios.h
00028  *  This is an internal header file, included by other library headers.
00029  *  Do not attempt to use it directly. @headername{ios}
00030  */
00031 
00032 #ifndef _BASIC_IOS_H
00033 #define _BASIC_IOS_H 1
00034 
00035 #pragma GCC system_header
00036 
00037 #include <bits/localefwd.h>
00038 #include <bits/locale_classes.h>
00039 #include <bits/locale_facets.h>
00040 #include <bits/streambuf_iterator.h>
00041 
00042 namespace std _GLIBCXX_VISIBILITY(default)
00043 {
00044 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00045 
00046   template<typename _Facet>
00047     inline const _Facet&
00048     __check_facet(const _Facet* __f)
00049     {
00050       if (!__f)
00051     __throw_bad_cast();
00052       return *__f;
00053     }
00054 
00055   /**
00056    *  @brief Template class basic_ios, virtual base class for all
00057    *  stream classes. 
00058    *  @ingroup io
00059    *
00060    *  @tparam _CharT  Type of character stream.
00061    *  @tparam _Traits  Traits for character type, defaults to
00062    *                   char_traits<_CharT>.
00063    *
00064    *  Most of the member functions called dispatched on stream objects
00065    *  (e.g., @c std::cout.foo(bar);) are consolidated in this class.
00066   */
00067   template<typename _CharT, typename _Traits>
00068     class basic_ios : public ios_base
00069     {
00070     public:
00071       //@{
00072       /**
00073        *  These are standard types.  They permit a standardized way of
00074        *  referring to names of (or names dependant on) the template
00075        *  parameters, which are specific to the implementation.
00076       */
00077       typedef _CharT                                 char_type;
00078       typedef typename _Traits::int_type             int_type;
00079       typedef typename _Traits::pos_type             pos_type;
00080       typedef typename _Traits::off_type             off_type;
00081       typedef _Traits                                traits_type;
00082       //@}
00083 
00084       //@{
00085       /**
00086        *  These are non-standard types.
00087       */
00088       typedef ctype<_CharT>                          __ctype_type;
00089       typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
00090                              __num_put_type;
00091       typedef num_get<_CharT, istreambuf_iterator<_CharT, _Traits> >
00092                              __num_get_type;
00093       //@}
00094 
00095       // Data members:
00096     protected:
00097       basic_ostream<_CharT, _Traits>*                _M_tie;
00098       mutable char_type                              _M_fill;
00099       mutable bool                                   _M_fill_init;
00100       basic_streambuf<_CharT, _Traits>*              _M_streambuf;
00101 
00102       // Cached use_facet<ctype>, which is based on the current locale info.
00103       const __ctype_type*                            _M_ctype;
00104       // For ostream.
00105       const __num_put_type*                          _M_num_put;
00106       // For istream.
00107       const __num_get_type*                          _M_num_get;
00108 
00109     public:
00110       //@{
00111       /**
00112        *  @brief  The quick-and-easy status check.
00113        *
00114        *  This allows you to write constructs such as
00115        *  <code>if (!a_stream) ...</code> and <code>while (a_stream) ...</code>
00116       */
00117       operator void*() const
00118       { return this->fail() ? 0 : const_cast<basic_ios*>(this); }
00119 
00120       bool
00121       operator!() const
00122       { return this->fail(); }
00123       //@}
00124 
00125       /**
00126        *  @brief  Returns the error state of the stream buffer.
00127        *  @return  A bit pattern (well, isn't everything?)
00128        *
00129        *  See std::ios_base::iostate for the possible bit values.  Most
00130        *  users will call one of the interpreting wrappers, e.g., good().
00131       */
00132       iostate
00133       rdstate() const
00134       { return _M_streambuf_state; }
00135 
00136       /**
00137        *  @brief  [Re]sets the error state.
00138        *  @param  __state  The new state flag(s) to set.
00139        *
00140        *  See std::ios_base::iostate for the possible bit values.  Most
00141        *  users will not need to pass an argument.
00142       */
00143       void
00144       clear(iostate __state = goodbit);
00145 
00146       /**
00147        *  @brief  Sets additional flags in the error state.
00148        *  @param  __state  The additional state flag(s) to set.
00149        *
00150        *  See std::ios_base::iostate for the possible bit values.
00151       */
00152       void
00153       setstate(iostate __state)
00154       { this->clear(this->rdstate() | __state); }
00155 
00156       // Flip the internal state on for the proper state bits, then re
00157       // throws the propagated exception if bit also set in
00158       // exceptions().
00159       void
00160       _M_setstate(iostate __state)
00161       {
00162     // 27.6.1.2.1 Common requirements.
00163     // Turn this on without causing an ios::failure to be thrown.
00164     _M_streambuf_state |= __state;
00165     if (this->exceptions() & __state)
00166       __throw_exception_again;
00167       }
00168 
00169       /**
00170        *  @brief  Fast error checking.
00171        *  @return  True if no error flags are set.
00172        *
00173        *  A wrapper around rdstate.
00174       */
00175       bool
00176       good() const
00177       { return this->rdstate() == 0; }
00178 
00179       /**
00180        *  @brief  Fast error checking.
00181        *  @return  True if the eofbit is set.
00182        *
00183        *  Note that other iostate flags may also be set.
00184       */
00185       bool
00186       eof() const
00187       { return (this->rdstate() & eofbit) != 0; }
00188 
00189       /**
00190        *  @brief  Fast error checking.
00191        *  @return  True if either the badbit or the failbit is set.
00192        *
00193        *  Checking the badbit in fail() is historical practice.
00194        *  Note that other iostate flags may also be set.
00195       */
00196       bool
00197       fail() const
00198       { return (this->rdstate() & (badbit | failbit)) != 0; }
00199 
00200       /**
00201        *  @brief  Fast error checking.
00202        *  @return  True if the badbit is set.
00203        *
00204        *  Note that other iostate flags may also be set.
00205       */
00206       bool
00207       bad() const
00208       { return (this->rdstate() & badbit) != 0; }
00209 
00210       /**
00211        *  @brief  Throwing exceptions on errors.
00212        *  @return  The current exceptions mask.
00213        *
00214        *  This changes nothing in the stream.  See the one-argument version
00215        *  of exceptions(iostate) for the meaning of the return value.
00216       */
00217       iostate
00218       exceptions() const
00219       { return _M_exception; }
00220 
00221       /**
00222        *  @brief  Throwing exceptions on errors.
00223        *  @param  __except  The new exceptions mask.
00224        *
00225        *  By default, error flags are set silently.  You can set an
00226        *  exceptions mask for each stream; if a bit in the mask becomes set
00227        *  in the error flags, then an exception of type
00228        *  std::ios_base::failure is thrown.
00229        *
00230        *  If the error flag is already set when the exceptions mask is
00231        *  added, the exception is immediately thrown.  Try running the
00232        *  following under GCC 3.1 or later:
00233        *  @code
00234        *  #include <iostream>
00235        *  #include <fstream>
00236        *  #include <exception>
00237        *
00238        *  int main()
00239        *  {
00240        *      std::set_terminate (__gnu_cxx::__verbose_terminate_handler);
00241        *
00242        *      std::ifstream f ("/etc/motd");
00243        *
00244        *      std::cerr << "Setting badbit\n";
00245        *      f.setstate (std::ios_base::badbit);
00246        *
00247        *      std::cerr << "Setting exception mask\n";
00248        *      f.exceptions (std::ios_base::badbit);
00249        *  }
00250        *  @endcode
00251       */
00252       void
00253       exceptions(iostate __except)
00254       {
00255         _M_exception = __except;
00256         this->clear(_M_streambuf_state);
00257       }
00258 
00259       // Constructor/destructor:
00260       /**
00261        *  @brief  Constructor performs initialization.
00262        *
00263        *  The parameter is passed by derived streams.
00264       */
00265       explicit
00266       basic_ios(basic_streambuf<_CharT, _Traits>* __sb)
00267       : ios_base(), _M_tie(0), _M_fill(), _M_fill_init(false), _M_streambuf(0),
00268     _M_ctype(0), _M_num_put(0), _M_num_get(0)
00269       { this->init(__sb); }
00270 
00271       /**
00272        *  @brief  Empty.
00273        *
00274        *  The destructor does nothing.  More specifically, it does not
00275        *  destroy the streambuf held by rdbuf().
00276       */
00277       virtual
00278       ~basic_ios() { }
00279 
00280       // Members:
00281       /**
00282        *  @brief  Fetches the current @e tied stream.
00283        *  @return  A pointer to the tied stream, or NULL if the stream is
00284        *           not tied.
00285        *
00286        *  A stream may be @e tied (or synchronized) to a second output
00287        *  stream.  When this stream performs any I/O, the tied stream is
00288        *  first flushed.  For example, @c std::cin is tied to @c std::cout.
00289       */
00290       basic_ostream<_CharT, _Traits>*
00291       tie() const
00292       { return _M_tie; }
00293 
00294       /**
00295        *  @brief  Ties this stream to an output stream.
00296        *  @param  __tiestr  The output stream.
00297        *  @return  The previously tied output stream, or NULL if the stream
00298        *           was not tied.
00299        *
00300        *  This sets up a new tie; see tie() for more.
00301       */
00302       basic_ostream<_CharT, _Traits>*
00303       tie(basic_ostream<_CharT, _Traits>* __tiestr)
00304       {
00305         basic_ostream<_CharT, _Traits>* __old = _M_tie;
00306         _M_tie = __tiestr;
00307         return __old;
00308       }
00309 
00310       /**
00311        *  @brief  Accessing the underlying buffer.
00312        *  @return  The current stream buffer.
00313        *
00314        *  This does not change the state of the stream.
00315       */
00316       basic_streambuf<_CharT, _Traits>*
00317       rdbuf() const
00318       { return _M_streambuf; }
00319 
00320       /**
00321        *  @brief  Changing the underlying buffer.
00322        *  @param  __sb  The new stream buffer.
00323        *  @return  The previous stream buffer.
00324        *
00325        *  Associates a new buffer with the current stream, and clears the
00326        *  error state.
00327        *
00328        *  Due to historical accidents which the LWG refuses to correct, the
00329        *  I/O library suffers from a design error:  this function is hidden
00330        *  in derived classes by overrides of the zero-argument @c rdbuf(),
00331        *  which is non-virtual for hysterical raisins.  As a result, you
00332        *  must use explicit qualifications to access this function via any
00333        *  derived class.  For example:
00334        *
00335        *  @code
00336        *  std::fstream     foo;         // or some other derived type
00337        *  std::streambuf*  p = .....;
00338        *
00339        *  foo.ios::rdbuf(p);            // ios == basic_ios<char>
00340        *  @endcode
00341       */
00342       basic_streambuf<_CharT, _Traits>*
00343       rdbuf(basic_streambuf<_CharT, _Traits>* __sb);
00344 
00345       /**
00346        *  @brief  Copies fields of __rhs into this.
00347        *  @param  __rhs  The source values for the copies.
00348        *  @return  Reference to this object.
00349        *
00350        *  All fields of __rhs are copied into this object except that rdbuf()
00351        *  and rdstate() remain unchanged.  All values in the pword and iword
00352        *  arrays are copied.  Before copying, each callback is invoked with
00353        *  erase_event.  After copying, each (new) callback is invoked with
00354        *  copyfmt_event.  The final step is to copy exceptions().
00355       */
00356       basic_ios&
00357       copyfmt(const basic_ios& __rhs);
00358 
00359       /**
00360        *  @brief  Retrieves the @a empty character.
00361        *  @return  The current fill character.
00362        *
00363        *  It defaults to a space (' ') in the current locale.
00364       */
00365       char_type
00366       fill() const
00367       {
00368     if (!_M_fill_init)
00369       {
00370         _M_fill = this->widen(' ');
00371         _M_fill_init = true;
00372       }
00373     return _M_fill;
00374       }
00375 
00376       /**
00377        *  @brief  Sets a new @a empty character.
00378        *  @param  __ch  The new character.
00379        *  @return  The previous fill character.
00380        *
00381        *  The fill character is used to fill out space when P+ characters
00382        *  have been requested (e.g., via setw), Q characters are actually
00383        *  used, and Q<P.  It defaults to a space (' ') in the current locale.
00384       */
00385       char_type
00386       fill(char_type __ch)
00387       {
00388     char_type __old = this->fill();
00389     _M_fill = __ch;
00390     return __old;
00391       }
00392 
00393       // Locales:
00394       /**
00395        *  @brief  Moves to a new locale.
00396        *  @param  __loc  The new locale.
00397        *  @return  The previous locale.
00398        *
00399        *  Calls @c ios_base::imbue(loc), and if a stream buffer is associated
00400        *  with this stream, calls that buffer's @c pubimbue(loc).
00401        *
00402        *  Additional l10n notes are at
00403        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html
00404       */
00405       locale
00406       imbue(const locale& __loc);
00407 
00408       /**
00409        *  @brief  Squeezes characters.
00410        *  @param  __c  The character to narrow.
00411        *  @param  __dfault  The character to narrow.
00412        *  @return  The narrowed character.
00413        *
00414        *  Maps a character of @c char_type to a character of @c char,
00415        *  if possible.
00416        *
00417        *  Returns the result of
00418        *  @code
00419        *    std::use_facet<ctype<char_type> >(getloc()).narrow(c,dfault)
00420        *  @endcode
00421        *
00422        *  Additional l10n notes are at
00423        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html
00424       */
00425       char
00426       narrow(char_type __c, char __dfault) const
00427       { return __check_facet(_M_ctype).narrow(__c, __dfault); }
00428 
00429       /**
00430        *  @brief  Widens characters.
00431        *  @param  __c  The character to widen.
00432        *  @return  The widened character.
00433        *
00434        *  Maps a character of @c char to a character of @c char_type.
00435        *
00436        *  Returns the result of
00437        *  @code
00438        *    std::use_facet<ctype<char_type> >(getloc()).widen(c)
00439        *  @endcode
00440        *
00441        *  Additional l10n notes are at
00442        *  http://gcc.gnu.org/onlinedocs/libstdc++/manual/localization.html
00443       */
00444       char_type
00445       widen(char __c) const
00446       { return __check_facet(_M_ctype).widen(__c); }
00447 
00448     protected:
00449       // 27.4.5.1  basic_ios constructors
00450       /**
00451        *  @brief  Empty.
00452        *
00453        *  The default constructor does nothing and is not normally
00454        *  accessible to users.
00455       */
00456       basic_ios()
00457       : ios_base(), _M_tie(0), _M_fill(char_type()), _M_fill_init(false), 
00458     _M_streambuf(0), _M_ctype(0), _M_num_put(0), _M_num_get(0)
00459       { }
00460 
00461       /**
00462        *  @brief  All setup is performed here.
00463        *
00464        *  This is called from the public constructor.  It is not virtual and
00465        *  cannot be redefined.
00466       */
00467       void
00468       init(basic_streambuf<_CharT, _Traits>* __sb);
00469 
00470       void
00471       _M_cache_locale(const locale& __loc);
00472     };
00473 
00474 _GLIBCXX_END_NAMESPACE_VERSION
00475 } // namespace
00476 
00477 #include <bits/basic_ios.tcc>
00478 
00479 #endif /* _BASIC_IOS_H */