OSDN Git Service

676080c1f20bdf40b6c86a0d43ee2c286be4d40f
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / std / std_ostream.h
1 // Output streams -*- C++ -*-
2
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.
11
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.
16
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.
21
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.
30
31 //
32 // ISO C++ 14882: 27.6.2  Output streams
33 //
34
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.
38  */
39
40 #ifndef _CPP_OSTREAM
41 #define _CPP_OSTREAM    1
42
43 #pragma GCC system_header
44
45 #include <ios>
46
47 namespace std
48 {
49   // 27.6.2.1 Template class basic_ostream
50   template<typename _CharT, typename _Traits>
51     class basic_ostream : virtual public basic_ios<_CharT, _Traits>
52     {
53     public:
54       // Types (inherited from basic_ios (27.4.4)):
55       typedef _CharT                                    char_type;
56       typedef typename _Traits::int_type                int_type;
57       typedef typename _Traits::pos_type                pos_type;
58       typedef typename _Traits::off_type                off_type;
59       typedef _Traits                                   traits_type;
60       
61       // Non-standard Types:
62       typedef basic_streambuf<_CharT, _Traits>          __streambuf_type;
63       typedef basic_ios<_CharT, _Traits>                __ios_type;
64       typedef basic_ostream<_CharT, _Traits>            __ostream_type;
65       typedef ostreambuf_iterator<_CharT, _Traits>      __ostreambuf_iter;
66       typedef num_put<_CharT, __ostreambuf_iter>        __numput_type;
67       typedef ctype<_CharT>                             __ctype_type;
68
69       // 27.6.2.2 Constructor/destructor:
70       explicit 
71       basic_ostream(__streambuf_type* __sb)
72       { this->init(__sb); }
73
74       virtual 
75       ~basic_ostream() { }
76
77       // 27.6.2.3 Prefix/suffix:
78       class sentry;
79       friend class sentry;
80       
81       // 27.6.2.5 Formatted output:
82       // 27.6.2.5.3  basic_ostream::operator<<
83       __ostream_type&
84       operator<<(__ostream_type& (*__pf)(__ostream_type&));
85       
86       __ostream_type&
87       operator<<(__ios_type& (*__pf)(__ios_type&));
88       
89       __ostream_type&
90       operator<<(ios_base& (*__pf) (ios_base&));
91
92       // 27.6.2.5.2 Arithmetic Inserters
93       __ostream_type& 
94       operator<<(long __n);
95       
96       __ostream_type& 
97       operator<<(unsigned long __n);
98
99       __ostream_type& 
100       operator<<(bool __n);
101
102       __ostream_type& 
103       operator<<(short __n)
104       { 
105         ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
106         if (__fmt & ios_base::oct || __fmt & ios_base::hex)
107           return this->operator<<(static_cast<unsigned long>
108                                   (static_cast<unsigned short>(__n)));
109         else
110           return this->operator<<(static_cast<long>(__n));
111       }
112
113       __ostream_type& 
114       operator<<(unsigned short __n)
115       { return this->operator<<(static_cast<unsigned long>(__n)); }
116
117       __ostream_type& 
118       operator<<(int __n)
119       { 
120         ios_base::fmtflags __fmt = this->flags() & ios_base::basefield;
121         if (__fmt & ios_base::oct || __fmt & ios_base::hex)
122           return this->operator<<(static_cast<unsigned long>
123                                   (static_cast<unsigned int>(__n)));
124         else
125           return this->operator<<(static_cast<long>(__n));
126       }
127
128       __ostream_type& 
129       operator<<(unsigned int __n)
130       { return this->operator<<(static_cast<unsigned long>(__n)); }
131
132 #ifdef _GLIBCPP_USE_LONG_LONG
133       __ostream_type& 
134       operator<<(long long __n);
135
136       __ostream_type& 
137       operator<<(unsigned long long __n);
138 #endif
139
140       __ostream_type& 
141       operator<<(double __f);
142
143       __ostream_type& 
144       operator<<(float __f)
145       { return this->operator<<(static_cast<double>(__f)); }
146
147       __ostream_type& 
148       operator<<(long double __f);
149
150       __ostream_type& 
151       operator<<(const void* __p);
152
153       __ostream_type& 
154       operator<<(__streambuf_type* __sb);
155
156       // Unformatted output:
157       __ostream_type& 
158       put(char_type __c);
159
160       __ostream_type& 
161       write(const char_type* __s, streamsize __n);
162
163       __ostream_type& 
164       flush();
165
166       // Seeks:
167       pos_type 
168       tellp();
169
170       __ostream_type& 
171       seekp(pos_type);
172
173       __ostream_type& 
174       seekp(off_type, ios_base::seekdir);
175
176     private:
177 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
178       // Not defined.  (Side effect of DR 50.)
179       __ostream_type& 
180       operator=(const __ostream_type&);
181
182       basic_ostream(const __ostream_type&);
183 #endif
184     };
185
186   // 27.6.2.3  Class basic_ostream::sentry
187   template <typename _CharT, typename _Traits>
188     class basic_ostream<_CharT, _Traits>::sentry
189     {
190       // Data Members:
191       bool                              _M_ok;
192       basic_ostream<_CharT,_Traits>&    _M_os;
193       
194     public:
195       explicit
196       sentry(basic_ostream<_CharT,_Traits>& __os);
197
198       ~sentry()
199       {
200         // XXX MT
201         if (_M_os.flags() & ios_base::unitbuf && !uncaught_exception())
202           {
203             // Can't call flush directly or else will get into recursive lock.
204             if (_M_os.rdbuf() && _M_os.rdbuf()->pubsync() == -1)
205               _M_os.setstate(ios_base::badbit);
206           }
207       }
208
209       operator bool() 
210       { return _M_ok; }
211     };
212
213   template<typename _CharT, typename _Traits>
214     basic_ostream<_CharT, _Traits>&
215     operator<<(basic_ostream<_CharT, _Traits>& __out, _CharT __c);
216
217   template<typename _CharT, typename _Traits>
218     basic_ostream<_CharT, _Traits>&
219     operator<<(basic_ostream<_CharT, _Traits>& __out, char __c)
220     { return (__out << __out.widen(__c)); }
221
222   // Specialization
223   template <class _Traits> 
224     basic_ostream<char, _Traits>&
225     operator<<(basic_ostream<char, _Traits>& __out, char __c);
226
227   // Signed and unsigned
228   template<class _Traits>
229     basic_ostream<char, _Traits>&
230     operator<<(basic_ostream<char, _Traits>& __out, signed char __c)
231     { return (__out << static_cast<char>(__c)); }
232   
233   template<class _Traits>
234     basic_ostream<char, _Traits>&
235     operator<<(basic_ostream<char, _Traits>& __out, unsigned char __c)
236     { return (__out << static_cast<char>(__c)); }
237   
238   template<typename _CharT, typename _Traits>
239     basic_ostream<_CharT, _Traits>&
240     operator<<(basic_ostream<_CharT, _Traits>& __out, const _CharT* __s);
241
242   template<typename _CharT, typename _Traits>
243     basic_ostream<_CharT, _Traits> &
244     operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s);
245
246   // Partial specializationss
247   template<class _Traits>
248     basic_ostream<char, _Traits>&
249     operator<<(basic_ostream<char, _Traits>& __out, const char* __s);
250  
251   // Signed and unsigned
252   template<class _Traits>
253     basic_ostream<char, _Traits>&
254     operator<<(basic_ostream<char, _Traits>& __out, const signed char* __s)
255     { return (__out << reinterpret_cast<const char*>(__s)); }
256
257   template<class _Traits>
258     basic_ostream<char, _Traits> &
259     operator<<(basic_ostream<char, _Traits>& __out, const unsigned char* __s)
260     { return (__out << reinterpret_cast<const char*>(__s)); }
261
262   // 27.6.2.7 Standard basic_ostream manipulators
263   template<typename _CharT, typename _Traits>
264     basic_ostream<_CharT, _Traits>& 
265     endl(basic_ostream<_CharT, _Traits>& __os)
266     { return flush(__os.put(__os.widen('\n'))); }
267
268   template<typename _CharT, typename _Traits>
269     basic_ostream<_CharT, _Traits>& 
270     ends(basic_ostream<_CharT, _Traits>& __os)
271     { return __os.put(_CharT()); }
272   
273   template<typename _CharT, typename _Traits>
274     basic_ostream<_CharT, _Traits>& 
275     flush(basic_ostream<_CharT, _Traits>& __os)
276     { return __os.flush(); }
277
278 } // namespace std
279
280 #ifdef _GLIBCPP_NO_TEMPLATE_EXPORT
281 # define export
282 #ifdef  _GLIBCPP_FULLY_COMPLIANT_HEADERS
283 # include <bits/ostream.tcc>
284 #endif
285 #endif
286
287 #endif  /* _CPP_OSTREAM */
288