2002-11-21 Phil Edwards <pme@gcc.gnu.org>
[official-gcc.git] / libstdc++-v3 / include / std / std_ostream.h
blob7a5532961024b4c19ec069a9636a1c448213a114
1 // Output streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
32 // ISO C++ 14882: 27.6.2 Output streams
35 /** @file ostream
36 * This is a Standard C++ Library header. You should @c #include this header
37 * in your programs, rather than any of the "st[dl]_*.h" implementation files.
40 #ifndef _CPP_OSTREAM
41 #define _CPP_OSTREAM 1
43 #pragma GCC system_header
45 #include <ios>
47 namespace std
49 // [27.6.2.1] Template class basic_ostream
50 /**
51 * @brief Controlling output.
53 * This is the base class for all output streams. It provides text
54 * formatting of all builtin types, and communicates with any class
55 * derived from basic_streambuf to do the actual output.
57 template<typename _CharT, typename _Traits>
58 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
60 public:
61 // Types (inherited from basic_ios (27.4.4)):
62 typedef _CharT char_type;
63 typedef typename _Traits::int_type int_type;
64 typedef typename _Traits::pos_type pos_type;
65 typedef typename _Traits::off_type off_type;
66 typedef _Traits traits_type;
68 // Non-standard Types:
69 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
70 typedef basic_ios<_CharT, _Traits> __ios_type;
71 typedef basic_ostream<_CharT, _Traits> __ostream_type;
72 typedef ostreambuf_iterator<_CharT, _Traits> __ostreambuf_iter;
73 typedef num_put<_CharT, __ostreambuf_iter> __numput_type;
74 typedef ctype<_CharT> __ctype_type;
76 // [27.6.2.2] constructor/destructor
77 /**
78 * @brief Base constructor.
80 * This ctor is almost never called by the user directly, rather from
81 * derived classes' initialization lists, which pass a pointer to
82 * their own stream buffer.
84 explicit
85 basic_ostream(__streambuf_type* __sb)
86 { this->init(__sb); }
88 /**
89 * @brief Base destructor.
91 * This does very little apart from providing a virtual base dtor.
93 virtual
94 ~basic_ostream() { }
96 // [27.6.2.3] prefix/suffix
97 class sentry;
98 friend class sentry;
100 // [27.6.2.5] formatted output
101 // [27.6.2.5.3] basic_ostream::operator<<
102 //@{
104 * @brief Interface for manipulators.
106 * Manuipulators such as @c std::endl and @c std::hex use these
107 * functions in constructs like "std::cout << std::endl". For more
108 * information, see the iomanip header.
110 __ostream_type&
111 operator<<(__ostream_type& (*__pf)(__ostream_type&));
113 __ostream_type&
114 operator<<(__ios_type& (*__pf)(__ios_type&));
116 __ostream_type&
117 operator<<(ios_base& (*__pf) (ios_base&));
118 //@}
120 // [27.6.2.5.2] arithmetic inserters
122 * @name Arithmetic Inserters
124 * All the @c operator<< functions (aka <em>formatted output
125 * functions</em>) have some common behavior. Each starts by
126 * constructing a temporary object of type std::basic_ostream::sentry.
127 * This can have several effects, concluding with the setting of a
128 * status flag; see the sentry documentation for more.
130 * If the sentry status is good, the function tries to generate
131 * whatever data is appropriate for the type of the argument.
133 * If an exception is thrown during insertion, ios_base::badbit
134 * will be turned on in the stream's error state without causing an
135 * ios_base::failure to be thrown. The original exception will then
136 * be rethrown.
138 //@{
140 * @brief Basic arithmetic inserters
141 * @param A variable of builtin type.
142 * @return @c *this if successful
144 * These functions use the stream's current locale (specifically, the
145 * @c num_get facet) to perform numeric formatting.
147 __ostream_type&
148 operator<<(long __n);
150 __ostream_type&
151 operator<<(unsigned long __n);
153 __ostream_type&
154 operator<<(bool __n);
156 __ostream_type&
157 operator<<(short __n)
159 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
160 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
161 return this->operator<<(static_cast<unsigned long>
162 (static_cast<unsigned short>(__n)));
163 else
164 return this->operator<<(static_cast<long>(__n));
167 __ostream_type&
168 operator<<(unsigned short __n)
169 { return this->operator<<(static_cast<unsigned long>(__n)); }
171 __ostream_type&
172 operator<<(int __n)
174 ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
175 if (__fmt & ios_base::oct || __fmt & ios_base::hex)
176 return this->operator<<(static_cast<unsigned long>
177 (static_cast<unsigned int>(__n)));
178 else
179 return this->operator<<(static_cast<long>(__n));
182 __ostream_type&
183 operator<<(unsigned int __n)
184 { return this->operator<<(static_cast<unsigned long>(__n)); }
186 #ifdef _GLIBCPP_USE_LONG_LONG
187 __ostream_type&
188 operator<<(long long __n);
190 __ostream_type&
191 operator<<(unsigned long long __n);
192 #endif
194 __ostream_type&
195 operator<<(double __f);
197 __ostream_type&
198 operator<<(float __f)
199 { return this->operator<<(static_cast<double>(__f)); }
201 __ostream_type&
202 operator<<(long double __f);
204 __ostream_type&
205 operator<<(const void* __p);
208 * @brief Extracting from another streambuf.
209 * @param sb A pointer to a streambuf
211 * This function behaves like one of the basic arithmetic extractors,
212 * in that it also constructs a sentry onject and has the same error
213 * handling behavior.
215 * If @a sb is NULL, the stream will set failbit in its error state.
217 * Characters are extracted from @a sb and inserted into @c *this
218 * until one of the following occurs:
220 * - the input stream reaches end-of-file,
221 * - insertion into the output sequence fails (in this case, the
222 * character that would have been inserted is not extracted), or
223 * - an exception occurs while getting a character from @a sb, which
224 * sets failbit in the error state
226 * If the function inserts no characters, failbit is set.
228 __ostream_type&
229 operator<<(__streambuf_type* __sb);
230 //@}
232 // [27.6.2.6] unformatted output functions
234 * @name Unformatted Output Functions
236 * All the unformatted output functions have some common behavior.
237 * Each starts by constructing a temporary object of type
238 * std::basic_ostream::sentry. This has several effects, concluding
239 * with the setting of a status flag; see the sentry documentation
240 * for more.
242 * If the sentry status is good, the function tries to generate
243 * whatever data is appropriate for the type of the argument.
245 * If an exception is thrown during insertion, ios_base::badbit
246 * will be turned on in the stream's error state. If badbit is on in
247 * the stream's exceptions mask, the exception will be rethrown
248 * without completing its actions.
250 //@{
252 * @brief Simple insertion.
253 * @param c The character to insert.
254 * @return *this
256 * Tries to insert @a c.
258 * @note This function is not overloaded on signed char and
259 * unsigned char.
261 __ostream_type&
262 put(char_type __c);
265 * @brief Character string insertion.
266 * @param s The array to insert.
267 * @param n Maximum number of characters to insert.
268 * @return *this
270 * Characters are copied from @a s and inserted into the stream until
271 * one of the following happens:
273 * - @a n characters are inserted
274 * - inserting into the output sequence fails (in this case, badbit
275 * will be set in the stream's error state)
277 * @note This function is not overloaded on signed char and
278 * unsigned char.
280 __ostream_type&
281 write(const char_type* __s, streamsize __n);
282 //@}
285 * @brief Synchronizing the stream buffer.
286 * @return *this
288 * If @c rdbuf() is a null pointer, changes nothing.
290 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
291 * sets badbit.
293 __ostream_type&
294 flush();
296 // [27.6.2.4] seek members
298 * @brief Getting the current write position.
299 * @return A file position object.
301 * If @c fail() is not false, returns @c pos_type(-1) to indicate
302 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
304 pos_type
305 tellp();
308 * @brief Changing the current write position.
309 * @param pos A file position object.
310 * @return *this
312 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
313 * that function fails, sets failbit.
315 __ostream_type&
316 seekp(pos_type);
319 * @brief Changing the current write position.
320 * @param off A file offset object.
321 * @param dir The direction in which to seek.
322 * @return *this
324 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
325 * If that function fails, sets failbit.
327 __ostream_type&
328 seekp(off_type, ios_base::seekdir);
332 * @brief Performs setup work for output streams.
334 * Objects of this class are created before all of the standard
335 * inserters are run. It is responsible for "exception-safe prefix and
336 * suffix operations." Additional actions may be added by the
337 * implementation, and we list them in
338 * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
339 * under [27.6] notes.
341 template <typename _CharT, typename _Traits>
342 class basic_ostream<_CharT, _Traits>::sentry
344 // Data Members:
345 bool _M_ok;
346 basic_ostream<_CharT,_Traits>& _M_os;
348 public:
350 * @brief The constructor performs preparatory work.
351 * @param os The output stream to guard.
353 * If the stream state is good (@a os.good() is true), then if the
354 * stream is tied to another output stream, @c is.tie()->flush()
355 * is called to synchronize the output sequences.
357 * If the stream state is still good, then the sentry state becomes
358 * true ("okay").
360 explicit
361 sentry(basic_ostream<_CharT,_Traits>& __os);
364 * @brief Possibly flushes the stream.
366 * If @c ios_base::unitbuf is set in @c os.flags(), and
367 * @c std::uncaught_exception() is true, the sentry destructor calls
368 * @c flush() on the output stream.
370 ~sentry()
372 // XXX MT
373 if (_M_os.flags() & ios_base::unitbuf && !uncaught_exception())
375 // Can't call flush directly or else will get into recursive lock.
376 if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
377 _M_os.setstate(ios_base::badbit);
382 * @brief Quick status checking.
383 * @return The sentry state.
385 * For ease of use, sentries may be converted to booleans. The
386 * return value is that of the sentry state (true == okay).
388 operator bool()
389 { return _M_ok; }
392 // [27.6.2.5.4] character insertion templates
393 //@{
395 * @brief Character inserters
396 * @param out An output stream.
397 * @param c A character.
398 * @return out
400 * Behaves like one of the formatted arithmetic inserters described in
401 * std::basic_ostream. After constructing a sentry object with good
402 * status, this function inserts a single character and any required
403 * padding (as determined by [22.2.2.2.2]). @c out.width(0) is then
404 * called.
406 * If @a c is of type @c char and the character type of the stream is not
407 * @c char, the character is widened before insertion.
409 template<typename _CharT, typename _Traits>
410 basic_ostream<_CharT, _Traits>&
411 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c);
413 template<typename _CharT, typename _Traits>
414 basic_ostream<_CharT, _Traits>&
415 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
416 { return (__out << __out.widen(__c)); }
418 // Specialization
419 template <class _Traits>
420 basic_ostream<char, _Traits>&
421 operator<<(basic_ostream<char, _Traits>& __out, char __c);
423 // Signed and unsigned
424 template<class _Traits>
425 basic_ostream<char, _Traits>&
426 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
427 { return (__out << static_cast<char>(__c)); }
429 template<class _Traits>
430 basic_ostream<char, _Traits>&
431 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
432 { return (__out << static_cast<char>(__c)); }
433 //@}
435 //@{
437 * @brief String inserters
438 * @param out An output stream.
439 * @param s A character string.
440 * @return out
441 * @pre @a s must be a non-NULL pointer
443 * Behaves like one of the formatted arithmetic inserters described in
444 * std::basic_ostream. After constructing a sentry object with good
445 * status, this function inserts @c traits::length(s) characters starting
446 * at @a s, widened if necessary, followed by any required padding (as
447 * determined by [22.2.2.2.2]). @c out.width(0) is then called.
449 template<typename _CharT, typename _Traits>
450 basic_ostream<_CharT, _Traits>&
451 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s);
453 template<typename _CharT, typename _Traits>
454 basic_ostream<_CharT, _Traits> &
455 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
457 // Partial specializationss
458 template<class _Traits>
459 basic_ostream<char, _Traits>&
460 operator<<(basic_ostream<char, _Traits>& __out, const char* __s);
462 // Signed and unsigned
463 template<class _Traits>
464 basic_ostream<char, _Traits>&
465 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
466 { return (__out << reinterpret_cast<const char*>(__s)); }
468 template<class _Traits>
469 basic_ostream<char, _Traits> &
470 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
471 { return (__out << reinterpret_cast<const char*>(__s)); }
472 //@}
474 // [27.6.2.7] standard basic_ostream manipulators
476 * @brief Write a newline and flush the stream.
478 * This manipulator is often mistakenly used when a simple newline is
479 * desired, leading to poor buffering performance. See
480 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
481 * on this subject.
483 template<typename _CharT, typename _Traits>
484 basic_ostream<_CharT, _Traits>&
485 endl(basic_ostream<_CharT, _Traits>& __os)
486 { return flush(__os.put(__os.widen('\n'))); }
489 * @brief Write a null character into the output sequence.
491 * "Null character" is @c CharT() by definition. For CharT of @c char,
492 * this correctly writes the ASCII @c NUL character string terminator.
494 template<typename _CharT, typename _Traits>
495 basic_ostream<_CharT, _Traits>&
496 ends(basic_ostream<_CharT, _Traits>& __os)
497 { return __os.put(_CharT()); }
500 * @brief Flushes the output stream.
502 * This manipulator simply calls the stream's @c flush() member function.
504 template<typename _CharT, typename _Traits>
505 basic_ostream<_CharT, _Traits>&
506 flush(basic_ostream<_CharT, _Traits>& __os)
507 { return __os.flush(); }
509 } // namespace std
511 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
512 # define export
513 #endif
514 #ifdef _GLIBCPP_FULLY_COMPLIANT_HEADERS
515 # include <bits/ostream.tcc>
516 #endif
518 #endif /* _CPP_OSTREAM */