OSDN Git Service

2009-02-20 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / ostream
1 // Output streams -*- C++ -*-
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
4 // 2006, 2007, 2008, 2009
5 // Free Software Foundation, Inc.
6 //
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)
11 // any later version.
12
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.
17
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.
22
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.
31
32 /** @file ostream
33  *  This is a Standard C++ Library header.
34  */
35
36 //
37 // ISO C++ 14882: 27.6.2  Output streams
38 //
39
40 #ifndef _GLIBCXX_OSTREAM
41 #define _GLIBCXX_OSTREAM 1
42
43 #pragma GCC system_header
44
45 #include <ios>
46 #include <bits/ostream_insert.h>
47
48 _GLIBCXX_BEGIN_NAMESPACE(std)
49
50   // [27.6.2.1] Template class basic_ostream
51   /**
52    *  @brief  Controlling output.
53    *  @ingroup io
54    *
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.
58   */
59   template<typename _CharT, typename _Traits>
60     class basic_ostream : virtual public basic_ios<_CharT, _Traits>
61     {
62     public:
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;
69       
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> >        
75                                                         __num_put_type;
76       typedef ctype<_CharT>                             __ctype_type;
77
78       // [27.6.2.2] constructor/destructor
79       /**
80        *  @brief  Base constructor.
81        *
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.
85       */
86       explicit 
87       basic_ostream(__streambuf_type* __sb)
88       { this->init(__sb); }
89
90       /**
91        *  @brief  Base destructor.
92        *
93        *  This does very little apart from providing a virtual base dtor.
94       */
95       virtual 
96       ~basic_ostream() { }
97
98       // [27.6.2.3] prefix/suffix
99       class sentry;
100       friend class sentry;
101       
102       // [27.6.2.5] formatted output
103       // [27.6.2.5.3]  basic_ostream::operator<<
104       //@{
105       /**
106        *  @brief  Interface for manipulators.
107        *
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.
111       */
112       __ostream_type&
113       operator<<(__ostream_type& (*__pf)(__ostream_type&))
114       {
115         // _GLIBCXX_RESOLVE_LIB_DEFECTS
116         // DR 60. What is a formatted input function?
117         // The inserters for manipulators are *not* formatted output functions.
118         return __pf(*this);
119       }
120
121       __ostream_type&
122       operator<<(__ios_type& (*__pf)(__ios_type&))
123       {
124         // _GLIBCXX_RESOLVE_LIB_DEFECTS
125         // DR 60. What is a formatted input function?
126         // The inserters for manipulators are *not* formatted output functions.
127         __pf(*this);
128         return *this;
129       }
130
131       __ostream_type&
132       operator<<(ios_base& (*__pf) (ios_base&))
133       {
134         // _GLIBCXX_RESOLVE_LIB_DEFECTS
135         // DR 60. What is a formatted input function?
136         // The inserters for manipulators are *not* formatted output functions.
137         __pf(*this);
138         return *this;
139       }
140       //@}
141
142       // [27.6.2.5.2] arithmetic inserters
143       /**
144        *  @name Arithmetic Inserters
145        *
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.
151        *
152        *  If the sentry status is good, the function tries to generate
153        *  whatever data is appropriate for the type of the argument.
154        *
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
158        *  be rethrown.
159       */
160       //@{
161       /**
162        *  @brief  Basic arithmetic inserters
163        *  @param  A variable of builtin type.
164        *  @return  @c *this if successful
165        *
166        *  These functions use the stream's current locale (specifically, the
167        *  @c num_get facet) to perform numeric formatting.
168       */
169       __ostream_type& 
170       operator<<(long __n)
171       { return _M_insert(__n); }
172       
173       __ostream_type& 
174       operator<<(unsigned long __n)
175       { return _M_insert(__n); }        
176
177       __ostream_type& 
178       operator<<(bool __n)
179       { return _M_insert(__n); }
180
181       __ostream_type& 
182       operator<<(short __n);
183
184       __ostream_type& 
185       operator<<(unsigned short __n)
186       {
187         // _GLIBCXX_RESOLVE_LIB_DEFECTS
188         // 117. basic_ostream uses nonexistent num_put member functions.
189         return _M_insert(static_cast<unsigned long>(__n));
190       }
191
192       __ostream_type& 
193       operator<<(int __n);
194
195       __ostream_type& 
196       operator<<(unsigned int __n)
197       {
198         // _GLIBCXX_RESOLVE_LIB_DEFECTS
199         // 117. basic_ostream uses nonexistent num_put member functions.
200         return _M_insert(static_cast<unsigned long>(__n));
201       }
202
203 #ifdef _GLIBCXX_USE_LONG_LONG
204       __ostream_type& 
205       operator<<(long long __n)
206       { return _M_insert(__n); }
207
208       __ostream_type& 
209       operator<<(unsigned long long __n)
210       { return _M_insert(__n); }        
211 #endif
212
213       __ostream_type& 
214       operator<<(double __f)
215       { return _M_insert(__f); }
216
217       __ostream_type& 
218       operator<<(float __f)
219       {
220         // _GLIBCXX_RESOLVE_LIB_DEFECTS
221         // 117. basic_ostream uses nonexistent num_put member functions.
222         return _M_insert(static_cast<double>(__f));
223       }
224
225       __ostream_type& 
226       operator<<(long double __f)
227       { return _M_insert(__f); }
228
229       __ostream_type& 
230       operator<<(const void* __p)
231       { return _M_insert(__p); }
232
233       /**
234        *  @brief  Extracting from another streambuf.
235        *  @param  sb  A pointer to a streambuf
236        *
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
239        *  handling behavior.
240        *
241        *  If @a sb is NULL, the stream will set failbit in its error state.
242        *
243        *  Characters are extracted from @a sb and inserted into @c *this
244        *  until one of the following occurs:
245        *
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
251        *
252        *  If the function inserts no characters, failbit is set.
253       */
254       __ostream_type& 
255       operator<<(__streambuf_type* __sb);
256       //@}
257
258       // [27.6.2.6] unformatted output functions
259       /**
260        *  @name Unformatted Output Functions
261        *
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
266        *  for more.
267        *
268        *  If the sentry status is good, the function tries to generate
269        *  whatever data is appropriate for the type of the argument.
270        *
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.
275       */
276       //@{
277       /**
278        *  @brief  Simple insertion.
279        *  @param  c  The character to insert.
280        *  @return  *this
281        *
282        *  Tries to insert @a c.
283        *
284        *  @note  This function is not overloaded on signed char and
285        *         unsigned char.
286       */
287       __ostream_type& 
288       put(char_type __c);
289
290       // Core write functionality, without sentry.
291       void
292       _M_write(const char_type* __s, streamsize __n)
293       {
294         const streamsize __put = this->rdbuf()->sputn(__s, __n);
295         if (__put != __n)
296           this->setstate(ios_base::badbit);
297       }
298
299       /**
300        *  @brief  Character string insertion.
301        *  @param  s  The array to insert.
302        *  @param  n  Maximum number of characters to insert.
303        *  @return  *this
304        *
305        *  Characters are copied from @a s and inserted into the stream until
306        *  one of the following happens:
307        *
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)
311        *
312        *  @note  This function is not overloaded on signed char and
313        *         unsigned char.
314       */
315       __ostream_type& 
316       write(const char_type* __s, streamsize __n);
317       //@}
318
319       /**
320        *  @brief  Synchronizing the stream buffer.
321        *  @return  *this
322        *
323        *  If @c rdbuf() is a null pointer, changes nothing.
324        *
325        *  Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
326        *  sets badbit.
327       */
328       __ostream_type& 
329       flush();
330
331       // [27.6.2.4] seek members
332       /**
333        *  @brief  Getting the current write position.
334        *  @return  A file position object.
335        *
336        *  If @c fail() is not false, returns @c pos_type(-1) to indicate
337        *  failure.  Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
338       */
339       pos_type 
340       tellp();
341
342       /**
343        *  @brief  Changing the current write position.
344        *  @param  pos  A file position object.
345        *  @return  *this
346        *
347        *  If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos).  If
348        *  that function fails, sets failbit.
349       */
350       __ostream_type& 
351       seekp(pos_type);
352
353       /**
354        *  @brief  Changing the current write position.
355        *  @param  off  A file offset object.
356        *  @param  dir  The direction in which to seek.
357        *  @return  *this
358        *
359        *  If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
360        *  If that function fails, sets failbit.
361       */
362        __ostream_type& 
363       seekp(off_type, ios_base::seekdir);
364       
365     protected:
366       basic_ostream()
367       { this->init(0); }
368
369       template<typename _ValueT>
370         __ostream_type&
371         _M_insert(_ValueT __v);
372     };
373
374   /**
375    *  @brief  Performs setup work for output streams.
376    *
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." 
380   */
381   template <typename _CharT, typename _Traits>
382     class basic_ostream<_CharT, _Traits>::sentry
383     {
384       // Data Members:
385       bool                              _M_ok;
386       basic_ostream<_CharT, _Traits>&   _M_os;
387       
388     public:
389       /**
390        *  @brief  The constructor performs preparatory work.
391        *  @param  os  The output stream to guard.
392        *
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.
396        *
397        *  If the stream state is still good, then the sentry state becomes
398        *  true ("okay").
399       */
400       explicit
401       sentry(basic_ostream<_CharT, _Traits>& __os);
402
403       /**
404        *  @brief  Possibly flushes the stream.
405        *
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.
409       */
410       ~sentry()
411       {
412         // XXX MT
413         if (bool(_M_os.flags() & ios_base::unitbuf) && !uncaught_exception())
414           {
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);
418           }
419       }
420
421       /**
422        *  @brief  Quick status checking.
423        *  @return  The sentry state.
424        *
425        *  For ease of use, sentries may be converted to booleans.  The
426        *  return value is that of the sentry state (true == okay).
427       */
428       operator bool() const
429       { return _M_ok; }
430     };
431
432   // [27.6.2.5.4] character insertion templates
433   //@{
434   /**
435    *  @brief  Character inserters
436    *  @param  out  An output stream.
437    *  @param  c  A character.
438    *  @return  out
439    *
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.
445    *
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.
448   */
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); }
453
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)); }
458
459   // Specialization
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); }
464
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)); }
470   
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)); }
475   //@}
476   
477   //@{
478   /**
479    *  @brief  String inserters
480    *  @param  out  An output stream.
481    *  @param  s  A character string.
482    *  @return  out
483    *  @pre  @a s must be a non-NULL pointer
484    *
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.
490   */
491   template<typename _CharT, typename _Traits>
492     inline basic_ostream<_CharT, _Traits>&
493     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
494     {
495       if (!__s)
496         __out.setstate(ios_base::badbit);
497       else
498         __ostream_insert(__out, __s,
499                          static_cast<streamsize>(_Traits::length(__s)));
500       return __out;
501     }
502
503   template<typename _CharT, typename _Traits>
504     basic_ostream<_CharT, _Traits> &
505     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
506
507   // Partial specializations
508   template<class _Traits>
509     inline basic_ostream<char, _Traits>&
510     operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
511     {
512       if (!__s)
513         __out.setstate(ios_base::badbit);
514       else
515         __ostream_insert(__out, __s,
516                          static_cast<streamsize>(_Traits::length(__s)));
517       return __out;
518     }
519
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)); }
525
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)); }
530   //@}
531
532   // [27.6.2.7] standard basic_ostream manipulators
533   /**
534    *  @brief  Write a newline and flush the stream.
535    *
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
539    *  on this subject.
540   */
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'))); }
545
546   /**
547    *  @brief  Write a null character into the output sequence.
548    *
549    *  "Null character" is @c CharT() by definition.  For CharT of @c char,
550    *  this correctly writes the ASCII @c NUL character string terminator.
551   */
552   template<typename _CharT, typename _Traits>
553     inline basic_ostream<_CharT, _Traits>& 
554     ends(basic_ostream<_CharT, _Traits>& __os)
555     { return __os.put(_CharT()); }
556   
557   /**
558    *  @brief  Flushes the output stream.
559    *
560    *  This manipulator simply calls the stream's @c flush() member function.
561   */
562   template<typename _CharT, typename _Traits>
563     inline basic_ostream<_CharT, _Traits>& 
564     flush(basic_ostream<_CharT, _Traits>& __os)
565     { return __os.flush(); }
566
567 _GLIBCXX_END_NAMESPACE
568
569 #ifndef _GLIBCXX_EXPORT_TEMPLATE
570 # include <bits/ostream.tcc>
571 #endif
572
573 #endif  /* _GLIBCXX_OSTREAM */