source: trunk/src/gcc/libstdc++-v3/include/bits/ostream.tcc@ 1389

Last change on this file since 1389 was 2, checked in by bird, 23 years ago

Initial revision

  • Property cvs2svn:cvs-rev set to 1.1
  • Property svn:eol-style set to native
  • Property svn:executable set to *
File size: 20.3 KB
Line 
1// ostream classes -*- C++ -*-
2
3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4// Free Software Foundation, Inc.
5//
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)
10// any later version.
11
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.
16
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,
20// USA.
21
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.
30
31//
32// ISO C++ 14882: 27.6.2 Output streams
33//
34
35#pragma GCC system_header
36
37#include <locale>
38
39namespace std
40{
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)
45 {
46 // XXX MT
47 if (_M_ok && __os.tie())
48 __os.tie()->flush();
49 }
50
51 template<typename _CharT, typename _Traits>
52 basic_ostream<_CharT, _Traits>&
53 basic_ostream<_CharT, _Traits>::
54 operator<<(__ostream_type& (*__pf)(__ostream_type&))
55 {
56 sentry __cerb(*this);
57 if (__cerb)
58 {
59 try
60 { __pf(*this); }
61 catch(exception& __fail)
62 {
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;
68 }
69 }
70 return *this;
71 }
72
73 template<typename _CharT, typename _Traits>
74 basic_ostream<_CharT, _Traits>&
75 basic_ostream<_CharT, _Traits>::
76 operator<<(__ios_type& (*__pf)(__ios_type&))
77 {
78 sentry __cerb(*this);
79 if (__cerb)
80 {
81 try
82 { __pf(*this); }
83 catch(exception& __fail)
84 {
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;
90 }
91 }
92 return *this;
93 }
94
95 template<typename _CharT, typename _Traits>
96 basic_ostream<_CharT, _Traits>&
97 basic_ostream<_CharT, _Traits>::
98 operator<<(ios_base& (*__pf)(ios_base&))
99 {
100 sentry __cerb(*this);
101 if (__cerb)
102 {
103 try
104 { __pf(*this); }
105 catch(exception& __fail)
106 {
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;
112 }
113 }
114 return *this;
115 }
116
117 template<typename _CharT, typename _Traits>
118 basic_ostream<_CharT, _Traits>&
119 basic_ostream<_CharT, _Traits>::operator<<(__streambuf_type* __sbin)
120 {
121 sentry __cerb(*this);
122 if (__cerb && __sbin)
123 {
124 try
125 {
126 if (!__copy_streambufs(*this, __sbin, this->rdbuf()))
127 this->setstate(ios_base::failbit);
128 }
129 catch(exception& __fail)
130 {
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;
136 }
137 }
138 else if (!__sbin)
139 this->setstate(ios_base::badbit);
140 return *this;
141 }
142
143 template<typename _CharT, typename _Traits>
144 basic_ostream<_CharT, _Traits>&
145 basic_ostream<_CharT, _Traits>::operator<<(bool __n)
146 {
147 sentry __cerb(*this);
148 if (__cerb)
149 {
150 try
151 {
152 if (_M_check_facet(_M_fnumput))
153 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
154 this->setstate(ios_base::badbit);
155 }
156 catch(exception& __fail)
157 {
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;
163 }
164 }
165 return *this;
166 }
167
168 template<typename _CharT, typename _Traits>
169 basic_ostream<_CharT, _Traits>&
170 basic_ostream<_CharT, _Traits>::operator<<(long __n)
171 {
172 sentry __cerb(*this);
173 if (__cerb)
174 {
175 try
176 {
177 char_type __c = this->fill();
178 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
179 if (_M_check_facet(_M_fnumput))
180 {
181 bool __b = false;
182 if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
183 {
184 unsigned long __l = static_cast<unsigned long>(__n);
185 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
186 }
187 else
188 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
189 if (__b)
190 this->setstate(ios_base::badbit);
191 }
192 }
193 catch(exception& __fail)
194 {
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;
200 }
201 }
202 return *this;
203 }
204
205 template<typename _CharT, typename _Traits>
206 basic_ostream<_CharT, _Traits>&
207 basic_ostream<_CharT, _Traits>::operator<<(unsigned long __n)
208 {
209 sentry __cerb(*this);
210 if (__cerb)
211 {
212 try
213 {
214 if (_M_check_facet(_M_fnumput))
215 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
216 this->setstate(ios_base::badbit);
217 }
218 catch(exception& __fail)
219 {
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;
225 }
226 }
227 return *this;
228 }
229
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)
234 {
235 sentry __cerb(*this);
236 if (__cerb)
237 {
238 try
239 {
240 char_type __c = this->fill();
241 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
242 if (_M_check_facet(_M_fnumput))
243 {
244 bool __b = false;
245 if ((__fmt & ios_base::oct) || (__fmt & ios_base::hex))
246 {
247 unsigned long long __l;
248 __l = static_cast<unsigned long long>(__n);
249 __b = _M_fnumput->put(*this, *this, __c, __l).failed();
250 }
251 else
252 __b = _M_fnumput->put(*this, *this, __c, __n).failed();
253 if (__b)
254 this->setstate(ios_base::badbit);
255 }
256 }
257 catch(exception& __fail)
258 {
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;
264 }
265 }
266 return *this;
267 }
268
269 template<typename _CharT, typename _Traits>
270 basic_ostream<_CharT, _Traits>&
271 basic_ostream<_CharT, _Traits>::operator<<(unsigned long long __n)
272 {
273 sentry __cerb(*this);
274 if (__cerb)
275 {
276 try
277 {
278 if (_M_check_facet(_M_fnumput))
279 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
280 this->setstate(ios_base::badbit);
281 }
282 catch(exception& __fail)
283 {
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;
289 }
290 }
291 return *this;
292 }
293#endif
294
295 template<typename _CharT, typename _Traits>
296 basic_ostream<_CharT, _Traits>&
297 basic_ostream<_CharT, _Traits>::operator<<(double __n)
298 {
299 sentry __cerb(*this);
300 if (__cerb)
301 {
302 try
303 {
304 if (_M_check_facet(_M_fnumput))
305 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
306 this->setstate(ios_base::badbit);
307 }
308 catch(exception& __fail)
309 {
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;
315 }
316 }
317 return *this;
318 }
319
320 template<typename _CharT, typename _Traits>
321 basic_ostream<_CharT, _Traits>&
322 basic_ostream<_CharT, _Traits>::operator<<(long double __n)
323 {
324 sentry __cerb(*this);
325 if (__cerb)
326 {
327 try
328 {
329 if (_M_check_facet(_M_fnumput))
330 if (_M_fnumput->put(*this, *this, this->fill(), __n).failed())
331 this->setstate(ios_base::badbit);
332 }
333 catch(exception& __fail)
334 {
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;
340 }
341 }
342 return *this;