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
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
289 // Core write functionality, without sentry.
291 _M_write(const char_type* __s, streamsize __n)
293 const streamsize __put = this->rdbuf()->sputn(__s, __n);
295 this->setstate(ios_base::badbit);
299 * @brief Character string insertion.
300 * @param s The array to insert.
301 * @param n Maximum number of characters to insert.
304 * Characters are copied from @a s and inserted into the stream until
305 * one of the following happens:
307 * - @a n characters are inserted
308 * - inserting into the output sequence fails (in this case, badbit
309 * will be set in the stream's error state)
311 * @note This function is not overloaded on signed char and
315 write(const char_type* __s, streamsize __n);
319 * @brief Synchronizing the stream buffer.
322 * If @c rdbuf() is a null pointer, changes nothing.
324 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
330 // [27.6.2.4] seek members
332 * @brief Getting the current write position.
333 * @return A file position object.
335 * If @c fail() is not false, returns @c pos_type(-1) to indicate
336 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
342 * @brief Changing the current write position.
343 * @param pos A file position object.
346 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
347 * that function fails, sets failbit.
353 * @brief Changing the current write position.
354 * @param off A file offset object.
355 * @param dir The direction in which to seek.
358 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
359 * If that function fails, sets failbit.
362 seekp(off_type, ios_base::seekdir);
368 template<typename _ValueT>
370 _M_insert(_ValueT __v);
374 * @brief Performs setup work for output streams.
376 * Objects of this class are created before all of the standard
377 * inserters are run. It is responsible for "exception-safe prefix and
378 * suffix operations."
380 template <typename _CharT, typename _Traits>
381 class basic_ostream<_CharT, _Traits>::sentry
385 basic_ostream<_CharT, _Traits>& _M_os;
389 * @brief The constructor performs preparatory work.
390 * @param os The output stream to guard.
392 * If the stream state is good (@a os.good() is true), then if the
393 * stream is tied to another output stream, @c is.tie()->flush()
394 * is called to synchronize the output sequences.
396 * If the stream state is still good, then the sentry state becomes
400 sentry(basic_ostream<_CharT, _Traits>& __os);
403 * @brief Possibly flushes the stream.
405 * If @c ios_base::unitbuf is set in @c os.flags(), and
406 * @c std::uncaught_exception() is true, the sentry destructor calls
407 * @c flush() on the output stream.
412 if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
414 // Can't call flush directly or else will get into recursive lock.
415 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
416 _M_os.setstate(ios_base::badbit);
421 * @brief Quick status checking.
422 * @return The sentry state.
424 * For ease of use, sentries may be converted to booleans. The
425 * return value is that of the sentry state (true == okay).
427 operator bool() const
431 // [27.6.2.5.4] character insertion templates
434 * @brief Character inserters
435 * @param out An output stream.
436 * @param c A character.
439 * Behaves like one of the formatted arithmetic inserters described in
440 * std::basic_ostream. After constructing a sentry object with good
441 * status, this function inserts a single character and any required
442 * padding (as determined by [22.2.2.2.2]). @c out.width(0) is then
445 * If @a c is of type @c char and the character type of the stream is not
446 * @c char, the character is widened before insertion.
448 template<typename _CharT, typename _Traits>
449 inline basic_ostream<_CharT, _Traits>&
450 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c)
451 { return __ostream_insert(__out, &__c, 1); }
453 template<typename _CharT, typename _Traits>
454 inline basic_ostream<_CharT, _Traits>&
455 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
456 { return (__out << __out.widen(__c)); }
459 template <class _Traits>
460 inline basic_ostream<char, _Traits>&
461 operator<<(basic_ostream<char, _Traits>& __out, char __c)
462 { return __ostream_insert(__out, &__c, 1); }
464 // Signed and unsigned
465 template<class _Traits>
466 inline basic_ostream<char, _Traits>&
467 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
468 { return (__out << static_cast<char>(__c)); }
470 template<class _Traits>
471 inline basic_ostream<char, _Traits>&
472 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
473 { return (__out << static_cast<char>(__c)); }
478 * @brief String inserters
479 * @param out An output stream.
480 * @param s A character string.
482 * @pre @a s must be a non-NULL pointer
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 @c traits::length(s) characters starting
487 * at @a s, widened if necessary, followed by any required padding (as
488 * determined by [22.2.2.2.2]). @c out.width(0) is then called.
490 template<typename _CharT, typename _Traits>
491 inline basic_ostream<_CharT, _Traits>&
492 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
495 __out.setstate(ios_base::badbit);
497 __ostream_insert(__out, __s,
498 static_cast<streamsize>(_Traits::length(__s)));
502 template<typename _CharT, typename _Traits>
503 basic_ostream<_CharT, _Traits> &
504 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
506 // Partial specializations
507 template<class _Traits>
508 inline basic_ostream<char, _Traits>&
509 operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
512 __out.setstate(ios_base::badbit);
514 __ostream_insert(__out, __s,
515 static_cast<streamsize>(_Traits::length(__s)));
519 // Signed and unsigned
520 template<class _Traits>
521 inline basic_ostream<char, _Traits>&
522 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
523 { return (__out << reinterpret_cast<const char*>(__s)); }
525 template<class _Traits>
526 inline basic_ostream<char, _Traits> &
527 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
528 { return (__out << reinterpret_cast<const char*>(__s)); }
531 // [27.6.2.7] standard basic_ostream manipulators
533 * @brief Write a newline and flush the stream.
535 * This manipulator is often mistakenly used when a simple newline is
536 * desired, leading to poor buffering performance. See
537 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
540 template<typename _CharT, typename _Traits>
541 inline basic_ostream<_CharT, _Traits>&
542 endl(basic_ostream<_CharT, _Traits>& __os)
543 { return flush(__os.put(__os.widen('\n'))); }
546 * @brief Write a null character into the output sequence.
548 * "Null character" is @c CharT() by definition. For CharT of @c char,
549 * this correctly writes the ASCII @c NUL character string terminator.
551 template<typename _CharT, typename _Traits>
552 inline basic_ostream<_CharT, _Traits>&
553 ends(basic_ostream<_CharT, _Traits>& __os)
554 { return __os.put(_CharT()); }
557 * @brief Flushes the output stream.
559 * This manipulator simply calls the stream's @c flush() member function.
561 template<typename _CharT, typename _Traits>
562 inline basic_ostream<_CharT, _Traits>&
563 flush(basic_ostream<_CharT, _Traits>& __os)
564 { return __os.flush(); }
566 _GLIBCXX_END_NAMESPACE
568 #ifndef _GLIBCXX_EXPORT_TEMPLATE
569 # include <bits/ostream.tcc>
572 #endif /* _GLIBCXX_OSTREAM */