ios.cc

00001 // Iostreams base classes -*- C++ -*-
00002 
00003 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
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.4  Iostreams base classes
00033 //
00034 
00035 #include <ios>
00036 #include <limits>
00037 #include <bits/atomicity.h>
00038 
00039 namespace std 
00040 {  
00041   // XXX GLIBCXX_ABI Deprecated
00042    // Definitions for static const data members of __ios_flags.
00043   const __ios_flags::__int_type __ios_flags::_S_boolalpha;
00044   const __ios_flags::__int_type __ios_flags::_S_dec;
00045   const __ios_flags::__int_type __ios_flags::_S_fixed;
00046   const __ios_flags::__int_type __ios_flags::_S_hex;
00047   const __ios_flags::__int_type __ios_flags::_S_internal;
00048   const __ios_flags::__int_type __ios_flags::_S_left;
00049   const __ios_flags::__int_type __ios_flags::_S_oct;
00050   const __ios_flags::__int_type __ios_flags::_S_right;
00051   const __ios_flags::__int_type __ios_flags::_S_scientific;
00052   const __ios_flags::__int_type __ios_flags::_S_showbase;
00053   const __ios_flags::__int_type __ios_flags::_S_showpoint;
00054   const __ios_flags::__int_type __ios_flags::_S_showpos;
00055   const __ios_flags::__int_type __ios_flags::_S_skipws;
00056   const __ios_flags::__int_type __ios_flags::_S_unitbuf;
00057   const __ios_flags::__int_type __ios_flags::_S_uppercase;
00058   const __ios_flags::__int_type __ios_flags::_S_adjustfield;
00059   const __ios_flags::__int_type __ios_flags::_S_basefield;
00060   const __ios_flags::__int_type __ios_flags::_S_floatfield;
00061 
00062   const __ios_flags::__int_type __ios_flags::_S_badbit;
00063   const __ios_flags::__int_type __ios_flags::_S_eofbit;
00064   const __ios_flags::__int_type __ios_flags::_S_failbit;
00065 
00066   const __ios_flags::__int_type __ios_flags::_S_app;
00067   const __ios_flags::__int_type __ios_flags::_S_ate;
00068   const __ios_flags::__int_type __ios_flags::_S_bin;
00069   const __ios_flags::__int_type __ios_flags::_S_in;
00070   const __ios_flags::__int_type __ios_flags::_S_out;
00071   const __ios_flags::__int_type __ios_flags::_S_trunc;
00072 
00073   // Definitions for static const members of ios_base.
00074   const ios_base::fmtflags ios_base::boolalpha;
00075   const ios_base::fmtflags ios_base::dec;
00076   const ios_base::fmtflags ios_base::fixed;
00077   const ios_base::fmtflags ios_base::hex;
00078   const ios_base::fmtflags ios_base::internal;
00079   const ios_base::fmtflags ios_base::left;
00080   const ios_base::fmtflags ios_base::oct;
00081   const ios_base::fmtflags ios_base::right;
00082   const ios_base::fmtflags ios_base::scientific;
00083   const ios_base::fmtflags ios_base::showbase;
00084   const ios_base::fmtflags ios_base::showpoint;
00085   const ios_base::fmtflags ios_base::showpos;
00086   const ios_base::fmtflags ios_base::skipws;
00087   const ios_base::fmtflags ios_base::unitbuf;
00088   const ios_base::fmtflags ios_base::uppercase;
00089   const ios_base::fmtflags ios_base::adjustfield;
00090   const ios_base::fmtflags ios_base::basefield;
00091   const ios_base::fmtflags ios_base::floatfield;
00092 
00093   const ios_base::iostate ios_base::badbit;
00094   const ios_base::iostate ios_base::eofbit;
00095   const ios_base::iostate ios_base::failbit;
00096   const ios_base::iostate ios_base::goodbit;
00097 
00098   const ios_base::openmode ios_base::app;
00099   const ios_base::openmode ios_base::ate;
00100   const ios_base::openmode ios_base::binary;
00101   const ios_base::openmode ios_base::in;
00102   const ios_base::openmode ios_base::out;
00103   const ios_base::openmode ios_base::trunc;
00104 
00105   const ios_base::seekdir ios_base::beg;
00106   const ios_base::seekdir ios_base::cur;
00107   const ios_base::seekdir ios_base::end;
00108 
00109   const int ios_base::_S_local_word_size;
00110 
00111   _Atomic_word ios_base::Init::_S_refcount;
00112 
00113   bool ios_base::Init::_S_synced_with_stdio = true;
00114 
00115   ios_base::ios_base() 
00116   : _M_precision(), _M_width(), _M_flags(), _M_exception(), 
00117   _M_streambuf_state(), _M_callbacks(0), _M_word_zero(), 
00118   _M_word_size(_S_local_word_size), _M_word(_M_local_word), _M_ios_locale()
00119   {
00120     // Do nothing: basic_ios::init() does it.  
00121     // NB: _M_callbacks and _M_word must be zero for non-initialized
00122     // ios_base to go through ~ios_base gracefully.
00123   }
00124   
00125   // 27.4.2.7  ios_base constructors/destructors
00126   ios_base::~ios_base()
00127   {
00128     _M_call_callbacks(erase_event);
00129     _M_dispose_callbacks();
00130     if (_M_word != _M_local_word) 
00131       {
00132     delete [] _M_word;
00133     _M_word = 0;
00134       }
00135   }
00136 
00137   // 27.4.2.5  ios_base storage functions
00138   int 
00139   ios_base::xalloc() throw()
00140   {
00141     // Implementation note: Initialize top to zero to ensure that
00142     // initialization occurs before main() is started.
00143     static _Atomic_word _S_top = 0; 
00144     return __gnu_cxx::__exchange_and_add(&_S_top, 1) + 4;
00145   }
00146 
00147   void 
00148   ios_base::register_callback(event_callback __fn, int __index)
00149   { _M_callbacks = new _Callback_list(__fn, __index, _M_callbacks); }
00150 
00151   // 27.4.2.5  iword/pword storage
00152   ios_base::_Words&
00153   ios_base::_M_grow_words(int ix, bool iword)
00154   {
00155     // Precondition: _M_word_size <= ix
00156     int newsize = _S_local_word_size;
00157     _Words* words = _M_local_word;
00158     if (ix > _S_local_word_size - 1)
00159       {
00160     if (ix < numeric_limits<int>::max())
00161       {
00162         newsize = ix + 1;
00163         try
00164           { words = new _Words[newsize]; }
00165         catch (...)
00166           {
00167         _M_streambuf_state |= badbit;
00168         if (_M_streambuf_state & _M_exception)
00169           __throw_ios_failure(__N("ios_base::_M_grow_words "
00170                       "allocation failed"));
00171         if (iword)
00172           _M_word_zero._M_iword = 0;
00173         else
00174           _M_word_zero._M_pword = 0;
00175         return _M_word_zero;
00176           }
00177         for (int i = 0; i < _M_word_size; i++) 
00178           words[i] = _M_word[i];
00179         if (_M_word && _M_word != _M_local_word) 
00180           {
00181         delete [] _M_word;
00182         _M_word = 0;
00183           }
00184       }
00185     else
00186       {
00187         _M_streambuf_state |= badbit;
00188         if (_M_streambuf_state & _M_exception)
00189           __throw_ios_failure(__N("ios_base::_M_grow_words is not valid"));
00190         if (iword)
00191           _M_word_zero._M_iword = 0;
00192         else
00193           _M_word_zero._M_pword = 0;
00194         return _M_word_zero;
00195       }
00196       }
00197     _M_word = words;
00198     _M_word_size = newsize;
00199     return _M_word[ix];
00200   }
00201 
00202   void 
00203   ios_base::_M_call_callbacks(event __e) throw()
00204   {
00205     _Callback_list* __p = _M_callbacks;
00206     while (__p)
00207       {
00208     try 
00209       { (*__p->_M_fn) (__e, *this, __p->_M_index); } 
00210     catch (...) 
00211       { }
00212     __p = __p->_M_next;
00213       }
00214   }
00215 
00216   void 
00217   ios_base::_M_dispose_callbacks(void)
00218   {
00219     _Callback_list* __p = _M_callbacks;
00220     while (__p && __p->_M_remove_reference() == 0)
00221       {
00222     _Callback_list* __next = __p->_M_next;
00223     delete __p;
00224     __p = __next;
00225       }
00226     _M_callbacks = 0;
00227   }
00228 } // namespace std

Generated on Fri May 6 01:08:59 2005 for libstdc++-v3 Source by  doxygen 1.4.2