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
46 #include <cxxabi-internal.h>
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;
81 catch(__cxxabiv1::__forced_unwind&)
83 this->_M_setstate(ios_base::badbit);
84 __throw_exception_again;
87 { this->_M_setstate(ios_base::badbit); }
89 this->setstate(__err);
94 template<typename _CharT, typename _Traits>
95 basic_ostream<_CharT, _Traits>&
96 basic_ostream<_CharT, _Traits>::
99 // _GLIBCXX_RESOLVE_LIB_DEFECTS
100 // 117. basic_ostream uses nonexistent num_put member functions.
101 const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
102 if (__fmt == ios_base::oct || __fmt == ios_base::hex)
103 return _M_insert(static_cast<long>(static_cast<unsigned short>(__n)));
105 return _M_insert(static_cast<long>(__n));
108 template<typename _CharT, typename _Traits>
109 basic_ostream<_CharT, _Traits>&
110 basic_ostream<_CharT, _Traits>::
113 // _GLIBCXX_RESOLVE_LIB_DEFECTS
114 // 117. basic_ostream uses nonexistent num_put member functions.
115 const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
116 if (__fmt == ios_base::oct || __fmt == ios_base::hex)
117 return _M_insert(static_cast<long>(static_cast<unsigned int>(__n)));
119 return _M_insert(static_cast<long>(__n));
122 template<typename _CharT, typename _Traits>
123 basic_ostream<_CharT, _Traits>&
124 basic_ostream<_CharT, _Traits>::
125 operator<<(__streambuf_type* __sbin)
127 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
128 sentry __cerb(*this);
129 if (__cerb && __sbin)
133 if (!__copy_streambufs(__sbin, this->rdbuf()))
134 __err |= ios_base::failbit;
136 catch(__cxxabiv1::__forced_unwind&)
138 this->_M_setstate(ios_base::badbit);
139 __throw_exception_again;
142 { this->_M_setstate(ios_base::failbit); }
145 __err |= ios_base::badbit;
147 this->setstate(__err);
151 template<typename _CharT, typename _Traits>
152 basic_ostream<_CharT, _Traits>&
153 basic_ostream<_CharT, _Traits>::
156 // _GLIBCXX_RESOLVE_LIB_DEFECTS
157 // DR 60. What is a formatted input function?
158 // basic_ostream::put(char_type) is an unformatted output function.
159 // DR 63. Exception-handling policy for unformatted output.
160 // Unformatted output functions should catch exceptions thrown
161 // from streambuf members.
162 sentry __cerb(*this);
165 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
168 const int_type __put = this->rdbuf()->sputc(__c);
169 if (traits_type::eq_int_type(__put, traits_type::eof()))
170 __err |= ios_base::badbit;
172 catch(__cxxabiv1::__forced_unwind&)
174 this->_M_setstate(ios_base::badbit);
175 __throw_exception_again;
178 { this->_M_setstate(ios_base::badbit); }
180 this->setstate(__err);
185 template<typename _CharT, typename _Traits>
186 basic_ostream<_CharT, _Traits>&
187 basic_ostream<_CharT, _Traits>::
188 write(const _CharT* __s, streamsize __n)
190 // _GLIBCXX_RESOLVE_LIB_DEFECTS
191 // DR 60. What is a formatted input function?
192 // basic_ostream::write(const char_type*, streamsize) is an
193 // unformatted output function.
194 // DR 63. Exception-handling policy for unformatted output.
195 // Unformatted output functions should catch exceptions thrown
196 // from streambuf members.
197 sentry __cerb(*this);
201 { _M_write(__s, __n); }
202 catch(__cxxabiv1::__forced_unwind&)
204 this->_M_setstate(ios_base::badbit);
205 __throw_exception_again;
208 { this->_M_setstate(ios_base::badbit); }
213 template<typename _CharT, typename _Traits>
214 basic_ostream<_CharT, _Traits>&
215 basic_ostream<_CharT, _Traits>::
218 // _GLIBCXX_RESOLVE_LIB_DEFECTS
219 // DR 60. What is a formatted input function?
220 // basic_ostream::flush() is *not* an unformatted output function.
221 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
224 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
225 __err |= ios_base::badbit;
227 catch(__cxxabiv1::__forced_unwind&)
229 this->_M_setstate(ios_base::badbit);
230 __throw_exception_again;
233 { this->_M_setstate(ios_base::badbit); }
235 this->setstate(__err);
239 template<typename _CharT, typename _Traits>
240 typename basic_ostream<_CharT, _Traits>::pos_type
241 basic_ostream<_CharT, _Traits>::
244 pos_type __ret = pos_type(-1);
248 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
250 catch(__cxxabiv1::__forced_unwind&)
252 this->_M_setstate(ios_base::badbit);
253 __throw_exception_again;
256 { this->_M_setstate(ios_base::badbit); }
260 template<typename _CharT, typename _Traits>
261 basic_ostream<_CharT, _Traits>&
262 basic_ostream<_CharT, _Traits>::
263 seekp(pos_type __pos)
265 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
270 // _GLIBCXX_RESOLVE_LIB_DEFECTS
271 // 136. seekp, seekg setting wrong streams?
272 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
275 // 129. Need error indication from seekp() and seekg()
276 if (__p == pos_type(off_type(-1)))
277 __err |= ios_base::failbit;
280 catch(__cxxabiv1::__forced_unwind&)
282 this->_M_setstate(ios_base::badbit);
283 __throw_exception_again;
286 { this->_M_setstate(ios_base::badbit); }
288 this->setstate(__err);
292 template<typename _CharT, typename _Traits>
293 basic_ostream<_CharT, _Traits>&
294 basic_ostream<_CharT, _Traits>::
295 seekp(off_type __off, ios_base::seekdir __dir)
297 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
302 // _GLIBCXX_RESOLVE_LIB_DEFECTS
303 // 136. seekp, seekg setting wrong streams?
304 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
307 // 129. Need error indication from seekp() and seekg()
308 if (__p == pos_type(off_type(-1)))
309 __err |= ios_base::failbit;
312 catch(__cxxabiv1::__forced_unwind&)
314 this->_M_setstate(ios_base::badbit);
315 __throw_exception_again;
318 { this->_M_setstate(ios_base::badbit); }
320 this->setstate(__err);
324 template<typename _CharT, typename _Traits>
325 basic_ostream<_CharT, _Traits>&
326 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
329 __out.setstate(ios_base::badbit);
332 // _GLIBCXX_RESOLVE_LIB_DEFECTS
333 // 167. Improper use of traits_type::length()
334 const size_t __clen = char_traits<char>::length(__s);
340 __ptr_guard (_CharT *__ip): __p(__ip) { }
341 ~__ptr_guard() { delete[] __p; }
342 _CharT* __get() { return __p; }
343 } __pg (new _CharT[__clen]);
345 _CharT *__ws = __pg.__get();
346 for (size_t __i = 0; __i < __clen; ++__i)
347 __ws[__i] = __out.widen(__s[__i]);
348 __ostream_insert(__out, __ws, __clen);
350 catch(__cxxabiv1::__forced_unwind&)
352 __out._M_setstate(ios_base::badbit);
353 __throw_exception_again;
356 { __out._M_setstate(ios_base::badbit); }
361 // Inhibit implicit instantiations for required instantiations,
362 // which are defined via explicit instantiations elsewhere.
363 // NB: This syntax is a GNU extension.
364 #if _GLIBCXX_EXTERN_TEMPLATE
365 extern template class basic_ostream<char>;
366 extern template ostream& endl(ostream&);
367 extern template ostream& ends(ostream&);
368 extern template ostream& flush(ostream&);
369 extern template ostream& operator<<(ostream&, char);
370 extern template ostream& operator<<(ostream&, unsigned char);
371 extern template ostream& operator<<(ostream&, signed char);
372 extern template ostream& operator<<(ostream&, const char*);
373 extern template ostream& operator<<(ostream&, const unsigned char*);
374 extern template ostream& operator<<(ostream&, const signed char*);
376 extern template ostream& ostream::_M_insert(long);
377 extern template ostream& ostream::_M_insert(unsigned long);
378 extern template ostream& ostream::_M_insert(bool);
379 #ifdef _GLIBCXX_USE_LONG_LONG
380 extern template ostream& ostream::_M_insert(long long);
381 extern template ostream& ostream::_M_insert(unsigned long long);
383 extern template ostream& ostream::_M_insert(double);
384 extern template ostream& ostream::_M_insert(long double);
385 extern template ostream& ostream::_M_insert(const void*);
387 #ifdef _GLIBCXX_USE_WCHAR_T
388 extern template class basic_ostream<wchar_t>;
389 extern template wostream& endl(wostream&);
390 extern template wostream& ends(wostream&);
391 extern template wostream& flush(wostream&);
392 extern template wostream& operator<<(wostream&, wchar_t);
393 extern template wostream& operator<<(wostream&, char);
394 extern template wostream& operator<<(wostream&, const wchar_t*);
395 extern template wostream& operator<<(wostream&, const char*);
397 extern template wostream& wostream::_M_insert(long);
398 extern template wostream& wostream::_M_insert(unsigned long);
399 extern template wostream& wostream::_M_insert(bool);
400 #ifdef _GLIBCXX_USE_LONG_LONG
401 extern template wostream& wostream::_M_insert(long long);
402 extern template wostream& wostream::_M_insert(unsigned long long);
404 extern template wostream& wostream::_M_insert(double);
405 extern template wostream& wostream::_M_insert(long double);
406 extern template wostream& wostream::_M_insert(const void*);
410 _GLIBCXX_END_NAMESPACE