OSDN Git Service

2a84f808d7e134000ba517705a798def541e7645
[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
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 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,
21 // 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 <locale>
47
48 _GLIBCXX_BEGIN_NAMESPACE(std)
49
50   // [27.6.2.1] Template class basic_ostream
51   /**
52    *  @brief  Controlling output.
53    *
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.
57   */
58   template<typename _CharT, typename _Traits>
59     class basic_ostream : virtual public basic_ios<_CharT, _Traits>
60     {
61     public:
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;
68       
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> >        
74                                                         __num_put_type;
75       typedef ctype<_CharT>                             __ctype_type;
76
77       template<typename _CharT2, typename _Traits2>
78         friend basic_ostream<_CharT2, _Traits2>&
79         operator<<(basic_ostream<_CharT2, _Traits2>&, _CharT2);
80  
81       template<typename _Traits2>
82         friend basic_ostream<char, _Traits2>&
83         operator<<(basic_ostream<char, _Traits2>&, char);
84  
85       template<typename _CharT2, typename _Traits2>
86         friend basic_ostream<_CharT2, _Traits2>&
87         operator<<(basic_ostream<_CharT2, _Traits2>&, const _CharT2*);
88  
89       template<typename _Traits2>
90         friend basic_ostream<char, _Traits2>&
91         operator<<(basic_ostream<char, _Traits2>&, const char*);
92  
93       template<typename _CharT2, typename _Traits2>
94         friend basic_ostream<_CharT2, _Traits2>&
95         operator<<(basic_ostream<_CharT2, _Traits2>&, const char*);
96
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>&);
101
102       // [27.6.2.2] constructor/destructor
103       /**
104        *  @brief  Base constructor.
105        *
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.
109       */
110       explicit 
111       basic_ostream(__streambuf_type* __sb)
112       { this->init(__sb); }
113
114       /**
115        *  @brief  Base destructor.
116        *
117        *  This does very little apart from providing a virtual base dtor.
118       */
119       virtual 
120       ~basic_ostream() { }
121
122       // [27.6.2.3] prefix/suffix
123       class sentry;
124       friend class sentry;
125       
126       // [27.6.2.5] formatted output
127       // [27.6.2.5.3]  basic_ostream::operator<<
128       //@{
129       /**
130        *  @brief  Interface for manipulators.
131        *
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.
135       */
136       __ostream_type&
137       operator<<(__ostream_type& (*__pf)(__ostream_type&))
138       {
139         // _GLIBCXX_RESOLVE_LIB_DEFECTS
140         // DR 60. What is a formatted input function?
141         // The inserters for manipulators are *not* formatted output functions.
142         return __pf(*this);
143       }
144
145       __ostream_type&
146       operator<<(__ios_type& (*__pf)(__ios_type&))
147       {
148         // _GLIBCXX_RESOLVE_LIB_DEFECTS
149         // DR 60. What is a formatted input function?
150         // The inserters for manipulators are *not* formatted output functions.
151         __pf(*this);
152         return *this;
153       }
154
155       __ostream_type&
156       operator<<(ios_base& (*__pf) (ios_base&))
157       {
158         // _GLIBCXX_RESOLVE_LIB_DEFECTS
159         // DR 60. What is a formatted input function?
160         // The inserters for manipulators are *not* formatted output functions.
161         __pf(*this);
162         return *this;
163       }
164       //@}
165
166       // [27.6.2.5.2] arithmetic inserters
167       /**
168        *  @name Arithmetic Inserters
169        *
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.
175        *
176        *  If the sentry status is good, the function tries to generate
177        *  whatever data is appropriate for the type of the argument.
178        *
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
182        *  be rethrown.
183       */
184       //@{
185       /**
186        *  @brief  Basic arithmetic inserters
187        *  @param  A variable of builtin type.
188        *  @return  @c *this if successful
189        *
190        *  These functions use the stream's current locale (specifically, the
191        *  @c num_get facet) to perform numeric formatting.
192       */
193       __ostream_type& 
194       operator<<(long __n)
195       { return _M_insert(__n); }
196       
197       __ostream_type& 
198       operator<<(unsigned long __n)
199       { return _M_insert(__n); }        
200
201       __ostream_type& 
202       operator<<(bool __n)
203       { return _M_insert(__n); }
204
205       __ostream_type& 
206       operator<<(short __n);
207
208       __ostream_type& 
209       operator<<(unsigned short __n)
210       {
211         // _GLIBCXX_RESOLVE_LIB_DEFECTS
212         // 117. basic_ostream uses nonexistent num_put member functions.
213         return _M_insert(static_cast<unsigned long>(__n));
214       }
215
216       __ostream_type& 
217       operator<<(int __n);
218
219       __ostream_type& 
220       operator<<(unsigned int __n)
221       {
222         // _GLIBCXX_RESOLVE_LIB_DEFECTS
223         // 117. basic_ostream uses nonexistent num_put member functions.
224         return _M_insert(static_cast<unsigned long>(__n));
225       }
226
227 #ifdef _GLIBCXX_USE_LONG_LONG
228       __ostream_type& 
229       operator<<(long long __n)
230       { return _M_insert(__n); }
231
232       __ostream_type& 
233       operator<<(unsigned long long __n)
234       { return _M_insert(__n); }        
235 #endif
236
237       __ostream_type& 
238       operator<<(double __f)
239       { return _M_insert(__f); }
240
241       __ostream_type& 
242       operator<<(float __f)
243       {
244         // _GLIBCXX_RESOLVE_LIB_DEFECTS
245         // 117. basic_ostream uses nonexistent num_put member functions.
246         return _M_insert(static_cast<double>(__f));
247       }
248
249       __ostream_type& 
250       operator<<(long double __f)
251       { return _M_insert(__f); }
252
253       __ostream_type& 
254       operator<<(const void* __p)
255       { return _M_insert(__p); }
256
257       /**
258        *  @brief  Extracting from another streambuf.
259        *  @param  sb  A pointer to a streambuf
260        *
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
263        *  handling behavior.
264        *
265        *  If @a sb is NULL, the stream will set failbit in its error state.
266        *
267        *  Characters are extracted from @a sb and inserted into @c *this
268        *  until one of the following occurs:
269        *
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
275        *
276        *  If the function inserts no characters, failbit is set.
277       */
278       __ostream_type& 
279       operator<<(__streambuf_type* __sb);
280       //@}
281
282       // [27.6.2.6] unformatted output functions
283       /**
284        *  @name Unformatted Output Functions
285        *
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
290        *  for more.
291        *
292        *  If the sentry status is good, the function tries to generate
293        *  whatever data is appropriate for the type of the argument.
294        *
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.
299       */
300       //@{
301       /**
302        *  @brief  Simple insertion.
303        *  @param  c  The character to insert.
304        *  @return  *this
305        *
306        *  Tries to insert @a c.
307        *
308        *  @note  This function is not overloaded on signed char and
309        *         unsigned char.
310       */
311       __ostream_type& 
312       put(char_type __c);
313
314       // Core write functionality, without sentry.
315       void
316       _M_write(const char_type* __s, streamsize __n)
317       {
318         const streamsize __put = this->rdbuf()->sputn(__s, __n);
319         if (__put != __n)
320           this->setstate(ios_base::badbit);
321       }
322
323       void
324       _M_write(char_type __c, streamsize __n)
325       {
326         for (; __n > 0; --__n)
327           {
328             const int_type __put = this->rdbuf()->sputc(__c);
329             if (traits_type::eq_int_type(__put, traits_type::eof()))
330               {
331                 this->setstate(ios_base::badbit);
332                 break;
333               }
334           }
335       }
336
337       /**
338        *  @brief  Character string insertion.
339        *  @param  s  The array to insert.
340        *  @param  n  Maximum number of characters to insert.
341        *  @return  *this
342        *
343        *  Characters are copied from @a s and inserted into the stream until
344        *  one of the following happens:
345        *
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)
349        *
350        *  @note  This function is not overloaded on signed char and
351        *         unsigned char.
352       */
353       __ostream_type& 
354       write(const char_type* __s, streamsize __n);
355       //@}
356
357       /**
358        *  @brief  Synchronizing the stream buffer.
359        *  @return  *this
360        *
361        *  If @c rdbuf() is a null pointer, changes nothing.
362        *
363        *  Otherwise, calls @c rdbuf()->pubsync(), and if that returns -1,
364        *  sets badbit.
365       */
366       __ostream_type& 
367       flush();
368
369       // [27.6.2.4] seek members
370       /**
371        *  @brief  Getting the current write position.
372        *  @return  A file position object.
373        *
374        *  If @c fail() is not false, returns @c pos_type(-1) to indicate
375        *  failure.  Otherwise returns @c rdbuf()->pubseekoff(0,cur,out).
376       */
377       pos_type 
378       tellp();
379
380       /**
381        *  @brief  Changing the current write position.
382        *  @param  pos  A file position object.
383        *  @return  *this
384        *
385        *  If @c fail() is not true, calls @c rdbuf()->pubseekpos(pos).  If
386        *  that function fails, sets failbit.
387       */
388       __ostream_type& 
389       seekp(pos_type);
390
391       /**
392        *  @brief  Changing the current write position.
393        *  @param  off  A file offset object.
394        *  @param  dir  The direction in which to seek.
395        *  @return  *this
396        *
397        *  If @c fail() is not true, calls @c rdbuf()->pubseekoff(off,dir).
398        *  If that function fails, sets failbit.
399       */
400        __ostream_type& 
401       seekp(off_type, ios_base::seekdir);
402       
403     protected:
404       explicit 
405       basic_ostream() { }
406
407       template<typename _ValueT>
408         __ostream_type&
409         _M_insert(_ValueT __v);
410
411       __ostream_type&
412       _M_insert(const char_type* __s, streamsize __n);
413     };
414
415   /**
416    *  @brief  Performs setup work for output streams.
417    *
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.
424   */
425   template <typename _CharT, typename _Traits>
426     class basic_ostream<_CharT, _Traits>::sentry
427     {
428       // Data Members:
429       bool                              _M_ok;
430       basic_ostream<_CharT, _Traits>&   _M_os;
431       
432     public:
433       /**
434        *  @brief  The constructor performs preparatory work.
435        *  @param  os  The output stream to guard.
436        *
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.
440        *
441        *  If the stream state is still good, then the sentry state becomes
442        *  true ("okay").
443       */
444       explicit
445       sentry(basic_ostream<_CharT, _Traits>& __os);
446
447       /**
448        *  @brief  Possibly flushes the stream.
449        *
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.
453       */
454       ~sentry()
455       {
456         // XXX MT
457         if (_M_os.flags() & ios_base::unitbuf && !uncaught_exception())
458           {
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);
462           }
463       }
464
465       /**
466        *  @brief  Quick status checking.
467        *  @return  The sentry state.
468        *
469        *  For ease of use, sentries may be converted to booleans.  The
470        *  return value is that of the sentry state (true == okay).
471       */
472       operator bool() const
473       { return _M_ok; }
474     };
475
476   // [27.6.2.5.4] character insertion templates
477   //@{
478   /**
479    *  @brief  Character inserters
480    *  @param  out  An output stream.
481    *  @param  c  A character.
482    *  @return  out
483    *
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
488    *  called.
489    *
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.
492   */
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); }
497
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)); }
502
503   // Specialization
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); }
508
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)); }
514   
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)); }
519   //@}
520   
521   //@{
522   /**
523    *  @brief  String inserters
524    *  @param  out  An output stream.
525    *  @param  s  A character string.
526    *  @return  out
527    *  @pre  @a s must be a non-NULL pointer
528    *
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.
534   */
535   template<typename _CharT, typename _Traits>
536     inline basic_ostream<_CharT, _Traits>&
537     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s)
538     {
539       if (!__s)
540         __out.setstate(ios_base::badbit);
541       else
542         __out._M_insert(__s, static_cast<streamsize>(_Traits::length(__s)));
543       return __out;
544     }
545
546   template<typename _CharT, typename _Traits>
547     basic_ostream<_CharT, _Traits> &
548     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
549
550   // Partial specializationss
551   template<class _Traits>
552     inline basic_ostream<char, _Traits>&
553     operator<<(basic_ostream<char, _Traits>& __out, const char* __s)
554     {
555       if (!__s)
556         __out.setstate(ios_base::badbit);
557       else
558         __out._M_insert(__s, static_cast<streamsize>(_Traits::length(__s)));
559       return __out;
560     }
561
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)); }
567
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)); }
572   //@}
573
574   // [27.6.2.7] standard basic_ostream manipulators
575   /**
576    *  @brief  Write a newline and flush the stream.
577    *
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
581    *  on this subject.
582   */
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'))); }
587
588   /**
589    *  @brief  Write a null character into the output sequence.
590    *
591    *  "Null character" is @c CharT() by definition.  For CharT of @c char,
592    *  this correctly writes the ASCII @c NUL character string terminator.
593   */
594   template<typename _CharT, typename _Traits>
595     inline basic_ostream<_CharT, _Traits>& 
596     ends(basic_ostream<_CharT, _Traits>& __os)
597     { return __os.put(_CharT()); }
598   
599   /**
600    *  @brief  Flushes the output stream.
601    *
602    *  This manipulator simply calls the stream's @c flush() member function.
603   */
604   template<typename _CharT, typename _Traits>
605     inline basic_ostream<_CharT, _Traits>& 
606     flush(basic_ostream<_CharT, _Traits>& __os)
607     { return __os.flush(); }
608
609 _GLIBCXX_END_NAMESPACE
610
611 #ifndef _GLIBCXX_EXPORT_TEMPLATE
612 # include <bits/ostream.tcc>
613 #endif
614
615 #endif  /* _GLIBCXX_OSTREAM */