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