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);
122 if (__cerb && __sbin)
126 if (!__copy_streambufs(*this, __sbin, this->rdbuf()))
127 this->setstate(ios_base::failbit);
129 catch(exception& __fail)
131 // 27.6.2.5.1 Common requirements.
132 // Turn this on without causing an ios::failure to be thrown.
133 this->setstate(ios_base::badbit);
134 if ((this->exceptions() & ios_base::badbit) != 0)
135 __throw_exception_again;
139 this->setstate(ios_base::badbit);
143 template<typename _CharT, typename _Traits>
144 basic_ostream<_CharT, _Traits>&
145 basic_ostream<_CharT, _Traits>::operator<<(bool __n)
147 sentry __cerb(*this);
152 if (_M_check_facet(_M_fnumput))
153 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
154 this->setstate(ios_base::badbit);
156 catch(exception& __fail)
158 // 27.6.1.2.1 Common requirements.
159 // Turn this on without causing an ios::failure to be thrown.
160 this->setstate(ios_base::badbit);
161 if ((this->exceptions() & ios_base::badbit) != 0)
162 __throw_exception_again;
168 template<typename _CharT, typename _Traits>
169 basic_ostream<_CharT, _Traits>&
170 basic_ostream<_CharT, _Traits>::operator<<(long __n)
172 sentry __cerb(*this);
177 char_type __c = this->fill();
178 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
179 if (_M_check_facet(_M_fnumput))
182 if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
184 unsigned long __l = static_cast<unsigned long>(__n);
185 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
188 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
190 this->setstate(ios_base::badbit);
193 catch(exception& __fail)
195 // 27.6.1.2.1 Common requirements.
196 // Turn this on without causing an ios::failure to be thrown.
197 this->setstate(ios_base::badbit);
198 if ((this->exceptions() & ios_base::badbit) != 0)
199 __throw_exception_again;
205 template<typename _CharT, typename _Traits>
206 basic_ostream<_CharT, _Traits>&
207 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
209 sentry __cerb(*this);
214 if (_M_check_facet(_M_fnumput))
215 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
216 this->setstate(ios_base::badbit);
218 catch(exception& __fail)
220 // 27.6.1.2.1 Common requirements.
221 // Turn this on without causing an ios::failure to be thrown.
222 this->setstate(ios_base::badbit);
223 if ((this->exceptions() & ios_base::badbit) != 0)
224 __throw_exception_again;
230 #ifdef _GLIBCPP_USE_LONG_LONG
231 template<typename _CharT, typename _Traits>
232 basic_ostream<_CharT, _Traits>&
233 basic_ostream<_CharT, _Traits>::operator<<(long long __n)
235 sentry __cerb(*this);
240 char_type __c = this->fill();
241 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
242 if (_M_check_facet(_M_fnumput))
245 if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
247 unsigned long long __l;
248 __l = static_cast<unsigned long long>(__n);
249 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
252 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
254 this->setstate(ios_base::badbit);
257 catch(exception& __fail)
259 // 27.6.1.2.1 Common requirements.
260 // Turn this on without causing an ios::failure to be thrown.
261 this->setstate(ios_base::badbit);
262 if ((this->exceptions() & ios_base::badbit) != 0)
263 __throw_exception_again;
269 template<typename _CharT, typename _Traits>
270 basic_ostream<_CharT, _Traits>&
271 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
273 sentry __cerb(*this);
278 if (_M_check_facet(_M_fnumput))
279 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
280 this->setstate(ios_base::badbit);
282 catch(exception& __fail)
284 // 27.6.1.2.1 Common requirements.
285 // Turn this on without causing an ios::failure to be thrown.
286 this->setstate(ios_base::badbit);
287 if ((this->exceptions() & ios_base::badbit) != 0)
288 __throw_exception_again;
295 template<typename _CharT, typename _Traits>
296 basic_ostream<_CharT, _Traits>&
297 basic_ostream<_CharT, _Traits>::operator<<(double __n)
299 sentry __cerb(*this);
304 if (_M_check_facet(_M_fnumput))
305 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
306 this->setstate(ios_base::badbit);
308 catch(exception& __fail)
310 // 27.6.1.2.1 Common requirements.
311 // Turn this on without causing an ios::failure to be thrown.
312 this->setstate(ios_base::badbit);
313 if ((this->exceptions() & ios_base::badbit) != 0)
314 __throw_exception_again;
320 template<typename _CharT, typename _Traits>
321 basic_ostream<_CharT, _Traits>&
322 basic_ostream<_CharT, _Traits>::operator<<(long double __n)
324 sentry __cerb(*this);
329 if (_M_check_facet(_M_fnumput))
330 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
331 this->setstate(ios_base::badbit);
333 catch(exception& __fail)
335 // 27.6.1.2.1 Common requirements.
336 // Turn this on without causing an ios::failure to be thrown.
337 this->setstate(ios_base::badbit);
338 if ((this->exceptions() & ios_base::badbit) != 0)
339 __throw_exception_again;
345 template<typename _CharT, typename _Traits>
346 basic_ostream<_CharT, _Traits>&
347 basic_ostream<_CharT, _Traits>::operator<<(const void* __n)
349 sentry __cerb(*this);
354 if (_M_check_facet(_M_fnumput))
355 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
356 this->setstate(ios_base::badbit);
358 catch(exception& __fail)
360 // 27.6.1.2.1 Common requirements.
361 // Turn this on without causing an ios::failure to be thrown.
362 this->setstate(ios_base::badbit);
363 if ((this->exceptions() & ios_base::badbit) != 0)
364 __throw_exception_again;
370 template<typename _CharT, typename _Traits>
371 basic_ostream<_CharT, _Traits>&
372 basic_ostream<_CharT, _Traits>::put(char_type __c)
374 sentry __cerb(*this);
377 int_type __put = rdbuf()->sputc(__c);
378 if (traits_type::eq_int_type(__put, traits_type::eof()))
379 this->setstate(ios_base::badbit);
384 template<typename _CharT, typename _Traits>
385 basic_ostream<_CharT, _Traits>&
386 basic_ostream<_CharT, _Traits>::write(const _CharT* __s, streamsize __n)
388 sentry __cerb(*this);
391 streamsize __put = this->rdbuf()->sputn(__s, __n);
393 this->setstate(ios_base::badbit);
398 template<typename _CharT, typename _Traits>
399 basic_ostream<_CharT, _Traits>&
400 basic_ostream<_CharT, _Traits>::flush()
402 sentry __cerb(*this);
405 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
406 this->setstate(ios_base::badbit);
411 template<typename _CharT, typename _Traits>
412 typename basic_ostream<_CharT, _Traits>::pos_type
413 basic_ostream<_CharT, _Traits>::tellp()
415 pos_type __ret = pos_type(-1);
417 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
422 template<typename _CharT, typename _Traits>
423 basic_ostream<_CharT, _Traits>&
424 basic_ostream<_CharT, _Traits>::seekp(pos_type __pos)
428 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
429 // 136. seekp, seekg setting wrong streams?
430 pos_type __err = this->rdbuf()->pubseekpos(__pos, ios_base::out);
432 // 129. Need error indication from seekp() and seekg()
433 if (__err == pos_type(off_type(-1)))
434 this->setstate(ios_base::failbit);
440 template<typename _CharT, typename _Traits>
441 basic_ostream<_CharT, _Traits>&
442 basic_ostream<_CharT, _Traits>::
443 seekp(off_type __off, ios_base::seekdir __d)
447 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
448 // 136. seekp, seekg setting wrong streams?
449 pos_type __err = this->rdbuf()->pubseekoff(__off, __d,
452 // 129. Need error indication from seekp() and seekg()
453 if (__err == pos_type(off_type(-1)))
454 this->setstate(ios_base::failbit);
460 // 27.6.2.5.4 Character inserters.
461 template<typename _CharT, typename _Traits>
462 basic_ostream<_CharT, _Traits>&
463 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
465 typedef basic_ostream<_CharT, _Traits> __ostream_type;
466 typename __ostream_type::sentry __cerb(__out);
471 streamsize __w = __out.width();
472 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__w + 1)));
474 streamsize __len = 1;
477 __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
480 __out.write(__pads, __len);
483 catch(exception& __fail)
485 // 27.6.1.2.1 Common requirements.
486 // Turn this on without causing an ios::failure to be thrown.
487 __out.setstate(ios_base::badbit);
488 if ((__out.exceptions() & ios_base::badbit) != 0)
489 __throw_exception_again;
496 template <class _Traits>
497 basic_ostream<char, _Traits>&
498 operator<<(basic_ostream<char, _Traits>& __out, char __c)
500 typedef basic_ostream<char, _Traits> __ostream_type;
501 typename __ostream_type::sentry __cerb(__out);
506 streamsize __w = __out.width();
507 char* __pads = static_cast<char*>(__builtin_alloca(__w + 1));
509 streamsize __len = 1;
512 __pad(__out, __out.fill(), __pads, &__c, __w, __len, false);
515 __out.write(__pads, __len);
518 catch(exception& __fail)
520 // 27.6.1.2.1 Common requirements.
521 // Turn this on without causing an ios::failure to be thrown.
522 __out.setstate(ios_base::badbit);
523 if ((__out.exceptions() & ios_base::badbit) != 0)
524 __throw_exception_again;
530 template<typename _CharT, typename _Traits>
531 basic_ostream<_CharT, _Traits>&
532 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
534 typedef basic_ostream<_CharT, _Traits> __ostream_type;
535 typename __ostream_type::sentry __cerb(__out);
540 streamsize __w = __out.width();
541 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
542 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
545 __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
549 __out.write(__s, __len);
552 catch(exception& __fail)
554 // 27.6.1.2.1 Common requirements.
555 // Turn this on without causing an ios::failure to be thrown.
556 __out.setstate(ios_base::badbit);
557 if ((__out.exceptions() & ios_base::badbit) != 0)
558 __throw_exception_again;
562 __out.setstate(ios_base::badbit);
566 template<typename _CharT, typename _Traits>
567 basic_ostream<_CharT, _Traits>&
568 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
570 typedef basic_ostream<_CharT, _Traits> __ostream_type;
571 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
572 // 167. Improper use of traits_type::length()
573 // Note that this is only in 'Review' status.
574 typedef char_traits<char> __traits_type;
576 typename __ostream_type::sentry __cerb(__out);
579 size_t __clen = __traits_type::length(__s);
580 _CharT* __ws = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * (__clen + 1)));
581 for (size_t __i = 0; __i < __clen; ++__i)
582 __ws[__i] = __out.widen(__s[__i]);
583 _CharT* __str = __ws;
587 streamsize __len = static_cast<streamsize>(__clen);
588 streamsize __w = __out.width();
589 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
593 __pad(__out, __out.fill(), __pads, __ws, __w, __len, false);
597 __out.write(__str, __len);
600 catch(exception& __fail)
602 // 27.6.1.2.1 Common requirements.
603 // Turn this on without causing an ios::failure to be thrown.
604 __out.setstate(ios_base::badbit);
605 if ((__out.exceptions() & ios_base::badbit) != 0)
606 __throw_exception_again;
610 __out.setstate(ios_base::badbit);
614 // Partial specializations.
615 template<class _Traits>
616 basic_ostream<char, _Traits>&
617 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
619 typedef basic_ostream<char, _Traits> __ostream_type;
620 typename __ostream_type::sentry __cerb(__out);
625 streamsize __w = __out.width();
626 char* __pads = static_cast<char*>(__builtin_alloca(__w));
627 streamsize __len = static_cast<streamsize>(_Traits::length(__s));
631 __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
635 __out.write(__s, __len);
638 catch(exception& __fail)
640 // 27.6.1.2.1 Common requirements.
641 // Turn this on without causing an ios::failure to be thrown.
642 __out.setstate(ios_base::badbit);
643 if ((__out.exceptions() & ios_base::badbit) != 0)
644 __throw_exception_again;
648 __out.setstate(ios_base::badbit);
652 // 21.3.7.9 basic_string::operator<<
653 template<typename _CharT, typename _Traits, typename _Alloc>
654 basic_ostream<_CharT, _Traits>&
655 operator<<(basic_ostream<_CharT, _Traits>& __out,
656 const basic_string<_CharT, _Traits, _Alloc>& __str)
658 typedef basic_ostream<_CharT, _Traits> __ostream_type;
659 typename __ostream_type::sentry __cerb(__out);
662 const _CharT* __s = __str.data();
663 streamsize __w = __out.width();
664 _CharT* __pads = static_cast<_CharT*>(__builtin_alloca(sizeof(_CharT) * __w));
665 streamsize __len = static_cast<streamsize>(__str.size());
666 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
667 // 25. String operator<< uses width() value wrong
671 __pad(__out, __out.fill(), __pads, __s, __w, __len, false);
675 streamsize __res = __out.rdbuf()->sputn(__s, __len);
678 __out.setstate(ios_base::failbit);
683 // Inhibit implicit instantiations for required instantiations,
684 // which are defined via explicit instantiations elsewhere.
685 // NB: This syntax is a GNU extension.
686 extern template class basic_ostream<char>;
687 extern template ostream& endl(ostream&);
688 extern template ostream& ends(ostream&);
689 extern template ostream& flush(ostream&);
690 extern template ostream& operator<<(ostream&, char);
691 extern template ostream& operator<<(ostream&, unsigned char);
692 extern template ostream& operator<<(ostream&, signed char);
693 extern template ostream& operator<<(ostream&, const char*);
694 extern template ostream& operator<<(ostream&, const unsigned char*);
695 extern template ostream& operator<<(ostream&, const signed char*);
697 extern template class basic_ostream<wchar_t>;
698 extern template wostream& endl(wostream&);
699 extern template wostream& ends(wostream&);
700 extern template wostream& flush(wostream&);
701 extern template wostream& operator<<(wostream&, wchar_t);
702 extern template wostream& operator<<(wostream&, char);
703 extern template wostream& operator<<(wostream&, const wchar_t*);
704 extern template wostream& operator<<(wostream&, const char*);