# basic_ios.tcc

    00001 // basic_ios locale and locale-related member functions -*- C++ -*-
    00002
    00003 // Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
    00004 //
    00005 // This file is part of the GNU ISO C++ Library.  This library is free
    00006 // software; you can redistribute it and/or modify it under the
    00007 // terms of the GNU General Public License as published by the
    00008 // Free Software Foundation; either version 2, or (at your option)
    00009 // any later version.
    00010
    00011 // This library is distributed in the hope that it will be useful,
    00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
    00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    00014 // GNU General Public License for more details.
    00015
    00016 // You should have received a copy of the GNU General Public License along
    00017 // with this library; see the file COPYING.  If not, write to the Free
    00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
    00019 // USA.
    00020
    00021 // As a special exception, you may use this file as part of a free software
    00022 // library without restriction.  Specifically, if other files instantiate
    00023 // templates or use macros or inline functions from this file, or you compile
    00024 // this file and link it with other files to produce an executable, this
    00025 // file does not by itself cause the resulting executable to be covered by
    00026 // the GNU General Public License.  This exception does not however
    00027 // invalidate any other reasons why the executable file might be covered by
    00028 // the GNU General Public License.
    00029
    00030 #ifndef _CPP_BITS_BASICIOS_TCC
    00031 #define _CPP_BITS_BASICIOS_TCC 1
    00032
    00033 #pragma GCC system_header
    00034
    00035 namespace std
    00036 {
    00037   template<typename _CharT, typename _Traits>
    00038     void
    00039     basic_ios<_CharT, _Traits>::clear(iostate __state)
    00040     {
    00041       if (this->rdbuf())
    00042     _M_streambuf_state = __state;
    00043       else
    00044       _M_streambuf_state = __state | badbit;
    00045       if ((this->rdstate() & this->exceptions()))
    00046     __throw_ios_failure("basic_ios::clear(iostate) caused exception");
    00047     }
    00048
    00049   template<typename _CharT, typename _Traits>
    00050     basic_streambuf<_CharT, _Traits>*
    00051     basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
    00052     {
    00053       basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
    00054       _M_streambuf = __sb;
    00055       this->clear();
    00056       return __old;
    00057     }
    00058
    00059   template<typename _CharT, typename _Traits>
    00060     basic_ios<_CharT, _Traits>&
    00061     basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
    00062     {
    00063       // Per 27.1.1.1, do not call imbue, yet must trash all caches
    00064       // associated with imbue()
    00065
    00066       // Alloc any new word array first, so if it fails we have "rollback".
    00067       _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
    00068     _M_local_word : new _Words[__rhs._M_word_size];
    00069
    00070       // Bump refs before doing callbacks, for safety.
    00071       _Callback_list* __cb = __rhs._M_callbacks;
    00072       if (__cb)
    00073     __cb->_M_add_reference();
    00074       _M_call_callbacks(erase_event);
    00075       if (_M_word != _M_local_word)
    00076     {
    00077       delete [] _M_word;
    00078       _M_word = 0;
    00079     }
    00080       _M_dispose_callbacks();
    00081
    00082       _M_callbacks = __cb;  // NB: Don't want any added during above.
    00083       for (int __i = 0; __i < __rhs._M_word_size; ++__i)
    00084     __words[__i] = __rhs._M_word[__i];
    00085       if (_M_word != _M_local_word)
    00086     {
    00087       delete [] _M_word;
    00088       _M_word = 0;
    00089     }
    00090       _M_word = __words;
    00091       _M_word_size = __rhs._M_word_size;
    00092
    00093       this->flags(__rhs.flags());
    00094       this->width(__rhs.width());
    00095       this->precision(__rhs.precision());
    00096       this->tie(__rhs.tie());
    00097       this->fill(__rhs.fill());
    00098       _M_ios_locale = __rhs.getloc();
    00099
    00100       // This removes the link to __rhs locale cache
    00101       _M_call_callbacks(copyfmt_event);
    00102
    00103       _M_cache_locale(_M_ios_locale);
    00104
    00105
    00106       // The next is required to be the last assignment.
    00107       this->exceptions(__rhs.exceptions());
    00108
    00109       return *this;
    00110     }
    00111
    00112   template<typename _CharT, typename _Traits>
    00113     char
    00114     basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
    00115     {
    00116       char __ret = __dfault;
    00117       if (_M_check_facet(_M_fctype))
    00118     __ret = _M_fctype->narrow(__c, __dfault);
    00119       return __ret;
    00120     }
    00121
    00122   template<typename _CharT, typename _Traits>
    00123     _CharT
    00124     basic_ios<_CharT, _Traits>::widen(char __c) const
    00125     {
    00126       char_type __ret = char_type();
    00127       if (_M_check_facet(_M_fctype))
    00128     __ret = _M_fctype->widen(__c);
    00129       return __ret;
    00130     }
    00131
    00132   // Locales:
    00133   template<typename _CharT, typename _Traits>
    00134     locale
    00135     basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
    00136     {
    00137       locale __old(this->getloc());
    00138       ios_base::imbue(__loc);
    00139       _M_cache_locale(__loc);
    00140       if (this->rdbuf() != 0)
    00141     this->rdbuf()->pubimbue(__loc);
    00142       return __old;
    00143     }
    00144
    00145   template<typename _CharT, typename _Traits>
    00146     void
    00147     basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb,
    00148                      __locale_cache<_CharT>* __cache)
    00149     {
    00150       // NB: This may be called more than once on the same object.
    00151       ios_base::_M_init();
    00152       _M_cache_locale(_M_ios_locale, __cache);
    00153       _M_tie = 0;
    00154
    00155       // NB: The 27.4.4.1 Postconditions Table specifies requirements
    00156       // after basic_ios::init() has been called. As part of this,
    00157       // fill() must return widen(' ') any time after init() has been
    00158       // called, which needs an imbued ctype facet of char_type to
    00159       // return without throwing an exception. Unfortunately,
    00160       // ctype<char_type> is not necessarily a required facet, so
    00161       // streams with char_type != [char, wchar_t] will not have it by
    00162       // default. Because of this, the correct value for _M_fill is
    00163       // constructed on the first call of fill(). That way,
    00164       // unformatted input and output with non-required basic_ios
    00165       // instantiations is possible even without imbuing the expected
    00166       // ctype<char_type> facet.
    00167       _M_fill = _CharT();
    00168       _M_fill_init = false;
    00169
    00170       _M_exception = goodbit;
    00171       _M_streambuf = __sb;
    00172       _M_streambuf_state = __sb ? goodbit : badbit;
    00173     }
    00174
    00175   template<typename _CharT, typename _Traits>
    00176     void
    00177     basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc,
    00178                         __locale_cache<_CharT>* __cache)
    00179     {
    00180       if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
    00181     _M_fctype = &use_facet<__ctype_type>(__loc);
    00182       else
    00183     _M_fctype = 0;
    00184       if (__builtin_expect(has_facet<__numput_type>(__loc), true))
    00185     _M_fnumput = &use_facet<__numput_type>(__loc);
    00186       else
    00187     _M_fnumput = 0;
    00188       if (__builtin_expect(has_facet<__numget_type>(__loc), true))
    00189     _M_fnumget = &use_facet<__numget_type>(__loc);
    00190       else
    00191     _M_fnumget = 0;
    00192       typedef __locale_cache<_CharT> __cache_t;
    00193       if (!pword(0)) {
    00194     // We store the cache at pword(0).  iword(0) is set to 1 to
    00195     // indicate that this is a static storage cache that shouldn't
    00196     // be deleted.
    00197     if (__cache)
    00198       {
    00199         pword(0) = auto_ptr<__cache_t>(new (__cache) __cache_t()).release();
    00200         iword(0) = 1;       // so we don't try to clobber static cache
    00201       }
    00202     else
    00203       pword(0) = auto_ptr<__cache_t>(new __cache_t()).release();
    00204     register_callback(__cache_t::_S_callback, 0);
    00205       }
    00206       static_cast<__cache_t&>(_M_cache())._M_init(__loc);
    00207     }
    00208
    00209 #if 1
    00210       // XXX GLIBCXX_ABI Deprecated, compatibility only.
    00211   template<typename _CharT, typename _Traits>
    00212     void
    00213     basic_ios<_CharT, _Traits>::_M_cache_facets(const locale& __loc)
    00214     {
    00215       if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
    00216     _M_fctype = &use_facet<__ctype_type>(__loc);
    00217       if (__builtin_expect(has_facet<__numput_type>(__loc), true))
    00218     _M_fnumput = &use_facet<__numput_type>(__loc);
    00219       if (__builtin_expect(has_facet<__numget_type>(__loc), true))
    00220     _M_fnumget = &use_facet<__numget_type>(__loc);
    00221     }
    00222 #endif
    00223
    00224   // Inhibit implicit instantiations for required instantiations,
    00225   // which are defined via explicit instantiations elsewhere.
    00226   // NB:  This syntax is a GNU extension.
    00227 #if _GLIBCPP_EXTERN_TEMPLATE
    00228   extern template class basic_ios<char>;
    00229
    00230 #ifdef _GLIBCPP_USE_WCHAR_T
    00231   extern template class basic_ios<wchar_t>;
    00232 #endif
    00233 #endif
    00234 } // namespace std
    00235
    00236 #endif