# basic_ios.tcc

Go to the documentation of this file.

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