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