1 | // ostream classes -*- C++ -*- |
2 | |
3 | // Copyright (C) 1997-2022 Free Software Foundation, Inc. |
4 | // |
5 | // This file is part of the GNU ISO C++ Library. This library is free |
6 | // software; you can redistribute it and/or modify it under the |
7 | // terms of the GNU General Public License as published by the |
8 | // Free Software Foundation; either version 3, or (at your option) |
9 | // any later version. |
10 | |
11 | // This library is distributed in the hope that it will be useful, |
12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | // GNU General Public License for more details. |
15 | |
16 | // Under Section 7 of GPL version 3, you are granted additional |
17 | // permissions described in the GCC Runtime Library Exception, version |
18 | // 3.1, as published by the Free Software Foundation. |
19 | |
20 | // You should have received a copy of the GNU General Public License and |
21 | // a copy of the GCC Runtime Library Exception along with this program; |
22 | // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see |
23 | // <http://www.gnu.org/licenses/>. |
24 | |
25 | /** @file bits/ostream.tcc |
26 | * This is an internal header file, included by other library headers. |
27 | * Do not attempt to use it directly. @headername{ostream} |
28 | */ |
29 | |
30 | // |
31 | // ISO C++ 14882: 27.6.2 Output streams |
32 | // |
33 | |
34 | #ifndef _OSTREAM_TCC |
35 | #define _OSTREAM_TCC 1 |
36 | |
37 | #pragma GCC system_header |
38 | |
39 | #include <bits/cxxabi_forced.h> |
40 | |
41 | namespace std _GLIBCXX_VISIBILITY(default) |
42 | { |
43 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
44 | |
45 | template<typename _CharT, typename _Traits> |
46 | basic_ostream<_CharT, _Traits>::sentry:: |
47 | sentry(basic_ostream<_CharT, _Traits>& __os) |
48 | : _M_ok(false), _M_os(__os) |
49 | { |
50 | // XXX MT |
51 | if (__os.tie() && __os.good()) |
52 | __os.tie()->flush(); |
53 | |
54 | if (__os.good()) |
55 | _M_ok = true; |
56 | else if (__os.bad()) |
57 | __os.setstate(ios_base::failbit); |
58 | } |
59 | |
60 | template<typename _CharT, typename _Traits> |
61 | template<typename _ValueT> |
62 | basic_ostream<_CharT, _Traits>& |
63 | basic_ostream<_CharT, _Traits>:: |
64 | _M_insert(_ValueT __v) |
65 | { |
66 | sentry __cerb(*this); |
67 | if (__cerb) |
68 | { |
69 | ios_base::iostate __err = ios_base::goodbit; |
70 | __try |
71 | { |
72 | const __num_put_type& __np = __check_facet(this->_M_num_put); |
73 | if (__np.put(*this, *this, this->fill(), __v).failed()) |
74 | __err |= ios_base::badbit; |
75 | } |
76 | __catch(__cxxabiv1::__forced_unwind&) |
77 | { |
78 | this->_M_setstate(ios_base::badbit); |
79 | __throw_exception_again; |
80 | } |
81 | __catch(...) |
82 | { this->_M_setstate(ios_base::badbit); } |
83 | if (__err) |
84 | this->setstate(__err); |
85 | } |
86 | return *this; |
87 | } |
88 | |
89 | template<typename _CharT, typename _Traits> |
90 | basic_ostream<_CharT, _Traits>& |
91 | basic_ostream<_CharT, _Traits>:: |
92 | operator<<(short __n) |
93 | { |
94 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
95 | // 117. basic_ostream uses nonexistent num_put member functions. |
96 | const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield; |
97 | if (__fmt == ios_base::oct || __fmt == ios_base::hex) |
98 | return _M_insert(static_cast<long>(static_cast<unsigned short>(__n))); |
99 | else |
100 | return _M_insert(static_cast<long>(__n)); |
101 | } |
102 | |
103 | template<typename _CharT, typename _Traits> |
104 | basic_ostream<_CharT, _Traits>& |
105 | basic_ostream<_CharT, _Traits>:: |
106 | operator<<(int __n) |
107 | { |
108 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
109 | // 117. basic_ostream uses nonexistent num_put member functions. |
110 | const ios_base::fmtflags __fmt = this->flags() & ios_base::basefield; |
111 | if (__fmt == ios_base::oct || __fmt == ios_base::hex) |
112 | return _M_insert(static_cast<long>(static_cast<unsigned int>(__n))); |
113 | else |
114 | return _M_insert(static_cast<long>(__n)); |
115 | } |
116 | |
117 | template<typename _CharT, typename _Traits> |
118 | basic_ostream<_CharT, _Traits>& |
119 | basic_ostream<_CharT, _Traits>:: |
120 | operator<<(__streambuf_type* __sbin) |
121 | { |
122 | ios_base::iostate __err = ios_base::goodbit; |
123 | sentry __cerb(*this); |
124 | if (__cerb && __sbin) |
125 | { |
126 | __try |
127 | { |
128 | if (!__copy_streambufs(__sbin, this->rdbuf())) |
129 | __err |= ios_base::failbit; |
130 | } |
131 | __catch(__cxxabiv1::__forced_unwind&) |
132 | { |
133 | this->_M_setstate(ios_base::badbit); |
134 | __throw_exception_again; |
135 | } |
136 | __catch(...) |
137 | { this->_M_setstate(ios_base::failbit); } |
138 | } |
139 | else if (!__sbin) |
140 | __err |= ios_base::badbit; |
141 | if (__err) |
142 | this->setstate(__err); |
143 | return *this; |
144 | } |
145 | |
146 | template<typename _CharT, typename _Traits> |
147 | basic_ostream<_CharT, _Traits>& |
148 | basic_ostream<_CharT, _Traits>:: |
149 | put(char_type __c) |
150 | { |
151 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
152 | // DR 60. What is a formatted input function? |
153 | // basic_ostream::put(char_type) is an unformatted output function. |
154 | // DR 63. Exception-handling policy for unformatted output. |
155 | // Unformatted output functions should catch exceptions thrown |
156 | // from streambuf members. |
157 | sentry __cerb(*this); |
158 | if (__cerb) |
159 | { |
160 | ios_base::iostate __err = ios_base::goodbit; |
161 | __try |
162 | { |
163 | const int_type __put = this->rdbuf()->sputc(__c); |
164 | if (traits_type::eq_int_type(__put, traits_type::eof())) |
165 | __err |= ios_base::badbit; |
166 | } |
167 | __catch(__cxxabiv1::__forced_unwind&) |
168 | { |
169 | this->_M_setstate(ios_base::badbit); |
170 | __throw_exception_again; |
171 | } |
172 | __catch(...) |
173 | { this->_M_setstate(ios_base::badbit); } |
174 | if (__err) |
175 | this->setstate(__err); |
176 | } |
177 | return *this; |
178 | } |
179 | |
180 | template<typename _CharT, typename _Traits> |
181 | basic_ostream<_CharT, _Traits>& |
182 | basic_ostream<_CharT, _Traits>:: |
183 | write(const _CharT* __s, streamsize __n) |
184 | { |
185 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
186 | // DR 60. What is a formatted input function? |
187 | // basic_ostream::write(const char_type*, streamsize) is an |
188 | // unformatted output function. |
189 | // DR 63. Exception-handling policy for unformatted output. |
190 | // Unformatted output functions should catch exceptions thrown |
191 | // from streambuf members. |
192 | sentry __cerb(*this); |
193 | if (__cerb) |
194 | { |
195 | ios_base::iostate __err = ios_base::goodbit; |
196 | __try |
197 | { |
198 | if (this->rdbuf()->sputn(__s, __n) != __n) |
199 | __err = ios_base::badbit; |
200 | } |
201 | __catch(__cxxabiv1::__forced_unwind&) |
202 | { |
203 | this->_M_setstate(ios_base::badbit); |
204 | __throw_exception_again; |
205 | } |
206 | __catch(...) |
207 | { this->_M_setstate(ios_base::badbit); } |
208 | if (__err) |
209 | this->setstate(ios_base::badbit); |
210 | } |
211 | return *this; |
212 | } |
213 | |
214 | template<typename _CharT, typename _Traits> |
215 | basic_ostream<_CharT, _Traits>& |
216 | basic_ostream<_CharT, _Traits>:: |
217 | flush() |
218 | { |
219 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
220 | // DR 60. What is a formatted input function? |
221 | // basic_ostream::flush() is *not* an unformatted output function. |
222 | // 581. flush() not unformatted function |
223 | // basic_ostream::flush() *is* an unformatted output function. |
224 | if (__streambuf_type* __buf = this->rdbuf()) |
225 | { |
226 | sentry __cerb(*this); |
227 | if (__cerb) |
228 | { |
229 | ios_base::iostate __err = ios_base::goodbit; |
230 | __try |
231 | { |
232 | if (this->rdbuf()->pubsync() == -1) |
233 | __err |= ios_base::badbit; |
234 | } |
235 | __catch(__cxxabiv1::__forced_unwind&) |
236 | { |
237 | this->_M_setstate(ios_base::badbit); |
238 | __throw_exception_again; |
239 | } |
240 | __catch(...) |
241 | { this->_M_setstate(ios_base::badbit); } |
242 | if (__err) |
243 | this->setstate(__err); |
244 | } |
245 | } |
246 | return *this; |
247 | } |
248 | |
249 | template<typename _CharT, typename _Traits> |
250 | typename basic_ostream<_CharT, _Traits>::pos_type |
251 | basic_ostream<_CharT, _Traits>:: |
252 | tellp() |
253 | { |
254 | sentry __cerb(*this); |
255 | pos_type __ret = pos_type(-1); |
256 | if (!this->fail()) |
257 | __ret = this->rdbuf()->pubseekoff(0, ios_base::cur, ios_base::out); |
258 | return __ret; |
259 | } |
260 | |
261 | template<typename _CharT, typename _Traits> |
262 | basic_ostream<_CharT, _Traits>& |
263 | basic_ostream<_CharT, _Traits>:: |
264 | seekp(pos_type __pos) |
265 | { |
266 | sentry __cerb(*this); |
267 | if (!this->fail()) |
268 | { |
269 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
270 | // 136. seekp, seekg setting wrong streams? |
271 | const pos_type __p = this->rdbuf()->pubseekpos(__pos, ios_base::out); |
272 | |
273 | // 129. Need error indication from seekp() and seekg() |
274 | if (__p == pos_type(off_type(-1))) |
275 | this->setstate(ios_base::failbit); |
276 | } |
277 | return *this; |
278 | } |
279 | |
280 | template<typename _CharT, typename _Traits> |
281 | basic_ostream<_CharT, _Traits>& |
282 | basic_ostream<_CharT, _Traits>:: |
283 | seekp(off_type __off, ios_base::seekdir __dir) |
284 | { |
285 | sentry __cerb(*this); |
286 | if (!this->fail()) |
287 | { |
288 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
289 | // 136. seekp, seekg setting wrong streams? |
290 | const pos_type __p = this->rdbuf()->pubseekoff(__off, __dir, |
291 | ios_base::out); |
292 | |
293 | // 129. Need error indication from seekp() and seekg() |
294 | if (__p == pos_type(off_type(-1))) |
295 | this->setstate(ios_base::failbit); |
296 | } |
297 | return *this; |
298 | } |
299 | |
300 | template<typename _CharT, typename _Traits> |
301 | basic_ostream<_CharT, _Traits>& |
302 | operator<<(basic_ostream<_CharT, _Traits>& __out, const char* __s) |
303 | { |
304 | if (!__s) |
305 | __out.setstate(ios_base::badbit); |
306 | else |
307 | { |
308 | // _GLIBCXX_RESOLVE_LIB_DEFECTS |
309 | // 167. Improper use of traits_type::length() |
310 | const size_t __clen = char_traits<char>::length(__s); |
311 | __try |
312 | { |
313 | struct __ptr_guard |
314 | { |
315 | _CharT *__p; |
316 | __ptr_guard (_CharT *__ip): __p(__ip) { } |
317 | ~__ptr_guard() { delete[] __p; } |
318 | _CharT* __get() { return __p; } |
319 | } __pg (new _CharT[__clen]); |
320 | |
321 | _CharT *__ws = __pg.__get(); |
322 | for (size_t __i = 0; __i < __clen; ++__i) |
323 | __ws[__i] = __out.widen(__s[__i]); |
324 | __ostream_insert(__out, __ws, __clen); |
325 | } |
326 | __catch(__cxxabiv1::__forced_unwind&) |
327 | { |
328 | __out._M_setstate(ios_base::badbit); |
329 | __throw_exception_again; |
330 | } |
331 | __catch(...) |
332 | { __out._M_setstate(ios_base::badbit); } |
333 | } |
334 | return __out; |
335 | } |
336 | |
337 | // Inhibit implicit instantiations for required instantiations, |
338 | // which are defined via explicit instantiations elsewhere. |
339 | #if _GLIBCXX_EXTERN_TEMPLATE |
340 | extern template class basic_ostream<char>; |
341 | extern template ostream& endl(ostream&); |
342 | extern template ostream& ends(ostream&); |
343 | extern template ostream& flush(ostream&); |
344 | extern template ostream& operator<<(ostream&, char); |
345 | extern template ostream& operator<<(ostream&, unsigned char); |
346 | extern template ostream& operator<<(ostream&, signed char); |
347 | extern template ostream& operator<<(ostream&, const char*); |
348 | extern template ostream& operator<<(ostream&, const unsigned char*); |
349 | extern template ostream& operator<<(ostream&, const signed char*); |
350 | |
351 | extern template ostream& ostream::_M_insert(long); |
352 | extern template ostream& ostream::_M_insert(unsigned long); |
353 | extern template ostream& ostream::_M_insert(bool); |
354 | #ifdef _GLIBCXX_USE_LONG_LONG |
355 | extern template ostream& ostream::_M_insert(long long); |
356 | extern template ostream& ostream::_M_insert(unsigned long long); |
357 | #endif |
358 | extern template ostream& ostream::_M_insert(double); |
359 | extern template ostream& ostream::_M_insert(long double); |
360 | extern template ostream& ostream::_M_insert(const void*); |
361 | |
362 | #ifdef _GLIBCXX_USE_WCHAR_T |
363 | extern template class basic_ostream<wchar_t>; |
364 | extern template wostream& endl(wostream&); |
365 | extern template wostream& ends(wostream&); |
366 | extern template wostream& flush(wostream&); |
367 | extern template wostream& operator<<(wostream&, wchar_t); |
368 | extern template wostream& operator<<(wostream&, char); |
369 | extern template wostream& operator<<(wostream&, const wchar_t*); |
370 | extern template wostream& operator<<(wostream&, const char*); |
371 | |
372 | extern template wostream& wostream::_M_insert(long); |
373 | extern template wostream& wostream::_M_insert(unsigned long); |
374 | extern template wostream& wostream::_M_insert(bool); |
375 | #ifdef _GLIBCXX_USE_LONG_LONG |
376 | extern template wostream& wostream::_M_insert(long long); |
377 | extern template wostream& wostream::_M_insert(unsigned long long); |
378 | #endif |
379 | extern template wostream& wostream::_M_insert(double); |
380 | extern template wostream& wostream::_M_insert(long double); |
381 | extern template wostream& wostream::_M_insert(const void*); |
382 | #endif |
383 | #endif |
384 | |
385 | _GLIBCXX_END_NAMESPACE_VERSION |
386 | } // namespace std |
387 | |
388 | #endif |
389 | |