# basic_ios.tcc

Go to the documentation of this file.

    00001 // basic_ios member functions -*- C++ -*-
    00002
    00003 // Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007
    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   // Locales:
    00116   template<typename _CharT, typename _Traits>
    00117     locale
    00118     basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
    00119     {
    00120       locale __old(this->getloc());
    00121       ios_base::imbue(__loc);
    00122       _M_cache_locale(__loc);
    00123       if (this->rdbuf() != 0)
    00124     this->rdbuf()->pubimbue(__loc);
    00125       return __old;
    00126     }
    00127
    00128   template<typename _CharT, typename _Traits>
    00129     void
    00130     basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
    00131     {
    00132       // NB: This may be called more than once on the same object.
    00133       ios_base::_M_init();
    00134
    00135       // Cache locale data and specific facets used by iostreams.
    00136       _M_cache_locale(_M_ios_locale);
    00137
    00138       // NB: The 27.4.4.1 Postconditions Table specifies requirements
    00139       // after basic_ios::init() has been called. As part of this,
    00140       // fill() must return widen(' ') any time after init() has been
    00141       // called, which needs an imbued ctype facet of char_type to
    00142       // return without throwing an exception. Unfortunately,
    00143       // ctype<char_type> is not necessarily a required facet, so
    00144       // streams with char_type != [char, wchar_t] will not have it by
    00145       // default. Because of this, the correct value for _M_fill is
    00146       // constructed on the first call of fill(). That way,
    00147       // unformatted input and output with non-required basic_ios
    00148       // instantiations is possible even without imbuing the expected
    00149       // ctype<char_type> facet.
    00150       _M_fill = _CharT();
    00151       _M_fill_init = false;
    00152
    00153       _M_tie = 0;
    00154       _M_exception = goodbit;
    00155       _M_streambuf = __sb;
    00156       _M_streambuf_state = __sb ? goodbit : badbit;
    00157     }
    00158
    00159   template<typename _CharT, typename _Traits>
    00160     void
    00161     basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
    00162     {
    00163       if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
    00164     _M_ctype = &use_facet<__ctype_type>(__loc);
    00165       else
    00166     _M_ctype = 0;
    00167
    00168       if (__builtin_expect(has_facet<__num_put_type>(__loc), true))
    00169     _M_num_put = &use_facet<__num_put_type>(__loc);
    00170       else
    00171     _M_num_put = 0;
    00172
    00173       if (__builtin_expect(has_facet<__num_get_type>(__loc), true))
    00174     _M_num_get = &use_facet<__num_get_type>(__loc);
    00175       else
    00176     _M_num_get = 0;
    00177     }
    00178
    00179   // Inhibit implicit instantiations for required instantiations,
    00180   // which are defined via explicit instantiations elsewhere.
    00181   // NB:  This syntax is a GNU extension.
    00182 #if _GLIBCXX_EXTERN_TEMPLATE
    00183   extern template class basic_ios<char>;
    00184
    00185 #ifdef _GLIBCXX_USE_WCHAR_T
    00186   extern template class basic_ios<wchar_t>;
    00187 #endif
    00188 #endif
    00189
    00190 _GLIBCXX_END_NAMESPACE
    00191
    00192 #endif