1 // Output streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
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 2, 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 // 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.
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.
33 * This is a Standard C++ Library header.
37 // ISO C++ 14882: 27.6.2 Output streams
40 #ifndef _GLIBCXX_OSTREAM
41 #define _GLIBCXX_OSTREAM 1
43 #pragma GCC system_header
46 #include <bits/ostream_insert.h>
48 _GLIBCXX_BEGIN_NAMESPACE(std)
50 // [27.6.2.1] Template class basic_ostream
52 * @brief Controlling output.
54 * This is the base class for all output streams. It provides text
55 * formatting of all builtin types, and communicates with any class
56 * derived from basic_streambuf to do the actual output.
58 template<typename _CharT, typename _Traits>
59 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
62 // Types (inherited from basic_ios (27.4.4)):
63 typedef _CharT char_type;
64 typedef typename _Traits::int_type int_type;
65 typedef typename _Traits::pos_type pos_type;
66 typedef typename _Traits::off_type off_type;
67 typedef _Traits traits_type;
69 // Non-standard Types:
70 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
71 typedef basic_ios<_CharT, _Traits> __ios_type;
72 typedef basic_ostream<_CharT, _Traits> __ostream_type;
73 typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
75 typedef ctype<_CharT> __ctype_type;
77 // [27.6.2.2] constructor/destructor
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 // [27.6.2.3] prefix/suffix
101 // [27.6.2.5] formatted output
102 // [27.6.2.5.3] basic_ostream::operator<<
105 * @brief Interface for manipulators.
107 * Manipulators such as @c std::endl and @c std::hex use these
108 * functions in constructs like "std::cout << std::endl". For more
109 * information, see the iomanip header.
112 operator<<(__ostream_type& (*__pf)(__ostream_type&))
114 // _GLIBCXX_RESOLVE_LIB_DEFECTS
115 // DR 60. What is a formatted input function?
116 // The inserters for manipulators are *not* formatted output functions.
121 operator<<(__ios_type& (*__pf)(__ios_type&))
123 // _GLIBCXX_RESOLVE_LIB_DEFECTS
124 // DR 60. What is a formatted input function?
125 // The inserters for manipulators are *not* formatted output functions.
131 operator<<(ios_base& (*__pf) (ios_base&))
133 // _GLIBCXX_RESOLVE_LIB_DEFECTS
134 // DR 60. What is a formatted input function?
135 // The inserters for manipulators are *not* formatted output functions.
141 // [27.6.2.5.2] arithmetic inserters
143 * @name Arithmetic Inserters
145 * All the @c operator<< functions (aka <em>formatted output
146 * functions</em>) have some common behavior. Each starts by
147 * constructing a temporary object of type std::basic_ostream::sentry.
148 * This can have several effects, concluding with the setting of a
149 * status flag; see the sentry documentation for more.
151 * If the sentry status is good, the function tries to generate
152 * whatever data is appropriate for the type of the argument.
154 * If an exception is thrown during insertion, ios_base::badbit
155 * will be turned on in the stream's error state without causing an
156 * ios_base::failure to be thrown. The original exception will then
161 * @brief Basic arithmetic inserters
162 * @param A variable of builtin type.
163 * @return @c *this if successful
165 * These functions use the stream's current locale (specifically, the
166 * @c num_get facet) to perform numeric formatting.
170 { return _M_insert(__n); }
173 operator<<(unsigned long __n)
174 { return _M_insert(__n); }
178 { return _M_insert(__n); }
181 operator<<(short __n);
184 operator<<(unsigned short __n)
186 // _GLIBCXX_RESOLVE_LIB_DEFECTS
187 // 117. basic_ostream uses nonexistent num_put member functions.
188 return _M_insert(static_cast<unsigned long>(__n));
195 operator<<(unsigned int __n)
197 // _GLIBCXX_RESOLVE_LIB_DEFECTS
198 // 117. basic_ostream uses nonexistent num_put member functions.
199 return _M_insert(static_cast<unsigned long>(__n));
202 #ifdef _GLIBCXX_USE_LONG_LONG
204 operator<<(long long __n)
205 { return _M_insert(__n); }
208 operator<<(unsigned long long __n)
209 { return _M_insert(__n); }
213 operator<<(double __f)
214 { return _M_insert(__f); }
217 operator<<(float __f)
219 // _GLIBCXX_RESOLVE_LIB_DEFECTS
220 // 117. basic_ostream uses nonexistent num_put member functions.
221 return _M_insert(static_cast<double>(__f));
225 operator<<(long double __f)
226 { return _M_insert(__f); }
229 operator<<(const void* __p)
230 { return _M_insert(__p); }
233 * @brief Extracting from another streambuf.
234 * @param sb A pointer to a streambuf
236 * This function behaves like one of the basic arithmetic extractors,
237 * in that it also constructs a sentry object and has the same error
240 * If @a sb is NULL, the stream will set failbit in its error state.
242 * Characters are extracted from @a sb and inserted into @c *this
243 * until one of the following occurs:
245 * - the input stream reaches end-of-file,
246 * - insertion into the output sequence fails (in this case, the
247 * character that would have been inserted is not extracted), or
248 * - an exception occurs while getting a character from @a sb, which
249 * sets failbit in the error state
251 * If the function inserts no characters, failbit is set.
254 operator<<(__streambuf_type* __sb);
257 // [27.6.2.6] unformatted output functions
259 * @name Unformatted Output Functions
261 * All the unformatted output functions have some common behavior.
262 * Each starts by constructing a temporary object of type
263 * std::basic_ostream::sentry. This has several effects, concluding
264 * with the setting of a status flag; see the sentry documentation
267 * If the sentry status is good, the function tries to generate
268 * whatever data is appropriate for the type of the argument.
270 * If an exception is thrown during insertion, ios_base::badbit
271 * will be turned on in the stream's error state. If badbit is on in
272 * the stream's exceptions mask, the exception will be rethrown
273 * without completing its actions.
277 * @brief Simple insertion.
278 * @param c The character to insert.
281 * Tries to insert @a c.
283 * @note This function is not overloaded on signed char and
290 * @brief Character string insertion.
291 * @param s The array to insert.
292 * @param n Maximum number of characters to insert.
295 * Characters are copied from @a s and inserted into the stream until
296 * one of the following happens:
298 * - @a n characters are inserted
299 * - inserting into the output sequence fails (in this case, badbit
300 * will be set in the stream's error state)
302 * @note This function is not overloaded on signed char and
306 write(const char_type* __s, streamsize __n);
310 * @brief Synchronizing the stream buffer.
313 * If @c rdbuf() is a null pointer, changes nothing.
315 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
321 // [27.6.2.4] seek members
323 * @brief Getting the current write position.
324 * @return A file position object.
326 * If @c fail() is not false, returns @c pos_type(-1) to indicate
327 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
333 * @brief Changing the current write position.
334 * @param pos A file position object.
337 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
338 * that function fails, sets failbit.
344 * @brief Changing the current write position.
345 * @param off A file offset object.
346 * @param dir The direction in which to seek.
349 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
350 * If that function fails, sets failbit.
353 seekp(off_type, ios_base::seekdir);
360 _M_write(const char_type* __s, streamsize __n)
362 const streamsize __put = _M_write_(__s, __n);
364 this->setstate(ios_base::badbit);
368 _M_write_(const char_type* __s, streamsize __n);
370 template<typename _ValueT>
372 _M_insert(_ValueT __v);
377 basic_ostream<char>::
378 _M_write_(const char_type* __s, streamsize __n)
379 { return this->rdbuf()->__streambuf_type::xsputn(__s, __n); }
381 #ifdef _GLIBCXX_USE_WCHAR_T
384 basic_ostream<wchar_t>::
385 _M_write_(const char_type* __s, streamsize __n)
386 { return this->rdbuf()->__streambuf_type::xsputn(__s, __n); }
390 * @brief Performs setup work for output streams.
392 * Objects of this class are created before all of the standard
393 * inserters are run. It is responsible for "exception-safe prefix and
394 * suffix operations."
396 template <typename _CharT, typename _Traits>
397 class basic_ostream<_CharT, _Traits>::sentry
401 basic_ostream<_CharT, _Traits>& _M_os;
405 * @brief The constructor performs preparatory work.
406 * @param os The output stream to guard.
408 * If the stream state is good (@a os.good() is true), then if the
409 * stream is tied to another output stream, @c is.tie()->flush()
410 * is called to synchronize the output sequences.
412 * If the stream state is still good, then the sentry state becomes
416 sentry(basic_ostream<_CharT, _Traits>& __os);
419 * @brief Possibly flushes the stream.
421 * If @c ios_base::unitbuf is set in @c os.flags(), and
422 * @c std::uncaught_exception() is true, the sentry destructor calls
423 * @c flush() on the output stream.
428 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
430 // Can't call flush directly or else will get into recursive lock.
431 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
432 _M_os.setstate(ios_base::badbit);
437 * @brief Quick status checking.
438 * @return The sentry state.
440 * For ease of use, sentries may be converted to booleans. The
441 * return value is that of the sentry state (true == okay).
443 operator bool() const
447 // [27.6.2.5.4] character insertion templates
450 * @brief Character inserters
451 * @param out An output stream.
452 * @param c A character.
455 * Behaves like one of the formatted arithmetic inserters described in
456 * std::basic_ostream. After constructing a sentry object with good
457 * status, this function inserts a single character and any required
458 * padding (as determined by [22.2.2.2.2]). @c out.width(0) is then
461 * If @a c is of type @c char and the character type of the stream is not
462 * @c char, the character is widened before insertion.
464 template<typename _CharT, typename _Traits>
465 inline basic_ostream<_CharT, _Traits>&
466 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
467 { return __ostream_insert(__out, &__c, 1); }
469 template<typename _CharT, typename _Traits>
470 inline basic_ostream<_CharT, _Traits>&
471 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
472 { return (__out << __out.widen(__c)); }
475 template <class _Traits>
476 inline basic_ostream<char, _Traits>&
477 operator<<(basic_ostream<char, _Traits>& __out, char __c)
478 { return __ostream_insert(__out, &__c, 1); }
480 // Signed and unsigned
481 template<class _Traits>
482 inline basic_ostream<char, _Traits>&
483 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
484 { return (__out << static_cast<char>(__c)); }
486 template<class _Traits>
487 inline basic_ostream<char, _Traits>&
488 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
489 { return (__out << static_cast<char>(__c)); }
494 * @brief String inserters
495 * @param out An output stream.
496 * @param s A character string.
498 * @pre @a s must be a non-NULL pointer
500 * Behaves like one of the formatted arithmetic inserters described in
501 * std::basic_ostream. After constructing a sentry object with good
502 * status, this function inserts @c traits::length(s) characters starting
503 * at @a s, widened if necessary, followed by any required padding (as
504 * determined by [22.2.2.2.2]). @c out.width(0) is then called.
506 template<typename _CharT, typename _Traits>
507 inline basic_ostream<_CharT, _Traits>&
508 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
511 __out.setstate(ios_base::badbit);
513 __ostream_insert(__out, __s,
514 static_cast<streamsize>(_Traits::length(__s)));
518 template<typename _CharT, typename _Traits>
519 basic_ostream<_CharT, _Traits> &
520 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
522 // Partial specializations
523 template<class _Traits>
524 inline basic_ostream<char, _Traits>&
525 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
528 __out.setstate(ios_base::badbit);
530 __ostream_insert(__out, __s,
531 static_cast<streamsize>(_Traits::length(__s)));
535 // Signed and unsigned
536 template<class _Traits>
537 inline basic_ostream<char, _Traits>&
538 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
539 { return (__out << reinterpret_cast<const char*>(__s)); }
541 template<class _Traits>
542 inline basic_ostream<char, _Traits> &
543 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
544 { return (__out << reinterpret_cast<const char*>(__s)); }
547 // [27.6.2.7] standard basic_ostream manipulators
549 * @brief Write a newline and flush the stream.
551 * This manipulator is often mistakenly used when a simple newline is
552 * desired, leading to poor buffering performance. See
553 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
556 template<typename _CharT, typename _Traits>
557 inline basic_ostream<_CharT, _Traits>&
558 endl(basic_ostream<_CharT, _Traits>& __os)
559 { return flush(__os.put(__os.widen('\n'))); }
562 * @brief Write a null character into the output sequence.
564 * "Null character" is @c CharT() by definition. For CharT of @c char,
565 * this correctly writes the ASCII @c NUL character string terminator.
567 template<typename _CharT, typename _Traits>
568 inline basic_ostream<_CharT, _Traits>&
569 ends(basic_ostream<_CharT, _Traits>& __os)
570 { return __os.put(_CharT()); }
573 * @brief Flushes the output stream.
575 * This manipulator simply calls the stream's @c flush() member function.
577 template<typename _CharT, typename _Traits>
578 inline basic_ostream<_CharT, _Traits>&
579 flush(basic_ostream<_CharT, _Traits>& __os)
580 { return __os.flush(); }
582 _GLIBCXX_END_NAMESPACE
584 #ifndef _GLIBCXX_EXPORT_TEMPLATE
585 # include <bits/ostream.tcc>
588 #endif /* _GLIBCXX_OSTREAM */