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

Generated on Thu Jul 23 21:15:57 2009 for libstdc++ by  doxygen 1.5.8