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)
44 : _M_ok(__os.good()), _M_os(__os)
47 if (_M_ok && __os.tie())
51 template<typename _CharT, typename _Traits>
52 basic_ostream<_CharT, _Traits>&
53 basic_ostream<_CharT, _Traits>::
54 operator<<(__ostream_type& (*__pf)(__ostream_type&))
61 catch(exception& __fail)
63 // 27.6.2.5.1 Common requirements.
64 // Turn this on without causing an ios::failure to be thrown.
65 this->setstate(ios_base::badbit);
66 if ((this->exceptions() & ios_base::badbit) != 0)
67 __throw_exception_again;
73 template<typename _CharT, typename _Traits>
74 basic_ostream<_CharT, _Traits>&
75 basic_ostream<_CharT, _Traits>::
76 operator<<(__ios_type& (*__pf)(__ios_type&))
83 catch(exception& __fail)
85 // 27.6.2.5.1 Common requirements.
86 // Turn this on without causing an ios::failure to be thrown.
87 this->setstate(ios_base::badbit);
88 if ((this->exceptions() & ios_base::badbit) != 0)
89 __throw_exception_again;
95 template<typename _CharT, typename _Traits>
96 basic_ostream<_CharT, _Traits>&
97 basic_ostream<_CharT, _Traits>::
98 operator<<(ios_base& (*__pf)(ios_base&))
100 sentry __cerb(*this);
105 catch(exception& __fail)
107 // 27.6.2.5.1 Common requirements.
108 // Turn this on without causing an ios::failure to be thrown.
109 this->setstate(ios_base::badbit);
110 if ((this->exceptions() & ios_base::badbit) != 0)
111 __throw_exception_again;
117 template<typename _CharT, typename _Traits>
118 basic_ostream<_CharT, _Traits>&
119 basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
121 sentry __cerb(*this);
126 streamsize __xtrct = 0;
129 __streambuf_type* __sbout = this->rdbuf();
130 __xtrct = __copy_streambufs(*this, __sbin, __sbout);
133 this->setstate(ios_base::badbit);
135 this->setstate(ios_base::failbit);
137 catch(exception& __fail)
139 // 27.6.2.5.1 Common requirements.
140 // Turn this on without causing an ios::failure to be thrown.
141 this->setstate(ios_base::badbit);
142 if ((this->exceptions() & ios_base::badbit) != 0)
143 __throw_exception_again;
149 template<typename _CharT, typename _Traits>
150 basic_ostream<_CharT, _Traits>&
151 basic_ostream<_CharT, _Traits>::operator<<(bool __n)
153 sentry __cerb(*this);
158 if (_M_check_facet(_M_fnumput))
159 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
160 this->setstate(ios_base::badbit);
162 catch(exception& __fail)
164 // 27.6.1.2.1 Common requirements.
165 // Turn this on without causing an ios::failure to be thrown.
166 this->setstate(ios_base::badbit);
167 if ((this->exceptions() & ios_base::badbit) != 0)
168 __throw_exception_again;
174 template<typename _CharT, typename _Traits>
175 basic_ostream<_CharT, _Traits>&
176 basic_ostream<_CharT, _Traits>::operator<<(long __n)
178 sentry __cerb(*this);
183 char_type __c = this->fill();
184 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
185 if (_M_check_facet(_M_fnumput))
188 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
190 unsigned long __l = static_cast<unsigned long>(__n);
191 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
194 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
196 this->setstate(ios_base::badbit);
199 catch(exception& __fail)
201 // 27.6.1.2.1 Common requirements.
202 // Turn this on without causing an ios::failure to be thrown.
203 this->setstate(ios_base::badbit);
204 if ((this->exceptions() & ios_base::badbit) != 0)
205 __throw_exception_again;
211 template<typename _CharT, typename _Traits>
212 basic_ostream<_CharT, _Traits>&
213 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
215 sentry __cerb(*this);
220 if (_M_check_facet(_M_fnumput))
221 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
222 this->setstate(ios_base::badbit);
224 catch(exception& __fail)
226 // 27.6.1.2.1 Common requirements.
227 // Turn this on without causing an ios::failure to be thrown.
228 this->setstate(ios_base::badbit);
229 if ((this->exceptions() & ios_base::badbit) != 0)
230 __throw_exception_again;
236 #ifdef _GLIBCPP_USE_LONG_LONG
237 template<typename _CharT, typename _Traits>
238 basic_ostream<_CharT, _Traits>&
239 basic_ostream<_CharT, _Traits>::operator<<(long long __n)
241 sentry __cerb(*this);
246 char_type __c = this->fill();
247 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
248 if (_M_check_facet(_M_fnumput))
251 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
253 unsigned long long __l;
254 __l = static_cast<unsigned long long>(__n);
255 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
258 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
260 this->setstate(ios_base::badbit);
263 catch(exception& __fail)
265 // 27.6.1.2.1 Common requirements.
266 // Turn this on without causing an ios::failure to be thrown.
267 this->setstate(ios_base::badbit);
268 if ((this->exceptions() & ios_base::badbit) != 0)
269 __throw_exception_again;
275 template<typename _CharT, typename _Traits>
276 basic_ostream<_CharT, _Traits>&
277 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
279 sentry __cerb(*this);
284 if (_M_check_facet(_M_fnumput))
285 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
286 this->setstate(ios_base::badbit);
288 catch(exception& __fail)
290 // 27.6.1.2.1 Common requirements.
291 // Turn this on without causing an ios::failure to be thrown.
292 this->setstate(ios_base::badbit);
293 if ((this->exceptions() & ios_base::badbit) != 0)
294 __throw_exception_again;
301 template<typename _CharT, typename _Traits>
302 basic_ostream<_CharT, _Traits>&
303 basic_ostream<_CharT, _Traits>::operator<<(double __n)
305 sentry __cerb(*this);
310 if (_M_check_facet(_M_fnumput))
311 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
312 this->setstate(ios_base::badbit);
314 catch(exception& __fail)
316 // 27.6.1.2.1 Common requirements.
317 // Turn this on without causing an ios::failure to be thrown.
318 this->setstate(ios_base::badbit);
319 if ((this->exceptions() & ios_base::badbit) != 0)
320 __throw_exception_again;
326 template<typename _CharT, typename _Traits>
327 basic_ostream<_CharT, _Traits>&
328 basic_ostream<_CharT, _Traits>::operator<<(long double __n)
330 sentry __cerb(*this);
335 if (_M_check_facet(_M_fnumput))
336 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
337 this->setstate(ios_base::badbit);
339 catch(exception& __fail)
341 // 27.6.1.2.1 Common requirements.
342 // Turn this on without causing an ios::failure to be thrown.
343 this->setstate(ios_base::badbit);
344 if ((this->exceptions() & ios_base::badbit) != 0)
345 __throw_exception_again;
351 template<typename _CharT, typename _Traits>
352 basic_ostream<_CharT, _Traits>&
353 basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
355 sentry __cerb(*this);
360 if (_M_check_facet(_M_fnumput))
361 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
362 this->setstate(ios_base::badbit);
364 catch(exception& __fail)
366 // 27.6.1.2.1 Common requirements.
367 // Turn this on without causing an ios::failure to be thrown.
368 this->setstate(ios_base::badbit);
369 if ((this->exceptions() & ios_base::badbit) != 0)
370 __throw_exception_again;
376 template<typename _CharT, typename _Traits>
377 basic_ostream<_CharT, _Traits>&
378 basic_ostream<_CharT, _Traits>::put(char_type __c)
380 sentry __cerb(*this);
383 int_type __put = rdbuf()->sputc(__c);
384 if (traits_type::eq_int_type(__put, traits_type::eof()))
385 this->setstate(ios_base::badbit);
390 template<typename _CharT, typename _Traits>
391 basic_ostream<_CharT, _Traits>&
392 basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
394 sentry __cerb(*this);
397 streamsize __put = this->rdbuf()->sputn(__s, __n);
399 this->setstate(ios_base::badbit);
404 template<typename _CharT, typename _Traits>
405 basic_ostream<_CharT, _Traits>&
406 basic_ostream<_CharT, _Traits>::flush()
408 sentry __cerb(*this);
411 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
412 this->setstate(ios_base::badbit);
417 template<typename _CharT, typename _Traits>
418 typename basic_ostream<_CharT, _Traits>::pos_type
419 basic_ostream<_CharT, _Traits>::tellp()
421 pos_type __ret = pos_type(-1);
422 bool __testok = this->fail() != true;
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)
434 bool __testok = this->fail() != true;
438 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
439 // 136. seekp, seekg setting wrong streams?
440 pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::out);
442 // 129. Need error indication from seekp() and seekg()
443 if (__err == pos_type(off_type(-1)))
444 this->setstate(ios_base::failbit);
450 template<typename _CharT, typename _Traits>
451 basic_ostream<_CharT, _Traits>&
452 basic_ostream<_CharT, _Traits>::
453 seekp(off_type __off, ios_base::seekdir __d)
455 bool __testok = this->fail() != true;
459 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
460 // 136. seekp, seekg setting wrong streams?
461 pos_type __err = this->rdbuf()->pubseekoff(__off, __d,
464 // 129. Need error indication from seekp() and seekg()
465 if (__err == pos_type(off_type(-1)))
466 this->setstate(ios_base::failbit);
472 // 27.6.2.5.4 Character inserters.
473 template<typename _CharT, typename _Traits>
474 basic_ostream<_CharT, _Traits>&
475 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
477 typedef basic_ostream<_CharT, _Traits> __ostream_type;
478 typename __ostream_type::sentry __cerb(__out);
483 streamsize __w = __out.width();
484 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
486 streamsize __len = 1;
489 __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
492 __out.write(__pads, __len);
495 catch(exception& __fail)
497 // 27.6.1.2.1 Common requirements.
498 // Turn this on without causing an ios::failure to be thrown.
499 __out.setstate(ios_base::badbit);
500 if ((__out.exceptions() & ios_base::badbit) != 0)
501 __throw_exception_again;
508 template <class _Traits>
509 basic_ostream<char, _Traits>&
510 operator<<(basic_ostream<char, _Traits>& __out, char __c)
512 typedef basic_ostream<char, _Traits> __ostream_type;
513 typename __ostream_type::sentry __cerb(__out);
518 streamsize __w = __out.width();
519 char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
521 streamsize __len = 1;
524 __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
527 __out.write(__pads, __len);
530 catch(exception& __fail)
532 // 27.6.1.2.1 Common requirements.
533 // Turn this on without causing an ios::failure to be thrown.
534 __out.setstate(ios_base::badbit);
535 if ((__out.exceptions() & ios_base::badbit) != 0)
536 __throw_exception_again;
542 template<typename _CharT, typename _Traits>
543 basic_ostream<_CharT, _Traits>&
544 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
546 typedef basic_ostream<_CharT, _Traits> __ostream_type;
547 typename __ostream_type::sentry __cerb(__out);
552 streamsize __w = __out.width();
553 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
554 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
557 __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
561 __out.write(__s, __len);
564 catch(exception& __fail)
566 // 27.6.1.2.1 Common requirements.
567 // Turn this on without causing an ios::failure to be thrown.
568 __out.setstate(ios_base::badbit);
569 if ((__out.exceptions() & ios_base::badbit) != 0)
570 __throw_exception_again;
576 template<typename _CharT, typename _Traits>
577 basic_ostream<_CharT, _Traits>&
578 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
580 typedef basic_ostream<_CharT, _Traits> __ostream_type;
581 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
582 // 167. Improper use of traits_type::length()
583 // Note that this is only in 'Review' status.
584 typedef char_traits<char> __ctraits_type;
586 typename __ostream_type::sentry __cerb(__out);
589 size_t __clen = __ctraits_type::length(__s);
590 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
591 for (size_t __i = 0; __i <= __clen; ++__i)
592 __ws[__i] = __out.widen(__s[__i]);
593 _CharT* __str = __ws;
597 streamsize __len = static_cast<streamsize>(__clen);
598 streamsize __w = __out.width();
599 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
603 __pad(__out, __out.fill(), __pads, __ws, __w, __len, false);
607 __out.write(__str, __len);
610 catch(exception& __fail)
612 // 27.6.1.2.1 Common requirements.
613 // Turn this on without causing an ios::failure to be thrown.
614 __out.setstate(ios_base::badbit);
615 if ((__out.exceptions() & ios_base::badbit) != 0)
616 __throw_exception_again;
622 // Partial specializations.
623 template<class _Traits>
624 basic_ostream<char, _Traits>&
625 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
627 typedef basic_ostream<char, _Traits> __ostream_type;
628 typename __ostream_type::sentry __cerb(__out);
633 streamsize __w = __out.width();
634 char* __pads = static_cast<char*>(__builtin_alloca(__w));
635 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
638 __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
642 __out.write(__s, __len);
645 catch(exception& __fail)
647 // 27.6.1.2.1 Common requirements.
648 // Turn this on without causing an ios::failure to be thrown.
649 __out.setstate(ios_base::badbit);
650 if ((__out.exceptions() & ios_base::badbit) != 0)
651 __throw_exception_again;
657 // 21.3.7.9 basic_string::operator<<
658 template<typename _CharT, typename _Traits, typename _Alloc>
659 basic_ostream<_CharT, _Traits>&
660 operator<<(basic_ostream<_CharT, _Traits>& __out,
661 const basic_string<_CharT, _Traits, _Alloc>& __str)
663 typedef basic_ostream<_CharT, _Traits> __ostream_type;
664 typename __ostream_type::sentry __cerb(__out);
667 const _CharT* __s = __str.data();
668 streamsize __w = __out.width();
669 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
670 streamsize __len = static_cast<streamsize>(__str.size());
671 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
672 // 25. String operator<< uses width() value wrong
676 __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
680 streamsize __res = __out.rdbuf()->sputn(__s, __len);
683 __out.setstate(ios_base::failbit);
688 // Inhibit implicit instantiations for required instantiations,
689 // which are defined via explicit instantiations elsewhere.
690 // NB: This syntax is a GNU extension.
691 extern template class basic_ostream<char>;
692 extern template ostream& endl(ostream&);
693 extern template ostream& ends(ostream&);
694 extern template ostream& flush(ostream&);
695 extern template ostream& operator<<(ostream&, char);
696 extern template ostream& operator<<(ostream&, unsigned char);
697 extern template ostream& operator<<(ostream&, signed char);
698 extern template ostream& operator<<(ostream&, const char*);
699 extern template ostream& operator<<(ostream&, const unsigned char*);
700 extern template ostream& operator<<(ostream&, const signed char*);
702 extern template class basic_ostream<wchar_t>;
703 extern template wostream& endl(wostream&);
704 extern template wostream& ends(wostream&);
705 extern template wostream& flush(wostream&);
706 extern template wostream& operator<<(wostream&, wchar_t);
707 extern template wostream& operator<<(wostream&, char);
708 extern template wostream& operator<<(wostream&, const wchar_t*);
709 extern template wostream& operator<<(wostream&, const char*);