1 // basic_ios locale and locale-related member functions -*- C++ -*-
3 // Copyright (C) 1999, 2001, 2002 Free Software Foundation, Inc.
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)
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.
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,
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.
30 #ifndef _CPP_BITS_BASICIOS_TCC
31 #define _CPP_BITS_BASICIOS_TCC 1
33 #pragma GCC system_header
37 template<typename _CharT, typename _Traits>
39 basic_ios<_CharT, _Traits>::clear(iostate __state)
42 _M_streambuf_state = __state;
44 _M_streambuf_state = __state | badbit;
45 if ((this->rdstate() & this->exceptions()))
46 __throw_ios_failure("basic_ios::clear(iostate) caused exception");
49 template<typename _CharT, typename _Traits>
50 basic_streambuf<_CharT, _Traits>*
51 basic_ios<_CharT, _Traits>::rdbuf(basic_streambuf<_CharT, _Traits>* __sb)
53 basic_streambuf<_CharT, _Traits>* __old = _M_streambuf;
59 template<typename _CharT, typename _Traits>
60 basic_ios<_CharT, _Traits>&
61 basic_ios<_CharT, _Traits>::copyfmt(const basic_ios& __rhs)
63 // Per 27.1.1.1, do not call imbue, yet must trash all caches
64 // associated with imbue()
66 // Alloc any new word array first, so if it fails we have "rollback".
67 _Words* __words = (__rhs._M_word_size <= _S_local_word_size) ?
68 _M_local_word : new _Words[__rhs._M_word_size];
70 // Bump refs before doing callbacks, for safety.
71 _Callback_list* __cb = __rhs._M_callbacks;
73 __cb->_M_add_reference();
74 _M_call_callbacks(erase_event);
75 if (_M_word != _M_local_word)
80 _M_dispose_callbacks();
82 _M_callbacks = __cb; // NB: Don't want any added during above.
83 for (int __i = 0; __i < __rhs._M_word_size; ++__i)
84 __words[__i] = __rhs._M_word[__i];
85 if (_M_word != _M_local_word)
91 _M_word_size = __rhs._M_word_size;
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());
101 _M_call_callbacks(copyfmt_event);
105 template<typename _CharT, typename _Traits>
107 basic_ios<_CharT, _Traits>::narrow(char_type __c, char __dfault) const
109 char __ret = __dfault;
110 if (_M_check_facet(_M_fctype))
111 __ret = _M_fctype->narrow(__c, __dfault);
115 template<typename _CharT, typename _Traits>
117 basic_ios<_CharT, _Traits>::widen(char __c) const
119 char_type __ret = char_type();
120 if (_M_check_facet(_M_fctype))
121 __ret = _M_fctype->widen(__c);
126 template<typename _CharT, typename _Traits>
128 basic_ios<_CharT, _Traits>::imbue(const locale& __loc)
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);
138 template<typename _CharT, typename _Traits>
140 basic_ios<_CharT, _Traits>::init(basic_streambuf<_CharT, _Traits>* __sb)
142 // NB: This may be called more than once on the same object.
144 _M_cache_facets(_M_ios_locale);
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.
160 _M_fill_init = false;
162 _M_exception = goodbit;
164 _M_streambuf_state = __sb ? goodbit : badbit;
167 template<typename _CharT, typename _Traits>
169 basic_ios<_CharT, _Traits>::_M_cache_facets(const locale& __loc)
171 if (has_facet<__ctype_type>(__loc))
172 _M_fctype = &use_facet<__ctype_type>(__loc);
175 // Should be filled in by ostream and istream, respectively.
176 if (has_facet<__numput_type>(__loc))
177 _M_fnumput = &use_facet<__numput_type>(__loc);
180 if (has_facet<__numget_type>(__loc))
181 _M_fnumget = &use_facet<__numget_type>(__loc);
186 // Inhibit implicit instantiations for required instantiations,
187 // which are defined via explicit instantiations elsewhere.
188 // NB: This syntax is a GNU extension.
189 extern template class basic_ios<char>;
190 extern template class basic_ios<wchar_t>;