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