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