1139815Simp// basic_ios member functions -*- C++ -*-
21573Srgrimes
31573Srgrimes// Copyright (C) 1999, 2001, 2002, 2003, 2004, 2005
41573Srgrimes// Free Software Foundation, Inc.
51573Srgrimes//
61573Srgrimes// This file is part of the GNU ISO C++ Library.  This library is free
71573Srgrimes// software; you can redistribute it and/or modify it under the
81573Srgrimes// terms of the GNU General Public License as published by the
91573Srgrimes// Free Software Foundation; either version 2, or (at your option)
101573Srgrimes// any later version.
111573Srgrimes
121573Srgrimes// This library is distributed in the hope that it will be useful,
131573Srgrimes// but WITHOUT ANY WARRANTY; without even the implied warranty of
141573Srgrimes// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
151573Srgrimes// GNU General Public License for more details.
161573Srgrimes
171573Srgrimes// You should have received a copy of the GNU General Public License along
181573Srgrimes// with this library; see the file COPYING.  If not, write to the Free
191573Srgrimes// Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
201573Srgrimes// USA.
211573Srgrimes
221573Srgrimes// As a special exception, you may use this file as part of a free software
231573Srgrimes// library without restriction.  Specifically, if other files instantiate
241573Srgrimes// templates or use macros or inline functions from this file, or you compile
251573Srgrimes// this file and link it with other files to produce an executable, this
261573Srgrimes// file does not by itself cause the resulting executable to be covered by
271573Srgrimes// the GNU General Public License.  This exception does not however
281573Srgrimes// invalidate any other reasons why the executable file might be covered by
291573Srgrimes// the GNU General Public License.
301573Srgrimes
311573Srgrimes/** @file basic_ios.tcc
321573Srgrimes *  This is an internal header file, included by other library headers.
33116189Sobrien *  You should not attempt to use it directly.
34116189Sobrien */
35116189Sobrien
361573Srgrimes#ifndef _BASIC_IOS_TCC
371573Srgrimes#define _BASIC_IOS_TCC 1
381573Srgrimes
391573Srgrimes#pragma GCC system_header
401573Srgrimes
41104652Sdd_GLIBCXX_BEGIN_NAMESPACE(std)
42104652Sdd
43104652Sdd  template<typename _CharT, typename _Traits>
441573Srgrimes    void
451573Srgrimes    basic_ios<_CharT, _Traits>::clear(iostate __state)
461573Srgrimes    {
4726484Sache      if (this->rdbuf())
4826484Sache	_M_streambuf_state = __state;
4926484Sache      else
501573Srgrimes	  _M_streambuf_state = __state | badbit;
5190045Sobrien      if (this->exceptions() & this->rdstate())
5226484Sache	__throw_ios_failure(__N("basic_ios::clear"));
531573Srgrimes    }
54154660Srwatson
551573Srgrimes  template<typename _CharT, typename _Traits>
561573Srgrimes    basic_streambuf<_CharT, _Traits>*
5726484Sache    basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
581573Srgrimes    {
591573Srgrimes      basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
601573Srgrimes      _M_streambuf = __sb;
611573Srgrimes      this->clear();
621573Srgrimes      return __old;
6319132Sache    }
6419132Sache
651573Srgrimes  template<typename _CharT, typename _Traits>
661573Srgrimes    basic_ios<_CharT, _Traits>&
671573Srgrimes    basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
681573Srgrimes    {
691573Srgrimes      // _GLIBCXX_RESOLVE_LIB_DEFECTS
701573Srgrimes      // 292. effects of a.copyfmt (a)
711573Srgrimes      if (this != &__rhs)
721573Srgrimes	{
731573Srgrimes	  // Per 27.1.1, do not call imbue, yet must trash all caches
741573Srgrimes	  // associated with imbue()
751573Srgrimes
761573Srgrimes	  // Alloc any new word array first, so if it fails we have "rollback".
771573Srgrimes	  _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
781573Srgrimes	                     _M_local_word : new _Words[__rhs._M_word_size];
791573Srgrimes
801573Srgrimes	  // Bump refs before doing callbacks, for safety.
811573Srgrimes	  _Callback_list* __cb = __rhs._M_callbacks;
821573Srgrimes	  if (__cb)
831573Srgrimes	    __cb->_M_add_reference();
841573Srgrimes	  _M_call_callbacks(erase_event);
851573Srgrimes	  if (_M_word != _M_local_word)
861573Srgrimes	    {
871573Srgrimes	      delete [] _M_word;
881573Srgrimes	      _M_word = 0;
891573Srgrimes	    }
901573Srgrimes	  _M_dispose_callbacks();
9125269Sjdp
92229272Sed	  // NB: Don't want any added during above.
931573Srgrimes	  _M_callbacks = __cb;
941573Srgrimes	  for (int __i = 0; __i < __rhs._M_word_size; ++__i)
951573Srgrimes	    __words[__i] = __rhs._M_word[__i];
961573Srgrimes	  _M_word = __words;
97229272Sed	  _M_word_size = __rhs._M_word_size;
981573Srgrimes
991573Srgrimes	  this->flags(__rhs.flags());
1001573Srgrimes	  this->width(__rhs.width());
1011573Srgrimes	  this->precision(__rhs.precision());
1021573Srgrimes	  this->tie(__rhs.tie());
1031573Srgrimes	  this->fill(__rhs.fill());
1041573Srgrimes	  _M_ios_locale = __rhs.getloc();
1051573Srgrimes	  _M_cache_locale(_M_ios_locale);
1061573Srgrimes
1071573Srgrimes	  _M_call_callbacks(copyfmt_event);
1081573Srgrimes
1091573Srgrimes	  // The next is required to be the last assignment.
1101573Srgrimes	  this->exceptions(__rhs.exceptions());
1111573Srgrimes	}
1121573Srgrimes      return *this;
1131573Srgrimes    }
11426486Sache
1151573Srgrimes  template<typename _CharT, typename _Traits>
11626486Sache    char
11726486Sache    basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
11826486Sache    { return __check_facet(_M_ctype).narrow(__c, __dfault); }
11926486Sache
12026486Sache  template<typename _CharT, typename _Traits>
12126484Sache    _CharT
12226484Sache    basic_ios<_CharT, _Traits>::widen(char __c) const
12326484Sache    { return __check_facet(_M_ctype).widen(__c); }
12426484Sache
12526484Sache  // Locales:
12626484Sache  template<typename _CharT, typename _Traits>
12726484Sache    locale
1281573Srgrimes    basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
12926484Sache    {
1301573Srgrimes      locale __old(this->getloc());
1311573Srgrimes      ios_base::imbue(__loc);
1321573Srgrimes      _M_cache_locale(__loc);
1331573Srgrimes      if (this->rdbuf() != 0)
1341573Srgrimes	this->rdbuf()->pubimbue(__loc);
1351573Srgrimes      return __old;
1361573Srgrimes    }
1371573Srgrimes
1381573Srgrimes  template<typename _CharT, typename _Traits>
1391573Srgrimes    void
1401573Srgrimes    basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
14126484Sache    {
14219059Swosch      // NB: This may be called more than once on the same object.
14319059Swosch      ios_base::_M_init();
14419132Sache
14519132Sache      // Cache locale data and specific facets used by iostreams.
14619132Sache      _M_cache_locale(_M_ios_locale);
14719059Swosch
14819059Swosch      // NB: The 27.4.4.1 Postconditions Table specifies requirements
1491573Srgrimes      // after basic_ios::init() has been called. As part of this,
15019059Swosch      // fill() must return widen(' ') any time after init() has been
1511573Srgrimes      // called, which needs an imbued ctype facet of char_type to
1521573Srgrimes      // return without throwing an exception. Unfortunately,
1531573Srgrimes      // ctype<char_type> is not necessarily a required facet, so
1541573Srgrimes      // streams with char_type != [char, wchar_t] will not have it by
1551573Srgrimes      // default. Because of this, the correct value for _M_fill is
15626484Sache      // constructed on the first call of fill(). That way,
157154660Srwatson      // unformatted input and output with non-required basic_ios
1581573Srgrimes      // instantiations is possible even without imbuing the expected
15926492Sache      // ctype<char_type> facet.
1601573Srgrimes      _M_fill = _CharT();
1611573Srgrimes      _M_fill_init = false;
1621573Srgrimes
1631573Srgrimes      _M_tie = 0;
1641573Srgrimes      _M_exception = goodbit;
1651573Srgrimes      _M_streambuf = __sb;
1661573Srgrimes      _M_streambuf_state = __sb ? goodbit : badbit;
1671573Srgrimes    }
1681573Srgrimes
16926486Sache  template<typename _CharT, typename _Traits>
1701573Srgrimes    void
1718870Srgrimes    basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
17219132Sache    {
17319132Sache      if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
17419059Swosch	_M_ctype = &use_facet<__ctype_type>(__loc);
17526484Sache      else
17626484Sache	_M_ctype = 0;
17726484Sache
17826484Sache      if (__builtin_expect(has_facet<__num_put_type>(__loc), true))
17926484Sache	_M_num_put = &use_facet<__num_put_type>(__loc);
18026492Sache      else
18126492Sache	_M_num_put = 0;
18226492Sache
1831573Srgrimes      if (__builtin_expect(has_facet<__num_get_type>(__loc), true))
1841573Srgrimes	_M_num_get = &use_facet<__num_get_type>(__loc);
1851573Srgrimes      else
18626484Sache	_M_num_get = 0;
18719059Swosch    }
18826486Sache
18926486Sache  // Inhibit implicit instantiations for required instantiations,
19026486Sache  // which are defined via explicit instantiations elsewhere.
19119132Sache  // NB:  This syntax is a GNU extension.
19219132Sache#if _GLIBCXX_EXTERN_TEMPLATE
19319059Swosch  extern template class basic_ios<char>;
1948870Srgrimes
1951573Srgrimes#ifdef _GLIBCXX_USE_WCHAR_T
1961573Srgrimes  extern template class basic_ios<wchar_t>;
1971573Srgrimes#endif
1981573Srgrimes#endif
1991573Srgrimes
20026484Sache_GLIBCXX_END_NAMESPACE
20119059Swosch
20219132Sache#endif
20319132Sache