xref: /openbsd-src/gnu/lib/libstdc++/libstdc++/include/bits/basic_ios.tcc (revision c2fb321235c8dbfba0909bc43913400e90646ae7)
1 // basic_ios locale and locale-related member functions -*- C++ -*-
2 
3 // Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
4 //
5 // This file is part of the GNU ISO C++ Library.  This library is free
6 // software; you can redistribute it and/or modify it under the
7 // terms of the GNU General Public License as published by the
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
10 
11 // This library is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 // GNU General Public License for more details.
15 
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING.  If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
20 
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction.  Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License.  This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
29 
30 #ifndef _CPP_BITS_BASICIOS_TCC
31 #define _CPP_BITS_BASICIOS_TCC 1
32 
33 #pragma GCC system_header
34 
35 namespace std
36 {
37   template<typename _CharT, typename _Traits>
38     void
clear(iostate __state)39     basic_ios<_CharT, _Traits>::clear(iostate __state)
40     {
41       if (this->rdbuf())
42 	_M_streambuf_state = __state;
43       else
44 	  _M_streambuf_state = __state | badbit;
45       if ((this->rdstate() & this->exceptions()))
46 	__throw_ios_failure("basic_ios::clear(iostate) caused exception");
47     }
48 
49   template<typename _CharT, typename _Traits>
50     basic_streambuf<_CharT, _Traits>*
rdbuf(basic_streambuf<_CharT,_Traits> * __sb)51     basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
52     {
53       basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
54       _M_streambuf = __sb;
55       this->clear();
56       return __old;
57     }
58 
59   template<typename _CharT, typename _Traits>
60     basic_ios<_CharT, _Traits>&
copyfmt(const basic_ios & __rhs)61     basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
62     {
63       // _GLIBCXX_RESOLVE_LIB_DEFECTS
64       // 292. effects of a.copyfmt (a)
65       if (this != &__rhs)
66 	{
67 	  // Per 27.1.1.1, do not call imbue, yet must trash all caches
68 	  // associated with imbue()
69 
70 	  // Alloc any new word array first, so if it fails we have "rollback".
71 	  _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
72 	    _M_local_word : new _Words[__rhs._M_word_size];
73 
74 	  // Bump refs before doing callbacks, for safety.
75 	  _Callback_list* __cb = __rhs._M_callbacks;
76 	  if (__cb)
77 	    __cb->_M_add_reference();
78 	  _M_call_callbacks(erase_event);
79 	  if (_M_word != _M_local_word)
80 	    {
81 	      delete [] _M_word;
82 	      _M_word = 0;
83 	    }
84 	  _M_dispose_callbacks();
85 
86 	  _M_callbacks = __cb;  // NB: Don't want any added during above.
87 	  for (int __i = 0; __i < __rhs._M_word_size; ++__i)
88 	    __words[__i] = __rhs._M_word[__i];
89 	  if (_M_word != _M_local_word)
90 	    {
91 	      delete [] _M_word;
92 	      _M_word = 0;
93 	    }
94 	  _M_word = __words;
95 	  _M_word_size = __rhs._M_word_size;
96 
97 	  this->flags(__rhs.flags());
98 	  this->width(__rhs.width());
99 	  this->precision(__rhs.precision());
100 	  this->tie(__rhs.tie());
101 	  this->fill(__rhs.fill());
102 	  _M_ios_locale = __rhs.getloc();
103 
104 	  // This removes the link to __rhs locale cache
105 	  _M_call_callbacks(copyfmt_event);
106 
107 	  _M_cache_locale(_M_ios_locale);
108 
109 
110 	  // The next is required to be the last assignment.
111 	  this->exceptions(__rhs.exceptions());
112 	}
113       return *this;
114     }
115 
116   template<typename _CharT, typename _Traits>
117     char
narrow(char_type __c,char __dfault) const118     basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
119     {
120       char __ret = __dfault;
121       if (_M_check_facet(_M_fctype))
122 	__ret = _M_fctype->narrow(__c, __dfault);
123       return __ret;
124     }
125 
126   template<typename _CharT, typename _Traits>
127     _CharT
widen(char __c) const128     basic_ios<_CharT, _Traits>::widen(char __c) const
129     {
130       char_type __ret = char_type();
131       if (_M_check_facet(_M_fctype))
132 	__ret = _M_fctype->widen(__c);
133       return __ret;
134     }
135 
136   // Locales:
137   template<typename _CharT, typename _Traits>
138     locale
imbue(const locale & __loc)139     basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
140     {
141       locale __old(this->getloc());
142       ios_base::imbue(__loc);
143       _M_cache_locale(__loc);
144       if (this->rdbuf() != 0)
145 	this->rdbuf()->pubimbue(__loc);
146       return __old;
147     }
148 
149   template<typename _CharT, typename _Traits>
150     void
init(basic_streambuf<_CharT,_Traits> * __sb)151     basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
152     {
153       // NB: This may be called more than once on the same object.
154       ios_base::_M_init();
155       _M_cache_locale(_M_ios_locale);
156       _M_tie = 0;
157 
158       // NB: The 27.4.4.1 Postconditions Table specifies requirements
159       // after basic_ios::init() has been called. As part of this,
160       // fill() must return widen(' ') any time after init() has been
161       // called, which needs an imbued ctype facet of char_type to
162       // return without throwing an exception. Unfortunately,
163       // ctype<char_type> is not necessarily a required facet, so
164       // streams with char_type != [char, wchar_t] will not have it by
165       // default. Because of this, the correct value for _M_fill is
166       // constructed on the first call of fill(). That way,
167       // unformatted input and output with non-required basic_ios
168       // instantiations is possible even without imbuing the expected
169       // ctype<char_type> facet.
170       _M_fill = _CharT();
171       _M_fill_init = false;
172 
173       _M_exception = goodbit;
174       _M_streambuf = __sb;
175       _M_streambuf_state = __sb ? goodbit : badbit;
176     }
177 
178   template<typename _CharT, typename _Traits>
179     void
_M_cache_locale(const locale & __loc)180     basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
181     {
182       if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
183 	_M_fctype = &use_facet<__ctype_type>(__loc);
184       else
185 	_M_fctype = 0;
186       if (__builtin_expect(has_facet<__numput_type>(__loc), true))
187 	_M_fnumput = &use_facet<__numput_type>(__loc);
188       else
189 	_M_fnumput = 0;
190       if (__builtin_expect(has_facet<__numget_type>(__loc), true))
191 	_M_fnumget = &use_facet<__numget_type>(__loc);
192       else
193 	_M_fnumget = 0;
194     }
195 
196 #if 1
197       // XXX GLIBCXX_ABI Deprecated, compatibility only.
198   template<typename _CharT, typename _Traits>
199     void
_M_cache_facets(const locale & __loc)200     basic_ios<_CharT, _Traits>::_M_cache_facets(const locale& __loc)
201     {
202       if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
203 	_M_fctype = &use_facet<__ctype_type>(__loc);
204       if (__builtin_expect(has_facet<__numput_type>(__loc), true))
205 	_M_fnumput = &use_facet<__numput_type>(__loc);
206       if (__builtin_expect(has_facet<__numget_type>(__loc), true))
207 	_M_fnumget = &use_facet<__numget_type>(__loc);
208     }
209 #endif
210 
211   // Inhibit implicit instantiations for required instantiations,
212   // which are defined via explicit instantiations elsewhere.
213   // NB:  This syntax is a GNU extension.
214 #if defined(_GLIBCPP_EXTERN_TEMPLATE)
215   extern template class basic_ios<char>;
216 
217 #if defined(_GLIBCPP_USE_WCHAR_T) || defined(_GLIBCPP_USE_TYPE_WCHAR_T)
218   extern template class basic_ios<wchar_t>;
219 #endif
220 #endif
221 } // namespace std
222 
223 #endif
224