1193326Sed// basic_ios member functions -*- C++ -*-
2193326Sed
3193326Sed// Copyright (C) 1999-2022 Free Software Foundation, Inc.
4193326Sed//
5193326Sed// This file is part of the GNU ISO C++ Library.  This library is free
6193326Sed// software; you can redistribute it and/or modify it under the
7193326Sed// terms of the GNU General Public License as published by the
8193326Sed// Free Software Foundation; either version 3, or (at your option)
9193326Sed// any later version.
10193326Sed
11193326Sed// This library is distributed in the hope that it will be useful,
12193326Sed// but WITHOUT ANY WARRANTY; without even the implied warranty of
13193326Sed// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14193326Sed// GNU General Public License for more details.
15193326Sed
16193326Sed// Under Section 7 of GPL version 3, you are granted additional
17218893Sdim// permissions described in the GCC Runtime Library Exception, version
18193326Sed// 3.1, as published by the Free Software Foundation.
19226633Sdim
20212904Sdim// You should have received a copy of the GNU General Public License and
21193326Sed// a copy of the GCC Runtime Library Exception along with this program;
22226633Sdim// see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
23202379Srdivacky// <http://www.gnu.org/licenses/>.
24210299Sed
25193326Sed/** @file bits/basic_ios.tcc
26203955Srdivacky *  This is an internal header file, included by other library headers.
27193326Sed *  Do not attempt to use it directly. @headername{ios}
28193326Sed */
29210299Sed
30218893Sdim#ifndef _BASIC_IOS_TCC
31199990Srdivacky#define _BASIC_IOS_TCC 1
32228379Sdim
33193326Sed#pragma GCC system_header
34193326Sed
35193326Sednamespace std _GLIBCXX_VISIBILITY(default)
36193326Sed{
37193326Sed_GLIBCXX_BEGIN_NAMESPACE_VERSION
38193326Sed
39193326Sed  template<typename _CharT, typename _Traits>
40201361Srdivacky    void
41204793Srdivacky    basic_ios<_CharT, _Traits>::clear(iostate __state)
42218893Sdim    {
43193326Sed      if (this->rdbuf())
44207619Srdivacky	_M_streambuf_state = __state;
45199482Srdivacky      else
46193326Sed	_M_streambuf_state = __state | badbit;
47193326Sed      if (this->exceptions() & this->rdstate())
48193326Sed	__throw_ios_failure(__N("basic_ios::clear"));
49226633Sdim    }
50226633Sdim
51212904Sdim  template<typename _CharT, typename _Traits>
52226633Sdim    basic_streambuf<_CharT, _Traits>*
53212904Sdim    basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
54212904Sdim    {
55212904Sdim      basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
56212904Sdim      _M_streambuf = __sb;
57193326Sed      this->clear();
58212904Sdim      return __old;
59212904Sdim    }
60212904Sdim
61212904Sdim  template<typename _CharT, typename _Traits>
62212904Sdim    basic_ios<_CharT, _Traits>&
63199482Srdivacky    basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
64193326Sed    {
65226633Sdim      // _GLIBCXX_RESOLVE_LIB_DEFECTS
66218893Sdim      // 292. effects of a.copyfmt (a)
67202379Srdivacky      if (this != std::__addressof(__rhs))
68212904Sdim	{
69224145Sdim	  // Per 27.1.1, do not call imbue, yet must trash all caches
70218893Sdim	  // associated with imbue()
71226633Sdim
72226633Sdim	  // Alloc any new word array first, so if it fails we have "rollback".
73226633Sdim	  _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
74212904Sdim	                     _M_local_word : new _Words[__rhs._M_word_size];
75212904Sdim
76218893Sdim	  // Bump refs before doing callbacks, for safety.
77218893Sdim	  _Callback_list* __cb = __rhs._M_callbacks;
78221345Sdim	  if (__cb)
79226633Sdim	    __cb->_M_add_reference();
80226633Sdim	  _M_call_callbacks(erase_event);
81226633Sdim	  if (_M_word != _M_local_word)
82226633Sdim	    {
83226633Sdim	      delete [] _M_word;
84193326Sed	      _M_word = 0;
85218893Sdim	    }
86218893Sdim	  _M_dispose_callbacks();
87218893Sdim
88218893Sdim	  // NB: Don't want any added during above.
89218893Sdim	  _M_callbacks = __cb;
90221345Sdim	  for (int __i = 0; __i < __rhs._M_word_size; ++__i)
91221345Sdim	    __words[__i] = __rhs._M_word[__i];
92221345Sdim	  _M_word = __words;
93221345Sdim	  _M_word_size = __rhs._M_word_size;
94221345Sdim
95218893Sdim	  this->flags(__rhs.flags());
96218893Sdim	  this->width(__rhs.width());
97218893Sdim	  this->precision(__rhs.precision());
98224145Sdim	  this->tie(__rhs.tie());
99224145Sdim	  this->fill(__rhs.fill());
100224145Sdim	  _M_ios_locale = __rhs.getloc();
101224145Sdim	  _M_cache_locale(_M_ios_locale);
102218893Sdim
103218893Sdim	  _M_call_callbacks(copyfmt_event);
104223017Sdim
105223017Sdim	  // The next is required to be the last assignment.
106223017Sdim	  this->exceptions(__rhs.exceptions());
107223017Sdim	}
108226633Sdim      return *this;
109219077Sdim    }
110226633Sdim
111226633Sdim  // Locales:
112218893Sdim  template<typename _CharT, typename _Traits>
113218893Sdim    locale
114218893Sdim    basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
115193326Sed    {
116193326Sed      locale __old(this->getloc());
117193326Sed      ios_base::imbue(__loc);
118226633Sdim      _M_cache_locale(__loc);
119226633Sdim      if (this->rdbuf() != 0)
120226633Sdim	this->rdbuf()->pubimbue(__loc);
121226633Sdim      return __old;
122212904Sdim    }
123218893Sdim
124193326Sed  template<typename _CharT, typename _Traits>
125224145Sdim    void
126224145Sdim    basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
127193326Sed    {
128193326Sed      // NB: This may be called more than once on the same object.
129202879Srdivacky      ios_base::_M_init();
130202879Srdivacky
131226633Sdim      // Cache locale data and specific facets used by iostreams.
132202879Srdivacky      _M_cache_locale(_M_ios_locale);
133226633Sdim
134202879Srdivacky      // NB: The 27.4.4.1 Postconditions Table specifies requirements
135202879Srdivacky      // after basic_ios::init() has been called. As part of this,
136226633Sdim      // fill() must return widen(' ') any time after init() has been
137226633Sdim      // called, which needs an imbued ctype facet of char_type to
138226633Sdim      // return without throwing an exception. Unfortunately,
139226633Sdim      // ctype<char_type> is not necessarily a required facet, so
140226633Sdim      // streams with char_type != [char, wchar_t] will not have it by
141226633Sdim      // default. Because of this, the correct value for _M_fill is
142226633Sdim      // constructed on the first call of fill(). That way,
143226633Sdim      // unformatted input and output with non-required basic_ios
144193326Sed      // instantiations is possible even without imbuing the expected
145202379Srdivacky      // ctype<char_type> facet.
146198092Srdivacky      _M_fill = _CharT();
147205408Srdivacky      _M_fill_init = false;
148226633Sdim
149226633Sdim      _M_tie = 0;
150193326Sed      _M_exception = goodbit;
151193326Sed      _M_streambuf = __sb;
152193326Sed      _M_streambuf_state = __sb ? goodbit : badbit;
153226633Sdim    }
154193326Sed
155210299Sed  template<typename _CharT, typename _Traits>
156218893Sdim    void
157218893Sdim    basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
158210299Sed    {
159210299Sed      if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
160223017Sdim	_M_ctype = std::__addressof(use_facet<__ctype_type>(__loc));
161223017Sdim      else
162223017Sdim	_M_ctype = 0;
163226633Sdim
164226633Sdim      if (__builtin_expect(has_facet<__num_put_type>(__loc), true))
165226633Sdim	_M_num_put = std::__addressof(use_facet<__num_put_type>(__loc));
166193326Sed      else
167193326Sed	_M_num_put = 0;
168221345Sdim
169221345Sdim      if (__builtin_expect(has_facet<__num_get_type>(__loc), true))
170221345Sdim	_M_num_get = std::__addressof(use_facet<__num_get_type>(__loc));
171221345Sdim      else
172221345Sdim	_M_num_get = 0;
173221345Sdim    }
174221345Sdim
175218893Sdim  // Inhibit implicit instantiations for required instantiations,
176218893Sdim  // which are defined via explicit instantiations elsewhere.
177218893Sdim#if _GLIBCXX_EXTERN_TEMPLATE
178218893Sdim  extern template class basic_ios<char>;
179218893Sdim
180218893Sdim#ifdef _GLIBCXX_USE_WCHAR_T
181218893Sdim  extern template class basic_ios<wchar_t>;
182218893Sdim#endif
183218893Sdim#endif
184218893Sdim
185218893Sdim_GLIBCXX_END_NAMESPACE_VERSION
186204793Srdivacky} // namespace std
187226633Sdim
188204793Srdivacky#endif
189204793Srdivacky