1 // ostream classes -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011
5 // Free Software Foundation, Inc.
7 // This file is part of the GNU ISO C++ Library. This library is free
8 // software; you can redistribute it and/or modify it under the
9 // terms of the GNU General Public License as published by the
10 // Free Software Foundation; either version 3, or (at your option)
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // Under Section 7 of GPL version 3, you are granted additional
19 // permissions described in the GCC Runtime Library Exception, version
20 // 3.1, as published by the Free Software Foundation.
22 // You should have received a copy of the GNU General Public License and
23 // a copy of the GCC Runtime Library Exception along with this program;
24 // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
25 // <http://www.gnu.org/licenses/>.
27 /** @file bits/ostream.tcc
28 * This is an internal header file, included by other library headers.
29 * Do not attempt to use it directly. @headername{ostream}
33 // ISO C++ 14882: 27.6.2 Output streams
37 #define _OSTREAM_TCC 1
39 #pragma GCC system_header
41 #include <bits/cxxabi_forced.h>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 template<typename _CharT, typename _Traits>
48 basic_ostream<_CharT, _Traits>::sentry::
49 sentry(basic_ostream<_CharT, _Traits>& __os)
50 : _M_ok(false), _M_os(__os)
53 if (__os.tie() && __os.good())
59 __os.setstate(ios_base::failbit);
62 template<typename _CharT, typename _Traits>
63 template<typename _ValueT>
64 basic_ostream<_CharT, _Traits>&
65 basic_ostream<_CharT, _Traits>::
66 _M_insert(_ValueT __v)
71 ios_base::iostate __err = ios_base::goodbit;
74 const __num_put_type& __np = __check_facet(this->_M_num_put);
75 if (__np.put(*this, *this, this->fill(), __v).failed())
76 __err |= ios_base::badbit;
78 __catch(__cxxabiv1::__forced_unwind&)
80 this->_M_setstate(ios_base::badbit);
81 __throw_exception_again;
84 { this->_M_setstate(ios_base::badbit); }
86 this->setstate(__err);
91 template<typename _CharT, typename _Traits>
92 basic_ostream<_CharT, _Traits>&
93 basic_ostream<_CharT, _Traits>::
96 // _GLIBCXX_RESOLVE_LIB_DEFECTS
97 // 117. basic_ostream uses nonexistent num_put member functions.
98 const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
99 if (__fmt == ios_base::oct || __fmt == ios_base::hex)
100 return _M_insert(static_cast<long>(static_cast<unsigned short>(__n)));
102 return _M_insert(static_cast<long>(__n));
105 template<typename _CharT, typename _Traits>
106 basic_ostream<_CharT, _Traits>&
107 basic_ostream<_CharT, _Traits>::
110 // _GLIBCXX_RESOLVE_LIB_DEFECTS
111 // 117. basic_ostream uses nonexistent num_put member functions.
112 const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
113 if (__fmt == ios_base::oct || __fmt == ios_base::hex)
114 return _M_insert(static_cast<long>(static_cast<unsigned int>(__n)));
116 return _M_insert(static_cast<long>(__n));
119 template<typename _CharT, typename _Traits>
120 basic_ostream<_CharT, _Traits>&
121 basic_ostream<_CharT, _Traits>::
122 operator<<(__streambuf_type* __sbin)
124 ios_base::iostate __err = ios_base::goodbit;
125 sentry __cerb(*this);
126 if (__cerb && __sbin)
130 if (!__copy_streambufs(__sbin, this->rdbuf()))
131 __err |= ios_base::failbit;
133 __catch(__cxxabiv1::__forced_unwind&)
135 this->_M_setstate(ios_base::badbit);
136 __throw_exception_again;
139 { this->_M_setstate(ios_base::failbit); }
142 __err |= ios_base::badbit;
144 this->setstate(__err);
148 template<typename _CharT, typename _Traits>
149 basic_ostream<_CharT, _Traits>&
150 basic_ostream<_CharT, _Traits>::
153 // _GLIBCXX_RESOLVE_LIB_DEFECTS
154 // DR 60. What is a formatted input function?
155 // basic_ostream::put(char_type) is an unformatted output function.
156 // DR 63. Exception-handling policy for unformatted output.
157 // Unformatted output functions should catch exceptions thrown
158 // from streambuf members.
159 sentry __cerb(*this);
162 ios_base::iostate __err = ios_base::goodbit;
165 const int_type __put = this->rdbuf()->sputc(__c);
166 if (traits_type::eq_int_type(__put, traits_type::eof()))
167 __err |= ios_base::badbit;
169 __catch(__cxxabiv1::__forced_unwind&)
171 this->_M_setstate(ios_base::badbit);
172 __throw_exception_again;
175 { this->_M_setstate(ios_base::badbit); }
177 this->setstate(__err);
182 template<typename _CharT, typename _Traits>
183 basic_ostream<_CharT, _Traits>&
184 basic_ostream<_CharT, _Traits>::
185 write(const _CharT* __s, streamsize __n)
187 // _GLIBCXX_RESOLVE_LIB_DEFECTS
188 // DR 60. What is a formatted input function?
189 // basic_ostream::write(const char_type*, streamsize) is an
190 // unformatted output function.
191 // DR 63. Exception-handling policy for unformatted output.
192 // Unformatted output functions should catch exceptions thrown
193 // from streambuf members.
194 sentry __cerb(*this);
198 { _M_write(__s, __n); }
199 __catch(__cxxabiv1::__forced_unwind&)
201 this->_M_setstate(ios_base::badbit);
202 __throw_exception_again;
205 { this->_M_setstate(ios_base::badbit); }
210 template<typename _CharT, typename _Traits>
211 basic_ostream<_CharT, _Traits>&
212 basic_ostream<_CharT, _Traits>::
215 // _GLIBCXX_RESOLVE_LIB_DEFECTS
216 // DR 60. What is a formatted input function?
217 // basic_ostream::flush() is *not* an unformatted output function.
218 ios_base::iostate __err = ios_base::goodbit;
221 if (this->rdbuf() && this->rdbuf()->pubsync() == -1)
222 __err |= ios_base::badbit;
224 __catch(__cxxabiv1::__forced_unwind&)
226 this->_M_setstate(ios_base::badbit);
227 __throw_exception_again;
230 { this->_M_setstate(ios_base::badbit); }
232 this->setstate(__err);
236 template<typename _CharT, typename _Traits>
237 typename basic_ostream<_CharT, _Traits>::pos_type
238 basic_ostream<_CharT, _Traits>::
241 pos_type __ret = pos_type(-1);
245 __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out);
247 __catch(__cxxabiv1::__forced_unwind&)
249 this->_M_setstate(ios_base::badbit);
250 __throw_exception_again;
253 { this->_M_setstate(ios_base::badbit); }
257 template<typename _CharT, typename _Traits>
258 basic_ostream<_CharT, _Traits>&
259 basic_ostream<_CharT, _Traits>::
260 seekp(pos_type __pos)
262 ios_base::iostate __err = ios_base::goodbit;
267 // _GLIBCXX_RESOLVE_LIB_DEFECTS
268 // 136. seekp, seekg setting wrong streams?
269 const pos_type __p = this->rdbuf()->pubseekpos(__pos,
272 // 129. Need error indication from seekp() and seekg()
273 if (__p == pos_type(off_type(-1)))
274 __err |= ios_base::failbit;
277 __catch(__cxxabiv1::__forced_unwind&)
279 this->_M_setstate(ios_base::badbit);
280 __throw_exception_again;
283 { this->_M_setstate(ios_base::badbit); }
285 this->setstate(__err);
289 template<typename _CharT, typename _Traits>
290 basic_ostream<_CharT, _Traits>&
291 basic_ostream<_CharT, _Traits>::
292 seekp(off_type __off, ios_base::seekdir __dir)
294 ios_base::iostate __err = ios_base::goodbit;
299 // _GLIBCXX_RESOLVE_LIB_DEFECTS
300 // 136. seekp, seekg setting wrong streams?
301 const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir,
304 // 129. Need error indication from seekp() and seekg()
305 if (__p == pos_type(off_type(-1)))
306 __err |= ios_base::failbit;
309 __catch(__cxxabiv1::__forced_unwind&)
311 this->_M_setstate(ios_base::badbit);
312 __throw_exception_again;
315 { this->_M_setstate(ios_base::badbit); }
317 this->setstate(__err);
321 template<typename _CharT, typename _Traits>
322 basic_ostream<_CharT, _Traits>&
323 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s)
326 __out.setstate(ios_base::badbit);
329 // _GLIBCXX_RESOLVE_LIB_DEFECTS
330 // 167. Improper use of traits_type::length()
331 const size_t __clen = char_traits<char>::length(__s);
337 __ptr_guard (_CharT *__ip): __p(__ip) { }
338 ~__ptr_guard() { delete[] __p; }
339 _CharT* __get() { return __p; }
340 } __pg (new _CharT[__clen]);
342 _CharT *__ws = __pg.__get();
343 for (size_t __i = 0; __i < __clen; ++__i)
344 __ws[__i] = __out.widen(__s[__i]);
345 __ostream_insert(__out, __ws, __clen);
347 __catch(__cxxabiv1::__forced_unwind&)
349 __out._M_setstate(ios_base::badbit);
350 __throw_exception_again;
353 { __out._M_setstate(ios_base::badbit); }
358 // Inhibit implicit instantiations for required instantiations,
359 // which are defined via explicit instantiations elsewhere.
360 #if _GLIBCXX_EXTERN_TEMPLATE
361 extern template class basic_ostream<char>;
362 extern template ostream& endl(ostream&);
363 extern template ostream& ends(ostream&);
364 extern template ostream& flush(ostream&);
365 extern template ostream& operator<<(ostream&, char);
366 extern template ostream& operator<<(ostream&, unsigned char);
367 extern template ostream& operator<<(ostream&, signed char);
368 extern template ostream& operator<<(ostream&, const char*);
369 extern template ostream& operator<<(ostream&, const unsigned char*);
370 extern template ostream& operator<<(ostream&, const signed char*);
372 extern template ostream& ostream::_M_insert(long);
373 extern template ostream& ostream::_M_insert(unsigned long);
374 extern template ostream& ostream::_M_insert(bool);
375 #ifdef _GLIBCXX_USE_LONG_LONG
376 extern template ostream& ostream::_M_insert(long long);
377 extern template ostream& ostream::_M_insert(unsigned long long);
379 extern template ostream& ostream::_M_insert(double);
380 extern template ostream& ostream::_M_insert(long double);
381 extern template ostream& ostream::_M_insert(const void*);
383 #ifdef _GLIBCXX_USE_WCHAR_T
384 extern template class basic_ostream<wchar_t>;
385 extern template wostream& endl(wostream&);
386 extern template wostream& ends(wostream&);
387 extern template wostream& flush(wostream&);
388 extern template wostream& operator<<(wostream&, wchar_t);
389 extern template wostream& operator<<(wostream&, char);
390 extern template wostream& operator<<(wostream&, const wchar_t*);
391 extern template wostream& operator<<(wostream&, const char*);
393 extern template wostream& wostream::_M_insert(long);
394 extern template wostream& wostream::_M_insert(unsigned long);
395 extern template wostream& wostream::_M_insert(bool);
396 #ifdef _GLIBCXX_USE_LONG_LONG
397 extern template wostream& wostream::_M_insert(long long);
398 extern template wostream& wostream::_M_insert(unsigned long long);
400 extern template wostream& wostream::_M_insert(double);
401 extern template wostream& wostream::_M_insert(long double);
402 extern template wostream& wostream::_M_insert(const void*);
406 _GLIBCXX_END_NAMESPACE_VERSION