1 // Output streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
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 along
19 // with this library; see the file COPYING. If not, write to the Free
20 // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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
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 template<typename _CharT2, typename _Traits2>
78 friend basic_ostream<_CharT2, _Traits2>&
79 operator<<(basic_ostream<_CharT2, _Traits2>&, _CharT2);
81 template<typename _Traits2>
82 friend basic_ostream<char, _Traits2>&
83 operator<<(basic_ostream<char, _Traits2>&, char);
85 template<typename _CharT2, typename _Traits2>
86 friend basic_ostream<_CharT2, _Traits2>&
87 operator<<(basic_ostream<_CharT2, _Traits2>&, const _CharT2*);
89 template<typename _Traits2>
90 friend basic_ostream<char, _Traits2>&
91 operator<<(basic_ostream<char, _Traits2>&, const char*);
93 template<typename _CharT2, typename _Traits2>
94 friend basic_ostream<_CharT2, _Traits2>&
95 operator<<(basic_ostream<_CharT2, _Traits2>&, const char*);
97 template<typename _CharT2, typename _Traits2, typename _Alloc>
98 friend basic_ostream<_CharT2, _Traits2>&
99 operator<<(basic_ostream<_CharT2, _Traits2>&,
100 const basic_string<_CharT2, _Traits2, _Alloc>&);
102 // [27.6.2.2] constructor/destructor
104 * @brief Base constructor.
106 * This ctor is almost never called by the user directly, rather from
107 * derived classes' initialization lists, which pass a pointer to
108 * their own stream buffer.
111 basic_ostream(__streambuf_type* __sb)
112 { this->init(__sb); }
115 * @brief Base destructor.
117 * This does very little apart from providing a virtual base dtor.
122 // [27.6.2.3] prefix/suffix
126 // [27.6.2.5] formatted output
127 // [27.6.2.5.3] basic_ostream::operator<<
130 * @brief Interface for manipulators.
132 * Manuipulators such as @c std::endl and @c std::hex use these
133 * functions in constructs like "std::cout << std::endl". For more
134 * information, see the iomanip header.
137 operator<<(__ostream_type& (*__pf)(__ostream_type&))
139 // _GLIBCXX_RESOLVE_LIB_DEFECTS
140 // DR 60. What is a formatted input function?
141 // The inserters for manipulators are *not* formatted output functions.
146 operator<<(__ios_type& (*__pf)(__ios_type&))
148 // _GLIBCXX_RESOLVE_LIB_DEFECTS
149 // DR 60. What is a formatted input function?
150 // The inserters for manipulators are *not* formatted output functions.
156 operator<<(ios_base& (*__pf) (ios_base&))
158 // _GLIBCXX_RESOLVE_LIB_DEFECTS
159 // DR 60. What is a formatted input function?
160 // The inserters for manipulators are *not* formatted output functions.
166 // [27.6.2.5.2] arithmetic inserters
168 * @name Arithmetic Inserters
170 * All the @c operator<< functions (aka <em>formatted output
171 * functions</em>) have some common behavior. Each starts by
172 * constructing a temporary object of type std::basic_ostream::sentry.
173 * This can have several effects, concluding with the setting of a
174 * status flag; see the sentry documentation for more.
176 * If the sentry status is good, the function tries to generate
177 * whatever data is appropriate for the type of the argument.
179 * If an exception is thrown during insertion, ios_base::badbit
180 * will be turned on in the stream's error state without causing an
181 * ios_base::failure to be thrown. The original exception will then
186 * @brief Basic arithmetic inserters
187 * @param A variable of builtin type.
188 * @return @c *this if successful
190 * These functions use the stream's current locale (specifically, the
191 * @c num_get facet) to perform numeric formatting.
195 { return _M_insert(__n); }
198 operator<<(unsigned long __n)
199 { return _M_insert(__n); }
203 { return _M_insert(__n); }
206 operator<<(short __n);
209 operator<<(unsigned short __n)
211 // _GLIBCXX_RESOLVE_LIB_DEFECTS
212 // 117. basic_ostream uses nonexistent num_put member functions.
213 return _M_insert(static_cast<unsigned long>(__n));
220 operator<<(unsigned int __n)
222 // _GLIBCXX_RESOLVE_LIB_DEFECTS
223 // 117. basic_ostream uses nonexistent num_put member functions.
224 return _M_insert(static_cast<unsigned long>(__n));
227 #ifdef _GLIBCXX_USE_LONG_LONG
229 operator<<(long long __n)
230 { return _M_insert(__n); }
233 operator<<(unsigned long long __n)
234 { return _M_insert(__n); }
238 operator<<(double __f)
239 { return _M_insert(__f); }
242 operator<<(float __f)
244 // _GLIBCXX_RESOLVE_LIB_DEFECTS
245 // 117. basic_ostream uses nonexistent num_put member functions.
246 return _M_insert(static_cast<double>(__f));
250 operator<<(long double __f)
251 { return _M_insert(__f); }
254 operator<<(const void* __p)
255 { return _M_insert(__p); }
258 * @brief Extracting from another streambuf.
259 * @param sb A pointer to a streambuf
261 * This function behaves like one of the basic arithmetic extractors,
262 * in that it also constructs a sentry object and has the same error
265 * If @a sb is NULL, the stream will set failbit in its error state.
267 * Characters are extracted from @a sb and inserted into @c *this
268 * until one of the following occurs:
270 * - the input stream reaches end-of-file,
271 * - insertion into the output sequence fails (in this case, the
272 * character that would have been inserted is not extracted), or
273 * - an exception occurs while getting a character from @a sb, which
274 * sets failbit in the error state
276 * If the function inserts no characters, failbit is set.
279 operator<<(__streambuf_type* __sb);
282 // [27.6.2.6] unformatted output functions
284 * @name Unformatted Output Functions
286 * All the unformatted output functions have some common behavior.
287 * Each starts by constructing a temporary object of type
288 * std::basic_ostream::sentry. This has several effects, concluding
289 * with the setting of a status flag; see the sentry documentation
292 * If the sentry status is good, the function tries to generate
293 * whatever data is appropriate for the type of the argument.
295 * If an exception is thrown during insertion, ios_base::badbit
296 * will be turned on in the stream's error state. If badbit is on in
297 * the stream's exceptions mask, the exception will be rethrown
298 * without completing its actions.
302 * @brief Simple insertion.
303 * @param c The character to insert.
306 * Tries to insert @a c.
308 * @note This function is not overloaded on signed char and
314 // Core write functionality, without sentry.
316 _M_write(const char_type* __s, streamsize __n)
318 const streamsize __put = this->rdbuf()->sputn(__s, __n);
320 this->setstate(ios_base::badbit);
324 _M_write(char_type __c, streamsize __n)
326 for (; __n > 0; --__n)
328 const int_type __put = this->rdbuf()->sputc(__c);
329 if (traits_type::eq_int_type(__put, traits_type::eof()))
331 this->setstate(ios_base::badbit);
338 * @brief Character string insertion.
339 * @param s The array to insert.
340 * @param n Maximum number of characters to insert.
343 * Characters are copied from @a s and inserted into the stream until
344 * one of the following happens:
346 * - @a n characters are inserted
347 * - inserting into the output sequence fails (in this case, badbit
348 * will be set in the stream's error state)
350 * @note This function is not overloaded on signed char and
354 write(const char_type* __s, streamsize __n);
358 * @brief Synchronizing the stream buffer.
361 * If @c rdbuf() is a null pointer, changes nothing.
363 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
369 // [27.6.2.4] seek members
371 * @brief Getting the current write position.
372 * @return A file position object.
374 * If @c fail() is not false, returns @c pos_type(-1) to indicate
375 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
381 * @brief Changing the current write position.
382 * @param pos A file position object.
385 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
386 * that function fails, sets failbit.
392 * @brief Changing the current write position.
393 * @param off A file offset object.
394 * @param dir The direction in which to seek.
397 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
398 * If that function fails, sets failbit.
401 seekp(off_type, ios_base::seekdir);
407 template<typename _ValueT>
409 _M_insert(_ValueT __v);
412 _M_insert(const char_type* __s, streamsize __n);
416 * @brief Performs setup work for output streams.
418 * Objects of this class are created before all of the standard
419 * inserters are run. It is responsible for "exception-safe prefix and
420 * suffix operations." Additional actions may be added by the
421 * implementation, and we list them in
422 * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
423 * under [27.6] notes.
425 template <typename _CharT, typename _Traits>
426 class basic_ostream<_CharT, _Traits>::sentry
430 basic_ostream<_CharT, _Traits>& _M_os;
434 * @brief The constructor performs preparatory work.
435 * @param os The output stream to guard.
437 * If the stream state is good (@a os.good() is true), then if the
438 * stream is tied to another output stream, @c is.tie()->flush()
439 * is called to synchronize the output sequences.
441 * If the stream state is still good, then the sentry state becomes
445 sentry(basic_ostream<_CharT, _Traits>& __os);
448 * @brief Possibly flushes the stream.
450 * If @c ios_base::unitbuf is set in @c os.flags(), and
451 * @c std::uncaught_exception() is true, the sentry destructor calls
452 * @c flush() on the output stream.
457 if (_M_os.flags() & ios_base::unitbuf && !uncaught_exception())
459 // Can't call flush directly or else will get into recursive lock.
460 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
461 _M_os.setstate(ios_base::badbit);
466 * @brief Quick status checking.
467 * @return The sentry state.
469 * For ease of use, sentries may be converted to booleans. The
470 * return value is that of the sentry state (true == okay).
472 operator bool() const
476 // [27.6.2.5.4] character insertion templates
479 * @brief Character inserters
480 * @param out An output stream.
481 * @param c A character.
484 * Behaves like one of the formatted arithmetic inserters described in
485 * std::basic_ostream. After constructing a sentry object with good
486 * status, this function inserts a single character and any required
487 * padding (as determined by [22.2.2.2.2]). @c out.width(0) is then
490 * If @a c is of type @c char and the character type of the stream is not
491 * @c char, the character is widened before insertion.
493 template<typename _CharT, typename _Traits>
494 inline basic_ostream<_CharT, _Traits>&
495 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
496 { return __out._M_insert(&__c, 1); }
498 template<typename _CharT, typename _Traits>
499 inline basic_ostream<_CharT, _Traits>&
500 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
501 { return (__out << __out.widen(__c)); }
504 template <class _Traits>
505 inline basic_ostream<char, _Traits>&
506 operator<<(basic_ostream<char, _Traits>& __out, char __c)
507 { return __out._M_insert(&__c, 1); }
509 // Signed and unsigned
510 template<class _Traits>
511 inline basic_ostream<char, _Traits>&
512 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
513 { return (__out << static_cast<char>(__c)); }
515 template<class _Traits>
516 inline basic_ostream<char, _Traits>&
517 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
518 { return (__out << static_cast<char>(__c)); }
523 * @brief String inserters
524 * @param out An output stream.
525 * @param s A character string.
527 * @pre @a s must be a non-NULL pointer
529 * Behaves like one of the formatted arithmetic inserters described in
530 * std::basic_ostream. After constructing a sentry object with good
531 * status, this function inserts @c traits::length(s) characters starting
532 * at @a s, widened if necessary, followed by any required padding (as
533 * determined by [22.2.2.2.2]). @c out.width(0) is then called.
535 template<typename _CharT, typename _Traits>
536 inline basic_ostream<_CharT, _Traits>&
537 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
540 __out.setstate(ios_base::badbit);
542 __out._M_insert(__s, static_cast<streamsize>(_Traits::length(__s)));
546 template<typename _CharT, typename _Traits>
547 basic_ostream<_CharT, _Traits> &
548 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
550 // Partial specializationss
551 template<class _Traits>
552 inline basic_ostream<char, _Traits>&
553 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
556 __out.setstate(ios_base::badbit);
558 __out._M_insert(__s, static_cast<streamsize>(_Traits::length(__s)));
562 // Signed and unsigned
563 template<class _Traits>
564 inline basic_ostream<char, _Traits>&
565 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
566 { return (__out << reinterpret_cast<const char*>(__s)); }
568 template<class _Traits>
569 inline basic_ostream<char, _Traits> &
570 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
571 { return (__out << reinterpret_cast<const char*>(__s)); }
574 // [27.6.2.7] standard basic_ostream manipulators
576 * @brief Write a newline and flush the stream.
578 * This manipulator is often mistakenly used when a simple newline is
579 * desired, leading to poor buffering performance. See
580 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
583 template<typename _CharT, typename _Traits>
584 inline basic_ostream<_CharT, _Traits>&
585 endl(basic_ostream<_CharT, _Traits>& __os)
586 { return flush(__os.put(__os.widen('\n'))); }
589 * @brief Write a null character into the output sequence.
591 * "Null character" is @c CharT() by definition. For CharT of @c char,
592 * this correctly writes the ASCII @c NUL character string terminator.
594 template<typename _CharT, typename _Traits>
595 inline basic_ostream<_CharT, _Traits>&
596 ends(basic_ostream<_CharT, _Traits>& __os)
597 { return __os.put(_CharT()); }
600 * @brief Flushes the output stream.
602 * This manipulator simply calls the stream's @c flush() member function.
604 template<typename _CharT, typename _Traits>
605 inline basic_ostream<_CharT, _Traits>&
606 flush(basic_ostream<_CharT, _Traits>& __os)
607 { return __os.flush(); }
609 _GLIBCXX_END_NAMESPACE
611 #ifndef _GLIBCXX_EXPORT_TEMPLATE
612 # include <bits/ostream.tcc>
615 #endif /* _GLIBCXX_OSTREAM */