Import of virgin gcc 4.0.0 distribution.
[dragonfly.git] / contrib / gcc-4.0 / libstdc++-v3 / include / bits / basic_ios.tcc
1 // basic_ios member functions -*- C++ -*-
2
3 // Copyright (C) 1999, 2001, 2002, 2003, 2004 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 /** @file basic_ios.tcc
31  *  This is an internal header file, included by other library headers.
32  *  You should not attempt to use it directly.
33  */
34
35 #ifndef _BASIC_IOS_TCC
36 #define _BASIC_IOS_TCC 1
37
38 #pragma GCC system_header
39
40 namespace std
41 {
42   template<typename _CharT, typename _Traits>
43     void
44     basic_ios<_CharT, _Traits>::clear(iostate __state)
45     {
46       if (this->rdbuf())
47         _M_streambuf_state = __state;
48       else
49           _M_streambuf_state = __state | badbit;
50       if (this->exceptions() & this->rdstate())
51         __throw_ios_failure(__N("basic_ios::clear"));
52     }
53
54   template<typename _CharT, typename _Traits>
55     basic_streambuf<_CharT, _Traits>*
56     basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
57     {
58       basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
59       _M_streambuf = __sb;
60       this->clear();
61       return __old;
62     }
63
64   template<typename _CharT, typename _Traits>
65     basic_ios<_CharT, _Traits>&
66     basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
67     {
68       // _GLIBCXX_RESOLVE_LIB_DEFECTS
69       // 292. effects of a.copyfmt (a)
70       if (this != &__rhs)
71         {
72           // Per 27.1.1, do not call imbue, yet must trash all caches
73           // associated with imbue()
74
75           // Alloc any new word array first, so if it fails we have "rollback".
76           _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
77                              _M_local_word : new _Words[__rhs._M_word_size];
78
79           // Bump refs before doing callbacks, for safety.
80           _Callback_list* __cb = __rhs._M_callbacks;
81           if (__cb)
82             __cb->_M_add_reference();
83           _M_call_callbacks(erase_event);
84           if (_M_word != _M_local_word)
85             {
86               delete [] _M_word;
87               _M_word = 0;
88             }
89           _M_dispose_callbacks();
90
91           // NB: Don't want any added during above.
92           _M_callbacks = __cb;
93           for (int __i = 0; __i < __rhs._M_word_size; ++__i)
94             __words[__i] = __rhs._M_word[__i];
95           _M_word = __words;
96           _M_word_size = __rhs._M_word_size;
97
98           this->flags(__rhs.flags());
99           this->width(__rhs.width());
100           this->precision(__rhs.precision());
101           this->tie(__rhs.tie());
102           this->fill(__rhs.fill());
103           _M_ios_locale = __rhs.getloc();
104           _M_cache_locale(_M_ios_locale);
105
106           _M_call_callbacks(copyfmt_event);
107
108           // The next is required to be the last assignment.
109           this->exceptions(__rhs.exceptions());
110         }
111       return *this;
112     }
113
114   template<typename _CharT, typename _Traits>
115     char
116     basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
117     { return __check_facet(_M_ctype).narrow(__c, __dfault); }
118
119   template<typename _CharT, typename _Traits>
120     _CharT
121     basic_ios<_CharT, _Traits>::widen(char __c) const
122     { return __check_facet(_M_ctype).widen(__c); }
123
124   // Locales:
125   template<typename _CharT, typename _Traits>
126     locale
127     basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
128     {
129       locale __old(this->getloc());
130       ios_base::imbue(__loc);
131       _M_cache_locale(__loc);
132       if (this->rdbuf() != 0)
133         this->rdbuf()->pubimbue(__loc);
134       return __old;
135     }
136
137   template<typename _CharT, typename _Traits>
138     void
139     basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
140     {
141       // NB: This may be called more than once on the same object.
142       ios_base::_M_init();
143
144       // Cache locale data and specific facets used by iostreams.
145       _M_cache_locale(_M_ios_locale);
146
147       // NB: The 27.4.4.1 Postconditions Table specifies requirements
148       // after basic_ios::init() has been called. As part of this,
149       // fill() must return widen(' ') any time after init() has been
150       // called, which needs an imbued ctype facet of char_type to
151       // return without throwing an exception. Unfortunately,
152       // ctype<char_type> is not necessarily a required facet, so
153       // streams with char_type != [char, wchar_t] will not have it by
154       // default. Because of this, the correct value for _M_fill is
155       // constructed on the first call of fill(). That way,
156       // unformatted input and output with non-required basic_ios
157       // instantiations is possible even without imbuing the expected
158       // ctype<char_type> facet.
159       _M_fill = _CharT();
160       _M_fill_init = false;
161
162       _M_tie = 0;
163       _M_exception = goodbit;
164       _M_streambuf = __sb;
165       _M_streambuf_state = __sb ? goodbit : badbit;
166     }
167
168   template<typename _CharT, typename _Traits>
169     void
170     basic_ios<_CharT, _Traits>::_M_cache_locale(const locale& __loc)
171     {
172       if (__builtin_expect(has_facet<__ctype_type>(__loc), true))
173         _M_ctype = &use_facet<__ctype_type>(__loc);
174       else
175         _M_ctype = 0;
176
177       if (__builtin_expect(has_facet<__num_put_type>(__loc), true))
178         _M_num_put = &use_facet<__num_put_type>(__loc);
179       else
180         _M_num_put = 0;
181
182       if (__builtin_expect(has_facet<__num_get_type>(__loc), true))
183         _M_num_get = &use_facet<__num_get_type>(__loc);
184       else
185         _M_num_get = 0;
186     }
187
188   // Inhibit implicit instantiations for required instantiations,
189   // which are defined via explicit instantiations elsewhere.
190   // NB:  This syntax is a GNU extension.
191 #if _GLIBCXX_EXTERN_TEMPLATE
192   extern template class basic_ios<char>;
193
194 #ifdef _GLIBCXX_USE_WCHAR_T
195   extern template class basic_ios<wchar_t>;
196 #endif
197 #endif
198 } // namespace std
199
200 #endif