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