]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/basic_ios.tcc
Move from CPP to CXX.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / basic_ios.tcc
CommitLineData
cc5112c9 1// basic_ios member functions -*- C++ -*-
725dc051 2
1bc8b0ad 3// Copyright (C) 1999, 2001, 2002, 2003 Free Software Foundation, Inc.
725dc051
BK
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 2, 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// You should have received a copy of the GNU General Public License along
17// with this library; see the file COPYING. If not, write to the Free
18// Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19// USA.
20
21// As a special exception, you may use this file as part of a free software
22// library without restriction. Specifically, if other files instantiate
23// templates or use macros or inline functions from this file, or you compile
24// this file and link it with other files to produce an executable, this
25// file does not by itself cause the resulting executable to be covered by
26// the GNU General Public License. This exception does not however
27// invalidate any other reasons why the executable file might be covered by
28// the GNU General Public License.
29
3d7c150e
BK
30#ifndef _BASIC_IOS_TCC
31#define _BASIC_IOS_TCC 1
725dc051 32
3b794528
BK
33#pragma GCC system_header
34
5de197f2
BK
35namespace std
36{
a32e3c09
BK
37 template<typename _CharT, typename _Traits>
38 void
39 basic_ios<_CharT, _Traits>::clear(iostate __state)
40 {
41 if (this->rdbuf())
42 _M_streambuf_state = __state;
43 else
44 _M_streambuf_state = __state | badbit;
45 if ((this->rdstate() & this->exceptions()))
988ad90d 46 __throw_ios_failure("basic_ios::clear");
a32e3c09
BK
47 }
48
725dc051
BK
49 template<typename _CharT, typename _Traits>
50 basic_streambuf<_CharT, _Traits>*
51 basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
52 {
53 basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
54 _M_streambuf = __sb;
55 this->clear();
56 return __old;
57 }
58
59 template<typename _CharT, typename _Traits>
60 basic_ios<_CharT, _Traits>&
61 basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
62 {
6067bea4 63 // Per 27.1.1, do not call imbue, yet must trash all caches
725dc051
BK
64 // associated with imbue()
65
66 // Alloc any new word array first, so if it fails we have "rollback".
663653eb 67 _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
6067bea4 68 _M_local_word : new _Words[__rhs._M_word_size];
725dc051 69
725dc051
BK
70 // Bump refs before doing callbacks, for safety.
71 _Callback_list* __cb = __rhs._M_callbacks;
72 if (__cb)
73 __cb->_M_add_reference();
74 _M_call_callbacks(erase_event);
663653eb
BK
75 if (_M_word != _M_local_word)
76 {
77 delete [] _M_word;
78 _M_word = 0;
79 }
725dc051
BK
80 _M_dispose_callbacks();
81
6067bea4
BK
82 // NB: Don't want any added during above.
83 _M_callbacks = __cb;
663653eb
BK
84 for (int __i = 0; __i < __rhs._M_word_size; ++__i)
85 __words[__i] = __rhs._M_word[__i];
86 if (_M_word != _M_local_word)
87 {
88 delete [] _M_word;
89 _M_word = 0;
90 }
91 _M_word = __words;
92 _M_word_size = __rhs._M_word_size;
725dc051
BK
93
94 this->flags(__rhs.flags());
95 this->width(__rhs.width());
96 this->precision(__rhs.precision());
97 this->tie(__rhs.tie());
98 this->fill(__rhs.fill());
6067bea4
BK
99 _M_ios_locale = __rhs.getloc();
100 _M_cache_locale(_M_ios_locale);
101
102 _M_call_callbacks(copyfmt_event);
103
725dc051
BK
104 // The next is required to be the last assignment.
105 this->exceptions(__rhs.exceptions());
6067bea4 106
725dc051
BK
107 return *this;
108 }
109
110 template<typename _CharT, typename _Traits>
111 char
112 basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
0cd1de6f 113 { return __check_facet(_M_ctype).narrow(__c, __dfault); }
725dc051
BK
114
115 template<typename _CharT, typename _Traits>
116 _CharT
117 basic_ios<_CharT, _Traits>::widen(char __c) const
0cd1de6f 118 { return __check_facet(_M_ctype).widen(__c); }
725dc051
BK
119
120 // Locales:
121 template<typename _CharT, typename _Traits>
122 locale
123 basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
124 {
125 locale __old(this->getloc());
126 ios_base::imbue(__loc);
6067bea4 127 _M_cache_locale(__loc);
725dc051
BK
128 if (this->rdbuf() != 0)
129 this->rdbuf()->pubimbue(__loc);
130 return __old;
131 }
132
133 template<typename _CharT, typename _Traits>
134 void
135 basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
136 {
137 // NB: This may be called more than once on the same object.
138 ios_base::_M_init();
6067bea4
BK
139
140 // Cache locale data and specific facets used by iostreams.
215f9e28 141 _M_cache_locale(_M_ios_locale);
3a9ebf3c 142
ac39fabb
BK
143 // NB: The 27.4.4.1 Postconditions Table specifies requirements
144 // after basic_ios::init() has been called. As part of this,
145 // fill() must return widen(' ') any time after init() has been
146 // called, which needs an imbued ctype facet of char_type to
147 // return without throwing an exception. Unfortunately,
148 // ctype<char_type> is not necessarily a required facet, so
149 // streams with char_type != [char, wchar_t] will not have it by
150 // default. Because of this, the correct value for _M_fill is
151 // constructed on the first call of fill(). That way,
152 // unformatted input and output with non-required basic_ios
153 // instantiations is possible even without imbuing the expected
154 // ctype<char_type> facet.
49433044 155 _M_fill = _CharT();
ac39fabb 156 _M_fill_init = false;
3a9ebf3c 157
6067bea4 158 _M_tie = 0;
725dc051
BK
159 _M_exception = goodbit;
160 _M_streambuf = __sb;
d358ecd0 161 _M_streambuf_state = __sb ? goodbit : badbit;
725dc051
BK
162 }
163
bfa1e6b1
BK
164 template<typename _CharT, typename _Traits>
165 void
6067bea4 166 basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
bfa1e6b1 167 {
6067bea4 168 if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
0cd1de6f 169 _M_ctype = &use_facet<__ctype_type>(__loc);
6067bea4 170 if (__builtin_expect(has_facet<__numput_type>(__loc), true))
0cd1de6f 171 _M_num_put = &use_facet<__numput_type>(__loc);
6067bea4 172 if (__builtin_expect(has_facet<__numget_type>(__loc), true))
0cd1de6f 173 _M_num_get = &use_facet<__numget_type>(__loc);
bfa1e6b1 174 }
a32e3c09
BK
175
176 // Inhibit implicit instantiations for required instantiations,
177 // which are defined via explicit instantiations elsewhere.
178 // NB: This syntax is a GNU extension.
3d7c150e 179#if _GLIBCXX_EXTERN_TEMPLATE
a32e3c09 180 extern template class basic_ios<char>;
5112ae3a 181
3d7c150e 182#ifdef _GLIBCXX_USE_WCHAR_T
a32e3c09 183 extern template class basic_ios<wchar_t>;
5112ae3a 184#endif
1bc8b0ad 185#endif
725dc051
BK
186} // namespace std
187
3a9ebf3c 188#endif