locale_facets.tcc: Add pragma GCC system_header.
[gcc.git] / libstdc++-v3 / include / bits / basic_ios.tcc
1 // basic_ios locale and locale-related member functions -*- C++ -*-
2
3 // Copyright (C) 1999, 2001, 2002 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 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
30 #ifndef _CPP_BITS_BASICIOS_TCC
31 #define _CPP_BITS_BASICIOS_TCC 1
32
33 #pragma GCC system_header
34
35 namespace std
36 {
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()))
46 __throw_ios_failure("basic_ios::clear(iostate) caused exception");
47 }
48
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 {
63 // Per 27.1.1.1, do not call imbue, yet must trash all caches
64 // associated with imbue()
65
66 // Alloc any new word array first, so if it fails we have "rollback".
67 _Words* __words = (__rhs._M_word_limit <= _S_local_words) ?
68 _M_word_array : new _Words[__rhs._M_word_limit];
69
70 // XXX This is the only reason _Callback_list was defined
71 // inline. The suspicion is that this increased compilation
72 // times dramatically for functions that use this member
73 // function (inserters_extractors, ios_manip_fmtflags). FIX ME,
74 // clean this stuff up. Callbacks are broken right now, anyway.
75
76 // Bump refs before doing callbacks, for safety.
77 _Callback_list* __cb = __rhs._M_callbacks;
78 if (__cb)
79 __cb->_M_add_reference();
80 _M_call_callbacks(erase_event);
81 if (_M_words != _M_word_array)
82 delete [] _M_words;
83 _M_dispose_callbacks();
84
85 _M_callbacks = __cb; // NB: Don't want any added during above.
86 for (int __i = 0; __i < __rhs._M_word_limit; ++__i)
87 __words[__i] = __rhs._M_words[__i];
88 if (_M_words != _M_word_array)
89 delete [] _M_words;
90 _M_words = __words;
91 _M_word_limit = __rhs._M_word_limit;
92
93 this->flags(__rhs.flags());
94 this->width(__rhs.width());
95 this->precision(__rhs.precision());
96 this->tie(__rhs.tie());
97 this->fill(__rhs.fill());
98 // The next is required to be the last assignment.
99 this->exceptions(__rhs.exceptions());
100
101 _M_call_callbacks(copyfmt_event);
102 return *this;
103 }
104
105 template<typename _CharT, typename _Traits>
106 char
107 basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
108 {
109 char __ret = __dfault;
110 if (_M_check_facet(_M_ios_fctype))
111 __ret = _M_ios_fctype->narrow(__c, __dfault);
112 return __ret;
113 }
114
115 template<typename _CharT, typename _Traits>
116 _CharT
117 basic_ios<_CharT, _Traits>::widen(char __c) const
118 {
119 char_type __ret = char_type();
120 if (_M_check_facet(_M_ios_fctype))
121 __ret = _M_ios_fctype->widen(__c);
122 return __ret;
123 }
124
125 // Locales:
126 template<typename _CharT, typename _Traits>
127 locale
128 basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
129 {
130 locale __old(this->getloc());
131 ios_base::imbue(__loc);
132 _M_cache_facets(__loc);
133 if (this->rdbuf() != 0)
134 this->rdbuf()->pubimbue(__loc);
135 return __old;
136 }
137
138 template<typename _CharT, typename _Traits>
139 void
140 basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
141 {
142 // NB: This may be called more than once on the same object.
143 ios_base::_M_init();
144 _M_cache_facets(_M_ios_locale);
145 _M_tie = 0;
146
147 // NB: The 27.4.4.1 Postconditions Table only specifies
148 // requirements after basic_ios::init() has been called. As part
149 // of this, fill() must return widen(' '), which needs an imbued
150 // ctype facet of char_type to return without throwing an
151 // exception. This is not a required facet, so streams with
152 // char_type != [char, wchar_t] will not have it by
153 // default. However, because fill()'s signature is const, this
154 // data member cannot be lazily initialized. Thus, thoughts of
155 // using a non-const helper function in ostream inserters is
156 // really besides the point.
157 _M_fill = this->widen(' ');
158
159 _M_exception = goodbit;
160 _M_streambuf = __sb;
161 _M_streambuf_state = __sb ? goodbit : badbit;
162 }
163
164 template<typename _CharT, typename _Traits>
165 void
166 basic_ios<_CharT, _Traits>::_M_cache_facets(const locale& __loc)
167 {
168 if (has_facet<__ctype_type>(__loc))
169 _M_ios_fctype = &use_facet<__ctype_type>(__loc);
170 else
171 _M_ios_fctype = 0;
172 // Should be filled in by ostream and istream, respectively.
173 if (has_facet<__numput_type>(__loc))
174 _M_fnumput = &use_facet<__numput_type>(__loc);
175 else
176 _M_fnumput = 0;
177 if (has_facet<__numget_type>(__loc))
178 _M_fnumget = &use_facet<__numget_type>(__loc);
179 else
180 _M_fnumget = 0;
181 }
182
183 // Inhibit implicit instantiations for required instantiations,
184 // which are defined via explicit instantiations elsewhere.
185 // NB: This syntax is a GNU extension.
186 extern template class basic_ios<char>;
187 extern template class basic_ios<wchar_t>;
188 } // namespace std
189
190 #endif