]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/std/ostream
auto_ptr.h: Fix comment typos.
[thirdparty/gcc.git] / libstdc++-v3 / include / std / ostream
CommitLineData
54c1bf78 1// Output streams -*- C++ -*-
de96ac46 2
f4e39278
PC
3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4// 2006, 2007
9b6e0e57 5// Free Software Foundation, Inc.
de96ac46
BK
6//
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 2, or (at your option)
11// any later version.
12
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.
17
af13a7a6
BK
18// You should have received a copy of the GNU General Public License
19// along with this library; see the file COPYING. If not, write to
20// the Free Software Foundation, 51 Franklin Street, Fifth Floor,
21// Boston, MA 02110-1301, USA.
de96ac46
BK
22
23// As a special exception, you may use this file as part of a free software
24// library without restriction. Specifically, if other files instantiate
25// templates or use macros or inline functions from this file, or you compile
26// this file and link it with other files to produce an executable, this
27// file does not by itself cause the resulting executable to be covered by
28// the GNU General Public License. This exception does not however
29// invalidate any other reasons why the executable file might be covered by
30// the GNU General Public License.
31
ffe94f83 32/** @file ostream
0aa06b18 33 * This is a Standard C++ Library header.
2f9d51b8
PE
34 */
35
143c27b0
BK
36//
37// ISO C++ 14882: 27.6.2 Output streams
38//
39
1143680e
SE
40#ifndef _GLIBCXX_OSTREAM
41#define _GLIBCXX_OSTREAM 1
54c1bf78
BK
42
43#pragma GCC system_header
44
45#include <ios>
11202768 46#include <bits/ostream_insert.h>
54c1bf78 47
0646d8a3
BK
48#ifdef __GXX_EXPERIMENTAL_CXX0X__
49# include <system_error>
50#endif
51
3cbc7af0
BK
52_GLIBCXX_BEGIN_NAMESPACE(std)
53
840ceb34
PE
54 // [27.6.2.1] Template class basic_ostream
55 /**
56 * @brief Controlling output.
57 *
58 * This is the base class for all output streams. It provides text
59 * formatting of all builtin types, and communicates with any class
60 * derived from basic_streambuf to do the actual output.
61 */
54c1bf78
BK
62 template<typename _CharT, typename _Traits>
63 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
64 {
65 public:
54c1bf78
BK
66 // Types (inherited from basic_ios (27.4.4)):
67 typedef _CharT char_type;
68 typedef typename _Traits::int_type int_type;
69 typedef typename _Traits::pos_type pos_type;
70 typedef typename _Traits::off_type off_type;
71 typedef _Traits traits_type;
72
73 // Non-standard Types:
74 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
75 typedef basic_ios<_CharT, _Traits> __ios_type;
76 typedef basic_ostream<_CharT, _Traits> __ostream_type;
2803847d
BK
77 typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
78 __num_put_type;
54c1bf78
BK
79 typedef ctype<_CharT> __ctype_type;
80
840ceb34
PE
81 // [27.6.2.2] constructor/destructor
82 /**
83 * @brief Base constructor.
84 *
85 * This ctor is almost never called by the user directly, rather from
86 * derived classes' initialization lists, which pass a pointer to
87 * their own stream buffer.
88 */
54c1bf78
BK
89 explicit
90 basic_ostream(__streambuf_type* __sb)
91 { this->init(__sb); }
92
840ceb34
PE
93 /**
94 * @brief Base destructor.
95 *
96 * This does very little apart from providing a virtual base dtor.
97 */
54c1bf78
BK
98 virtual
99 ~basic_ostream() { }
100
840ceb34 101 // [27.6.2.3] prefix/suffix
54c1bf78
BK
102 class sentry;
103 friend class sentry;
104
840ceb34
PE
105 // [27.6.2.5] formatted output
106 // [27.6.2.5.3] basic_ostream::operator<<
107 //@{
108 /**
109 * @brief Interface for manipulators.
110 *
28dac70a 111 * Manipulators such as @c std::endl and @c std::hex use these
840ceb34
PE
112 * functions in constructs like "std::cout << std::endl". For more
113 * information, see the iomanip header.
114 */
f7ab3fd1
PC
115 __ostream_type&
116 operator<<(__ostream_type& (*__pf)(__ostream_type&))
117 {
118 // _GLIBCXX_RESOLVE_LIB_DEFECTS
119 // DR 60. What is a formatted input function?
120 // The inserters for manipulators are *not* formatted output functions.
121 return __pf(*this);
122 }
123
124 __ostream_type&
125 operator<<(__ios_type& (*__pf)(__ios_type&))
126 {
127 // _GLIBCXX_RESOLVE_LIB_DEFECTS
128 // DR 60. What is a formatted input function?
129 // The inserters for manipulators are *not* formatted output functions.
130 __pf(*this);
131 return *this;
132 }
133
134 __ostream_type&
135 operator<<(ios_base& (*__pf) (ios_base&))
136 {
137 // _GLIBCXX_RESOLVE_LIB_DEFECTS
138 // DR 60. What is a formatted input function?
139 // The inserters for manipulators are *not* formatted output functions.
140 __pf(*this);
141 return *this;
142 }
840ceb34
PE
143 //@}
144
145 // [27.6.2.5.2] arithmetic inserters
146 /**
147 * @name Arithmetic Inserters
148 *
149 * All the @c operator<< functions (aka <em>formatted output
150 * functions</em>) have some common behavior. Each starts by
151 * constructing a temporary object of type std::basic_ostream::sentry.
152 * This can have several effects, concluding with the setting of a
153 * status flag; see the sentry documentation for more.
154 *
155 * If the sentry status is good, the function tries to generate
156 * whatever data is appropriate for the type of the argument.
157 *
158 * If an exception is thrown during insertion, ios_base::badbit
159 * will be turned on in the stream's error state without causing an
160 * ios_base::failure to be thrown. The original exception will then
161 * be rethrown.
162 */
163 //@{
164 /**
165 * @brief Basic arithmetic inserters
166 * @param A variable of builtin type.
167 * @return @c *this if successful
168 *
169 * These functions use the stream's current locale (specifically, the
170 * @c num_get facet) to perform numeric formatting.
171 */
54c1bf78 172 __ostream_type&
49d5c016
PC
173 operator<<(long __n)
174 { return _M_insert(__n); }
54c1bf78
BK
175
176 __ostream_type&
49d5c016
PC
177 operator<<(unsigned long __n)
178 { return _M_insert(__n); }
54c1bf78
BK
179
180 __ostream_type&
49d5c016
PC
181 operator<<(bool __n)
182 { return _M_insert(__n); }
54c1bf78
BK
183
184 __ostream_type&
e7968bd8 185 operator<<(short __n);
54c1bf78
BK
186
187 __ostream_type&
49d5c016
PC
188 operator<<(unsigned short __n)
189 {
190 // _GLIBCXX_RESOLVE_LIB_DEFECTS
191 // 117. basic_ostream uses nonexistent num_put member functions.
192 return _M_insert(static_cast<unsigned long>(__n));
193 }
54c1bf78
BK
194
195 __ostream_type&
e7968bd8 196 operator<<(int __n);
54c1bf78
BK
197
198 __ostream_type&
49d5c016
PC
199 operator<<(unsigned int __n)
200 {
201 // _GLIBCXX_RESOLVE_LIB_DEFECTS
202 // 117. basic_ostream uses nonexistent num_put member functions.
203 return _M_insert(static_cast<unsigned long>(__n));
204 }
54c1bf78 205
3d7c150e 206#ifdef _GLIBCXX_USE_LONG_LONG
54c1bf78 207 __ostream_type&
49d5c016
PC
208 operator<<(long long __n)
209 { return _M_insert(__n); }
54c1bf78
BK
210
211 __ostream_type&
49d5c016
PC
212 operator<<(unsigned long long __n)
213 { return _M_insert(__n); }
54c1bf78
BK
214#endif
215
216 __ostream_type&
49d5c016
PC
217 operator<<(double __f)
218 { return _M_insert(__f); }
54c1bf78
BK
219
220 __ostream_type&
49d5c016
PC
221 operator<<(float __f)
222 {
223 // _GLIBCXX_RESOLVE_LIB_DEFECTS
224 // 117. basic_ostream uses nonexistent num_put member functions.
225 return _M_insert(static_cast<double>(__f));
226 }
54c1bf78
BK
227
228 __ostream_type&
49d5c016
PC
229 operator<<(long double __f)
230 { return _M_insert(__f); }
54c1bf78
BK
231
232 __ostream_type&
49d5c016
PC
233 operator<<(const void* __p)
234 { return _M_insert(__p); }
54c1bf78 235
840ceb34
PE
236 /**
237 * @brief Extracting from another streambuf.
238 * @param sb A pointer to a streambuf
239 *
240 * This function behaves like one of the basic arithmetic extractors,
0f35d192 241 * in that it also constructs a sentry object and has the same error
840ceb34
PE
242 * handling behavior.
243 *
244 * If @a sb is NULL, the stream will set failbit in its error state.
245 *
246 * Characters are extracted from @a sb and inserted into @c *this
247 * until one of the following occurs:
248 *
249 * - the input stream reaches end-of-file,
250 * - insertion into the output sequence fails (in this case, the
251 * character that would have been inserted is not extracted), or
252 * - an exception occurs while getting a character from @a sb, which
253 * sets failbit in the error state
254 *
255 * If the function inserts no characters, failbit is set.
256 */
54c1bf78
BK
257 __ostream_type&
258 operator<<(__streambuf_type* __sb);
840ceb34
PE
259 //@}
260
261 // [27.6.2.6] unformatted output functions
262 /**
263 * @name Unformatted Output Functions
264 *
265 * All the unformatted output functions have some common behavior.
266 * Each starts by constructing a temporary object of type
267 * std::basic_ostream::sentry. This has several effects, concluding
268 * with the setting of a status flag; see the sentry documentation
269 * for more.
270 *
271 * If the sentry status is good, the function tries to generate
272 * whatever data is appropriate for the type of the argument.
273 *
274 * If an exception is thrown during insertion, ios_base::badbit
275 * will be turned on in the stream's error state. If badbit is on in
276 * the stream's exceptions mask, the exception will be rethrown
277 * without completing its actions.
278 */
279 //@{
280 /**
281 * @brief Simple insertion.
282 * @param c The character to insert.
283 * @return *this
284 *
285 * Tries to insert @a c.
286 *
287 * @note This function is not overloaded on signed char and
288 * unsigned char.
289 */
54c1bf78
BK
290 __ostream_type&
291 put(char_type __c);
292
8d0a564b
JQ
293 // Core write functionality, without sentry.
294 void
295 _M_write(const char_type* __s, streamsize __n)
296 {
ec2061a9 297 const streamsize __put = this->rdbuf()->sputn(__s, __n);
8d0a564b
JQ
298 if (__put != __n)
299 this->setstate(ios_base::badbit);
300 }
301
840ceb34
PE
302 /**
303 * @brief Character string insertion.
304 * @param s The array to insert.
305 * @param n Maximum number of characters to insert.
306 * @return *this
307 *
308 * Characters are copied from @a s and inserted into the stream until
309 * one of the following happens:
310 *
311 * - @a n characters are inserted
312 * - inserting into the output sequence fails (in this case, badbit
313 * will be set in the stream's error state)
314 *
315 * @note This function is not overloaded on signed char and
316 * unsigned char.
317 */
54c1bf78
BK
318 __ostream_type&
319 write(const char_type* __s, streamsize __n);
840ceb34
PE
320 //@}
321
322 /**
323 * @brief Synchronizing the stream buffer.
324 * @return *this
325 *
326 * If @c rdbuf() is a null pointer, changes nothing.
327 *
328 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
329 * sets badbit.
330 */
54c1bf78
BK
331 __ostream_type&
332 flush();
333
840ceb34
PE
334 // [27.6.2.4] seek members
335 /**
336 * @brief Getting the current write position.
337 * @return A file position object.
338 *
339 * If @c fail() is not false, returns @c pos_type(-1) to indicate
340 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
341 */
54c1bf78
BK
342 pos_type
343 tellp();
344
840ceb34
PE
345 /**
346 * @brief Changing the current write position.
347 * @param pos A file position object.
348 * @return *this
349 *
350 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
351 * that function fails, sets failbit.
352 */
54c1bf78
BK
353 __ostream_type&
354 seekp(pos_type);
355
840ceb34
PE
356 /**
357 * @brief Changing the current write position.
358 * @param off A file offset object.
359 * @param dir The direction in which to seek.
360 * @return *this
361 *
362 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
363 * If that function fails, sets failbit.
364 */
365 __ostream_type&
54c1bf78 366 seekp(off_type, ios_base::seekdir);
d29cc32f
BK
367
368 protected:
369 explicit
370 basic_ostream() { }
49d5c016
PC
371
372 template<typename _ValueT>
373 __ostream_type&
374 _M_insert(_ValueT __v);
54c1bf78
BK
375 };
376
840ceb34
PE
377 /**
378 * @brief Performs setup work for output streams.
379 *
380 * Objects of this class are created before all of the standard
381 * inserters are run. It is responsible for "exception-safe prefix and
382 * suffix operations." Additional actions may be added by the
383 * implementation, and we list them in
384 * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
385 * under [27.6] notes.
386 */
54c1bf78
BK
387 template <typename _CharT, typename _Traits>
388 class basic_ostream<_CharT, _Traits>::sentry
389 {
390 // Data Members:
391 bool _M_ok;
f7ab3fd1 392 basic_ostream<_CharT, _Traits>& _M_os;
54c1bf78
BK
393
394 public:
840ceb34
PE
395 /**
396 * @brief The constructor performs preparatory work.
397 * @param os The output stream to guard.
398 *
399 * If the stream state is good (@a os.good() is true), then if the
400 * stream is tied to another output stream, @c is.tie()->flush()
401 * is called to synchronize the output sequences.
402 *
403 * If the stream state is still good, then the sentry state becomes
404 * true ("okay").
405 */
54c1bf78 406 explicit
f7ab3fd1 407 sentry(basic_ostream<_CharT, _Traits>& __os);
54c1bf78 408
840ceb34
PE
409 /**
410 * @brief Possibly flushes the stream.
411 *
412 * If @c ios_base::unitbuf is set in @c os.flags(), and
413 * @c std::uncaught_exception() is true, the sentry destructor calls
414 * @c flush() on the output stream.
415 */
54c1bf78
BK
416 ~sentry()
417 {
418 // XXX MT
7f786096 419 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
54c1bf78
BK
420 {
421 // Can't call flush directly or else will get into recursive lock.
422 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
423 _M_os.setstate(ios_base::badbit);
424 }
425 }
426
840ceb34
PE
427 /**
428 * @brief Quick status checking.
429 * @return The sentry state.
430 *
431 * For ease of use, sentries may be converted to booleans. The
432 * return value is that of the sentry state (true == okay).
433 */
afb6c265 434 operator bool() const
54c1bf78
BK
435 { return _M_ok; }
436 };
437
840ceb34
PE
438 // [27.6.2.5.4] character insertion templates
439 //@{
440 /**
441 * @brief Character inserters
442 * @param out An output stream.
443 * @param c A character.
444 * @return out
445 *
446 * Behaves like one of the formatted arithmetic inserters described in
447 * std::basic_ostream. After constructing a sentry object with good
448 * status, this function inserts a single character and any required
449 * padding (as determined by [22.2.2.2.2]). @c out.width(0) is then
450 * called.
451 *
452 * If @a c is of type @c char and the character type of the stream is not
453 * @c char, the character is widened before insertion.
454 */
54c1bf78 455 template<typename _CharT, typename _Traits>
f7ab3fd1 456 inline basic_ostream<_CharT, _Traits>&
ec2061a9 457 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
11202768 458 { return __ostream_insert(__out, &__c, 1); }
54c1bf78
BK
459
460 template<typename _CharT, typename _Traits>
f7ab3fd1 461 inline basic_ostream<_CharT, _Traits>&
54c1bf78
BK
462 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
463 { return (__out << __out.widen(__c)); }
464
465 // Specialization
466 template <class _Traits>
f7ab3fd1 467 inline basic_ostream<char, _Traits>&
ec2061a9 468 operator<<(basic_ostream<char, _Traits>& __out, char __c)
11202768 469 { return __ostream_insert(__out, &__c, 1); }
54c1bf78
BK
470
471 // Signed and unsigned
472 template<class _Traits>
f7ab3fd1 473 inline basic_ostream<char, _Traits>&
54c1bf78
BK
474 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
475 { return (__out << static_cast<char>(__c)); }
476
477 template<class _Traits>
f7ab3fd1 478 inline basic_ostream<char, _Traits>&
54c1bf78
BK
479 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
480 { return (__out << static_cast<char>(__c)); }
840ceb34 481 //@}
54c1bf78 482
840ceb34
PE
483 //@{
484 /**
485 * @brief String inserters
486 * @param out An output stream.
487 * @param s A character string.
488 * @return out
489 * @pre @a s must be a non-NULL pointer
490 *
491 * Behaves like one of the formatted arithmetic inserters described in
492 * std::basic_ostream. After constructing a sentry object with good
493 * status, this function inserts @c traits::length(s) characters starting
494 * at @a s, widened if necessary, followed by any required padding (as
495 * determined by [22.2.2.2.2]). @c out.width(0) is then called.
496 */
54c1bf78 497 template<typename _CharT, typename _Traits>
f7ab3fd1 498 inline basic_ostream<_CharT, _Traits>&
ec2061a9
PC
499 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
500 {
501 if (!__s)
502 __out.setstate(ios_base::badbit);
503 else
11202768
PC
504 __ostream_insert(__out, __s,
505 static_cast<streamsize>(_Traits::length(__s)));
ec2061a9
PC
506 return __out;
507 }
54c1bf78
BK
508
509 template<typename _CharT, typename _Traits>
510 basic_ostream<_CharT, _Traits> &
511 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
512
28dac70a 513 // Partial specializations
54c1bf78 514 template<class _Traits>
f7ab3fd1 515 inline basic_ostream<char, _Traits>&
ec2061a9
PC
516 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
517 {
518 if (!__s)
519 __out.setstate(ios_base::badbit);
520 else
11202768
PC
521 __ostream_insert(__out, __s,
522 static_cast<streamsize>(_Traits::length(__s)));
ec2061a9
PC
523 return __out;
524 }
525
54c1bf78
BK
526 // Signed and unsigned
527 template<class _Traits>
f7ab3fd1 528 inline basic_ostream<char, _Traits>&
54c1bf78
BK
529 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
530 { return (__out << reinterpret_cast<const char*>(__s)); }
531
532 template<class _Traits>
f7ab3fd1 533 inline basic_ostream<char, _Traits> &
54c1bf78
BK
534 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
535 { return (__out << reinterpret_cast<const char*>(__s)); }
840ceb34
PE
536 //@}
537
0646d8a3
BK
538#ifdef __GXX_EXPERIMENTAL_CXX0X__
539 template<typename _CharT, typename _Traits>
540 inline basic_ostream<_CharT, _Traits>&
541 operator<<(basic_ostream<_CharT, _Traits>& __out, const error_code& __e)
bd1a56a0 542 { return (__out << __e.category().name().c_str() << ':' << __e.value()); }
0646d8a3
BK
543#endif
544
840ceb34
PE
545 // [27.6.2.7] standard basic_ostream manipulators
546 /**
547 * @brief Write a newline and flush the stream.
548 *
549 * This manipulator is often mistakenly used when a simple newline is
550 * desired, leading to poor buffering performance. See
551 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
552 * on this subject.
553 */
54c1bf78 554 template<typename _CharT, typename _Traits>
f7ab3fd1 555 inline basic_ostream<_CharT, _Traits>&
54c1bf78
BK
556 endl(basic_ostream<_CharT, _Traits>& __os)
557 { return flush(__os.put(__os.widen('\n'))); }
558
840ceb34
PE
559 /**
560 * @brief Write a null character into the output sequence.
561 *
562 * "Null character" is @c CharT() by definition. For CharT of @c char,
563 * this correctly writes the ASCII @c NUL character string terminator.
564 */
54c1bf78 565 template<typename _CharT, typename _Traits>
f7ab3fd1 566 inline basic_ostream<_CharT, _Traits>&
54c1bf78
BK
567 ends(basic_ostream<_CharT, _Traits>& __os)
568 { return __os.put(_CharT()); }
569
840ceb34
PE
570 /**
571 * @brief Flushes the output stream.
572 *
573 * This manipulator simply calls the stream's @c flush() member function.
574 */
54c1bf78 575 template<typename _CharT, typename _Traits>
f7ab3fd1 576 inline basic_ostream<_CharT, _Traits>&
54c1bf78
BK
577 flush(basic_ostream<_CharT, _Traits>& __os)
578 { return __os.flush(); }
579
3cbc7af0 580_GLIBCXX_END_NAMESPACE
54c1bf78 581
5f697f7a 582#ifndef _GLIBCXX_EXPORT_TEMPLATE
54c1bf78
BK
583# include <bits/ostream.tcc>
584#endif
54c1bf78 585
1143680e 586#endif /* _GLIBCXX_OSTREAM */