1 // ostream classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 2, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
23 // As a special exception, you may use this file as part of a free software
24 // library without restriction. Specifically, if other files instantiate
25 // templates or use macros or inline functions from this file, or you compile
26 // this file and link it with other files to produce an executable, this
27 // file does not by itself cause the resulting executable to be covered by
28 // the GNU General Public License. This exception does not however
29 // invalidate any other reasons why the executable file might be covered by
30 // the GNU General Public License.
33 * This is an internal header file, included by other library headers.
34 * You should not attempt to use it directly.
38 // ISO C++ 14882: 27.6.2 Output streams
42 #define _OSTREAM_TCC 1
44 #pragma GCC system_header
48 _GLIBCXX_BEGIN_NAMESPACE(std)
50 template<typename _CharT, typename _Traits>
51 basic_ostream<_CharT, _Traits>::sentry::
52 sentry(basic_ostream<_CharT, _Traits>& __os)
53 : _M_ok(false), _M_os(__os)
56 if (__os.tie() && __os.good())
62 __os.setstate(ios_base::failbit);
65 template<typename _CharT, typename _Traits>
66 template<typename _ValueT>
67 basic_ostream<_CharT, _Traits>&
68 basic_ostream<_CharT, _Traits>::
69 _M_insert(_ValueT __v)
74 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
77 const __num_put_type& __np = __check_facet(this->_M_num_put);
78 if (__np.put(*this, *this, this->fill(), __v).failed())
79 __err |= ios_base::badbit;
82 { this->_M_setstate(ios_base::badbit); }
84 this->setstate(__err);
89 template<typename _CharT, typename _Traits>
90 basic_ostream<_CharT, _Traits>&
91 basic_ostream<_CharT, _Traits>::
94 // _GLIBCXX_RESOLVE_LIB_DEFECTS
95 // 117. basic_ostream uses nonexistent num_put member functions.
96 const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
97 if (__fmt == ios_base::oct || __fmt == ios_base::hex)
98 return _M_insert(static_cast<long>(static_cast<unsigned short>(__n)));
100 return _M_insert(static_cast<long>(__n));
103 template<typename _CharT, typename _Traits>
104 basic_ostream<_CharT, _Traits>&
105 basic_ostream<_CharT, _Traits>::
108 // _GLIBCXX_RESOLVE_LIB_DEFECTS
109 // 117. basic_ostream uses nonexistent num_put member functions.
110 const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
111 if (__fmt == ios_base::oct || __fmt == ios_base::hex)
112 return _M_insert(static_cast<long>(static_cast<unsigned int>(__n)));
114 return _M_insert(static_cast<long>(__n));
117 template<typename _CharT, typename _Traits>
118 basic_ostream<_CharT, _Traits>&
119 basic_ostream<_CharT, _Traits>::
120 operator<<(__streambuf_type* __sbin)
122 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
123 sentry __cerb(*this);
124 if (__cerb && __sbin)
128 if (!__copy_streambufs(__sbin, this->rdbuf()))
129 __err |= ios_base::failbit;
132 { this->_M_setstate(ios_base::failbit); }
135 __err |= ios_base::badbit;
137 this->setstate(__err);
141 template<typename _CharT, typename _Traits>
142 basic_ostream<_CharT, _Traits>&
143 basic_ostream<_CharT, _Traits>::
146 // _GLIBCXX_RESOLVE_LIB_DEFECTS
147 // DR 60. What is a formatted input function?
148 // basic_ostream::put(char_type) is an unformatted output function.
149 // DR 63. Exception-handling policy for unformatted output.
150 // Unformatted output functions should catch exceptions thrown
151 // from streambuf members.
152 sentry __cerb(*this);
155 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
158 const int_type __put = this->rdbuf()->sputc(__c);
159 if (traits_type::eq_int_type(__put, traits_type::eof()))
160 __err |= ios_base::badbit;
163 { this->_M_setstate(ios_base::badbit); }
165 this->setstate(__err);
170 template<typename _CharT, typename _Traits>
171 basic_ostream<_CharT, _Traits>&
172 basic_ostream<_CharT, _Traits>::
173 write(const _CharT* __s, streamsize __n)
175 // _GLIBCXX_RESOLVE_LIB_DEFECTS
176 // DR 60. What is a formatted input function?
177 // basic_ostream::write(const char_type*, streamsize) is an
178 // unformatted output function.
179 // DR 63. Exception-handling policy for unformatted output.
180 // Unformatted output functions should catch exceptions thrown
181 // from streambuf members.
182 sentry __cerb(*this);
186 { _M_write(__s, __n); }
188 { this->_M_setstate(ios_base::badbit); }
193 template<typename _CharT, typename _Traits>
194 basic_ostream<_CharT, _Traits>&
195 basic_ostream<_CharT, _Traits>::
198 // _GLIBCXX_RESOLVE_LIB_DEFECTS
199 // DR 60. What is a formatted input function?
200 // basic_ostream::flush() is *not* an unformatted output function.
201 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
204 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
205 __err |= ios_base::badbit;
208 { this->_M_setstate(ios_base::badbit); }
210 this->setstate(__err);
214 template<typename _CharT, typename _Traits>
215 typename basic_ostream<_CharT, _Traits>::pos_type
216 basic_ostream<_CharT, _Traits>::
219 pos_type __ret = pos_type(-1);
223 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
226 { this->_M_setstate(ios_base::badbit); }
230 template<typename _CharT, typename _Traits>
231 basic_ostream<_CharT, _Traits>&
232 basic_ostream<_CharT, _Traits>::
233 seekp(pos_type __pos)
235 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
240 // _GLIBCXX_RESOLVE_LIB_DEFECTS
241 // 136. seekp, seekg setting wrong streams?
242 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
245 // 129. Need error indication from seekp() and seekg()
246 if (__p == pos_type(off_type(-1)))
247 __err |= ios_base::failbit;
251 { this->_M_setstate(ios_base::badbit); }
253 this->setstate(__err);
257 template<typename _CharT, typename _Traits>
258 basic_ostream<_CharT, _Traits>&
259 basic_ostream<_CharT, _Traits>::
260 seekp(off_type __off, ios_base::seekdir __dir)
262 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
267 // _GLIBCXX_RESOLVE_LIB_DEFECTS
268 // 136. seekp, seekg setting wrong streams?
269 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
272 // 129. Need error indication from seekp() and seekg()
273 if (__p == pos_type(off_type(-1)))
274 __err |= ios_base::failbit;
278 { this->_M_setstate(ios_base::badbit); }
280 this->setstate(__err);
284 template<typename _CharT, typename _Traits>
285 basic_ostream<_CharT, _Traits>&
286 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
289 __out.setstate(ios_base::badbit);
292 // _GLIBCXX_RESOLVE_LIB_DEFECTS
293 // 167. Improper use of traits_type::length()
294 const size_t __clen = char_traits<char>::length(__s);
298 __ws = new _CharT[__clen];
299 for (size_t __i = 0; __i < __clen; ++__i)
300 __ws[__i] = __out.widen(__s[__i]);
305 __out._M_setstate(ios_base::badbit);
311 __ostream_insert(__out, __ws, __clen);
317 __throw_exception_again;
323 // Inhibit implicit instantiations for required instantiations,
324 // which are defined via explicit instantiations elsewhere.
325 // NB: This syntax is a GNU extension.
326 #if _GLIBCXX_EXTERN_TEMPLATE
327 extern template class basic_ostream<char>;
328 extern template ostream& endl(ostream&);
329 extern template ostream& ends(ostream&);
330 extern template ostream& flush(ostream&);
331 extern template ostream& operator<<(ostream&, char);
332 extern template ostream& operator<<(ostream&, unsigned char);
333 extern template ostream& operator<<(ostream&, signed char);
334 extern template ostream& operator<<(ostream&, const char*);
335 extern template ostream& operator<<(ostream&, const unsigned char*);
336 extern template ostream& operator<<(ostream&, const signed char*);
338 extern template ostream& ostream::_M_insert(long);
339 extern template ostream& ostream::_M_insert(unsigned long);
340 extern template ostream& ostream::_M_insert(bool);
341 #ifdef _GLIBCXX_USE_LONG_LONG
342 extern template ostream& ostream::_M_insert(long long);
343 extern template ostream& ostream::_M_insert(unsigned long long);
345 extern template ostream& ostream::_M_insert(double);
346 extern template ostream& ostream::_M_insert(long double);
347 extern template ostream& ostream::_M_insert(const void*);
349 #ifdef _GLIBCXX_USE_WCHAR_T
350 extern template class basic_ostream<wchar_t>;
351 extern template wostream& endl(wostream&);
352 extern template wostream& ends(wostream&);
353 extern template wostream& flush(wostream&);
354 extern template wostream& operator<<(wostream&, wchar_t);
355 extern template wostream& operator<<(wostream&, char);
356 extern template wostream& operator<<(wostream&, const wchar_t*);
357 extern template wostream& operator<<(wostream&, const char*);
359 extern template wostream& wostream::_M_insert(long);
360 extern template wostream& wostream::_M_insert(unsigned long);
361 extern template wostream& wostream::_M_insert(bool);
362 #ifdef _GLIBCXX_USE_LONG_LONG
363 extern template wostream& wostream::_M_insert(long long);
364 extern template wostream& wostream::_M_insert(unsigned long long);
366 extern template wostream& wostream::_M_insert(double);
367 extern template wostream& wostream::_M_insert(long double);
368 extern template wostream& wostream::_M_insert(const void*);
372 _GLIBCXX_END_NAMESPACE