Dead
[official-gcc.git] / gomp-20050608-branch / libstdc++-v3 / include / std / std_ostream.h
blob23a19c98d32b8b788291fdc51201f28d0d6813a6
1 // Output streams -*- C++ -*-
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2005, 2006
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, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
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.
39 #ifndef _GLIBCXX_OSTREAM
40 #define _GLIBCXX_OSTREAM 1
42 #pragma GCC system_header
44 #include <ios>
46 _GLIBCXX_BEGIN_NAMESPACE(std)
48 // [27.6.2.1] Template class basic_ostream
49 /**
50 * @brief Controlling output.
52 * This is the base class for all output streams. It provides text
53 * formatting of all builtin types, and communicates with any class
54 * derived from basic_streambuf to do the actual output.
56 template<typename _CharT, typename _Traits>
57 class basic_ostream : virtual public basic_ios<_CharT, _Traits>
59 public:
60 // Types (inherited from basic_ios (27.4.4)):
61 typedef _CharT char_type;
62 typedef typename _Traits::int_type int_type;
63 typedef typename _Traits::pos_type pos_type;
64 typedef typename _Traits::off_type off_type;
65 typedef _Traits traits_type;
67 // Non-standard Types:
68 typedef basic_streambuf<_CharT, _Traits> __streambuf_type;
69 typedef basic_ios<_CharT, _Traits> __ios_type;
70 typedef basic_ostream<_CharT, _Traits> __ostream_type;
71 typedef num_put<_CharT, ostreambuf_iterator<_CharT, _Traits> >
72 __num_put_type;
73 typedef ctype<_CharT> __ctype_type;
75 template<typename _CharT2, typename _Traits2>
76 friend basic_ostream<_CharT2, _Traits2>&
77 operator<<(basic_ostream<_CharT2, _Traits2>&, _CharT2);
79 template<typename _Traits2>
80 friend basic_ostream<char, _Traits2>&
81 operator<<(basic_ostream<char, _Traits2>&, char);
83 template<typename _CharT2, typename _Traits2>
84 friend basic_ostream<_CharT2, _Traits2>&
85 operator<<(basic_ostream<_CharT2, _Traits2>&, const _CharT2*);
87 template<typename _Traits2>
88 friend basic_ostream<char, _Traits2>&
89 operator<<(basic_ostream<char, _Traits2>&, const char*);
91 template<typename _CharT2, typename _Traits2>
92 friend basic_ostream<_CharT2, _Traits2>&
93 operator<<(basic_ostream<_CharT2, _Traits2>&, const char*);
95 // [27.6.2.2] constructor/destructor
96 /**
97 * @brief Base constructor.
99 * This ctor is almost never called by the user directly, rather from
100 * derived classes' initialization lists, which pass a pointer to
101 * their own stream buffer.
103 explicit
104 basic_ostream(__streambuf_type* __sb)
105 { this->init(__sb); }
108 * @brief Base destructor.
110 * This does very little apart from providing a virtual base dtor.
112 virtual
113 ~basic_ostream() { }
115 // [27.6.2.3] prefix/suffix
116 class sentry;
117 friend class sentry;
119 // [27.6.2.5] formatted output
120 // [27.6.2.5.3] basic_ostream::operator<<
121 //@{
123 * @brief Interface for manipulators.
125 * Manuipulators such as @c std::endl and @c std::hex use these
126 * functions in constructs like "std::cout << std::endl". For more
127 * information, see the iomanip header.
129 inline __ostream_type&
130 operator<<(__ostream_type& (*__pf)(__ostream_type&));
132 inline __ostream_type&
133 operator<<(__ios_type& (*__pf)(__ios_type&));
135 inline __ostream_type&
136 operator<<(ios_base& (*__pf) (ios_base&));
137 //@}
139 // [27.6.2.5.2] arithmetic inserters
141 * @name Arithmetic Inserters
143 * All the @c operator<< functions (aka <em>formatted output
144 * functions</em>) have some common behavior. Each starts by
145 * constructing a temporary object of type std::basic_ostream::sentry.
146 * This can have several effects, concluding with the setting of a
147 * status flag; see the sentry documentation for more.
149 * If the sentry status is good, the function tries to generate
150 * whatever data is appropriate for the type of the argument.
152 * If an exception is thrown during insertion, ios_base::badbit
153 * will be turned on in the stream's error state without causing an
154 * ios_base::failure to be thrown. The original exception will then
155 * be rethrown.
157 //@{
159 * @brief Basic arithmetic inserters
160 * @param A variable of builtin type.
161 * @return @c *this if successful
163 * These functions use the stream's current locale (specifically, the
164 * @c num_get facet) to perform numeric formatting.
166 __ostream_type&
167 operator<<(long __n)
168 { return _M_insert(__n); }
170 __ostream_type&
171 operator<<(unsigned long __n)
172 { return _M_insert(__n); }
174 __ostream_type&
175 operator<<(bool __n)
176 { return _M_insert(__n); }
178 __ostream_type&
179 operator<<(short __n);
181 __ostream_type&
182 operator<<(unsigned short __n)
184 // _GLIBCXX_RESOLVE_LIB_DEFECTS
185 // 117. basic_ostream uses nonexistent num_put member functions.
186 return _M_insert(static_cast<unsigned long>(__n));
189 __ostream_type&
190 operator<<(int __n);
192 __ostream_type&
193 operator<<(unsigned int __n)
195 // _GLIBCXX_RESOLVE_LIB_DEFECTS
196 // 117. basic_ostream uses nonexistent num_put member functions.
197 return _M_insert(static_cast<unsigned long>(__n));
200 #ifdef _GLIBCXX_USE_LONG_LONG
201 __ostream_type&
202 operator<<(long long __n)
203 { return _M_insert(__n); }
205 __ostream_type&
206 operator<<(unsigned long long __n)
207 { return _M_insert(__n); }
208 #endif
210 __ostream_type&
211 operator<<(double __f)
212 { return _M_insert(__f); }
214 __ostream_type&
215 operator<<(float __f)
217 // _GLIBCXX_RESOLVE_LIB_DEFECTS
218 // 117. basic_ostream uses nonexistent num_put member functions.
219 return _M_insert(static_cast<double>(__f));
222 __ostream_type&
223 operator<<(long double __f)
224 { return _M_insert(__f); }
226 __ostream_type&
227 operator<<(const void* __p)
228 { return _M_insert(__p); }
231 * @brief Extracting from another streambuf.
232 * @param sb A pointer to a streambuf
234 * This function behaves like one of the basic arithmetic extractors,
235 * in that it also constructs a sentry object and has the same error
236 * handling behavior.
238 * If @a sb is NULL, the stream will set failbit in its error state.
240 * Characters are extracted from @a sb and inserted into @c *this
241 * until one of the following occurs:
243 * - the input stream reaches end-of-file,
244 * - insertion into the output sequence fails (in this case, the
245 * character that would have been inserted is not extracted), or
246 * - an exception occurs while getting a character from @a sb, which
247 * sets failbit in the error state
249 * If the function inserts no characters, failbit is set.
251 __ostream_type&
252 operator<<(__streambuf_type* __sb);
253 //@}
255 // [27.6.2.6] unformatted output functions
257 * @name Unformatted Output Functions
259 * All the unformatted output functions have some common behavior.
260 * Each starts by constructing a temporary object of type
261 * std::basic_ostream::sentry. This has several effects, concluding
262 * with the setting of a status flag; see the sentry documentation
263 * for more.
265 * If the sentry status is good, the function tries to generate
266 * whatever data is appropriate for the type of the argument.
268 * If an exception is thrown during insertion, ios_base::badbit
269 * will be turned on in the stream's error state. If badbit is on in
270 * the stream's exceptions mask, the exception will be rethrown
271 * without completing its actions.
273 //@{
275 * @brief Simple insertion.
276 * @param c The character to insert.
277 * @return *this
279 * Tries to insert @a c.
281 * @note This function is not overloaded on signed char and
282 * unsigned char.
284 __ostream_type&
285 put(char_type __c);
287 // Core write functionality, without sentry.
288 void
289 _M_write(const char_type* __s, streamsize __n)
291 streamsize __put = this->rdbuf()->sputn(__s, __n);
292 if (__put != __n)
293 this->setstate(ios_base::badbit);
297 * @brief Character string insertion.
298 * @param s The array to insert.
299 * @param n Maximum number of characters to insert.
300 * @return *this
302 * Characters are copied from @a s and inserted into the stream until
303 * one of the following happens:
305 * - @a n characters are inserted
306 * - inserting into the output sequence fails (in this case, badbit
307 * will be set in the stream's error state)
309 * @note This function is not overloaded on signed char and
310 * unsigned char.
312 __ostream_type&
313 write(const char_type* __s, streamsize __n);
314 //@}
317 * @brief Synchronizing the stream buffer.
318 * @return *this
320 * If @c rdbuf() is a null pointer, changes nothing.
322 * Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
323 * sets badbit.
325 __ostream_type&
326 flush();
328 // [27.6.2.4] seek members
330 * @brief Getting the current write position.
331 * @return A file position object.
333 * If @c fail() is not false, returns @c pos_type(-1) to indicate
334 * failure. Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
336 pos_type
337 tellp();
340 * @brief Changing the current write position.
341 * @param pos A file position object.
342 * @return *this
344 * If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos). If
345 * that function fails, sets failbit.
347 __ostream_type&
348 seekp(pos_type);
351 * @brief Changing the current write position.
352 * @param off A file offset object.
353 * @param dir The direction in which to seek.
354 * @return *this
356 * If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
357 * If that function fails, sets failbit.
359 __ostream_type&
360 seekp(off_type, ios_base::seekdir);
362 protected:
363 explicit
364 basic_ostream() { }
366 template<typename _ValueT>
367 __ostream_type&
368 _M_insert(_ValueT __v);
372 * @brief Performs setup work for output streams.
374 * Objects of this class are created before all of the standard
375 * inserters are run. It is responsible for "exception-safe prefix and
376 * suffix operations." Additional actions may be added by the
377 * implementation, and we list them in
378 * http://gcc.gnu.org/onlinedocs/libstdc++/17_intro/howto.html#5
379 * under [27.6] notes.
381 template <typename _CharT, typename _Traits>
382 class basic_ostream<_CharT, _Traits>::sentry
384 // Data Members:
385 bool _M_ok;
386 basic_ostream<_CharT,_Traits>& _M_os;
388 public:
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
398 * true ("okay").
400 explicit
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.
410 ~sentry()
412 // XXX MT
413 if (_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
429 { return _M_ok; }
432 // [27.6.2.5.4] character insertion templates
433 //@{
435 * @brief Character inserters
436 * @param out An output stream.
437 * @param c A character.
438 * @return out
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
444 * called.
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 basic_ostream<_CharT, _Traits>&
451 operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c);
453 template<typename _CharT, typename _Traits>
454 basic_ostream<_CharT, _Traits>&
455 operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
456 { return (__out << __out.widen(__c)); }
458 // Specialization
459 template <class _Traits>
460 basic_ostream<char, _Traits>&
461 operator<<(basic_ostream<char, _Traits>& __out, char __c);
463 // Signed and unsigned
464 template<class _Traits>
465 basic_ostream<char, _Traits>&
466 operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
467 { return (__out << static_cast<char>(__c)); }
469 template<class _Traits>
470 basic_ostream<char, _Traits>&
471 operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
472 { return (__out << static_cast<char>(__c)); }
473 //@}
475 //@{
477 * @brief String inserters
478 * @param out An output stream.
479 * @param s A character string.
480 * @return out
481 * @pre @a s must be a non-NULL pointer
483 * Behaves like one of the formatted arithmetic inserters described in
484 * std::basic_ostream. After constructing a sentry object with good
485 * status, this function inserts @c traits::length(s) characters starting
486 * at @a s, widened if necessary, followed by any required padding (as
487 * determined by [22.2.2.2.2]). @c out.width(0) is then called.
489 template<typename _CharT, typename _Traits>
490 basic_ostream<_CharT, _Traits>&
491 operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s);
493 template<typename _CharT, typename _Traits>
494 basic_ostream<_CharT, _Traits> &
495 operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
497 // Partial specializationss
498 template<class _Traits>
499 basic_ostream<char, _Traits>&
500 operator<<(basic_ostream<char, _Traits>& __out, const char* __s);
502 // Signed and unsigned
503 template<class _Traits>
504 basic_ostream<char, _Traits>&
505 operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
506 { return (__out << reinterpret_cast<const char*>(__s)); }
508 template<class _Traits>
509 basic_ostream<char, _Traits> &
510 operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
511 { return (__out << reinterpret_cast<const char*>(__s)); }
512 //@}
514 // [27.6.2.7] standard basic_ostream manipulators
516 * @brief Write a newline and flush the stream.
518 * This manipulator is often mistakenly used when a simple newline is
519 * desired, leading to poor buffering performance. See
520 * http://gcc.gnu.org/onlinedocs/libstdc++/27_io/howto.html#2 for more
521 * on this subject.
523 template<typename _CharT, typename _Traits>
524 basic_ostream<_CharT, _Traits>&
525 endl(basic_ostream<_CharT, _Traits>& __os)
526 { return flush(__os.put(__os.widen('\n'))); }
529 * @brief Write a null character into the output sequence.
531 * "Null character" is @c CharT() by definition. For CharT of @c char,
532 * this correctly writes the ASCII @c NUL character string terminator.
534 template<typename _CharT, typename _Traits>
535 basic_ostream<_CharT, _Traits>&
536 ends(basic_ostream<_CharT, _Traits>& __os)
537 { return __os.put(_CharT()); }
540 * @brief Flushes the output stream.
542 * This manipulator simply calls the stream's @c flush() member function.
544 template<typename _CharT, typename _Traits>
545 basic_ostream<_CharT, _Traits>&
546 flush(basic_ostream<_CharT, _Traits>& __os)
547 { return __os.flush(); }
549 _GLIBCXX_END_NAMESPACE
551 #ifndef _GLIBCXX_EXPORT_TEMPLATE
552 # include <bits/ostream.tcc>
553 #endif
555 #endif /* _GLIBCXX_OSTREAM */