1 // basic_ios member functions -*- C++ -*-
3 // Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007,
4 // 2009, 2010, 2011 Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 3, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // Under Section 7 of GPL version 3, you are granted additional
18 // permissions described in the GCC Runtime Library Exception, version
19 // 3.1, as published by the Free Software Foundation.
21 // You should have received a copy of the GNU General Public License and
22 // a copy of the GCC Runtime Library Exception along with this program;
23 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
24 // <http://www.gnu.org/licenses/>.
26 /** @file bits/basic_ios.tcc
27 * This is an internal header file, included by other library headers.
28 * Do not attempt to use it directly. @headername{ios}
31 #ifndef _BASIC_IOS_TCC
32 #define _BASIC_IOS_TCC 1
34 #pragma GCC system_header
36 namespace std _GLIBCXX_VISIBILITY(default)
38 _GLIBCXX_BEGIN_NAMESPACE_VERSION
40 template<typename _CharT, typename _Traits>
42 basic_ios<_CharT, _Traits>::clear(iostate __state)
45 _M_streambuf_state = __state;
47 _M_streambuf_state = __state | badbit;
48 if (this->exceptions() & this->rdstate())
49 __throw_ios_failure(__N("basic_ios::clear"));
52 template<typename _CharT, typename _Traits>
53 basic_streambuf<_CharT, _Traits>*
54 basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
56 basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
62 template<typename _CharT, typename _Traits>
63 basic_ios<_CharT, _Traits>&
64 basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
66 // _GLIBCXX_RESOLVE_LIB_DEFECTS
67 // 292. effects of a.copyfmt (a)
70 // Per 27.1.1, do not call imbue, yet must trash all caches
71 // associated with imbue()
73 // Alloc any new word array first, so if it fails we have "rollback".
74 _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
75 _M_local_word : new _Words[__rhs._M_word_size];
77 // Bump refs before doing callbacks, for safety.
78 _Callback_list* __cb = __rhs._M_callbacks;
80 __cb->_M_add_reference();
81 _M_call_callbacks(erase_event);
82 if (_M_word != _M_local_word)
87 _M_dispose_callbacks();
89 // NB: Don't want any added during above.
91 for (int __i = 0; __i < __rhs._M_word_size; ++__i)
92 __words[__i] = __rhs._M_word[__i];
94 _M_word_size = __rhs._M_word_size;
96 this->flags(__rhs.flags());
97 this->width(__rhs.width());
98 this->precision(__rhs.precision());
99 this->tie(__rhs.tie());
100 this->fill(__rhs.fill());
101 _M_ios_locale = __rhs.getloc();
102 _M_cache_locale(_M_ios_locale);
104 _M_call_callbacks(copyfmt_event);
106 // The next is required to be the last assignment.
107 this->exceptions(__rhs.exceptions());
113 template<typename _CharT, typename _Traits>
115 basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
117 locale __old(this->getloc());
118 ios_base::imbue(__loc);
119 _M_cache_locale(__loc);
120 if (this->rdbuf() != 0)
121 this->rdbuf()->pubimbue(__loc);
125 template<typename _CharT, typename _Traits>
127 basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
129 // NB: This may be called more than once on the same object.
132 // Cache locale data and specific facets used by iostreams.
133 _M_cache_locale(_M_ios_locale);
135 // NB: The 27.4.4.1 Postconditions Table specifies requirements
136 // after basic_ios::init() has been called. As part of this,
137 // fill() must return widen(' ') any time after init() has been
138 // called, which needs an imbued ctype facet of char_type to
139 // return without throwing an exception. Unfortunately,
140 // ctype<char_type> is not necessarily a required facet, so
141 // streams with char_type != [char, wchar_t] will not have it by
142 // default. Because of this, the correct value for _M_fill is
143 // constructed on the first call of fill(). That way,
144 // unformatted input and output with non-required basic_ios
145 // instantiations is possible even without imbuing the expected
146 // ctype<char_type> facet.
148 _M_fill_init = false;
151 _M_exception = goodbit;
153 _M_streambuf_state = __sb ? goodbit : badbit;
156 template<typename _CharT, typename _Traits>
158 basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
160 if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
161 _M_ctype = &use_facet<__ctype_type>(__loc);
165 if (__builtin_expect(has_facet<__num_put_type>(__loc), true))
166 _M_num_put = &use_facet<__num_put_type>(__loc);
170 if (__builtin_expect(has_facet<__num_get_type>(__loc), true))
171 _M_num_get = &use_facet<__num_get_type>(__loc);
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>;
181 #ifdef _GLIBCXX_USE_WCHAR_T
182 extern template class basic_ios<wchar_t>;
186 _GLIBCXX_END_NAMESPACE_VERSION