1 // ostream classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
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
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 basic_ostream<_CharT, _Traits>&
101 basic_ostream<_CharT, _Traits>::
104 sentry __cerb(*this);
107 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
110 const __num_put_type& __np = __check_facet(this->_M_num_put);
111 if (__np.put(*this, *this, this->fill(), __n).failed())
112 __err |= ios_base::badbit;
115 { this->_M_setstate(ios_base::badbit); }
117 this->setstate(__err);
122 template<typename _CharT, typename _Traits>
123 basic_ostream<_CharT, _Traits>&
124 basic_ostream<_CharT, _Traits>::
125 operator<<(short __n)
127 sentry __cerb(*this);
130 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
133 // _GLIBCXX_RESOLVE_LIB_DEFECTS
134 // 117. basic_ostream uses nonexistent num_put member functions.
136 const ios_base::fmtflags __fmt = (this->flags()
137 & ios_base::basefield);
138 if (__fmt == ios_base::oct || __fmt == ios_base::hex)
139 __l = static_cast<long>(static_cast<unsigned short>(__n));
141 __l = static_cast<long>(__n);
142 const __num_put_type& __np = __check_facet(this->_M_num_put);
143 if (__np.put(*this, *this, this->fill(), __l).failed())
144 __err |= ios_base::badbit;
147 { this->_M_setstate(ios_base::badbit); }
149 this->setstate(__err);
154 template<typename _CharT, typename _Traits>
155 basic_ostream<_CharT, _Traits>&
156 basic_ostream<_CharT, _Traits>::
157 operator<<(unsigned short __n)
159 sentry __cerb(*this);
162 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
165 // _GLIBCXX_RESOLVE_LIB_DEFECTS
166 // 117. basic_ostream uses nonexistent num_put member functions.
167 const __num_put_type& __np = __check_facet(this->_M_num_put);
168 if (__np.put(*this, *this, this->fill(),
169 static_cast<unsigned long>(__n)).failed())
170 __err |= ios_base::badbit;
173 { this->_M_setstate(ios_base::badbit); }
175 this->setstate(__err);
180 template<typename _CharT, typename _Traits>
181 basic_ostream<_CharT, _Traits>&
182 basic_ostream<_CharT, _Traits>::
185 sentry __cerb(*this);
188 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
191 // _GLIBCXX_RESOLVE_LIB_DEFECTS
192 // 117. basic_ostream uses nonexistent num_put member functions.
194 const ios_base::fmtflags __fmt = (this->flags()
195 & ios_base::basefield);
196 if (__fmt == ios_base::oct || __fmt == ios_base::hex)
197 __l = static_cast<long>(static_cast<unsigned int>(__n));
199 __l = static_cast<long>(__n);
200 const __num_put_type& __np = __check_facet(this->_M_num_put);
201 if (__np.put(*this, *this, this->fill(), __l).failed())
202 __err |= ios_base::badbit;
205 { this->_M_setstate(ios_base::badbit); }
207 this->setstate(__err);
212 template<typename _CharT, typename _Traits>
213 basic_ostream<_CharT, _Traits>&
214 basic_ostream<_CharT, _Traits>::
215 operator<<(unsigned int __n)
217 sentry __cerb(*this);
220 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
223 // _GLIBCXX_RESOLVE_LIB_DEFECTS
224 // 117. basic_ostream uses nonexistent num_put member functions.
225 const __num_put_type& __np = __check_facet(this->_M_num_put);
226 if (__np.put(*this, *this, this->fill(),
227 static_cast<unsigned long>(__n)).failed())
228 __err |= ios_base::badbit;
231 { this->_M_setstate(ios_base::badbit); }
233 this->setstate(__err);
238 template<typename _CharT, typename _Traits>
239 basic_ostream<_CharT, _Traits>&
240 basic_ostream<_CharT, _Traits>::
243 sentry __cerb(*this);
246 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
249 const __num_put_type& __np = __check_facet(this->_M_num_put);
250 if (__np.put(*this, *this, this->fill(), __n).failed())
251 __err |= ios_base::badbit;
254 { this->_M_setstate(ios_base::badbit); }
256 this->setstate(__err);
261 template<typename _CharT, typename _Traits>
262 basic_ostream<_CharT, _Traits>&
263 basic_ostream<_CharT, _Traits>::
264 operator<<(unsigned long __n)
266 sentry __cerb(*this);
269 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
272 const __num_put_type& __np = __check_facet(this->_M_num_put);
273 if (__np.put(*this, *this, this->fill(), __n).failed())
274 __err |= ios_base::badbit;
277 { this->_M_setstate(ios_base::badbit); }
279 this->setstate(__err);
284 #ifdef _GLIBCXX_USE_LONG_LONG
285 template<typename _CharT, typename _Traits>
286 basic_ostream<_CharT, _Traits>&
287 basic_ostream<_CharT, _Traits>::
288 operator<<(long long __n)
290 sentry __cerb(*this);
293 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
296 const __num_put_type& __np = __check_facet(this->_M_num_put);
297 if (__np.put(*this, *this, this->fill(), __n).failed())
298 __err |= ios_base::badbit;
301 { this->_M_setstate(ios_base::badbit); }
303 this->setstate(__err);
308 template<typename _CharT, typename _Traits>
309 basic_ostream<_CharT, _Traits>&
310 basic_ostream<_CharT, _Traits>::
311 operator<<(unsigned long long __n)
313 sentry __cerb(*this);
316 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
319 const __num_put_type& __np = __check_facet(this->_M_num_put);
320 if (__np.put(*this, *this, this->fill(), __n).failed())
321 __err |= ios_base::badbit;
324 { this->_M_setstate(ios_base::badbit); }
326 this->setstate(__err);
332 template<typename _CharT, typename _Traits>
333 basic_ostream<_CharT, _Traits>&
334 basic_ostream<_CharT, _Traits>::
335 operator<<(float __n)
337 sentry __cerb(*this);
340 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
343 // _GLIBCXX_RESOLVE_LIB_DEFECTS
344 // 117. basic_ostream uses nonexistent num_put member functions.
345 const __num_put_type& __np = __check_facet(this->_M_num_put);
346 if (__np.put(*this, *this, this->fill(),
347 static_cast<double>(__n)).failed())
348 __err |= ios_base::badbit;
351 { this->_M_setstate(ios_base::badbit); }
353 this->setstate(__err);
358 template<typename _CharT, typename _Traits>
359 basic_ostream<_CharT, _Traits>&
360 basic_ostream<_CharT, _Traits>::
361 operator<<(double __n)
363 sentry __cerb(*this);
366 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
369 const __num_put_type& __np = __check_facet(this->_M_num_put);
370 if (__np.put(*this, *this, this->fill(), __n).failed())
371 __err |= ios_base::badbit;
374 { this->_M_setstate(ios_base::badbit); }
376 this->setstate(__err);
381 template<typename _CharT, typename _Traits>
382 basic_ostream<_CharT, _Traits>&
383 basic_ostream<_CharT, _Traits>::
384 operator<<(long double __n)
386 sentry __cerb(*this);
389 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
392 const __num_put_type& __np = __check_facet(this->_M_num_put);
393 if (__np.put(*this, *this, this->fill(), __n).failed())
394 __err |= ios_base::badbit;
397 { this->_M_setstate(ios_base::badbit); }
399 this->setstate(__err);
404 template<typename _CharT, typename _Traits>
405 basic_ostream<_CharT, _Traits>&
406 basic_ostream<_CharT, _Traits>::
407 operator<<(const void* __n)
409 sentry __cerb(*this);
412 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
415 const __num_put_type& __np = __check_facet(this->_M_num_put);
416 if (__np.put(*this, *this, this->fill(), __n).failed())
417 __err |= ios_base::badbit;
420 { this->_M_setstate(ios_base::badbit); }
422 this->setstate(__err);
427 template<typename _CharT, typename _Traits>
428 basic_ostream<_CharT, _Traits>&
429 basic_ostream<_CharT, _Traits>::
430 operator<<(__streambuf_type* __sbin)
432 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
433 sentry __cerb(*this);
434 if (__cerb && __sbin)
438 if (!__copy_streambufs(__sbin, this->rdbuf()))
439 __err |= ios_base::failbit;
442 { this->_M_setstate(ios_base::failbit); }
445 __err |= ios_base::badbit;
447 this->setstate(__err);
451 template<typename _CharT, typename _Traits>
452 basic_ostream<_CharT, _Traits>&
453 basic_ostream<_CharT, _Traits>::
456 // _GLIBCXX_RESOLVE_LIB_DEFECTS
457 // DR 60. What is a formatted input function?
458 // basic_ostream::put(char_type) is an unformatted output function.
459 // DR 63. Exception-handling policy for unformatted output.
460 // Unformatted output functions should catch exceptions thrown
461 // from streambuf members.
462 sentry __cerb(*this);
465 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
468 const int_type __put = this->rdbuf()->sputc(__c);
469 if (traits_type::eq_int_type(__put, traits_type::eof()))
470 __err |= ios_base::badbit;
473 { this->_M_setstate(ios_base::badbit); }
475 this->setstate(__err);
480 template<typename _CharT, typename _Traits>
481 basic_ostream<_CharT, _Traits>&
482 basic_ostream<_CharT, _Traits>::
483 write(const _CharT* __s, streamsize __n)
485 // _GLIBCXX_RESOLVE_LIB_DEFECTS
486 // DR 60. What is a formatted input function?
487 // basic_ostream::write(const char_type*, streamsize) is an
488 // unformatted output function.
489 // DR 63. Exception-handling policy for unformatted output.
490 // Unformatted output functions should catch exceptions thrown
491 // from streambuf members.
492 sentry __cerb(*this);
496 { _M_write(__s, __n); }
498 { this->_M_setstate(ios_base::badbit); }
503 template<typename _CharT, typename _Traits>
504 basic_ostream<_CharT, _Traits>&
505 basic_ostream<_CharT, _Traits>::
508 // _GLIBCXX_RESOLVE_LIB_DEFECTS
509 // DR 60. What is a formatted input function?
510 // basic_ostream::flush() is *not* an unformatted output function.
511 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
514 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
515 __err |= ios_base::badbit;
518 { this->_M_setstate(ios_base::badbit); }
520 this->setstate(__err);
524 template<typename _CharT, typename _Traits>
525 typename basic_ostream<_CharT, _Traits>::pos_type
526 basic_ostream<_CharT, _Traits>::
529 pos_type __ret = pos_type(-1);
533 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
536 { this->_M_setstate(ios_base::badbit); }
540 template<typename _CharT, typename _Traits>
541 basic_ostream<_CharT, _Traits>&
542 basic_ostream<_CharT, _Traits>::
543 seekp(pos_type __pos)
545 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
550 // _GLIBCXX_RESOLVE_LIB_DEFECTS
551 // 136. seekp, seekg setting wrong streams?
552 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
555 // 129. Need error indication from seekp() and seekg()
556 if (__p == pos_type(off_type(-1)))
557 __err |= ios_base::failbit;
561 { this->_M_setstate(ios_base::badbit); }
563 this->setstate(__err);
567 template<typename _CharT, typename _Traits>
568 basic_ostream<_CharT, _Traits>&
569 basic_ostream<_CharT, _Traits>::
570 seekp(off_type __off, ios_base::seekdir __dir)
572 ios_base::iostate __err = ios_base::iostate(ios_base::goodbit);
577 // _GLIBCXX_RESOLVE_LIB_DEFECTS
578 // 136. seekp, seekg setting wrong streams?
579 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
582 // 129. Need error indication from seekp() and seekg()
583 if (__p == pos_type(off_type(-1)))
584 __err |= ios_base::failbit;
588 { this->_M_setstate(ios_base::badbit); }
590 this->setstate(__err);
594 // 27.6.2.5.4 Character inserters.
595 template<typename _CharT, typename _Traits>
596 basic_ostream<_CharT, _Traits>&
597 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
599 typedef basic_ostream<_CharT, _Traits> __ostream_type;
600 typename __ostream_type::sentry __cerb(__out);
605 const streamsize __w = __out.width();
606 streamsize __len = 1;
610 __cs = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
612 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs,
613 &__c, __w, __len, false);
616 __out._M_write(__cs, __len);
620 { __out._M_setstate(ios_base::badbit); }
626 template <class _Traits>
627 basic_ostream<char, _Traits>&
628 operator<<(basic_ostream<char, _Traits>& __out, char __c)
630 typedef basic_ostream<char, _Traits> __ostream_type;
631 typename __ostream_type::sentry __cerb(__out);
636 const streamsize __w = __out.width();
637 streamsize __len = 1;
641 __cs = static_cast<char*>(__builtin_alloca(__w));
642 __pad<char, _Traits>::_S_pad(__out, __out.fill(), __cs,
643 &__c, __w, __len, false);
646 __out._M_write(__cs, __len);
650 { __out._M_setstate(ios_base::badbit); }
655 template<typename _CharT, typename _Traits>
656 basic_ostream<_CharT, _Traits>&
657 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
659 typedef basic_ostream<_CharT, _Traits> __ostream_type;
660 typename __ostream_type::sentry __cerb(__out);
665 const streamsize __w = __out.width();
666 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
669 _CharT* __cs = (static_cast<
670 _CharT*>(__builtin_alloca(sizeof(_CharT)
672 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs,
673 __s, __w, __len, false);
677 __out._M_write(__s, __len);
681 { __out._M_setstate(ios_base::badbit); }
684 __out.setstate(ios_base::badbit);
688 template<typename _CharT, typename _Traits>
689 basic_ostream<_CharT, _Traits>&
690 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
692 typedef basic_ostream<_CharT, _Traits> __ostream_type;
693 // _GLIBCXX_RESOLVE_LIB_DEFECTS
694 // 167. Improper use of traits_type::length()
695 // Note that this is only in 'Review' status.
696 typedef char_traits<char> __traits_type;
697 typename __ostream_type::sentry __cerb(__out);
700 size_t __clen = __traits_type::length(__s);
701 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT)
703 for (size_t __i = 0; __i < __clen; ++__i)
704 __ws[__i] = __out.widen(__s[__i]);
705 _CharT* __str = __ws;
709 const streamsize __w = __out.width();
710 streamsize __len = static_cast<streamsize>(__clen);
713 _CharT* __cs = (static_cast<
714 _CharT*>(__builtin_alloca(sizeof(_CharT)
716 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs,
717 __ws, __w, __len, false);
721 __out._M_write(__str, __len);
725 { __out._M_setstate(ios_base::badbit); }
728 __out.setstate(ios_base::badbit);
732 // Partial specializations.
733 template<class _Traits>
734 basic_ostream<char, _Traits>&
735 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
737 typedef basic_ostream<char, _Traits> __ostream_type;
738 typename __ostream_type::sentry __cerb(__out);
743 const streamsize __w = __out.width();
744 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
747 char* __cs = static_cast<char*>(__builtin_alloca(__w));
748 __pad<char, _Traits>::_S_pad(__out, __out.fill(), __cs,
749 __s, __w, __len, false);
753 __out._M_write(__s, __len);
757 { __out._M_setstate(ios_base::badbit); }
760 __out.setstate(ios_base::badbit);
764 // 21.3.7.9 basic_string::operator<<
765 template<typename _CharT, typename _Traits, typename _Alloc>
766 basic_ostream<_CharT, _Traits>&
767 operator<<(basic_ostream<_CharT, _Traits>& __out,
768 const basic_string<_CharT, _Traits, _Alloc>& __str)
770 typedef basic_ostream<_CharT, _Traits> __ostream_type;
771 typename __ostream_type::sentry __cerb(__out);
774 const streamsize __w = __out.width();
775 streamsize __len = static_cast<streamsize>(__str.size());
776 const _CharT* __s = __str.data();
778 // _GLIBCXX_RESOLVE_LIB_DEFECTS
779 // 25. String operator<< uses width() value wrong
782 _CharT* __cs = (static_cast<
783 _CharT*>(__builtin_alloca(sizeof(_CharT) * __w)));
784 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __cs, __s,
789 __out._M_write(__s, __len);
795 // Inhibit implicit instantiations for required instantiations,
796 // which are defined via explicit instantiations elsewhere.
797 // NB: This syntax is a GNU extension.
798 #if _GLIBCXX_EXTERN_TEMPLATE
799 extern template class basic_ostream<char>;
800 extern template ostream& endl(ostream&);
801 extern template ostream& ends(ostream&);
802 extern template ostream& flush(ostream&);
803 extern template ostream& operator<<(ostream&, char);
804 extern template ostream& operator<<(ostream&, unsigned char);
805 extern template ostream& operator<<(ostream&, signed char);
806 extern template ostream& operator<<(ostream&, const char*);
807 extern template ostream& operator<<(ostream&, const unsigned char*);
808 extern template ostream& operator<<(ostream&, const signed char*);
810 #ifdef _GLIBCXX_USE_WCHAR_T
811 extern template class basic_ostream<wchar_t>;
812 extern template wostream& endl(wostream&);
813 extern template wostream& ends(wostream&);
814 extern template wostream& flush(wostream&);
815 extern template wostream& operator<<(wostream&, wchar_t);
816 extern template wostream& operator<<(wostream&, char);
817 extern template wostream& operator<<(wostream&, const wchar_t*);
818 extern template wostream& operator<<(wostream&, const char*);