|
1 /* |
|
2 * Copyright (c) 1999 |
|
3 * Silicon Graphics Computer Systems, Inc. |
|
4 * |
|
5 * Copyright (c) 1999 |
|
6 * Boris Fomitchev |
|
7 * |
|
8 * This material is provided "as is", with absolutely no warranty expressed |
|
9 * or implied. Any use is at your own risk. |
|
10 * |
|
11 * Permission to use or copy this software for any purpose is hereby granted |
|
12 * without fee, provided the above notices are retained on all copies. |
|
13 * Permission to modify the code and to distribute modified code is granted, |
|
14 * provided the above notices are retained, and a notice that the code was |
|
15 * modified is included with the above copyright notice. |
|
16 * |
|
17 */ |
|
18 |
|
19 |
|
20 #ifndef _STLP_INTERNAL_OSTREAM_H |
|
21 #define _STLP_INTERNAL_OSTREAM_H |
|
22 |
|
23 #ifndef _STLP_INTERNAL_IOS_H |
|
24 # include <stl/_ios.h> // For basic_ios<>. Includes <iosfwd>. |
|
25 #endif |
|
26 |
|
27 #ifndef _STLP_INTERNAL_OSTREAMBUF_ITERATOR_H |
|
28 # include <stl/_ostreambuf_iterator.h> |
|
29 #endif |
|
30 |
|
31 #if !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT) && !defined (_STLP_INTERNAL_EXCEPTION) |
|
32 # include <stl/_exception.h> |
|
33 #endif |
|
34 |
|
35 _STLP_BEGIN_NAMESPACE |
|
36 |
|
37 #if defined (_STLP_USE_TEMPLATE_EXPORT) |
|
38 template <class _CharT, class _Traits> |
|
39 class _Osentry; |
|
40 #endif |
|
41 |
|
42 _STLP_MOVE_TO_PRIV_NAMESPACE |
|
43 |
|
44 template <class _CharT, class _Traits> |
|
45 bool __init_bostr(basic_ostream<_CharT, _Traits>& __str); |
|
46 |
|
47 _STLP_MOVE_TO_STD_NAMESPACE |
|
48 |
|
49 //---------------------------------------------------------------------- |
|
50 // class basic_ostream<> |
|
51 |
|
52 template <class _CharT, class _Traits> |
|
53 class basic_ostream : virtual public basic_ios<_CharT, _Traits> { |
|
54 typedef basic_ostream<_CharT, _Traits> _Self; |
|
55 |
|
56 #if defined (_STLP_MSVC) && (_STLP_MSVC >= 1300 && _STLP_MSVC <= 1310) |
|
57 //explicitely defined as private to avoid warnings: |
|
58 basic_ostream(_Self const&); |
|
59 _Self& operator = (_Self const&); |
|
60 #endif |
|
61 |
|
62 public: // Types |
|
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 typedef basic_ios<_CharT, _Traits> _Basic_ios; |
|
69 |
|
70 public: // Constructor and destructor. |
|
71 explicit basic_ostream(basic_streambuf<_CharT, _Traits>* __buf); |
|
72 ~basic_ostream(); |
|
73 |
|
74 public: // Hooks for manipulators. |
|
75 typedef basic_ios<_CharT, _Traits>& (_STLP_CALL *__ios_fn)(basic_ios<_CharT, _Traits>&); |
|
76 typedef ios_base& (_STLP_CALL *__ios_base_fn)(ios_base&); |
|
77 typedef _Self& (_STLP_CALL *__ostream_fn)(_Self&); |
|
78 _Self& operator<< (__ostream_fn __f) { return __f(*this); } |
|
79 _Self & operator<< (__ios_base_fn __f) { __f(*this); return *this; } |
|
80 _Self& operator<< (__ios_fn __ff) { __ff(*this); return *this; } |
|
81 |
|
82 private: |
|
83 bool _M_copy_buffered(basic_streambuf<_CharT, _Traits>* __from, |
|
84 basic_streambuf<_CharT, _Traits>* __to); |
|
85 bool _M_copy_unbuffered(basic_streambuf<_CharT, _Traits>* __from, |
|
86 basic_streambuf<_CharT, _Traits>* __to); |
|
87 |
|
88 public: |
|
89 void _M_put_char(_CharT __c); |
|
90 |
|
91 void _M_put_nowiden(const _CharT* __s); |
|
92 void _M_put_widen(const char* __s); |
|
93 bool _M_put_widen_aux(const char* __s, streamsize __n); |
|
94 |
|
95 public: // Unformatted output. |
|
96 _Self& put(char_type __c); |
|
97 _Self& write(const char_type* __s, streamsize __n); |
|
98 |
|
99 public: // Formatted output. |
|
100 // Formatted output from a streambuf. |
|
101 _Self& operator<<(basic_streambuf<_CharT, _Traits>* __buf); |
|
102 # ifndef _STLP_NO_FUNCTION_TMPL_PARTIAL_ORDER |
|
103 // this is needed for compiling with option char = unsigned |
|
104 _Self& operator<<(unsigned char __x) { _M_put_char(__x); return *this; } |
|
105 # endif |
|
106 _Self& operator<<(short __x); |
|
107 _Self& operator<<(unsigned short __x); |
|
108 _Self& operator<<(int __x); |
|
109 #if defined (_WIN64) || !defined (_STLP_MSVC) || (_STLP_MSVC < 1300) |
|
110 _Self& operator<<(unsigned int __x); |
|
111 #else |
|
112 /* We define this operator with size_t rather than unsigned int to avoid |
|
113 * 64 bits warning. |
|
114 */ |
|
115 _Self& operator<<(size_t __x); |
|
116 #endif |
|
117 _Self& operator<<(long __x); |
|
118 _Self& operator<<(unsigned long __x); |
|
119 #ifdef _STLP_LONG_LONG |
|
120 _Self& operator<< (_STLP_LONG_LONG __x); |
|
121 _Self& operator<< (unsigned _STLP_LONG_LONG __x); |
|
122 #endif |
|
123 _Self& operator<<(float __x); |
|
124 _Self& operator<<(double __x); |
|
125 # ifndef _STLP_NO_LONG_DOUBLE |
|
126 _Self& operator<<(long double __x); |
|
127 # endif |
|
128 _Self& operator<<(const void* __x); |
|
129 # ifndef _STLP_NO_BOOL |
|
130 _Self& operator<<(bool __x); |
|
131 # endif |
|
132 |
|
133 public: // Buffer positioning and manipulation. |
|
134 _Self& flush() { |
|
135 if (this->rdbuf()) |
|
136 if (this->rdbuf()->pubsync() == -1) |
|
137 this->setstate(ios_base::badbit); |
|
138 return *this; |
|
139 } |
|
140 |
|
141 pos_type tellp() { |
|
142 return this->rdbuf() && !this->fail() |
|
143 ? this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out) |
|
144 : pos_type(-1); |
|
145 } |
|
146 |
|
147 _Self& seekp(pos_type __pos) { |
|
148 if (this->rdbuf() && !this->fail()) { |
|
149 if (this->rdbuf()->pubseekpos(__pos) == pos_type(-1)) { |
|
150 this->setstate(ios_base::failbit); |
|
151 } |
|
152 } |
|
153 return *this; |
|
154 } |
|
155 |
|
156 _Self& seekp(off_type __off, ios_base::seekdir __dir) { |
|
157 if (this->rdbuf() && !this->fail()) |
|
158 this->rdbuf()->pubseekoff(__off, __dir); |
|
159 return *this; |
|
160 } |
|
161 |
|
162 #if defined (_STLP_USE_TEMPLATE_EXPORT) |
|
163 // If we are using DLL specs, we have not to use inner classes |
|
164 // end class declaration here |
|
165 typedef _Osentry<_CharT, _Traits> sentry; |
|
166 }; |
|
167 # define sentry _Osentry |
|
168 template <class _CharT, class _Traits> |
|
169 class _Osentry { |
|
170 typedef _Osentry<_CharT, _Traits> _Self; |
|
171 #else |
|
172 class sentry { |
|
173 typedef sentry _Self; |
|
174 #endif |
|
175 private: |
|
176 basic_ostream<_CharT, _Traits>& _M_str; |
|
177 // basic_streambuf<_CharT, _Traits>* _M_buf; |
|
178 bool _M_ok; |
|
179 public: |
|
180 explicit sentry(basic_ostream<_CharT, _Traits>& __str) |
|
181 : _M_str(__str), /* _M_buf(__str.rdbuf()), */ _M_ok(_STLP_PRIV __init_bostr(__str)) |
|
182 {} |
|
183 |
|
184 ~sentry() { |
|
185 if (_M_str.flags() & ios_base::unitbuf) |
|
186 #if !defined (_STLP_NO_UNCAUGHT_EXCEPT_SUPPORT) |
|
187 if (!uncaught_exception()) |
|
188 #endif |
|
189 _M_str.flush(); |
|
190 } |
|
191 |
|
192 operator bool() const { return _M_ok; } |
|
193 private: // Disable assignment and copy constructor. |
|
194 //Implementation is here only to avoid warning with some compilers. |
|
195 sentry(const _Self& __s) : _M_str(__s._M_str) {} |
|
196 _Self& operator=(const _Self&) { return *this; } |
|
197 }; |
|
198 #if defined (_STLP_USE_TEMPLATE_EXPORT) |
|
199 # undef sentry |
|
200 #else |
|
201 // close basic_ostream class definition here |
|
202 }; |
|
203 #endif |
|
204 |
|
205 #if defined (_STLP_USE_TEMPLATE_EXPORT) |
|
206 _STLP_EXPORT_TEMPLATE_CLASS basic_ostream<char, char_traits<char> >; |
|
207 _STLP_EXPORT_TEMPLATE_CLASS _Osentry<char, char_traits<char> >; |
|
208 # if !defined (_STLP_NO_WCHAR_T) |
|
209 _STLP_EXPORT_TEMPLATE_CLASS basic_ostream<wchar_t, char_traits<wchar_t> >; |
|
210 _STLP_EXPORT_TEMPLATE_CLASS _Osentry<wchar_t, char_traits<wchar_t> >; |
|
211 # endif |
|
212 #endif /* _STLP_USE_TEMPLATE_EXPORT */ |
|
213 |
|
214 _STLP_MOVE_TO_PRIV_NAMESPACE |
|
215 |
|
216 // Helper functions for istream<>::sentry constructor. |
|
217 template <class _CharT, class _Traits> |
|
218 bool __init_bostr(basic_ostream<_CharT, _Traits>& __str) { |
|
219 if (__str.good()) { |
|
220 // boris : check if this is needed ! |
|
221 if (!__str.rdbuf()) |
|
222 __str.setstate(ios_base::badbit); |
|
223 if (__str.tie()) |
|
224 __str.tie()->flush(); |
|
225 return __str.good(); |
|
226 } |
|
227 else |
|
228 return false; |
|
229 } |
|
230 |
|
231 template <class _CharT, class _Traits> |
|
232 inline basic_streambuf<_CharT, _Traits>* _STLP_CALL |
|
233 __get_ostreambuf(basic_ostream<_CharT, _Traits>& __St) |
|
234 { return __St.rdbuf(); } |
|
235 |
|
236 _STLP_MOVE_TO_STD_NAMESPACE |
|
237 |
|
238 // Non-member functions. |
|
239 template <class _CharT, class _Traits> |
|
240 inline basic_ostream<_CharT, _Traits>& _STLP_CALL |
|
241 operator<<(basic_ostream<_CharT, _Traits>& __os, _CharT __c){ |
|
242 __os._M_put_char(__c); |
|
243 return __os; |
|
244 } |
|
245 |
|
246 template <class _CharT, class _Traits> |
|
247 inline basic_ostream<_CharT, _Traits>& _STLP_CALL |
|
248 operator<<(basic_ostream<_CharT, _Traits>& __os, const _CharT* __s) { |
|
249 __os._M_put_nowiden(__s); |
|
250 return __os; |
|
251 } |
|
252 |
|
253 #if defined (_STLP_NO_FUNCTION_TMPL_PARTIAL_ORDER) |
|
254 // some specializations |
|
255 |
|
256 inline basic_ostream<char, char_traits<char> >& _STLP_CALL |
|
257 operator<<(basic_ostream<char, char_traits<char> >& __os, char __c) { |
|
258 __os._M_put_char(__c); |
|
259 return __os; |
|
260 } |
|
261 |
|
262 inline basic_ostream<char, char_traits<char> >& _STLP_CALL |
|
263 operator<<(basic_ostream<char, char_traits<char> >& __os, signed char __c) { |
|
264 __os._M_put_char(__c); |
|
265 return __os; |
|
266 } |
|
267 |
|
268 inline basic_ostream<char, char_traits<char> >& _STLP_CALL |
|
269 operator<<(basic_ostream<char, char_traits<char> >& __os, unsigned char __c) { |
|
270 __os._M_put_char(__c); |
|
271 return __os; |
|
272 } |
|
273 |
|
274 inline basic_ostream<char, char_traits<char> >& _STLP_CALL |
|
275 operator<<(basic_ostream<char, char_traits<char> >& __os, const char* __s) { |
|
276 __os._M_put_nowiden(__s); |
|
277 return __os; |
|
278 } |
|
279 |
|
280 inline basic_ostream<char, char_traits<char> >& _STLP_CALL |
|
281 operator<<(basic_ostream<char, char_traits<char> >& __os, const signed char* __s) { |
|
282 __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s)); |
|
283 return __os; |
|
284 } |
|
285 |
|
286 inline basic_ostream<char, char_traits<char> >& |
|
287 operator<<(basic_ostream<char, char_traits<char> >& __os, const unsigned char* __s) { |
|
288 __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s)); |
|
289 return __os; |
|
290 } |
|
291 |
|
292 #else |
|
293 |
|
294 // also for compilers who might use that |
|
295 template <class _CharT, class _Traits> |
|
296 inline basic_ostream<_CharT, _Traits>& _STLP_CALL |
|
297 operator<<(basic_ostream<_CharT, _Traits>& __os, char __c) { |
|
298 __os._M_put_char(__os.widen(__c)); |
|
299 return __os; |
|
300 } |
|
301 |
|
302 template <class _Traits> |
|
303 inline basic_ostream<char, _Traits>& _STLP_CALL |
|
304 operator<<(basic_ostream<char, _Traits>& __os, char __c) { |
|
305 __os._M_put_char(__c); |
|
306 return __os; |
|
307 } |
|
308 |
|
309 template <class _Traits> |
|
310 inline basic_ostream<char, _Traits>& _STLP_CALL |
|
311 operator<<(basic_ostream<char, _Traits>& __os, signed char __c) { |
|
312 __os._M_put_char(__c); |
|
313 return __os; |
|
314 } |
|
315 |
|
316 template <class _Traits> |
|
317 inline basic_ostream<char, _Traits>& _STLP_CALL |
|
318 operator<<(basic_ostream<char, _Traits>& __os, unsigned char __c) { |
|
319 __os._M_put_char(__c); |
|
320 return __os; |
|
321 } |
|
322 |
|
323 template <class _CharT, class _Traits> |
|
324 inline basic_ostream<_CharT, _Traits>& _STLP_CALL |
|
325 operator<<(basic_ostream<_CharT, _Traits>& __os, const char* __s) { |
|
326 __os._M_put_widen(__s); |
|
327 return __os; |
|
328 } |
|
329 |
|
330 template <class _Traits> |
|
331 inline basic_ostream<char, _Traits>& _STLP_CALL |
|
332 operator<<(basic_ostream<char, _Traits>& __os, const char* __s) { |
|
333 __os._M_put_nowiden(__s); |
|
334 return __os; |
|
335 } |
|
336 |
|
337 template <class _Traits> |
|
338 inline basic_ostream<char, _Traits>& _STLP_CALL |
|
339 operator<<(basic_ostream<char, _Traits>& __os, const signed char* __s) { |
|
340 __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s)); |
|
341 return __os; |
|
342 } |
|
343 |
|
344 template <class _Traits> |
|
345 inline basic_ostream<char, _Traits>& |
|
346 operator<<(basic_ostream<char, _Traits>& __os, const unsigned char* __s) { |
|
347 __os._M_put_nowiden(__REINTERPRET_CAST(const char*,__s)); |
|
348 return __os; |
|
349 } |
|
350 #endif /* _STLP_NO_FUNCTION_TMPL_PARTIAL_ORDER */ |
|
351 |
|
352 //---------------------------------------------------------------------- |
|
353 // basic_ostream manipulators. |
|
354 |
|
355 template <class _CharT, class _Traits> |
|
356 inline basic_ostream<_CharT, _Traits>& _STLP_CALL |
|
357 endl(basic_ostream<_CharT, _Traits>& __os) { |
|
358 __os.put(__os.widen('\n')); |
|
359 __os.flush(); |
|
360 return __os; |
|
361 } |
|
362 |
|
363 template <class _CharT, class _Traits> |
|
364 inline basic_ostream<_CharT, _Traits>& _STLP_CALL |
|
365 ends(basic_ostream<_CharT, _Traits>& __os) { |
|
366 __os.put(_STLP_DEFAULT_CONSTRUCTED(_CharT)); |
|
367 return __os; |
|
368 } |
|
369 |
|
370 template <class _CharT, class _Traits> |
|
371 inline basic_ostream<_CharT, _Traits>& _STLP_CALL |
|
372 flush(basic_ostream<_CharT, _Traits>& __os) { |
|
373 __os.flush(); |
|
374 return __os; |
|
375 } |
|
376 |
|
377 _STLP_END_NAMESPACE |
|
378 |
|
379 #if defined (_STLP_EXPOSE_STREAM_IMPLEMENTATION) && !defined (_STLP_LINK_TIME_INSTANTIATION) |
|
380 # include <stl/_ostream.c> |
|
381 #endif |
|
382 |
|
383 #endif /* _STLP_INTERNAL_OSTREAM_H */ |
|
384 |
|
385 // Local Variables: |
|
386 // mode:C++ |
|
387 // End: |