1 // ostream classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
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, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
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 // ISO C++ 14882: 27.6.2 Output streams
35 #pragma GCC system_header
41 template<typename _CharT, typename _Traits>
42 basic_ostream<_CharT, _Traits>::sentry::
43 sentry(basic_ostream<_CharT,_Traits>& __os)
47 if (__os.tie() && __os.good())
55 __os.setstate(ios_base::failbit);
59 template<typename _CharT, typename _Traits>
60 basic_ostream<_CharT, _Traits>&
61 basic_ostream<_CharT, _Traits>::
62 operator<<(__ostream_type& (*__pf)(__ostream_type&))
71 // 27.6.2.5.1 Common requirements.
72 // Turn this on without causing an ios::failure to be thrown.
73 this->_M_setstate(ios_base::badbit);
74 if ((this->exceptions() & ios_base::badbit) != 0)
75 __throw_exception_again;
81 template<typename _CharT, typename _Traits>
82 basic_ostream<_CharT, _Traits>&
83 basic_ostream<_CharT, _Traits>::
84 operator<<(__ios_type& (*__pf)(__ios_type&))
93 // 27.6.2.5.1 Common requirements.
94 // Turn this on without causing an ios::failure to be thrown.
95 this->_M_setstate(ios_base::badbit);
96 if ((this->exceptions() & ios_base::badbit) != 0)
97 __throw_exception_again;
103 template<typename _CharT, typename _Traits>
104 basic_ostream<_CharT, _Traits>&
105 basic_ostream<_CharT, _Traits>::
106 operator<<(ios_base& (*__pf)(ios_base&))
108 sentry __cerb(*this);
115 // 27.6.2.5.1 Common requirements.
116 // Turn this on without causing an ios::failure to be thrown.
117 this->_M_setstate(ios_base::badbit);
118 if ((this->exceptions() & ios_base::badbit) != 0)
119 __throw_exception_again;
125 template<typename _CharT, typename _Traits>
126 basic_ostream<_CharT, _Traits>&
127 basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
129 sentry __cerb(*this);
130 if (__cerb && __sbin)
134 if (!__copy_streambufs(*this, __sbin, this->rdbuf()))
135 this->setstate(ios_base::failbit);
139 // 27.6.2.5.1 Common requirements.
140 // Turn this on without causing an ios::failure to be thrown.
141 this->_M_setstate(ios_base::badbit);
142 if ((this->exceptions() & ios_base::badbit) != 0)
143 __throw_exception_again;
147 this->setstate(ios_base::badbit);
151 template<typename _CharT, typename _Traits>
152 basic_ostream<_CharT, _Traits>&
153 basic_ostream<_CharT, _Traits>::operator<<(bool __n)
155 sentry __cerb(*this);
160 if (_M_check_facet(_M_fnumput))
161 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
162 this->setstate(ios_base::badbit);
166 // 27.6.1.2.1 Common requirements.
167 // Turn this on without causing an ios::failure to be thrown.
168 this->_M_setstate(ios_base::badbit);
169 if ((this->exceptions() & ios_base::badbit) != 0)
170 __throw_exception_again;
176 template<typename _CharT, typename _Traits>
177 basic_ostream<_CharT, _Traits>&
178 basic_ostream<_CharT, _Traits>::operator<<(long __n)
180 sentry __cerb(*this);
185 char_type __c = this->fill();
186 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
187 if (_M_check_facet(_M_fnumput))
190 if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
192 unsigned long __l = static_cast<unsigned long>(__n);
193 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
196 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
198 this->setstate(ios_base::badbit);
203 // 27.6.1.2.1 Common requirements.
204 // Turn this on without causing an ios::failure to be thrown.
205 this->_M_setstate(ios_base::badbit);
206 if ((this->exceptions() & ios_base::badbit) != 0)
207 __throw_exception_again;
213 template<typename _CharT, typename _Traits>
214 basic_ostream<_CharT, _Traits>&
215 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
217 sentry __cerb(*this);
222 if (_M_check_facet(_M_fnumput))
223 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
224 this->setstate(ios_base::badbit);
228 // 27.6.1.2.1 Common requirements.
229 // Turn this on without causing an ios::failure to be thrown.
230 this->_M_setstate(ios_base::badbit);
231 if ((this->exceptions() & ios_base::badbit) != 0)
232 __throw_exception_again;
238 #ifdef _GLIBCPP_USE_LONG_LONG
239 template<typename _CharT, typename _Traits>
240 basic_ostream<_CharT, _Traits>&
241 basic_ostream<_CharT, _Traits>::operator<<(long long __n)
243 sentry __cerb(*this);
248 char_type __c = this->fill();
249 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
250 if (_M_check_facet(_M_fnumput))
253 if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
255 unsigned long long __l;
256 __l = static_cast<unsigned long long>(__n);
257 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
260 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
262 this->setstate(ios_base::badbit);
267 // 27.6.1.2.1 Common requirements.
268 // Turn this on without causing an ios::failure to be thrown.
269 this->_M_setstate(ios_base::badbit);
270 if ((this->exceptions() & ios_base::badbit) != 0)
271 __throw_exception_again;
277 template<typename _CharT, typename _Traits>
278 basic_ostream<_CharT, _Traits>&
279 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
281 sentry __cerb(*this);
286 if (_M_check_facet(_M_fnumput))
287 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
288 this->setstate(ios_base::badbit);
292 // 27.6.1.2.1 Common requirements.
293 // Turn this on without causing an ios::failure to be thrown.
294 this->_M_setstate(ios_base::badbit);
295 if ((this->exceptions() & ios_base::badbit) != 0)
296 __throw_exception_again;
303 template<typename _CharT, typename _Traits>
304 basic_ostream<_CharT, _Traits>&
305 basic_ostream<_CharT, _Traits>::operator<<(double __n)
307 sentry __cerb(*this);
312 if (_M_check_facet(_M_fnumput))
313 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
314 this->setstate(ios_base::badbit);
318 // 27.6.1.2.1 Common requirements.
319 // Turn this on without causing an ios::failure to be thrown.
320 this->_M_setstate(ios_base::badbit);
321 if ((this->exceptions() & ios_base::badbit) != 0)
322 __throw_exception_again;
328 template<typename _CharT, typename _Traits>
329 basic_ostream<_CharT, _Traits>&
330 basic_ostream<_CharT, _Traits>::operator<<(long double __n)
332 sentry __cerb(*this);
337 if (_M_check_facet(_M_fnumput))
338 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
339 this->setstate(ios_base::badbit);
343 // 27.6.1.2.1 Common requirements.
344 // Turn this on without causing an ios::failure to be thrown.
345 this->_M_setstate(ios_base::badbit);
346 if ((this->exceptions() & ios_base::badbit) != 0)
347 __throw_exception_again;
353 template<typename _CharT, typename _Traits>
354 basic_ostream<_CharT, _Traits>&
355 basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
357 sentry __cerb(*this);
362 if (_M_check_facet(_M_fnumput))
363 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
364 this->setstate(ios_base::badbit);
368 // 27.6.1.2.1 Common requirements.
369 // Turn this on without causing an ios::failure to be thrown.
370 this->_M_setstate(ios_base::badbit);
371 if ((this->exceptions() & ios_base::badbit) != 0)
372 __throw_exception_again;
378 template<typename _CharT, typename _Traits>
379 basic_ostream<_CharT, _Traits>&
380 basic_ostream<_CharT, _Traits>::put(char_type __c)
382 sentry __cerb(*this);
385 int_type __put = rdbuf()->sputc(__c);
386 if (traits_type::eq_int_type(__put, traits_type::eof()))
387 this->setstate(ios_base::badbit);
392 template<typename _CharT, typename _Traits>
393 basic_ostream<_CharT, _Traits>&
394 basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
396 sentry __cerb(*this);
399 streamsize __put = this->rdbuf()->sputn(__s, __n);
401 this->setstate(ios_base::badbit);
406 template<typename _CharT, typename _Traits>
407 basic_ostream<_CharT, _Traits>&
408 basic_ostream<_CharT, _Traits>::flush()
410 sentry __cerb(*this);
413 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
414 this->setstate(ios_base::badbit);
419 template<typename _CharT, typename _Traits>
420 typename basic_ostream<_CharT, _Traits>::pos_type
421 basic_ostream<_CharT, _Traits>::tellp()
423 pos_type __ret = pos_type(-1);
425 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
430 template<typename _CharT, typename _Traits>
431 basic_ostream<_CharT, _Traits>&
432 basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
436 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
437 // 136. seekp, seekg setting wrong streams?
438 pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::out);
440 // 129. Need error indication from seekp() and seekg()
441 if (__err == pos_type(off_type(-1)))
442 this->setstate(ios_base::failbit);
448 template<typename _CharT, typename _Traits>
449 basic_ostream<_CharT, _Traits>&
450 basic_ostream<_CharT, _Traits>::
451 seekp(off_type __off, ios_base::seekdir __d)
455 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
456 // 136. seekp, seekg setting wrong streams?
457 pos_type __err = this->rdbuf()->pubseekoff(__off, __d,
460 // 129. Need error indication from seekp() and seekg()
461 if (__err == pos_type(off_type(-1)))
462 this->setstate(ios_base::failbit);
468 // 27.6.2.5.4 Character inserters.
469 template<typename _CharT, typename _Traits>
470 basic_ostream<_CharT, _Traits>&
471 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
473 typedef basic_ostream<_CharT, _Traits> __ostream_type;
474 typename __ostream_type::sentry __cerb(__out);
479 const streamsize __w = __out.width() > 0 ? __out.width() : 0;
480 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__w + 1)));
482 streamsize __len = 1;
485 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads,
486 &__c, __w, __len, false);
489 __out.write(__pads, __len);
494 // 27.6.1.2.1 Common requirements.
495 // Turn this on without causing an ios::failure to be thrown.
496 __out._M_setstate(ios_base::badbit);
497 if ((__out.exceptions() & ios_base::badbit) != 0)
498 __throw_exception_again;
505 template <class _Traits>
506 basic_ostream<char, _Traits>&
507 operator<<(basic_ostream<char, _Traits>& __out, char __c)
509 typedef basic_ostream<char, _Traits> __ostream_type;
510 typename __ostream_type::sentry __cerb(__out);
515 const streamsize __w = __out.width() > 0 ? __out.width() : 0;
516 char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
518 streamsize __len = 1;
521 __pad<char, _Traits>::_S_pad(__out, __out.fill(), __pads,
522 &__c, __w, __len, false);
525 __out.write(__pads, __len);
530 // 27.6.1.2.1 Common requirements.
531 // Turn this on without causing an ios::failure to be thrown.
532 __out._M_setstate(ios_base::badbit);
533 if ((__out.exceptions() & ios_base::badbit) != 0)
534 __throw_exception_again;
540 template<typename _CharT, typename _Traits>
541 basic_ostream<_CharT, _Traits>&
542 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
544 typedef basic_ostream<_CharT, _Traits> __ostream_type;
545 typename __ostream_type::sentry __cerb(__out);
550 const streamsize __w = __out.width() > 0 ? __out.width() : 0;
551 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
552 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
555 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads,
556 __s, __w, __len, false);
560 __out.write(__s, __len);
565 // 27.6.1.2.1 Common requirements.
566 // Turn this on without causing an ios::failure to be thrown.
567 __out._M_setstate(ios_base::badbit);
568 if ((__out.exceptions() & ios_base::badbit) != 0)
569 __throw_exception_again;
573 __out.setstate(ios_base::badbit);
577 template<typename _CharT, typename _Traits>
578 basic_ostream<_CharT, _Traits>&
579 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
581 typedef basic_ostream<_CharT, _Traits> __ostream_type;
582 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
583 // 167. Improper use of traits_type::length()
584 // Note that this is only in 'Review' status.
585 typedef char_traits<char> __traits_type;
587 typename __ostream_type::sentry __cerb(__out);
590 size_t __clen = __traits_type::length(__s);
591 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
592 for (size_t __i = 0; __i < __clen; ++__i)
593 __ws[__i] = __out.widen(__s[__i]);
594 _CharT* __str = __ws;
598 streamsize __len = static_cast<streamsize>(__clen);
599 const streamsize __w = __out.width() > 0 ? __out.width() : 0;
600 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
604 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads,
605 __ws, __w, __len, false);
609 __out.write(__str, __len);
614 // 27.6.1.2.1 Common requirements.
615 // Turn this on without causing an ios::failure to be thrown.
616 __out._M_setstate(ios_base::badbit);
617 if ((__out.exceptions() & ios_base::badbit) != 0)
618 __throw_exception_again;
622 __out.setstate(ios_base::badbit);
626 // Partial specializations.
627 template<class _Traits>
628 basic_ostream<char, _Traits>&
629 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
631 typedef basic_ostream<char, _Traits> __ostream_type;
632 typename __ostream_type::sentry __cerb(__out);
637 const streamsize __w = __out.width() > 0 ? __out.width() : 0;
638 char* __pads = static_cast<char*>(__builtin_alloca(__w));
639 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
643 __pad<char, _Traits>::_S_pad(__out, __out.fill(), __pads,
644 __s, __w, __len, false);
648 __out.write(__s, __len);
653 // 27.6.1.2.1 Common requirements.
654 // Turn this on without causing an ios::failure to be thrown.
655 __out._M_setstate(ios_base::badbit);
656 if ((__out.exceptions() & ios_base::badbit) != 0)
657 __throw_exception_again;
661 __out.setstate(ios_base::badbit);
665 // 21.3.7.9 basic_string::operator<<
666 template<typename _CharT, typename _Traits, typename _Alloc>
667 basic_ostream<_CharT, _Traits>&
668 operator<<(basic_ostream<_CharT, _Traits>& __out,
669 const basic_string<_CharT, _Traits, _Alloc>& __str)
671 typedef basic_ostream<_CharT, _Traits> __ostream_type;
672 typename __ostream_type::sentry __cerb(__out);
675 const _CharT* __s = __str.data();
676 const streamsize __w = __out.width() > 0 ? __out.width() : 0;
677 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
678 streamsize __len = static_cast<streamsize>(__str.size());
679 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
680 // 25. String operator<< uses width() value wrong
684 __pad<_CharT, _Traits>::_S_pad(__out, __out.fill(), __pads, __s,
689 streamsize __res = __out.rdbuf()->sputn(__s, __len);
692 __out.setstate(ios_base::failbit);
697 // Inhibit implicit instantiations for required instantiations,
698 // which are defined via explicit instantiations elsewhere.
699 // NB: This syntax is a GNU extension.
700 #if _GLIBCPP_EXTERN_TEMPLATE
701 extern template class basic_ostream<char>;
702 extern template ostream& endl(ostream&);
703 extern template ostream& ends(ostream&);
704 extern template ostream& flush(ostream&);
705 extern template ostream& operator<<(ostream&, char);
706 extern template ostream& operator<<(ostream&, unsigned char);
707 extern template ostream& operator<<(ostream&, signed char);
708 extern template ostream& operator<<(ostream&, const char*);
709 extern template ostream& operator<<(ostream&, const unsigned char*);
710 extern template ostream& operator<<(ostream&, const signed char*);
712 #ifdef _GLIBCPP_USE_WCHAR_T
713 extern template class basic_ostream<wchar_t>;
714 extern template wostream& endl(wostream&);
715 extern template wostream& ends(wostream&);
716 extern template wostream& flush(wostream&);
717 extern template wostream& operator<<(wostream&, wchar_t);
718 extern template wostream& operator<<(wostream&, char);
719 extern template wostream& operator<<(wostream&, const wchar_t*);
720 extern template wostream& operator<<(wostream&, const char*);