libstdc++: basic_ios.tcc Source File

libstdc++

basic_ios.tcc
Go to the documentation of this file.
1// basic_ios member functions -*- C++ -*-
2
3// Copyright (C) 1999-2026 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
40namespace std _GLIBCXX_VISIBILITY(default)
41{
42_GLIBCXX_BEGIN_NAMESPACE_VERSION
43
44 template<typename _CharT, typename _Traits>
45 void
46 basic_ios<_CharT, _Traits>::clear(iostate __state)
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>
57 basic_streambuf<_CharT, _Traits>*
58 basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
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>
67 basic_ios<_CharT, _Traits>&
68 basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
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
119 basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
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
131 basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
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
std::tie
constexpr tuple< _Elements &... > tie(_Elements &... __args) noexcept
Return a tuple of lvalue references bound to the arguments.
Definition tuple:2836
std::__addressof
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition move.h:52
std
ISO C++ entities toplevel namespace is std.
std::basic_ios
Template class basic_ios, virtual base class for all stream classes.
Definition basic_ios.h:71
std::basic_ios::clear
void clear(iostate __state=goodbit)
[Re]sets the error state.
Definition basic_ios.tcc:46
std::basic_ios::imbue
locale imbue(const locale &__loc)
Moves to a new locale.
Definition basic_ios.tcc:119
std::basic_ios::tie
basic_ostream< _CharT, _Traits > * tie() const
Fetches the current tied stream.
Definition basic_ios.h:311
std::basic_ios::exceptions
iostate exceptions() const
Throwing exceptions on errors.
Definition basic_ios.h:237
std::basic_ios::copyfmt
basic_ios & copyfmt(const basic_ios &__rhs)
Copies fields of __rhs into this.
Definition basic_ios.tcc:68
std::basic_ios::rdstate
iostate rdstate() const
Returns the error state of the stream buffer.
Definition basic_ios.h:147
std::basic_ios::init
void init(basic_streambuf< _CharT, _Traits > *__sb)
All setup is performed here.
Definition basic_ios.tcc:131
std::basic_ios::fill
char_type fill() const
Retrieves the empty character.
Definition basic_ios.h:388
std::basic_ios::rdbuf
basic_streambuf< _CharT, _Traits > * rdbuf() const
Accessing the underlying buffer.
Definition basic_ios.h:338
std::basic_ios::basic_ios
basic_ios(basic_streambuf< _CharT, _Traits > *__sb)
Constructor performs initialization.
Definition basic_ios.h:285
std::basic_streambuf
The actual work of input and output (interface).
Definition streambuf:127
std::ios_base::iostate
_Ios_Iostate iostate
This is a bitmask type.
Definition ios_base.h:453
std::ios_base::precision
streamsize precision() const
Flags access.
Definition ios_base.h:765
std::ios_base::imbue
locale imbue(const locale &__loc)
Setting a new locale.
std::ios_base::flags
fmtflags flags() const
Access to format flags.
Definition ios_base.h:694
std::basic_ios< _CharT, char_traits< _CharT > >::goodbit
static const iostate goodbit
Definition ios_base.h:468
std::ios_base::badbit
static const iostate badbit
Indicates a loss of integrity in an input or output sequence (such as an irrecoverable read error fro...
Definition ios_base.h:457
std::ios_base::width
streamsize width() const
Flags access.
Definition ios_base.h:789
std::ios_base::getloc
locale getloc() const
Locale access.
Definition ios_base.h:841
std::locale
Container class for localization functionality.
Definition locale_classes.h:70