1 // Output streams -*- 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 include/ostream
28 * This is a Standard C++ Library header.
32 // ISO C++ 14882: 27.6.2 Output streams
35 #ifndef _GLIBCXX_OSTREAM
36 #define _GLIBCXX_OSTREAM 1
38 #pragma GCC system_header
41 #include <bits/ostream_insert.h>
43 namespace std _GLIBCXX_VISIBILITY(default)
45 _GLIBCXX_BEGIN_NAMESPACE_VERSION
47 // [27.6.2.1] Template class basic_ostream
49 * @brief Controlling output.
52 * This is the base class for all output streams. It provides text
53 * formatting of all builtin types, and communicates with any class
54 * derived from basic_streambuf to do the actual output.
56 template<typename _CharT, typename _Traits>
57 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
60 // Types (inherited from basic_ios (27.4.4)):
61 typedef _CharT char_type;
62 typedef typename _Traits::int_type int_type;
63 typedef typename _Traits::pos_type pos_type;
64 typedef typename _Traits::off_type off_type;
65 typedef _Traits traits_type;
67 // Non-standard Types:
68 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
69 typedef basic_ios<_CharT, _Traits> __ios_type;
70 typedef basic_ostream<_CharT, _Traits> __ostream_type;
71 typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
73 typedef ctype<_CharT> __ctype_type;
75 // [27.6.2.2] constructor/destructor
77 * @brief Base constructor.
79 * This ctor is almost never called by the user directly, rather from
80 * derived classes' initialization lists, which pass a pointer to
81 * their own stream buffer.
84 basic_ostream(__streambuf_type* __sb)
88 * @brief Base destructor.
90 * This does very little apart from providing a virtual base dtor.
95 // [27.6.2.3] prefix/suffix
99 // [27.6.2.5] formatted output
100 // [27.6.2.5.3] basic_ostream::operator<<
103 * @brief Interface for manipulators.
105 * Manipulators such as @c std::endl and @c std::hex use these
106 * functions in constructs like "std::cout << std::endl". For more
107 * information, see the iomanip header.
110 operator<<(__ostream_type& (*__pf)(__ostream_type&))
112 // _GLIBCXX_RESOLVE_LIB_DEFECTS
113 // DR 60. What is a formatted input function?
114 // The inserters for manipulators are *not* formatted output functions.
119 operator<<(__ios_type& (*__pf)(__ios_type&))
121 // _GLIBCXX_RESOLVE_LIB_DEFECTS
122 // DR 60. What is a formatted input function?
123 // The inserters for manipulators are *not* formatted output functions.
129 operator<<(ios_base& (*__pf) (ios_base&))
131 // _GLIBCXX_RESOLVE_LIB_DEFECTS
132 // DR 60. What is a formatted input function?
133 // The inserters for manipulators are *not* formatted output functions.
139 // [27.6.2.5.2] arithmetic inserters
141 * @name Arithmetic Inserters
143 * All the @c operator<< functions (aka <em>formatted output
144 * functions</em>) have some common behavior. Each starts by
145 * constructing a temporary object of type std::basic_ostream::sentry.
146 * This can have several effects, concluding with the setting of a
147 * status flag; see the sentry documentation for more.
149 * If the sentry status is good, the function tries to generate
150 * whatever data is appropriate for the type of the argument.
152 * If an exception is thrown during insertion, ios_base::badbit
153 * will be turned on in the stream's error state without causing an
154 * ios_base::failure to be thrown. The original exception will then
159 * @brief Basic arithmetic inserters
160 * @param A variable of builtin type.
161 * @return @c *this if successful
163 * These functions use the stream's current locale (specifically, the
164 * @c num_get facet) to perform numeric formatting.
168 { return _M_insert(__n); }
171 operator<<(unsigned long __n)
172 { return _M_insert(__n); }
176 { return _M_insert(__n); }
179 operator<<(short __n);
182 operator<<(unsigned short __n)
184 // _GLIBCXX_RESOLVE_LIB_DEFECTS
185 // 117. basic_ostream uses nonexistent num_put member functions.
186 return _M_insert(static_cast<unsigned long>(__n));
193 operator<<(unsigned int __n)
195 // _GLIBCXX_RESOLVE_LIB_DEFECTS
196 // 117. basic_ostream uses nonexistent num_put member functions.
197 return _M_insert(static_cast<unsigned long>(__n));
200 #ifdef _GLIBCXX_USE_LONG_LONG
202 operator<<(long long __n)
203 { return _M_insert(__n); }
206 operator<<(unsigned long long __n)
207 { return _M_insert(__n); }
211 operator<<(double __f)
212 { return _M_insert(__f); }
215 operator<<(float __f)
217 // _GLIBCXX_RESOLVE_LIB_DEFECTS
218 // 117. basic_ostream uses nonexistent num_put member functions.
219 return _M_insert(static_cast<double>(__f));
223 operator<<(long double __f)
224 { return _M_insert(__f); }
227 operator<<(const void* __p)
228 { return _M_insert(__p); }
231 * @brief Extracting from another streambuf.
232 * @param sb A pointer to a streambuf
234 * This function behaves like one of the basic arithmetic extractors,
235 * in that it also constructs a sentry object and has the same error
238 * If @a sb is NULL, the stream will set failbit in its error state.
240 * Characters are extracted from @a sb and inserted into @c *this
241 * until one of the following occurs:
243 * - the input stream reaches end-of-file,
244 * - insertion into the output sequence fails (in this case, the
245 * character that would have been inserted is not extracted), or
246 * - an exception occurs while getting a character from @a sb, which
247 * sets failbit in the error state
249 * If the function inserts no characters, failbit is set.
252 operator<<(__streambuf_type* __sb);
255 // [27.6.2.6] unformatted output functions
257 * @name Unformatted Output Functions
259 * All the unformatted output functions have some common behavior.
260 * Each starts by constructing a temporary object of type
261 * std::basic_ostream::sentry. This has several effects, concluding
262 * with the setting of a status flag; see the sentry documentation
265 * If the sentry status is good, the function tries to generate
266 * whatever data is appropriate for the type of the argument.
268 * If an exception is thrown during insertion, ios_base::badbit
269 * will be turned on in the stream's error state. If badbit is on in
270 * the stream's exceptions mask, the exception will be rethrown
271 * without completing its actions.
275 * @brief Simple insertion.
276 * @param c The character to insert.
279 * Tries to insert @a c.
281 * @note This function is not overloaded on signed char and
287 // Core write functionality, without sentry.
289 _M_write(const char_type* __s, streamsize __n)
291 const streamsize __put = this->rdbuf()->sputn(__s, __n);
293 this->setstate(ios_base::badbit);
297 * @brief Character string insertion.
298 * @param s The array to insert.
299 * @param n Maximum number of characters to insert.
302 * Characters are copied from @a s and inserted into the stream until
303 * one of the following happens:
305 * - @a n characters are inserted
306 * - inserting into the output sequence fails (in this case, badbit
307 * will be set in the stream's error state)
309 * @note This function is not overloaded on signed char and
313 write(const char_type* __s, streamsize __n);
317 * @brief Synchronizing the stream buffer.
320 * If @c rdbuf() is a null pointer, changes nothing.
322 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
328 // [27.6.2.4] seek members
330 * @brief Getting the current write position.
331 * @return A file position object.
333 * If @c fail() is not false, returns @c pos_type(-1) to indicate
334 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
340 * @brief Changing the current write position.
341 * @param pos A file position object.
344 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
345 * that function fails, sets failbit.
351 * @brief Changing the current write position.
352 * @param off A file offset object.
353 * @param dir The direction in which to seek.
356 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
357 * If that function fails, sets failbit.
360 seekp(off_type, ios_base::seekdir);
366 template<typename _ValueT>
368 _M_insert(_ValueT __v);
372 * @brief Performs setup work for output streams.
374 * Objects of this class are created before all of the standard
375 * inserters are run. It is responsible for <em>exception-safe prefix and
376 * suffix operations</em>.
378 template <typename _CharT, typename _Traits>
379 class basic_ostream<_CharT, _Traits>::sentry
383 basic_ostream<_CharT, _Traits>& _M_os;
387 * @brief The constructor performs preparatory work.
388 * @param os The output stream to guard.
390 * If the stream state is good (@a os.good() is true), then if the
391 * stream is tied to another output stream, @c is.tie()->flush()
392 * is called to synchronize the output sequences.
394 * If the stream state is still good, then the sentry state becomes
398 sentry(basic_ostream<_CharT, _Traits>& __os);
401 * @brief Possibly flushes the stream.
403 * If @c ios_base::unitbuf is set in @c os.flags(), and
404 * @c std::uncaught_exception() is true, the sentry destructor calls
405 * @c flush() on the output stream.
410 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
412 // Can't call flush directly or else will get into recursive lock.
413 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
414 _M_os.setstate(ios_base::badbit);
419 * @brief Quick status checking.
420 * @return The sentry state.
422 * For ease of use, sentries may be converted to booleans. The
423 * return value is that of the sentry state (true == okay).
425 #ifdef __GXX_EXPERIMENTAL_CXX0X__
428 operator bool() const
432 // [27.6.2.5.4] character insertion templates
435 * @brief Character inserters
436 * @param out An output stream.
437 * @param c A character.
440 * Behaves like one of the formatted arithmetic inserters described in
441 * std::basic_ostream. After constructing a sentry object with good
442 * status, this function inserts a single character and any required
443 * padding (as determined by [22.2.2.2.2]). @c out.width(0) is then
446 * If @a c is of type @c char and the character type of the stream is not
447 * @c char, the character is widened before insertion.
449 template<typename _CharT, typename _Traits>
450 inline basic_ostream<_CharT, _Traits>&
451 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
452 { return __ostream_insert(__out, &__c, 1); }
454 template<typename _CharT, typename _Traits>
455 inline basic_ostream<_CharT, _Traits>&
456 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
457 { return (__out << __out.widen(__c)); }
460 template <class _Traits>
461 inline basic_ostream<char, _Traits>&
462 operator<<(basic_ostream<char, _Traits>& __out, char __c)
463 { return __ostream_insert(__out, &__c, 1); }
465 // Signed and unsigned
466 template<class _Traits>
467 inline basic_ostream<char, _Traits>&
468 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
469 { return (__out << static_cast<char>(__c)); }
471 template<class _Traits>
472 inline basic_ostream<char, _Traits>&
473 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
474 { return (__out << static_cast<char>(__c)); }
479 * @brief String inserters
480 * @param out An output stream.
481 * @param s A character string.
483 * @pre @a s must be a non-NULL pointer
485 * Behaves like one of the formatted arithmetic inserters described in
486 * std::basic_ostream. After constructing a sentry object with good
487 * status, this function inserts @c traits::length(s) characters starting
488 * at @a s, widened if necessary, followed by any required padding (as
489 * determined by [22.2.2.2.2]). @c out.width(0) is then called.
491 template<typename _CharT, typename _Traits>
492 inline basic_ostream<_CharT, _Traits>&
493 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
496 __out.setstate(ios_base::badbit);
498 __ostream_insert(__out, __s,
499 static_cast<streamsize>(_Traits::length(__s)));
503 template<typename _CharT, typename _Traits>
504 basic_ostream<_CharT, _Traits> &
505 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
507 // Partial specializations
508 template<class _Traits>
509 inline basic_ostream<char, _Traits>&
510 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
513 __out.setstate(ios_base::badbit);
515 __ostream_insert(__out, __s,
516 static_cast<streamsize>(_Traits::length(__s)));
520 // Signed and unsigned
521 template<class _Traits>
522 inline basic_ostream<char, _Traits>&
523 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
524 { return (__out << reinterpret_cast<const char*>(__s)); }
526 template<class _Traits>
527 inline basic_ostream<char, _Traits> &
528 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
529 { return (__out << reinterpret_cast<const char*>(__s)); }
532 // [27.6.2.7] standard basic_ostream manipulators
534 * @brief Write a newline and flush the stream.
536 * This manipulator is often mistakenly used when a simple newline is
537 * desired, leading to poor buffering performance. See
538 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
539 * for more on this subject.
541 template<typename _CharT, typename _Traits>
542 inline basic_ostream<_CharT, _Traits>&
543 endl(basic_ostream<_CharT, _Traits>& __os)
544 { return flush(__os.put(__os.widen('\n'))); }
547 * @brief Write a null character into the output sequence.
549 * <em>Null character</em> is @c CharT() by definition. For CharT of @c char,
550 * this correctly writes the ASCII @c NUL character string terminator.
552 template<typename _CharT, typename _Traits>
553 inline basic_ostream<_CharT, _Traits>&
554 ends(basic_ostream<_CharT, _Traits>& __os)
555 { return __os.put(_CharT()); }
558 * @brief Flushes the output stream.
560 * This manipulator simply calls the stream's @c flush() member function.
562 template<typename _CharT, typename _Traits>
563 inline basic_ostream<_CharT, _Traits>&
564 flush(basic_ostream<_CharT, _Traits>& __os)
565 { return __os.flush(); }
567 #ifdef __GXX_EXPERIMENTAL_CXX0X__
568 // [27.7.2.9] Rvalue stream insertion
570 * @brief Generic inserter for rvalue stream
571 * @param os An input stream.
572 * @param x A reference to the object being inserted.
575 * This is just a forwarding function to allow insertion to
576 * rvalue streams since they won't bind to the inserter functions
577 * that take an lvalue reference.
579 template<typename _CharT, typename _Traits, typename _Tp>
580 inline basic_ostream<_CharT, _Traits>&
581 operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
582 { return (__os << __x); }
583 #endif // __GXX_EXPERIMENTAL_CXX0X__
585 _GLIBCXX_END_NAMESPACE_VERSION
588 #include <bits/ostream.tcc>
590 #endif /* _GLIBCXX_OSTREAM */