1 // Output streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009, 2010, 2011, 2012
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
48 * @brief Template class basic_ostream.
51 * @tparam _CharT Type of character stream.
52 * @tparam _Traits Traits for character type, defaults to
53 * char_traits<_CharT>.
55 * This is the base class for all output streams. It provides text
56 * formatting of all builtin types, and communicates with any class
57 * derived from basic_streambuf to do the actual output.
59 template<typename _CharT, typename _Traits>
60 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
63 // Types (inherited from basic_ios):
64 typedef _CharT char_type;
65 typedef typename _Traits::int_type int_type;
66 typedef typename _Traits::pos_type pos_type;
67 typedef typename _Traits::off_type off_type;
68 typedef _Traits traits_type;
70 // Non-standard Types:
71 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
72 typedef basic_ios<_CharT, _Traits> __ios_type;
73 typedef basic_ostream<_CharT, _Traits> __ostream_type;
74 typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
76 typedef ctype<_CharT> __ctype_type;
79 * @brief Base constructor.
81 * This ctor is almost never called by the user directly, rather from
82 * derived classes' initialization lists, which pass a pointer to
83 * their own stream buffer.
86 basic_ostream(__streambuf_type* __sb)
90 * @brief Base destructor.
92 * This does very little apart from providing a virtual base dtor.
97 /// Safe prefix/suffix operations.
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.
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
160 * @brief Integer arithmetic inserters
161 * @param __n A variable of builtin integral 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.
169 { return _M_insert(__n); }
172 operator<<(unsigned long __n)
173 { return _M_insert(__n); }
177 { return _M_insert(__n); }
180 operator<<(short __n);
183 operator<<(unsigned short __n)
185 // _GLIBCXX_RESOLVE_LIB_DEFECTS
186 // 117. basic_ostream uses nonexistent num_put member functions.
187 return _M_insert(static_cast<unsigned long>(__n));
194 operator<<(unsigned int __n)
196 // _GLIBCXX_RESOLVE_LIB_DEFECTS
197 // 117. basic_ostream uses nonexistent num_put member functions.
198 return _M_insert(static_cast<unsigned long>(__n));
201 #ifdef _GLIBCXX_USE_LONG_LONG
203 operator<<(long long __n)
204 { return _M_insert(__n); }
207 operator<<(unsigned long long __n)
208 { return _M_insert(__n); }
214 * @brief Floating point arithmetic inserters
215 * @param __f A variable of builtin floating point type.
216 * @return @c *this if successful
218 * These functions use the stream's current locale (specifically, the
219 * @c num_get facet) to perform numeric formatting.
222 operator<<(double __f)
223 { return _M_insert(__f); }
226 operator<<(float __f)
228 // _GLIBCXX_RESOLVE_LIB_DEFECTS
229 // 117. basic_ostream uses nonexistent num_put member functions.
230 return _M_insert(static_cast<double>(__f));
234 operator<<(long double __f)
235 { return _M_insert(__f); }
239 * @brief Pointer arithmetic inserters
240 * @param __p A variable of pointer type.
241 * @return @c *this if successful
243 * These functions use the stream's current locale (specifically, the
244 * @c num_get facet) to perform numeric formatting.
247 operator<<(const void* __p)
248 { return _M_insert(__p); }
251 * @brief Extracting from another streambuf.
252 * @param __sb A pointer to a streambuf
254 * This function behaves like one of the basic arithmetic extractors,
255 * in that it also constructs a sentry object and has the same error
258 * If @p __sb is NULL, the stream will set failbit in its error state.
260 * Characters are extracted from @p __sb and inserted into @c *this
261 * until one of the following occurs:
263 * - the input stream reaches end-of-file,
264 * - insertion into the output sequence fails (in this case, the
265 * character that would have been inserted is not extracted), or
266 * - an exception occurs while getting a character from @p __sb, which
267 * sets failbit in the error state
269 * If the function inserts no characters, failbit is set.
272 operator<<(__streambuf_type* __sb);
277 * @name Unformatted Output Functions
279 * All the unformatted output functions have some common behavior.
280 * Each starts by constructing a temporary object of type
281 * std::basic_ostream::sentry. This has several effects, concluding
282 * with the setting of a status flag; see the sentry documentation
285 * If the sentry status is good, the function tries to generate
286 * whatever data is appropriate for the type of the argument.
288 * If an exception is thrown during insertion, ios_base::badbit
289 * will be turned on in the stream's error state. If badbit is on in
290 * the stream's exceptions mask, the exception will be rethrown
291 * without completing its actions.
295 * @brief Simple insertion.
296 * @param __c The character to insert.
299 * Tries to insert @p __c.
301 * @note This function is not overloaded on signed char and
308 * @brief Core write functionality, without sentry.
309 * @param __s The array to insert.
310 * @param __n Maximum number of characters to insert.
313 _M_write(const char_type* __s, streamsize __n)
315 const streamsize __put = this->rdbuf()->sputn(__s, __n);
317 this->setstate(ios_base::badbit);
321 * @brief Character string insertion.
322 * @param __s The array to insert.
323 * @param __n Maximum number of characters to insert.
326 * Characters are copied from @p __s and inserted into the stream until
327 * one of the following happens:
329 * - @p __n characters are inserted
330 * - inserting into the output sequence fails (in this case, badbit
331 * will be set in the stream's error state)
333 * @note This function is not overloaded on signed char and
337 write(const char_type* __s, streamsize __n);
341 * @brief Synchronizing the stream buffer.
344 * If @c rdbuf() is a null pointer, changes nothing.
346 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
353 * @brief Getting the current write position.
354 * @return A file position object.
356 * If @c fail() is not false, returns @c pos_type(-1) to indicate
357 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
363 * @brief Changing the current write position.
364 * @param __pos A file position object.
367 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
368 * that function fails, sets failbit.
374 * @brief Changing the current write position.
375 * @param __off A file offset object.
376 * @param __dir The direction in which to seek.
379 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
380 * If that function fails, sets failbit.
383 seekp(off_type, ios_base::seekdir);
389 template<typename _ValueT>
391 _M_insert(_ValueT __v);
395 * @brief Performs setup work for output streams.
397 * Objects of this class are created before all of the standard
398 * inserters are run. It is responsible for <em>exception-safe prefix and
399 * suffix operations</em>.
401 template <typename _CharT, typename _Traits>
402 class basic_ostream<_CharT, _Traits>::sentry
406 basic_ostream<_CharT, _Traits>& _M_os;
410 * @brief The constructor performs preparatory work.
411 * @param __os The output stream to guard.
413 * If the stream state is good (@a __os.good() is true), then if the
414 * stream is tied to another output stream, @c is.tie()->flush()
415 * is called to synchronize the output sequences.
417 * If the stream state is still good, then the sentry state becomes
421 sentry(basic_ostream<_CharT, _Traits>& __os);
424 * @brief Possibly flushes the stream.
426 * If @c ios_base::unitbuf is set in @c os.flags(), and
427 * @c std::uncaught_exception() is true, the sentry destructor calls
428 * @c flush() on the output stream.
433 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
435 // Can't call flush directly or else will get into recursive lock.
436 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
437 _M_os.setstate(ios_base::badbit);
442 * @brief Quick status checking.
443 * @return The sentry state.
445 * For ease of use, sentries may be converted to booleans. The
446 * return value is that of the sentry state (true == okay).
448 #if __cplusplus >= 201103L
451 operator bool() const
457 * @brief Character inserters
458 * @param __out An output stream.
459 * @param __c A character.
462 * Behaves like one of the formatted arithmetic inserters described in
463 * std::basic_ostream. After constructing a sentry object with good
464 * status, this function inserts a single character and any required
465 * padding (as determined by [22.2.2.2.2]). @c __out.width(0) is then
468 * If @p __c is of type @c char and the character type of the stream is not
469 * @c char, the character is widened before insertion.
471 template<typename _CharT, typename _Traits>
472 inline basic_ostream<_CharT, _Traits>&
473 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
474 { return __ostream_insert(__out, &__c, 1); }
476 template<typename _CharT, typename _Traits>
477 inline basic_ostream<_CharT, _Traits>&
478 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
479 { return (__out << __out.widen(__c)); }
482 template <class _Traits>
483 inline basic_ostream<char, _Traits>&
484 operator<<(basic_ostream<char, _Traits>& __out, char __c)
485 { return __ostream_insert(__out, &__c, 1); }
487 // Signed and unsigned
488 template<class _Traits>
489 inline basic_ostream<char, _Traits>&
490 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
491 { return (__out << static_cast<char>(__c)); }
493 template<class _Traits>
494 inline basic_ostream<char, _Traits>&
495 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
496 { return (__out << static_cast<char>(__c)); }
501 * @brief String inserters
502 * @param __out An output stream.
503 * @param __s A character string.
505 * @pre @p __s must be a non-NULL pointer
507 * Behaves like one of the formatted arithmetic inserters described in
508 * std::basic_ostream. After constructing a sentry object with good
509 * status, this function inserts @c traits::length(__s) characters starting
510 * at @p __s, widened if necessary, followed by any required padding (as
511 * determined by [22.2.2.2.2]). @c __out.width(0) is then called.
513 template<typename _CharT, typename _Traits>
514 inline basic_ostream<_CharT, _Traits>&
515 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
518 __out.setstate(ios_base::badbit);
520 __ostream_insert(__out, __s,
521 static_cast<streamsize>(_Traits::length(__s)));
525 template<typename _CharT, typename _Traits>
526 basic_ostream<_CharT, _Traits> &
527 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
529 // Partial specializations
530 template<class _Traits>
531 inline basic_ostream<char, _Traits>&
532 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
535 __out.setstate(ios_base::badbit);
537 __ostream_insert(__out, __s,
538 static_cast<streamsize>(_Traits::length(__s)));
542 // Signed and unsigned
543 template<class _Traits>
544 inline basic_ostream<char, _Traits>&
545 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
546 { return (__out << reinterpret_cast<const char*>(__s)); }
548 template<class _Traits>
549 inline basic_ostream<char, _Traits> &
550 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
551 { return (__out << reinterpret_cast<const char*>(__s)); }
554 // Standard basic_ostream manipulators
557 * @brief Write a newline and flush the stream.
559 * This manipulator is often mistakenly used when a simple newline is
560 * desired, leading to poor buffering performance. See
561 * http://gcc.gnu.org/onlinedocs/libstdc++/manual/bk01pt11ch25s02.html
562 * for more on this subject.
564 template<typename _CharT, typename _Traits>
565 inline basic_ostream<_CharT, _Traits>&
566 endl(basic_ostream<_CharT, _Traits>& __os)
567 { return flush(__os.put(__os.widen('\n'))); }
570 * @brief Write a null character into the output sequence.
572 * <em>Null character</em> is @c CharT() by definition. For CharT
573 * of @c char, this correctly writes the ASCII @c NUL character
576 template<typename _CharT, typename _Traits>
577 inline basic_ostream<_CharT, _Traits>&
578 ends(basic_ostream<_CharT, _Traits>& __os)
579 { return __os.put(_CharT()); }
582 * @brief Flushes the output stream.
584 * This manipulator simply calls the stream's @c flush() member function.
586 template<typename _CharT, typename _Traits>
587 inline basic_ostream<_CharT, _Traits>&
588 flush(basic_ostream<_CharT, _Traits>& __os)
589 { return __os.flush(); }
591 #if __cplusplus >= 201103L
593 * @brief Generic inserter for rvalue stream
594 * @param __os An input stream.
595 * @param __x A reference to the object being inserted.
598 * This is just a forwarding function to allow insertion to
599 * rvalue streams since they won't bind to the inserter functions
600 * that take an lvalue reference.
602 template<typename _CharT, typename _Traits, typename _Tp>
603 inline basic_ostream<_CharT, _Traits>&
604 operator<<(basic_ostream<_CharT, _Traits>&& __os, const _Tp& __x)
605 { return (__os << __x); }
608 _GLIBCXX_END_NAMESPACE_VERSION
611 #include <bits/ostream.tcc>
613 #endif /* _GLIBCXX_OSTREAM */