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.
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 (27.4.4)):
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;
78 // [27.6.2.2] constructor/destructor
80 * @brief Base constructor.
82 * This ctor is almost never called by the user directly, rather from
83 * derived classes' initialization lists, which pass a pointer to
84 * their own stream buffer.
87 basic_ostream(__streambuf_type* __sb)
91 * @brief Base destructor.
93 * This does very little apart from providing a virtual base dtor.
98 // [27.6.2.3] prefix/suffix
102 // [27.6.2.5] formatted output
103 // [27.6.2.5.3] basic_ostream::operator<<
106 * @brief Interface for manipulators.
108 * Manipulators such as @c std::endl and @c std::hex use these
109 * functions in constructs like "std::cout << std::endl". For more
110 * information, see the iomanip header.
113 operator<<(__ostream_type& (*__pf)(__ostream_type&))
115 // _GLIBCXX_RESOLVE_LIB_DEFECTS
116 // DR 60. What is a formatted input function?
117 // The inserters for manipulators are *not* formatted output functions.
122 operator<<(__ios_type& (*__pf)(__ios_type&))
124 // _GLIBCXX_RESOLVE_LIB_DEFECTS
125 // DR 60. What is a formatted input function?
126 // The inserters for manipulators are *not* formatted output functions.
132 operator<<(ios_base& (*__pf) (ios_base&))
134 // _GLIBCXX_RESOLVE_LIB_DEFECTS
135 // DR 60. What is a formatted input function?
136 // The inserters for manipulators are *not* formatted output functions.
142 // [27.6.2.5.2] arithmetic inserters
144 * @name Arithmetic Inserters
146 * All the @c operator<< functions (aka <em>formatted output
147 * functions</em>) have some common behavior. Each starts by
148 * constructing a temporary object of type std::basic_ostream::sentry.
149 * This can have several effects, concluding with the setting of a
150 * status flag; see the sentry documentation for more.
152 * If the sentry status is good, the function tries to generate
153 * whatever data is appropriate for the type of the argument.
155 * If an exception is thrown during insertion, ios_base::badbit
156 * will be turned on in the stream's error state without causing an
157 * ios_base::failure to be thrown. The original exception will then
162 * @brief Basic arithmetic inserters
163 * @param A variable of builtin type.
164 * @return @c *this if successful
166 * These functions use the stream's current locale (specifically, the
167 * @c num_get facet) to perform numeric formatting.
171 { return _M_insert(__n); }
174 operator<<(unsigned long __n)
175 { return _M_insert(__n); }
179 { return _M_insert(__n); }
182 operator<<(short __n);
185 operator<<(unsigned short __n)
187 // _GLIBCXX_RESOLVE_LIB_DEFECTS
188 // 117. basic_ostream uses nonexistent num_put member functions.
189 return _M_insert(static_cast<unsigned long>(__n));
196 operator<<(unsigned int __n)
198 // _GLIBCXX_RESOLVE_LIB_DEFECTS
199 // 117. basic_ostream uses nonexistent num_put member functions.
200 return _M_insert(static_cast<unsigned long>(__n));
203 #ifdef _GLIBCXX_USE_LONG_LONG
205 operator<<(long long __n)
206 { return _M_insert(__n); }
209 operator<<(unsigned long long __n)
210 { return _M_insert(__n); }
214 operator<<(double __f)
215 { return _M_insert(__f); }
218 operator<<(float __f)
220 // _GLIBCXX_RESOLVE_LIB_DEFECTS
221 // 117. basic_ostream uses nonexistent num_put member functions.
222 return _M_insert(static_cast<double>(__f));
226 operator<<(long double __f)
227 { return _M_insert(__f); }
230 operator<<(const void* __p)
231 { return _M_insert(__p); }
234 * @brief Extracting from another streambuf.
235 * @param sb A pointer to a streambuf
237 * This function behaves like one of the basic arithmetic extractors,
238 * in that it also constructs a sentry object and has the same error
241 * If @a sb is NULL, the stream will set failbit in its error state.
243 * Characters are extracted from @a sb and inserted into @c *this
244 * until one of the following occurs:
246 * - the input stream reaches end-of-file,
247 * - insertion into the output sequence fails (in this case, the
248 * character that would have been inserted is not extracted), or
249 * - an exception occurs while getting a character from @a sb, which
250 * sets failbit in the error state
252 * If the function inserts no characters, failbit is set.
255 operator<<(__streambuf_type* __sb);
258 // [27.6.2.6] unformatted output functions
260 * @name Unformatted Output Functions
262 * All the unformatted output functions have some common behavior.
263 * Each starts by constructing a temporary object of type
264 * std::basic_ostream::sentry. This has several effects, concluding
265 * with the setting of a status flag; see the sentry documentation
268 * If the sentry status is good, the function tries to generate
269 * whatever data is appropriate for the type of the argument.
271 * If an exception is thrown during insertion, ios_base::badbit
272 * will be turned on in the stream's error state. If badbit is on in
273 * the stream's exceptions mask, the exception will be rethrown
274 * without completing its actions.
278 * @brief Simple insertion.
279 * @param c The character to insert.
282 * Tries to insert @a c.
284 * @note This function is not overloaded on signed char and
290 // Core write functionality, without sentry.
292 _M_write(const char_type* __s, streamsize __n)
294 const streamsize __put = this->rdbuf()->sputn(__s, __n);
296 this->setstate(ios_base::badbit);
300 * @brief Character string insertion.
301 * @param s The array to insert.
302 * @param n Maximum number of characters to insert.
305 * Characters are copied from @a s and inserted into the stream until
306 * one of the following happens:
308 * - @a n characters are inserted
309 * - inserting into the output sequence fails (in this case, badbit
310 * will be set in the stream's error state)
312 * @note This function is not overloaded on signed char and
316 write(const char_type* __s, streamsize __n);
320 * @brief Synchronizing the stream buffer.
323 * If @c rdbuf() is a null pointer, changes nothing.
325 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
331 // [27.6.2.4] seek members
333 * @brief Getting the current write position.
334 * @return A file position object.
336 * If @c fail() is not false, returns @c pos_type(-1) to indicate
337 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
343 * @brief Changing the current write position.
344 * @param pos A file position object.
347 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
348 * that function fails, sets failbit.
354 * @brief Changing the current write position.
355 * @param off A file offset object.
356 * @param dir The direction in which to seek.
359 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
360 * If that function fails, sets failbit.
363 seekp(off_type, ios_base::seekdir);
369 template<typename _ValueT>
371 _M_insert(_ValueT __v);
375 * @brief Performs setup work for output streams.
377 * Objects of this class are created before all of the standard
378 * inserters are run. It is responsible for "exception-safe prefix and
379 * suffix operations."
381 template <typename _CharT, typename _Traits>
382 class basic_ostream<_CharT, _Traits>::sentry
386 basic_ostream<_CharT, _Traits>& _M_os;
390 * @brief The constructor performs preparatory work.
391 * @param os The output stream to guard.
393 * If the stream state is good (@a os.good() is true), then if the
394 * stream is tied to another output stream, @c is.tie()->flush()
395 * is called to synchronize the output sequences.
397 * If the stream state is still good, then the sentry state becomes
401 sentry(basic_ostream<_CharT, _Traits>& __os);
404 * @brief Possibly flushes the stream.
406 * If @c ios_base::unitbuf is set in @c os.flags(), and
407 * @c std::uncaught_exception() is true, the sentry destructor calls
408 * @c flush() on the output stream.
413 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
415 // Can't call flush directly or else will get into recursive lock.
416 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
417 _M_os.setstate(ios_base::badbit);
422 * @brief Quick status checking.
423 * @return The sentry state.
425 * For ease of use, sentries may be converted to booleans. The
426 * return value is that of the sentry state (true == okay).
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++/27_io/howto.html#2 for more
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 * "Null character" 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 _GLIBCXX_END_NAMESPACE
569 #ifndef _GLIBCXX_EXPORT_TEMPLATE
570 # include <bits/ostream.tcc>
573 #endif /* _GLIBCXX_OSTREAM */