libstdc++
basic_ios.tcc
1 // basic_ios member functions -*- C++ -*-
2 
3 // Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 // 2009 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 basic_ios.tcc
27  * This is an internal header file, included by other library headers.
28  * You should not attempt to use it directly.
29  */
30 
31 #ifndef _BASIC_IOS_TCC
32 #define _BASIC_IOS_TCC 1
33 
34 #pragma GCC system_header
35 
36 _GLIBCXX_BEGIN_NAMESPACE(std)
37 
38  template<typename _CharT, typename _Traits>
39  void
40  basic_ios<_CharT, _Traits>::clear(iostate __state)
41  {
42  if (this->rdbuf())
43  _M_streambuf_state = __state;
44  else
45  _M_streambuf_state = __state | badbit;
46  if (this->exceptions() & this->rdstate())
47  __throw_ios_failure(__N("basic_ios::clear"));
48  }
49 
50  template<typename _CharT, typename _Traits>
51  basic_streambuf<_CharT, _Traits>*
52  basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
53  {
54  basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
55  _M_streambuf = __sb;
56  this->clear();
57  return __old;
58  }
59 
60  template<typename _CharT, typename _Traits>
61  basic_ios<_CharT, _Traits>&
62  basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
63  {
64  // _GLIBCXX_RESOLVE_LIB_DEFECTS
65  // 292. effects of a.copyfmt (a)
66  if (this != &__rhs)
67  {
68  // Per 27.1.1, do not call imbue, yet must trash all caches
69  // associated with imbue()
70 
71  // Alloc any new word array first, so if it fails we have "rollback".
72  _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
73  _M_local_word : new _Words[__rhs._M_word_size];
74 
75  // Bump refs before doing callbacks, for safety.
76  _Callback_list* __cb = __rhs._M_callbacks;
77  if (__cb)
78  __cb->_M_add_reference();
79  _M_call_callbacks(erase_event);
80  if (_M_word != _M_local_word)
81  {
82  delete [] _M_word;
83  _M_word = 0;
84  }
85  _M_dispose_callbacks();
86 
87  // NB: Don't want any added during above.
88  _M_callbacks = __cb;
89  for (int __i = 0; __i < __rhs._M_word_size; ++__i)
90  __words[__i] = __rhs._M_word[__i];
91  _M_word = __words;
92  _M_word_size = __rhs._M_word_size;
93 
94  this->flags(__rhs.flags());
95  this->width(__rhs.width());
96  this->precision(__rhs.precision());
97  this->tie(__rhs.tie());
98  this->fill(__rhs.fill());
99  _M_ios_locale = __rhs.getloc();
100  _M_cache_locale(_M_ios_locale);
101 
102  _M_call_callbacks(copyfmt_event);
103 
104  // The next is required to be the last assignment.
105  this->exceptions(__rhs.exceptions());
106  }
107  return *this;
108  }
109 
110  // Locales:
111  template<typename _CharT, typename _Traits>
112  locale
113  basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
114  {
115  locale __old(this->getloc());
116  ios_base::imbue(__loc);
117  _M_cache_locale(__loc);
118  if (this->rdbuf() != 0)
119  this->rdbuf()->pubimbue(__loc);
120  return __old;
121  }
122 
123  template<typename _CharT, typename _Traits>
124  void
125  basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
126  {
127  // NB: This may be called more than once on the same object.
128  ios_base::_M_init();
129 
130  // Cache locale data and specific facets used by iostreams.
131  _M_cache_locale(_M_ios_locale);
132 
133  // NB: The 27.4.4.1 Postconditions Table specifies requirements
134  // after basic_ios::init() has been called. As part of this,
135  // fill() must return widen(' ') any time after init() has been
136  // called, which needs an imbued ctype facet of char_type to
137  // return without throwing an exception. Unfortunately,
138  // ctype<char_type> is not necessarily a required facet, so
139  // streams with char_type != [char, wchar_t] will not have it by
140  // default. Because of this, the correct value for _M_fill is
141  // constructed on the first call of fill(). That way,
142  // unformatted input and output with non-required basic_ios
143  // instantiations is possible even without imbuing the expected
144  // ctype<char_type> facet.
145  _M_fill = _CharT();
146  _M_fill_init = false;
147 
148  _M_tie = 0;
149  _M_exception = goodbit;
150  _M_streambuf = __sb;
151  _M_streambuf_state = __sb ? goodbit : badbit;
152  }
153 
154  template<typename _CharT, typename _Traits>
155  void
156  basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
157  {
158  if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
159  _M_ctype = &use_facet<__ctype_type>(__loc);
160  else
161  _M_ctype = 0;
162 
163  if (__builtin_expect(has_facet<__num_put_type>(__loc), true))
164  _M_num_put = &use_facet<__num_put_type>(__loc);
165  else
166  _M_num_put = 0;
167 
168  if (__builtin_expect(has_facet<__num_get_type>(__loc), true))
169  _M_num_get = &use_facet<__num_get_type>(__loc);
170  else
171  _M_num_get = 0;
172  }
173 
174  // Inhibit implicit instantiations for required instantiations,
175  // which are defined via explicit instantiations elsewhere.
176  // NB: This syntax is a GNU extension.
177 #if _GLIBCXX_EXTERN_TEMPLATE
178  extern template class basic_ios<char>;
179 
180 #ifdef _GLIBCXX_USE_WCHAR_T
181  extern template class basic_ios<wchar_t>;
182 #endif
183 #endif
184 
185 _GLIBCXX_END_NAMESPACE
186 
187 #endif