# basic_ios.tcc

    00001 // basic_ios 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 _BASIC_IOS_TCC
    00031 #define _BASIC_IOS_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->exceptions() & this->rdstate())
    00046     __throw_ios_failure(__N("basic_ios::clear"));
    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       // _GLIBCXX_RESOLVE_LIB_DEFECTS
    00064       // 292. effects of a.copyfmt (a)
    00065       if (this != &__rhs)
    00066     {
    00067       // Per 27.1.1, do not call imbue, yet must trash all caches
    00068       // associated with imbue()
    00069
    00070       // Alloc any new word array first, so if it fails we have "rollback".
    00071       _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
    00072                          _M_local_word : new _Words[__rhs._M_word_size];
    00073
    00074       // Bump refs before doing callbacks, for safety.
    00075       _Callback_list* __cb = __rhs._M_callbacks;
    00076       if (__cb)
    00077         __cb->_M_add_reference();
    00078       _M_call_callbacks(erase_event);
    00079       if (_M_word != _M_local_word)
    00080         {
    00081           delete [] _M_word;
    00082           _M_word = 0;
    00083         }
    00084       _M_dispose_callbacks();
    00085
    00086       // NB: Don't want any added during above.
    00087       _M_callbacks = __cb;
    00088       for (int __i = 0; __i < __rhs._M_word_size; ++__i)
    00089         __words[__i] = __rhs._M_word[__i];
    00090       if (_M_word != _M_local_word)
    00091         {
    00092           delete [] _M_word;
    00093           _M_word = 0;
    00094         }
    00095       _M_word = __words;
    00096       _M_word_size = __rhs._M_word_size;
    00097
    00098       this->flags(__rhs.flags());
    00099       this->width(__rhs.width());
    00100       this->precision(__rhs.precision());
    00101       this->tie(__rhs.tie());
    00102       this->fill(__rhs.fill());
    00103       _M_ios_locale = __rhs.getloc();
    00104       _M_cache_locale(_M_ios_locale);
    00105
    00106       _M_call_callbacks(copyfmt_event);
    00107
    00108       // The next is required to be the last assignment.
    00109       this->exceptions(__rhs.exceptions());
    00110     }
    00111       return *this;
    00112     }
    00113
    00114   template<typename _CharT, typename _Traits>
    00115     char
    00116     basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
    00117     { return __check_facet(_M_ctype).narrow(__c, __dfault); }
    00118
    00119   template<typename _CharT, typename _Traits>
    00120     _CharT
    00121     basic_ios<_CharT, _Traits>::widen(char __c) const
    00122     { return __check_facet(_M_ctype).widen(__c); }
    00123
    00124   // Locales:
    00125   template<typename _CharT, typename _Traits>
    00126     locale
    00127     basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
    00128     {
    00129       locale __old(this->getloc());
    00130       ios_base::imbue(__loc);
    00131       _M_cache_locale(__loc);
    00132       if (this->rdbuf() != 0)
    00133     this->rdbuf()->pubimbue(__loc);
    00134       return __old;
    00135     }
    00136
    00137   template<typename _CharT, typename _Traits>
    00138     void
    00139     basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
    00140     {
    00141       // NB: This may be called more than once on the same object.
    00142       ios_base::_M_init();
    00143
    00144       // Cache locale data and specific facets used by iostreams.
    00145       _M_cache_locale(_M_ios_locale);
    00146
    00147       // NB: The 27.4.4.1 Postconditions Table specifies requirements
    00148       // after basic_ios::init() has been called. As part of this,
    00149       // fill() must return widen(' ') any time after init() has been
    00150       // called, which needs an imbued ctype facet of char_type to
    00151       // return without throwing an exception. Unfortunately,
    00152       // ctype<char_type> is not necessarily a required facet, so
    00153       // streams with char_type != [char, wchar_t] will not have it by
    00154       // default. Because of this, the correct value for _M_fill is
    00155       // constructed on the first call of fill(). That way,
    00156       // unformatted input and output with non-required basic_ios
    00157       // instantiations is possible even without imbuing the expected
    00158       // ctype<char_type> facet.
    00159       _M_fill = _CharT();
    00160       _M_fill_init = false;
    00161
    00162       _M_tie = 0;
    00163       _M_exception = goodbit;
    00164       _M_streambuf = __sb;
    00165       _M_streambuf_state = __sb ? goodbit : badbit;
    00166     }
    00167
    00168   template<typename _CharT, typename _Traits>
    00169     void
    00170     basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
    00171     {
    00172       if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
    00173     _M_ctype = &use_facet<__ctype_type>(__loc);
    00174       else
    00175     _M_ctype = 0;
    00176
    00177       if (__builtin_expect(has_facet<__num_put_type>(__loc), true))
    00178     _M_num_put = &use_facet<__num_put_type>(__loc);
    00179       else
    00180     _M_num_put = 0;
    00181
    00182       if (__builtin_expect(has_facet<__num_get_type>(__loc), true))
    00183     _M_num_get = &use_facet<__num_get_type>(__loc);
    00184       else
    00185     _M_num_get = 0;
    00186     }
    00187
    00188   // Inhibit implicit instantiations for required instantiations,
    00189   // which are defined via explicit instantiations elsewhere.
    00190   // NB:  This syntax is a GNU extension.
    00191 #if _GLIBCXX_EXTERN_TEMPLATE
    00192   extern template class basic_ios<char>;
    00193
    00194 #ifdef _GLIBCXX_USE_WCHAR_T
    00195   extern template class basic_ios<wchar_t>;
    00196 #endif
    00197 #endif
    00198 } // namespace std
    00199
    00200 #endif