1 // Output streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
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
36 * This is a Standard C++ Library header. You should @c #include this header
37 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
40 #ifndef _GLIBCXX_OSTREAM
41 #define _GLIBCXX_OSTREAM 1
43 #pragma GCC system_header
49 // [27.6.2.1] Template class basic_ostream
51 * @brief Controlling output.
53 * This is the base class for all output streams. It provides text
54 * formatting of all builtin types, and communicates with any class
55 * derived from basic_streambuf to do the actual output.
57 template<typename _CharT
, typename _Traits
>
58 class basic_ostream
: virtual public basic_ios
<_CharT
, _Traits
>
61 // Types (inherited from basic_ios (27.4.4)):
62 typedef _CharT char_type
;
63 typedef typename
_Traits::int_type int_type
;
64 typedef typename
_Traits::pos_type pos_type
;
65 typedef typename
_Traits::off_type off_type
;
66 typedef _Traits traits_type
;
68 // Non-standard Types:
69 typedef basic_streambuf
<_CharT
, _Traits
> __streambuf_type
;
70 typedef basic_ios
<_CharT
, _Traits
> __ios_type
;
71 typedef basic_ostream
<_CharT
, _Traits
> __ostream_type
;
72 typedef num_put
<_CharT
, ostreambuf_iterator
<_CharT
, _Traits
> >
74 typedef ctype
<_CharT
> __ctype_type
;
76 template<typename _CharT2
, typename _Traits2
>
77 friend basic_ostream
<_CharT2
, _Traits2
>&
78 operator<<(basic_ostream
<_CharT2
, _Traits2
>&, _CharT2
);
80 template<typename _Traits2
>
81 friend basic_ostream
<char, _Traits2
>&
82 operator<<(basic_ostream
<char, _Traits2
>&, char);
84 template<typename _CharT2
, typename _Traits2
>
85 friend basic_ostream
<_CharT2
, _Traits2
>&
86 operator<<(basic_ostream
<_CharT2
, _Traits2
>&, const _CharT2
*);
88 template<typename _Traits2
>
89 friend basic_ostream
<char, _Traits2
>&
90 operator<<(basic_ostream
<char, _Traits2
>&, const char*);
92 template<typename _CharT2
, typename _Traits2
>
93 friend basic_ostream
<_CharT2
, _Traits2
>&
94 operator<<(basic_ostream
<_CharT2
, _Traits2
>&, const char*);
96 // [27.6.2.2] constructor/destructor
98 * @brief Base constructor.
100 * This ctor is almost never called by the user directly, rather from
101 * derived classes' initialization lists, which pass a pointer to
102 * their own stream buffer.
105 basic_ostream(__streambuf_type
* __sb
)
106 { this->init(__sb
); }
109 * @brief Base destructor.
111 * This does very little apart from providing a virtual base dtor.
116 // [27.6.2.3] prefix/suffix
120 // [27.6.2.5] formatted output
121 // [27.6.2.5.3] basic_ostream::operator<<
124 * @brief Interface for manipulators.
126 * Manuipulators such as @c std::endl and @c std::hex use these
127 * functions in constructs like "std::cout << std::endl". For more
128 * information, see the iomanip header.
130 inline __ostream_type
&
131 operator<<(__ostream_type
& (*__pf
)(__ostream_type
&));
133 inline __ostream_type
&
134 operator<<(__ios_type
& (*__pf
)(__ios_type
&));
136 inline __ostream_type
&
137 operator<<(ios_base
& (*__pf
) (ios_base
&));
140 // [27.6.2.5.2] arithmetic inserters
142 * @name Arithmetic Inserters
144 * All the @c operator<< functions (aka <em>formatted output
145 * functions</em>) have some common behavior. Each starts by
146 * constructing a temporary object of type std::basic_ostream::sentry.
147 * This can have several effects, concluding with the setting of a
148 * status flag; see the sentry documentation for more.
150 * If the sentry status is good, the function tries to generate
151 * whatever data is appropriate for the type of the argument.
153 * If an exception is thrown during insertion, ios_base::badbit
154 * will be turned on in the stream's error state without causing an
155 * ios_base::failure to be thrown. The original exception will then
160 * @brief Basic arithmetic inserters
161 * @param A variable of builtin type.
162 * @return @c *this if successful
164 * These functions use the stream's current locale (specifically, the
165 * @c num_get facet) to perform numeric formatting.
168 operator<<(long __n
);
171 operator<<(unsigned long __n
);
174 operator<<(bool __n
);
177 operator<<(short __n
)
179 ios_base::fmtflags __fmt
= this->flags() & ios_base::basefield
;
180 if (__fmt
& ios_base::oct
|| __fmt
& ios_base::hex
)
181 return this->operator<<(static_cast<unsigned long>
182 (static_cast<unsigned short>(__n
)));
184 return this->operator<<(static_cast<long>(__n
));
188 operator<<(unsigned short __n
)
189 { return this->operator<<(static_cast<unsigned long>(__n
)); }
194 ios_base::fmtflags __fmt
= this->flags() & ios_base::basefield
;
195 if (__fmt
& ios_base::oct
|| __fmt
& ios_base::hex
)
196 return this->operator<<(static_cast<unsigned long>
197 (static_cast<unsigned int>(__n
)));
199 return this->operator<<(static_cast<long>(__n
));
203 operator<<(unsigned int __n
)
204 { return this->operator<<(static_cast<unsigned long>(__n
)); }
206 #ifdef _GLIBCXX_USE_LONG_LONG
208 operator<<(long long __n
);
211 operator<<(unsigned long long __n
);
215 operator<<(double __f
);
218 operator<<(float __f
)
219 { return this->operator<<(static_cast<double>(__f
)); }
222 operator<<(long double __f
);
225 operator<<(const void* __p
);
228 * @brief Extracting from another streambuf.
229 * @param sb A pointer to a streambuf
231 * This function behaves like one of the basic arithmetic extractors,
232 * in that it also constructs a sentry object and has the same error
235 * If @a sb is NULL, the stream will set failbit in its error state.
237 * Characters are extracted from @a sb and inserted into @c *this
238 * until one of the following occurs:
240 * - the input stream reaches end-of-file,
241 * - insertion into the output sequence fails (in this case, the
242 * character that would have been inserted is not extracted), or
243 * - an exception occurs while getting a character from @a sb, which
244 * sets failbit in the error state
246 * If the function inserts no characters, failbit is set.
249 operator<<(__streambuf_type
* __sb
);
252 // [27.6.2.6] unformatted output functions
254 * @name Unformatted Output Functions
256 * All the unformatted output functions have some common behavior.
257 * Each starts by constructing a temporary object of type
258 * std::basic_ostream::sentry. This has several effects, concluding
259 * with the setting of a status flag; see the sentry documentation
262 * If the sentry status is good, the function tries to generate
263 * whatever data is appropriate for the type of the argument.
265 * If an exception is thrown during insertion, ios_base::badbit
266 * will be turned on in the stream's error state. If badbit is on in
267 * the stream's exceptions mask, the exception will be rethrown
268 * without completing its actions.
272 * @brief Simple insertion.
273 * @param c The character to insert.
276 * Tries to insert @a c.
278 * @note This function is not overloaded on signed char and
284 // Core write functionality, without sentry.
286 _M_write(const char_type
* __s
, streamsize __n
)
288 streamsize __put
= this->rdbuf()->sputn(__s
, __n
);
290 this->setstate(ios_base::badbit
);
294 * @brief Character string insertion.
295 * @param s The array to insert.
296 * @param n Maximum number of characters to insert.
299 * Characters are copied from @a s and inserted into the stream until
300 * one of the following happens:
302 * - @a n characters are inserted
303 * - inserting into the output sequence fails (in this case, badbit
304 * will be set in the stream's error state)
306 * @note This function is not overloaded on signed char and
310 write(const char_type
* __s
, streamsize __n
);
314 * @brief Synchronizing the stream buffer.
317 * If @c rdbuf() is a null pointer, changes nothing.
319 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
325 // [27.6.2.4] seek members
327 * @brief Getting the current write position.
328 * @return A file position object.
330 * If @c fail() is not false, returns @c pos_type(-1) to indicate
331 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
337 * @brief Changing the current write position.
338 * @param pos A file position object.
341 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
342 * that function fails, sets failbit.
348 * @brief Changing the current write position.
349 * @param off A file offset object.
350 * @param dir The direction in which to seek.
353 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
354 * If that function fails, sets failbit.
357 seekp(off_type
, ios_base::seekdir
);
365 * @brief Performs setup work for output streams.
367 * Objects of this class are created before all of the standard
368 * inserters are run. It is responsible for "exception-safe prefix and
369 * suffix operations." Additional actions may be added by the
370 * implementation, and we list them in
371 * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
372 * under [27.6] notes.
374 template <typename _CharT
, typename _Traits
>
375 class basic_ostream
<_CharT
, _Traits
>::sentry
379 basic_ostream
<_CharT
,_Traits
>& _M_os
;
383 * @brief The constructor performs preparatory work.
384 * @param os The output stream to guard.
386 * If the stream state is good (@a os.good() is true), then if the
387 * stream is tied to another output stream, @c is.tie()->flush()
388 * is called to synchronize the output sequences.
390 * If the stream state is still good, then the sentry state becomes
394 sentry(basic_ostream
<_CharT
,_Traits
>& __os
);
397 * @brief Possibly flushes the stream.
399 * If @c ios_base::unitbuf is set in @c os.flags(), and
400 * @c std::uncaught_exception() is true, the sentry destructor calls
401 * @c flush() on the output stream.
406 if (_M_os
.flags() & ios_base::unitbuf
&& !uncaught_exception())
408 // Can't call flush directly or else will get into recursive lock.
409 if (_M_os
.rdbuf() && _M_os
.rdbuf()->pubsync() == -1)
410 _M_os
.setstate(ios_base::badbit
);
415 * @brief Quick status checking.
416 * @return The sentry state.
418 * For ease of use, sentries may be converted to booleans. The
419 * return value is that of the sentry state (true == okay).
421 operator bool() const
425 // [27.6.2.5.4] character insertion templates
428 * @brief Character inserters
429 * @param out An output stream.
430 * @param c A character.
433 * Behaves like one of the formatted arithmetic inserters described in
434 * std::basic_ostream. After constructing a sentry object with good
435 * status, this function inserts a single character and any required
436 * padding (as determined by [22.2.2.2.2]). @c out.width(0) is then
439 * If @a c is of type @c char and the character type of the stream is not
440 * @c char, the character is widened before insertion.
442 template<typename _CharT
, typename _Traits
>
443 basic_ostream
<_CharT
, _Traits
>&
444 operator<<(basic_ostream
<_CharT
, _Traits
>& __out
, _CharT __c
);
446 template<typename _CharT
, typename _Traits
>
447 basic_ostream
<_CharT
, _Traits
>&
448 operator<<(basic_ostream
<_CharT
, _Traits
>& __out
, char __c
)
449 { return (__out
<< __out
.widen(__c
)); }
452 template <class _Traits
>
453 basic_ostream
<char, _Traits
>&
454 operator<<(basic_ostream
<char, _Traits
>& __out
, char __c
);
456 // Signed and unsigned
457 template<class _Traits
>
458 basic_ostream
<char, _Traits
>&
459 operator<<(basic_ostream
<char, _Traits
>& __out
, signed char __c
)
460 { return (__out
<< static_cast<char>(__c
)); }
462 template<class _Traits
>
463 basic_ostream
<char, _Traits
>&
464 operator<<(basic_ostream
<char, _Traits
>& __out
, unsigned char __c
)
465 { return (__out
<< static_cast<char>(__c
)); }
470 * @brief String inserters
471 * @param out An output stream.
472 * @param s A character string.
474 * @pre @a s must be a non-NULL pointer
476 * Behaves like one of the formatted arithmetic inserters described in
477 * std::basic_ostream. After constructing a sentry object with good
478 * status, this function inserts @c traits::length(s) characters starting
479 * at @a s, widened if necessary, followed by any required padding (as
480 * determined by [22.2.2.2.2]). @c out.width(0) is then called.
482 template<typename _CharT
, typename _Traits
>
483 basic_ostream
<_CharT
, _Traits
>&
484 operator<<(basic_ostream
<_CharT
, _Traits
>& __out
, const _CharT
* __s
);
486 template<typename _CharT
, typename _Traits
>
487 basic_ostream
<_CharT
, _Traits
> &
488 operator<<(basic_ostream
<_CharT
, _Traits
>& __out
, const char* __s
);
490 // Partial specializationss
491 template<class _Traits
>
492 basic_ostream
<char, _Traits
>&
493 operator<<(basic_ostream
<char, _Traits
>& __out
, const char* __s
);
495 // Signed and unsigned
496 template<class _Traits
>
497 basic_ostream
<char, _Traits
>&
498 operator<<(basic_ostream
<char, _Traits
>& __out
, const signed char* __s
)
499 { return (__out
<< reinterpret_cast<const char*>(__s
)); }
501 template<class _Traits
>
502 basic_ostream
<char, _Traits
> &
503 operator<<(basic_ostream
<char, _Traits
>& __out
, const unsigned char* __s
)
504 { return (__out
<< reinterpret_cast<const char*>(__s
)); }
507 // [27.6.2.7] standard basic_ostream manipulators
509 * @brief Write a newline and flush the stream.
511 * This manipulator is often mistakenly used when a simple newline is
512 * desired, leading to poor buffering performance. See
513 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
516 template<typename _CharT
, typename _Traits
>
517 basic_ostream
<_CharT
, _Traits
>&
518 endl(basic_ostream
<_CharT
, _Traits
>& __os
)
519 { return flush(__os
.put(__os
.widen('\n'))); }
522 * @brief Write a null character into the output sequence.
524 * "Null character" is @c CharT() by definition. For CharT of @c char,
525 * this correctly writes the ASCII @c NUL character string terminator.
527 template<typename _CharT
, typename _Traits
>
528 basic_ostream
<_CharT
, _Traits
>&
529 ends(basic_ostream
<_CharT
, _Traits
>& __os
)
530 { return __os
.put(_CharT()); }
533 * @brief Flushes the output stream.
535 * This manipulator simply calls the stream's @c flush() member function.
537 template<typename _CharT
, typename _Traits
>
538 basic_ostream
<_CharT
, _Traits
>&
539 flush(basic_ostream
<_CharT
, _Traits
>& __os
)
540 { return __os
.flush(); }
544 #ifndef _GLIBCXX_EXPORT_TEMPLATE
545 # include <bits/ostream.tcc>
548 #endif /* _GLIBCXX_OSTREAM */