1 // ostream classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006
4 // Free Software Foundation, Inc.
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
37 // ISO C++ 14882: 27.6.2 Output streams
41 #define _OSTREAM_TCC 1
43 #pragma GCC system_header
47 _GLIBCXX_BEGIN_NAMESPACE(std)
49 template<typename _CharT, typename _Traits>
50 basic_ostream<_CharT, _Traits>::sentry::
51 sentry(basic_ostream<_CharT, _Traits>& __os)
52 : _M_ok(false), _M_os(__os)
55 if (__os.tie() && __os.good())
61 __os.setstate(ios_base::failbit);
64 template<typename _CharT, typename _Traits>
65 basic_ostream<_CharT, _Traits>&
66 basic_ostream<_CharT, _Traits>::
67 operator<<(__ostream_type& (*__pf)(__ostream_type&))
69 // _GLIBCXX_RESOLVE_LIB_DEFECTS
70 // DR 60. What is a formatted input function?
71 // The inserters for manipulators are *not* formatted output functions.
75 template<typename _CharT, typename _Traits>
76 basic_ostream<_CharT, _Traits>&
77 basic_ostream<_CharT, _Traits>::
78 operator<<(__ios_type& (*__pf)(__ios_type&))
80 // _GLIBCXX_RESOLVE_LIB_DEFECTS
81 // DR 60. What is a formatted input function?
82 // The inserters for manipulators are *not* formatted output functions.
87 template<typename _CharT, typename _Traits>
88 basic_ostream<_CharT, _Traits>&
89 basic_ostream<_CharT, _Traits>::
90 operator<<(ios_base& (*__pf)(ios_base&))
92 // _GLIBCXX_RESOLVE_LIB_DEFECTS
93 // DR 60. What is a formatted input function?
94 // The inserters for manipulators are *not* formatted output functions.
99 template<typename _CharT, typename _Traits>
100 template<typename _ValueT>
101 basic_ostream<_CharT, _Traits>&
102 basic_ostream<_CharT, _Traits>::
103 _M_insert(_ValueT __v)
105 sentry __cerb(*this);
108 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
111 const __num_put_type& __np = __check_facet(this->_M_num_put);
112 if (__np.put(*this, *this, this->fill(), __v).failed())
113 __err |= ios_base::badbit;
116 { this->_M_setstate(ios_base::badbit); }
118 this->setstate(__err);
123 template<typename _CharT, typename _Traits>
124 basic_ostream<_CharT, _Traits>&
125 basic_ostream<_CharT, _Traits>::
126 operator<<(short __n)
128 // _GLIBCXX_RESOLVE_LIB_DEFECTS
129 // 117. basic_ostream uses nonexistent num_put member functions.
130 const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
131 if (__fmt == ios_base::oct || __fmt == ios_base::hex)
132 return _M_insert(static_cast<long>(static_cast<unsigned short>(__n)));
134 return _M_insert(static_cast<long>(__n));
137 template<typename _CharT, typename _Traits>
138 basic_ostream<_CharT, _Traits>&
139 basic_ostream<_CharT, _Traits>::
142 // _GLIBCXX_RESOLVE_LIB_DEFECTS
143 // 117. basic_ostream uses nonexistent num_put member functions.
144 const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
145 if (__fmt == ios_base::oct || __fmt == ios_base::hex)
146 return _M_insert(static_cast<long>(static_cast<unsigned int>(__n)));
148 return _M_insert(static_cast<long>(__n));
151 template<typename _CharT, typename _Traits>
152 basic_ostream<_CharT, _Traits>&
153 basic_ostream<_CharT, _Traits>::
154 operator<<(__streambuf_type* __sbin)
156 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
157 sentry __cerb(*this);
158 if (__cerb && __sbin)
162 if (!__copy_streambufs(__sbin, this->rdbuf()))
163 __err |= ios_base::failbit;
166 { this->_M_setstate(ios_base::failbit); }
169 __err |= ios_base::badbit;
171 this->setstate(__err);
175 template<typename _CharT, typename _Traits>
176 basic_ostream<_CharT, _Traits>&
177 basic_ostream<_CharT, _Traits>::
180 // _GLIBCXX_RESOLVE_LIB_DEFECTS
181 // DR 60. What is a formatted input function?
182 // basic_ostream::put(char_type) is an unformatted output function.
183 // DR 63. Exception-handling policy for unformatted output.
184 // Unformatted output functions should catch exceptions thrown
185 // from streambuf members.
186 sentry __cerb(*this);
189 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
192 const int_type __put = this->rdbuf()->sputc(__c);
193 if (traits_type::eq_int_type(__put, traits_type::eof()))
194 __err |= ios_base::badbit;
197 { this->_M_setstate(ios_base::badbit); }
199 this->setstate(__err);
204 template<typename _CharT, typename _Traits>
205 basic_ostream<_CharT, _Traits>&
206 basic_ostream<_CharT, _Traits>::
207 write(const _CharT* __s, streamsize __n)
209 // _GLIBCXX_RESOLVE_LIB_DEFECTS
210 // DR 60. What is a formatted input function?
211 // basic_ostream::write(const char_type*, streamsize) is an
212 // unformatted output function.
213 // DR 63. Exception-handling policy for unformatted output.
214 // Unformatted output functions should catch exceptions thrown
215 // from streambuf members.
216 sentry __cerb(*this);
220 { _M_write(__s, __n); }
222 { this->_M_setstate(ios_base::badbit); }
227 template<typename _CharT, typename _Traits>
228 basic_ostream<_CharT, _Traits>&
229 basic_ostream<_CharT, _Traits>::
232 // _GLIBCXX_RESOLVE_LIB_DEFECTS
233 // DR 60. What is a formatted input function?
234 // basic_ostream::flush() is *not* an unformatted output function.
235 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
238 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
239 __err |= ios_base::badbit;
242 { this->_M_setstate(ios_base::badbit); }
244 this->setstate(__err);
248 template<typename _CharT, typename _Traits>
249 typename basic_ostream<_CharT, _Traits>::pos_type
250 basic_ostream<_CharT, _Traits>::
253 pos_type __ret = pos_type(-1);
257 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
260 { this->_M_setstate(ios_base::badbit); }
264 template<typename _CharT, typename _Traits>
265 basic_ostream<_CharT, _Traits>&
266 basic_ostream<_CharT, _Traits>::
267 seekp(pos_type __pos)
269 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
274 // _GLIBCXX_RESOLVE_LIB_DEFECTS
275 // 136. seekp, seekg setting wrong streams?
276 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
279 // 129. Need error indication from seekp() and seekg()
280 if (__p == pos_type(off_type(-1)))
281 __err |= ios_base::failbit;
285 { this->_M_setstate(ios_base::badbit); }
287 this->setstate(__err);
291 template<typename _CharT, typename _Traits>
292 basic_ostream<_CharT, _Traits>&
293 basic_ostream<_CharT, _Traits>::
294 seekp(off_type __off, ios_base::seekdir __dir)
296 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
301 // _GLIBCXX_RESOLVE_LIB_DEFECTS
302 // 136. seekp, seekg setting wrong streams?
303 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
306 // 129. Need error indication from seekp() and seekg()
307 if (__p == pos_type(off_type(-1)))
308 __err |= ios_base::failbit;
312 { this->_M_setstate(ios_base::badbit); }
314 this->setstate(__err);
318 // 27.6.2.5.4 Character inserters.
319 template<typename _CharT, typename _Traits>
320 basic_ostream<_CharT, _Traits>&
321 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
323 typedef basic_ostream<_CharT, _Traits> __ostream_type;
324 typename __ostream_type::sentry __cerb(__out);
329 const streamsize __w = __out.width();
330 streamsize __len = 1;
334 __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
336 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs,
337 &__c, __w, __len, false);
340 __out._M_write(__cs, __len);
344 { __out._M_setstate(ios_base::badbit); }
350 template <class _Traits>
351 basic_ostream<char, _Traits>&
352 operator<<(basic_ostream<char, _Traits>& __out, char __c)
354 typedef basic_ostream<char, _Traits> __ostream_type;
355 typename __ostream_type::sentry __cerb(__out);
360 const streamsize __w = __out.width();
361 streamsize __len = 1;
365 __cs = static_cast<char*>(__builtin_alloca(__w));
366 __pad<char, _Traits>::_S_pad(__out, __out.fill(), __cs,
367 &__c, __w, __len, false);
370 __out._M_write(__cs, __len);
374 { __out._M_setstate(ios_base::badbit); }
379 template<typename _CharT, typename _Traits>
380 basic_ostream<_CharT, _Traits>&
381 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
383 typedef basic_ostream<_CharT, _Traits> __ostream_type;
384 typename __ostream_type::sentry __cerb(__out);
389 const streamsize __w = __out.width();
390 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
393 _CharT* __cs = (static_cast<
394 _CharT*>(__builtin_alloca(sizeof(_CharT)
396 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs,
397 __s, __w, __len, false);
401 __out._M_write(__s, __len);
405 { __out._M_setstate(ios_base::badbit); }
408 __out.setstate(ios_base::badbit);
412 template<typename _CharT, typename _Traits>
413 basic_ostream<_CharT, _Traits>&
414 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
416 typedef basic_ostream<_CharT, _Traits> __ostream_type;
417 // _GLIBCXX_RESOLVE_LIB_DEFECTS
418 // 167. Improper use of traits_type::length()
419 // Note that this is only in 'Review' status.
420 typedef char_traits<char> __traits_type;
421 typename __ostream_type::sentry __cerb(__out);
424 size_t __clen = __traits_type::length(__s);
425 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
427 for (size_t __i = 0; __i < __clen; ++__i)
428 __ws[__i] = __out.widen(__s[__i]);
429 _CharT* __str = __ws;
433 const streamsize __w = __out.width();
434 streamsize __len = static_cast<streamsize>(__clen);
437 _CharT* __cs = (static_cast<
438 _CharT*>(__builtin_alloca(sizeof(_CharT)
440 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs,
441 __ws, __w, __len, false);
445 __out._M_write(__str, __len);
449 { __out._M_setstate(ios_base::badbit); }
452 __out.setstate(ios_base::badbit);
456 // Partial specializations.
457 template<class _Traits>
458 basic_ostream<char, _Traits>&
459 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
461 typedef basic_ostream<char, _Traits> __ostream_type;
462 typename __ostream_type::sentry __cerb(__out);
467 const streamsize __w = __out.width();
468 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
471 char* __cs = static_cast<char*>(__builtin_alloca(__w));
472 __pad<char, _Traits>::_S_pad(__out, __out.fill(), __cs,
473 __s, __w, __len, false);
477 __out._M_write(__s, __len);
481 { __out._M_setstate(ios_base::badbit); }
484 __out.setstate(ios_base::badbit);
488 // 21.3.7.9 basic_string::operator<<
489 template<typename _CharT, typename _Traits, typename _Alloc>
490 basic_ostream<_CharT, _Traits>&
491 operator<<(basic_ostream<_CharT, _Traits>& __out,
492 const basic_string<_CharT, _Traits, _Alloc>& __str)
494 typedef basic_ostream<_CharT, _Traits> __ostream_type;
495 typename __ostream_type::sentry __cerb(__out);
498 const streamsize __w = __out.width();
499 streamsize __len = static_cast<streamsize>(__str.size());
500 const _CharT* __s = __str.data();
502 // _GLIBCXX_RESOLVE_LIB_DEFECTS
503 // 25. String operator<< uses width() value wrong
506 _CharT* __cs = (static_cast<
507 _CharT*>(__builtin_alloca(sizeof(_CharT) * __w)));
508 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs, __s,
513 __out._M_write(__s, __len);
519 // Inhibit implicit instantiations for required instantiations,
520 // which are defined via explicit instantiations elsewhere.
521 // NB: This syntax is a GNU extension.
522 #if _GLIBCXX_EXTERN_TEMPLATE
523 extern template class basic_ostream<char>;
524 extern template ostream& endl(ostream&);
525 extern template ostream& ends(ostream&);
526 extern template ostream& flush(ostream&);
527 extern template ostream& operator<<(ostream&, char);
528 extern template ostream& operator<<(ostream&, unsigned char);
529 extern template ostream& operator<<(ostream&, signed char);
530 extern template ostream& operator<<(ostream&, const char*);
531 extern template ostream& operator<<(ostream&, const unsigned char*);
532 extern template ostream& operator<<(ostream&, const signed char*);
534 #ifdef _GLIBCXX_USE_WCHAR_T
535 extern template class basic_ostream<wchar_t>;
536 extern template wostream& endl(wostream&);
537 extern template wostream& ends(wostream&);
538 extern template wostream& flush(wostream&);
539 extern template wostream& operator<<(wostream&, wchar_t);
540 extern template wostream& operator<<(wostream&, char);
541 extern template wostream& operator<<(wostream&, const wchar_t*);
542 extern template wostream& operator<<(wostream&, const char*);
546 _GLIBCXX_END_NAMESPACE