1// basic_ios member functions -*- C++ -*-
2
3// Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4// 2009, 2010, 2011  Free Software Foundation, Inc.
5//
6// This file is part of the GNU ISO C++ Library.  This library is free
7// software; you can redistribute it and/or modify it under the
8// terms of the GNU General Public License as published by the
9// Free Software Foundation; either version 3, or (at your option)
10// any later version.
11
12// This library is distributed in the hope that it will be useful,
13// but WITHOUT ANY WARRANTY; without even the implied warranty of
14// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15// GNU General Public License for more details.
16
17// Under Section 7 of GPL version 3, you are granted additional
18// permissions described in the GCC Runtime Library Exception, version
19// 3.1, as published by the Free Software Foundation.
20
21// You should have received a copy of the GNU General Public License and
22// a copy of the GCC Runtime Library Exception along with this program;
23// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
24// <http://www.gnu.org/licenses/>.
25
26/** @file bits/basic_ios.tcc
27 *  This is an internal header file, included by other library headers.
28 *  Do not attempt to use it directly. @headername{ios}
29 */
30
31#ifndef _BASIC_IOS_TCC
32#define _BASIC_IOS_TCC 1
33
34#pragma GCC system_header
35
36namespace std _GLIBCXX_VISIBILITY(default)
37{
38_GLIBCXX_BEGIN_NAMESPACE_VERSION
39
40  template<typename _CharT, typename _Traits>
41    void
42    basic_ios<_CharT, _Traits>::clear(iostate __state)
43    {
44      if (this->rdbuf())
45	_M_streambuf_state = __state;
46      else
47	  _M_streambuf_state = __state | badbit;
48      if (this->exceptions() & this->rdstate())
49	__throw_ios_failure(__N("basic_ios::clear"));
50    }
51
52  template<typename _CharT, typename _Traits>
53    basic_streambuf<_CharT, _Traits>*
54    basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
55    {
56      basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
57      _M_streambuf = __sb;
58      this->clear();
59      return __old;
60    }
61
62  template<typename _CharT, typename _Traits>
63    basic_ios<_CharT, _Traits>&
64    basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
65    {
66      // _GLIBCXX_RESOLVE_LIB_DEFECTS
67      // 292. effects of a.copyfmt (a)
68      if (this != &__rhs)
69	{
70	  // Per 27.1.1, do not call imbue, yet must trash all caches
71	  // associated with imbue()
72
73	  // Alloc any new word array first, so if it fails we have "rollback".
74	  _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
75	                     _M_local_word : new _Words[__rhs._M_word_size];
76
77	  // Bump refs before doing callbacks, for safety.
78	  _Callback_list* __cb = __rhs._M_callbacks;
79	  if (__cb)
80	    __cb->_M_add_reference();
81	  _M_call_callbacks(erase_event);
82	  if (_M_word != _M_local_word)
83	    {
84	      delete [] _M_word;
85	      _M_word = 0;
86	    }
87	  _M_dispose_callbacks();
88
89	  // NB: Don't want any added during above.
90	  _M_callbacks = __cb;
91	  for (int __i = 0; __i < __rhs._M_word_size; ++__i)
92	    __words[__i] = __rhs._M_word[__i];
93	  _M_word = __words;
94	  _M_word_size = __rhs._M_word_size;
95
96	  this->flags(__rhs.flags());
97	  this->width(__rhs.width());
98	  this->precision(__rhs.precision());
99	  this->tie(__rhs.tie());
100	  this->fill(__rhs.fill());
101	  _M_ios_locale = __rhs.getloc();
102	  _M_cache_locale(_M_ios_locale);
103
104	  _M_call_callbacks(copyfmt_event);
105
106	  // The next is required to be the last assignment.
107	  this->exceptions(__rhs.exceptions());
108	}
109      return *this;
110    }
111
112  // Locales:
113  template<typename _CharT, typename _Traits>
114    locale
115    basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
116    {
117      locale __old(this->getloc());
118      ios_base::imbue(__loc);
119      _M_cache_locale(__loc);
120      if (this->rdbuf() != 0)
121	this->rdbuf()->pubimbue(__loc);
122      return __old;
123    }
124
125  template<typename _CharT, typename _Traits>
126    void
127    basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
128    {
129      // NB: This may be called more than once on the same object.
130      ios_base::_M_init();
131
132      // Cache locale data and specific facets used by iostreams.
133      _M_cache_locale(_M_ios_locale);
134
135      // NB: The 27.4.4.1 Postconditions Table specifies requirements
136      // after basic_ios::init() has been called. As part of this,
137      // fill() must return widen(' ') any time after init() has been
138      // called, which needs an imbued ctype facet of char_type to
139      // return without throwing an exception. Unfortunately,
140      // ctype<char_type> is not necessarily a required facet, so
141      // streams with char_type != [char, wchar_t] will not have it by
142      // default. Because of this, the correct value for _M_fill is
143      // constructed on the first call of fill(). That way,
144      // unformatted input and output with non-required basic_ios
145      // instantiations is possible even without imbuing the expected
146      // ctype<char_type> facet.
147      _M_fill = _CharT();
148      _M_fill_init = false;
149
150      _M_tie = 0;
151      _M_exception = goodbit;
152      _M_streambuf = __sb;
153      _M_streambuf_state = __sb ? goodbit : badbit;
154    }
155
156  template<typename _CharT, typename _Traits>
157    void
158    basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
159    {
160      if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
161	_M_ctype = &use_facet<__ctype_type>(__loc);
162      else
163	_M_ctype = 0;
164
165      if (__builtin_expect(has_facet<__num_put_type>(__loc), true))
166	_M_num_put = &use_facet<__num_put_type>(__loc);
167      else
168	_M_num_put = 0;
169
170      if (__builtin_expect(has_facet<__num_get_type>(__loc), true))
171	_M_num_get = &use_facet<__num_get_type>(__loc);
172      else
173	_M_num_get = 0;
174    }
175
176  // Inhibit implicit instantiations for required instantiations,
177  // which are defined via explicit instantiations elsewhere.
178#if _GLIBCXX_EXTERN_TEMPLATE
179  extern template class basic_ios<char>;
180
181#ifdef _GLIBCXX_USE_WCHAR_T
182  extern template class basic_ios<wchar_t>;
183#endif
184#endif
185
186_GLIBCXX_END_NAMESPACE_VERSION
187} // namespace std
188
189#endif
190