libstdc++
basic_ios.tcc
Go to the documentation of this file.
1 // basic_ios member functions -*- C++ -*-
2 
3 // Copyright (C) 1999-2025 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
17 // permissions described in the GCC Runtime Library Exception, version
18 // 3.1, as published by the Free Software Foundation.
19 
20 // You should have received a copy of the GNU General Public License and
21 // a copy of the GCC Runtime Library Exception along with this program;
22 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
23 // <http://www.gnu.org/licenses/>.
24 
25 /** @file bits/basic_ios.tcc
26  * This is an internal header file, included by other library headers.
27  * Do not attempt to use it directly. @headername{ios}
28  */
29 
30 #ifndef _BASIC_IOS_TCC
31 #define _BASIC_IOS_TCC 1
32 
33 #ifdef _GLIBCXX_SYSHDR
34 #pragma GCC system_header
35 #endif
36 
37 #pragma GCC diagnostic push
38 #pragma GCC diagnostic ignored "-Wc++11-extensions" // extern template
39 
40 namespace std _GLIBCXX_VISIBILITY(default)
41 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 
44  template<typename _CharT, typename _Traits>
45  void
47  {
48  if (this->rdbuf())
49  _M_streambuf_state = __state;
50  else
51  _M_streambuf_state = __state | badbit;
52  if (this->exceptions() & this->rdstate())
53  __throw_ios_failure(__N("basic_ios::clear"));
54  }
55 
56  template<typename _CharT, typename _Traits>
59  {
60  basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
61  _M_streambuf = __sb;
62  this->clear();
63  return __old;
64  }
65 
66  template<typename _CharT, typename _Traits>
69  {
70  // _GLIBCXX_RESOLVE_LIB_DEFECTS
71  // 292. effects of a.copyfmt (a)
72  if (this != std::__addressof(__rhs))
73  {
74  // Per 27.1.1, do not call imbue, yet must trash all caches
75  // associated with imbue()
76 
77  // Alloc any new word array first, so if it fails we have "rollback".
78  _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
79  _M_local_word : new _Words[__rhs._M_word_size];
80 
81  // Bump refs before doing callbacks, for safety.
82  _Callback_list* __cb = __rhs._M_callbacks;
83  if (__cb)
84  __cb->_M_add_reference();
85  _M_call_callbacks(erase_event);
86  if (_M_word != _M_local_word)
87  {
88  delete [] _M_word;
89  _M_word = 0;
90  }
91  _M_dispose_callbacks();
92 
93  // NB: Don't want any added during above.
94  _M_callbacks = __cb;
95  for (int __i = 0; __i < __rhs._M_word_size; ++__i)
96  __words[__i] = __rhs._M_word[__i];
97  _M_word = __words;
98  _M_word_size = __rhs._M_word_size;
99 
100  this->flags(__rhs.flags());
101  this->width(__rhs.width());
102  this->precision(__rhs.precision());
103  this->tie(__rhs.tie());
104  this->fill(__rhs.fill());
105  _M_ios_locale = __rhs.getloc();
106  _M_cache_locale(_M_ios_locale);
107 
108  _M_call_callbacks(copyfmt_event);
109 
110  // The next is required to be the last assignment.
111  this->exceptions(__rhs.exceptions());
112  }
113  return *this;
114  }
115 
116  // Locales:
117  template<typename _CharT, typename _Traits>
118  locale
120  {
121  locale __old(this->getloc());
122  ios_base::imbue(__loc);
123  _M_cache_locale(__loc);
124  if (this->rdbuf() != 0)
125  this->rdbuf()->pubimbue(__loc);
126  return __old;
127  }
128 
129  template<typename _CharT, typename _Traits>
130  void
132  {
133  // NB: This may be called more than once on the same object.
134  ios_base::_M_init();
135 
136  // Cache locale data and specific facets used by iostreams.
137  _M_cache_locale(_M_ios_locale);
138 
139  // NB: The 27.4.4.1 Postconditions Table specifies requirements
140  // after basic_ios::init() has been called. As part of this,
141  // fill() must return widen(' ') any time after init() has been
142  // called, which needs an imbued ctype facet of char_type to
143  // return without throwing an exception. Unfortunately,
144  // ctype<char_type> is not necessarily a required facet, so
145  // streams with char_type != [char, wchar_t] will not have it by
146  // default. If the ctype<char_type> facet is available now,
147  // _M_fill is set here, but otherwise no fill character will be
148  // cached and a call to fill() will check for the facet again later
149  // (and will throw if the facet is still not present). This way
150  // unformatted input and output with non-required basic_ios
151  // instantiations is possible even without imbuing the expected
152  // ctype<char_type> facet.
153  if (_M_ctype)
154  {
155  _M_fill = _M_ctype->widen(' ');
156  _M_fill_init = true;
157  }
158  else
159  _M_fill_init = false;
160 
161  _M_tie = 0;
162  _M_exception = goodbit;
163  _M_streambuf = __sb;
164  _M_streambuf_state = __sb ? goodbit : badbit;
165  }
166 
167  template<typename _CharT, typename _Traits>
168  void
169  basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
170  {
171  _M_ctype = std::__try_use_facet<__ctype_type>(__loc);
172  _M_num_put = std::__try_use_facet<__num_put_type>(__loc);
173  _M_num_get = std::__try_use_facet<__num_get_type>(__loc);
174  }
175 
176  // Inhibit implicit instantiations for required instantiations,
177  // which are defined via explicit instantiations elsewhere.
178 #if _GLIBCXX_EXTERN_TEMPLATE
179  extern template class basic_ios<char>;
180 
181 #ifdef _GLIBCXX_USE_WCHAR_T
182  extern template class basic_ios<wchar_t>;
183 #endif
184 #endif
185 
186 _GLIBCXX_END_NAMESPACE_VERSION
187 } // namespace std
188 
189 #pragma GCC diagnostic pop
190 #endif
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:52
constexpr tuple< _Elements &... > tie(_Elements &... __args) noexcept
Return a tuple of lvalue references bound to the arguments.
Definition: tuple:2817
ISO C++ entities toplevel namespace is std.
Template class basic_ios, virtual base class for all stream classes.
Definition: basic_ios.h:70
void clear(iostate __state=goodbit)
[Re]sets the error state.
Definition: basic_ios.tcc:46
locale imbue(const locale &__loc)
Moves to a new locale.
Definition: basic_ios.tcc:119
basic_ostream< _CharT, _Traits > * tie() const
Fetches the current tied stream.
Definition: basic_ios.h:310
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
Definition: basic_ios.h:337
iostate exceptions() const
Throwing exceptions on errors.
Definition: basic_ios.h:236
basic_ios & copyfmt(const basic_ios &__rhs)
Copies fields of __rhs into this.
Definition: basic_ios.tcc:68
void init(basic_streambuf< _CharT, _Traits > *__sb)
All setup is performed here.
Definition: basic_ios.tcc:131
char_type fill() const
Retrieves the empty character.
Definition: basic_ios.h:387
The actual work of input and output (interface).
Definition: streambuf:127
streamsize precision() const
Flags access.
Definition: ios_base.h:765
locale imbue(const locale &__loc)
Setting a new locale.
fmtflags flags() const
Access to format flags.
Definition: ios_base.h:694
streamsize width() const
Flags access.
Definition: ios_base.h:789
locale getloc() const
Locale access.
Definition: ios_base.h:841
Container class for localization functionality.