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