]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/streambuf_iterator.h
Fix typo in preprocessor check
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
CommitLineData
725dc051
BK
1// Streambuf iterators
2
a5544970 3// Copyright (C) 1997-2019 Free Software Foundation, Inc.
725dc051
BK
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
725dc051
BK
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
748086b7
JJ
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/>.
725dc051 24
f910786b 25/** @file bits/streambuf_iterator.h
729e3d3f 26 * This is an internal header file, included by other library headers.
f910786b 27 * Do not attempt to use it directly. @headername{iterator}
729e3d3f
PE
28 */
29
3d7c150e
BK
30#ifndef _STREAMBUF_ITERATOR_H
31#define _STREAMBUF_ITERATOR_H 1
725dc051 32
b0a85b86
GDR
33#pragma GCC system_header
34
2e2a38cd 35#include <streambuf>
285b36d6 36#include <debug/debug.h>
2e2a38cd 37
12ffa228
BK
38namespace std _GLIBCXX_VISIBILITY(default)
39{
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
e324f9cb 41
8e32aa11
BK
42 /**
43 * @addtogroup iterators
44 * @{
45 */
46
725dc051 47 // 24.5.3 Template class istreambuf_iterator
ffcec5c8 48 /// Provides input iterator semantics for streambufs.
39003c99 49 template<typename _CharT, typename _Traits>
725dc051
BK
50 class istreambuf_iterator
51 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
e324f9cb 52 _CharT*,
734f5023 53#if __cplusplus >= 201103L
ebb6e4af
PC
54 // LWG 445.
55 _CharT>
56#else
57 _CharT&>
58#endif
725dc051
BK
59 {
60 public:
725dc051 61 // Types:
ffcec5c8
JQ
62 //@{
63 /// Public typedefs
ed6814f7
BI
64 typedef _CharT char_type;
65 typedef _Traits traits_type;
66 typedef typename _Traits::int_type int_type;
67 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
68 typedef basic_istream<_CharT, _Traits> istream_type;
ffcec5c8 69 //@}
725dc051 70
0002d5d2 71 template<typename _CharT2>
105c6331 72 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
e324f9cb 73 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
74 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
75 ostreambuf_iterator<_CharT2>);
76
f0112db9 77 template<bool _IsMove, typename _CharT2>
e324f9cb 78 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
105c6331 79 _CharT2*>::__type
f0112db9
PC
80 __copy_move_a2(istreambuf_iterator<_CharT2>,
81 istreambuf_iterator<_CharT2>, _CharT2*);
0002d5d2 82
8ab38f6c
FD
83#if __cplusplus >= 201103L
84 template<typename _CharT2, typename _Size>
85 friend __enable_if_t<__is_char<_CharT2>::__value, _CharT2*>
86 __copy_n_a(istreambuf_iterator<_CharT2>, _Size, _CharT2*);
87#endif
88
0002d5d2 89 template<typename _CharT2>
105c6331 90 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
e324f9cb 91 istreambuf_iterator<_CharT2> >::__type
0002d5d2 92 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
976e25f4 93 const _CharT2&);
0002d5d2 94
e324f9cb
FD
95 template<typename _CharT2, typename _Distance>
96 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
97 void>::__type
98 advance(istreambuf_iterator<_CharT2>&, _Distance);
99
39003c99 100 private:
ed6814f7
BI
101 // 24.5.3 istreambuf_iterator
102 // p 1
39003c99
BK
103 // If the end of stream is reached (streambuf_type::sgetc()
104 // returns traits_type::eof()), the iterator becomes equal to
105 // the "end of stream" iterator value.
106 // NB: This implementation assumes the "end of stream" value
107 // is EOF, or -1.
ed6814f7 108 mutable streambuf_type* _M_sbuf;
4e914524 109 int_type _M_c;
39003c99
BK
110
111 public:
ffcec5c8 112 /// Construct end of input stream iterator.
23e4c4ee 113 _GLIBCXX_CONSTEXPR istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
f13a69ec 114 : _M_sbuf(0), _M_c(traits_type::eof()) { }
ed6814f7 115
734f5023 116#if __cplusplus >= 201103L
23e4c4ee
PC
117 istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
118
119 ~istreambuf_iterator() = default;
120#endif
121
ffcec5c8 122 /// Construct start of input stream iterator.
23e4c4ee 123 istreambuf_iterator(istream_type& __s) _GLIBCXX_USE_NOEXCEPT
f13a69ec 124 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
725dc051 125
ffcec5c8 126 /// Construct start of streambuf iterator.
23e4c4ee 127 istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
f13a69ec 128 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
ed6814f7 129
a1417556
JW
130#if __cplusplus >= 201103L
131 istreambuf_iterator&
132 operator=(const istreambuf_iterator&) noexcept = default;
133#endif
134
ffcec5c8
JQ
135 /// Return the current character pointed to by iterator. This returns
136 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
137 /// operator*() on an end of stream is undefined.
ed6814f7 138 char_type
725dc051 139 operator*() const
ed6814f7 140 {
4e914524
PO
141 int_type __c = _M_get();
142
285b36d6
BK
143#ifdef _GLIBCXX_DEBUG_PEDANTIC
144 // Dereferencing a past-the-end istreambuf_iterator is a
145 // libstdc++ extension
4e914524 146 __glibcxx_requires_cond(!_S_is_eof(__c),
285b36d6 147 _M_message(__gnu_debug::__msg_deref_istreambuf)
ed6814f7 148 ._M_iterator(*this));
285b36d6 149#endif
4e914524 150 return traits_type::to_char_type(__c);
285b36d6 151 }
ffcec5c8
JQ
152
153 /// Advance the iterator. Calls streambuf.sbumpc().
ed6814f7 154 istreambuf_iterator&
725dc051 155 operator++()
ed6814f7 156 {
4e914524
PO
157 __glibcxx_requires_cond(_M_sbuf &&
158 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
285b36d6 159 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 160 ._M_iterator(*this));
4e914524
PO
161
162 _M_sbuf->sbumpc();
163 _M_c = traits_type::eof();
ed6814f7 164 return *this;
725dc051
BK
165 }
166
ffcec5c8 167 /// Advance the iterator. Calls streambuf.sbumpc().
b581eaf7 168 istreambuf_iterator
725dc051
BK
169 operator++(int)
170 {
4e914524
PO
171 __glibcxx_requires_cond(_M_sbuf &&
172 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
285b36d6 173 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 174 ._M_iterator(*this));
285b36d6 175
b581eaf7 176 istreambuf_iterator __old = *this;
4e914524
PO
177 __old._M_c = _M_sbuf->sbumpc();
178 _M_c = traits_type::eof();
ed6814f7 179 return __old;
725dc051 180 }
725dc051 181
f5677b15 182 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725dc051 183 // 110 istreambuf_iterator::equal not const
0e7300bb 184 // NB: there is also number 111 (NAD) relevant to this function.
ffcec5c8 185 /// Return true both iterators are end or both are not end.
ed6814f7 186 bool
b581eaf7 187 equal(const istreambuf_iterator& __b) const
6bfcbf0d 188 { return _M_at_eof() == __b._M_at_eof(); }
9875ea05
BK
189
190 private:
ed6814f7 191 int_type
9875ea05 192 _M_get() const
ed6814f7 193 {
4e914524
PO
194 int_type __ret = _M_c;
195 if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc()))
196 _M_sbuf = 0;
9875ea05
BK
197 return __ret;
198 }
285b36d6 199
ed6814f7 200 bool
285b36d6 201 _M_at_eof() const
4e914524
PO
202 { return _S_is_eof(_M_get()); }
203
204 static bool
205 _S_is_eof(int_type __c)
285b36d6
BK
206 {
207 const int_type __eof = traits_type::eof();
4e914524 208 return traits_type::eq_int_type(__c, __eof);
285b36d6 209 }
725dc051
BK
210 };
211
212 template<typename _CharT, typename _Traits>
ed6814f7 213 inline bool
725dc051
BK
214 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
215 const istreambuf_iterator<_CharT, _Traits>& __b)
216 { return __a.equal(__b); }
217
218 template<typename _CharT, typename _Traits>
ed6814f7 219 inline bool
725dc051
BK
220 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
221 const istreambuf_iterator<_CharT, _Traits>& __b)
222 { return !__a.equal(__b); }
5c352a47 223
ffcec5c8 224 /// Provides output iterator semantics for streambufs.
5c352a47
BK
225 template<typename _CharT, typename _Traits>
226 class ostreambuf_iterator
227 : public iterator<output_iterator_tag, void, void, void, void>
228 {
229 public:
230 // Types:
ffcec5c8
JQ
231 //@{
232 /// Public typedefs
e324f9cb
FD
233 typedef _CharT char_type;
234 typedef _Traits traits_type;
5c352a47
BK
235 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
236 typedef basic_ostream<_CharT, _Traits> ostream_type;
ffcec5c8 237 //@}
5c352a47 238
0002d5d2 239 template<typename _CharT2>
105c6331 240 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
e324f9cb 241 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
242 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
243 ostreambuf_iterator<_CharT2>);
244
5c352a47 245 private:
ed6814f7
BI
246 streambuf_type* _M_sbuf;
247 bool _M_failed;
5c352a47
BK
248
249 public:
ffcec5c8 250 /// Construct output iterator from ostream.
23e4c4ee 251 ostreambuf_iterator(ostream_type& __s) _GLIBCXX_USE_NOEXCEPT
5c352a47 252 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
ed6814f7 253
ffcec5c8 254 /// Construct output iterator from streambuf.
23e4c4ee 255 ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
5c352a47
BK
256 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
257
ffcec5c8 258 /// Write character to streambuf. Calls streambuf.sputc().
ed6814f7 259 ostreambuf_iterator&
2e2a38cd
BK
260 operator=(_CharT __c)
261 {
ed6814f7 262 if (!_M_failed &&
2e2a38cd
BK
263 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
264 _M_failed = true;
265 return *this;
266 }
5c352a47 267
ffcec5c8 268 /// Return *this.
ed6814f7 269 ostreambuf_iterator&
e0ec69c9 270 operator*()
5c352a47
BK
271 { return *this; }
272
ffcec5c8 273 /// Return *this.
ed6814f7 274 ostreambuf_iterator&
e0ec69c9 275 operator++(int)
5c352a47
BK
276 { return *this; }
277
ffcec5c8 278 /// Return *this.
ed6814f7 279 ostreambuf_iterator&
e0ec69c9 280 operator++()
5c352a47
BK
281 { return *this; }
282
ffcec5c8 283 /// Return true if previous operator=() failed.
ed6814f7 284 bool
23e4c4ee 285 failed() const _GLIBCXX_USE_NOEXCEPT
5c352a47 286 { return _M_failed; }
5c352a47 287
ed6814f7 288 ostreambuf_iterator&
2e2a38cd
BK
289 _M_put(const _CharT* __ws, streamsize __len)
290 {
53279c10 291 if (__builtin_expect(!_M_failed, true)
15d72060
PC
292 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
293 false))
53279c10 294 _M_failed = true;
2e2a38cd
BK
295 return *this;
296 }
297 };
3cbc7af0 298
0002d5d2
PC
299 // Overloads for streambuf iterators.
300 template<typename _CharT>
105c6331 301 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
e324f9cb 302 ostreambuf_iterator<_CharT> >::__type
0002d5d2
PC
303 copy(istreambuf_iterator<_CharT> __first,
304 istreambuf_iterator<_CharT> __last,
305 ostreambuf_iterator<_CharT> __result)
306 {
307 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
308 {
309 bool __ineof;
310 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
311 if (!__ineof)
312 __result._M_failed = true;
313 }
314 return __result;
315 }
316
f0112db9 317 template<bool _IsMove, typename _CharT>
e324f9cb
FD
318 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
319 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
320 __copy_move_a2(_CharT* __first, _CharT* __last,
321 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
322 {
323 const streamsize __num = __last - __first;
324 if (__num > 0)
325 __result._M_put(__first, __num);
326 return __result;
327 }
328
f0112db9 329 template<bool _IsMove, typename _CharT>
105c6331
BK
330 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
331 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
332 __copy_move_a2(const _CharT* __first, const _CharT* __last,
333 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
334 {
335 const streamsize __num = __last - __first;
336 if (__num > 0)
337 __result._M_put(__first, __num);
338 return __result;
339 }
340
f0112db9 341 template<bool _IsMove, typename _CharT>
e324f9cb
FD
342 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
343 _CharT*>::__type
f0112db9
PC
344 __copy_move_a2(istreambuf_iterator<_CharT> __first,
345 istreambuf_iterator<_CharT> __last, _CharT* __result)
0002d5d2 346 {
e324f9cb
FD
347 typedef istreambuf_iterator<_CharT> __is_iterator_type;
348 typedef typename __is_iterator_type::traits_type traits_type;
0002d5d2 349 typedef typename __is_iterator_type::streambuf_type streambuf_type;
e324f9cb 350 typedef typename traits_type::int_type int_type;
0002d5d2
PC
351
352 if (__first._M_sbuf && !__last._M_sbuf)
353 {
354 streambuf_type* __sb = __first._M_sbuf;
355 int_type __c = __sb->sgetc();
356 while (!traits_type::eq_int_type(__c, traits_type::eof()))
357 {
358 const streamsize __n = __sb->egptr() - __sb->gptr();
359 if (__n > 1)
360 {
361 traits_type::copy(__result, __sb->gptr(), __n);
1139a735 362 __sb->__safe_gbump(__n);
0002d5d2
PC
363 __result += __n;
364 __c = __sb->underflow();
365 }
366 else
367 {
368 *__result++ = traits_type::to_char_type(__c);
369 __c = __sb->snextc();
370 }
371 }
372 }
373 return __result;
374 }
375
8ab38f6c
FD
376#if __cplusplus >= 201103L
377 template<typename _CharT, typename _Size>
378 __enable_if_t<__is_char<_CharT>::__value, _CharT*>
379 __copy_n_a(istreambuf_iterator<_CharT> __it, _Size __n, _CharT* __result)
380 {
381 if (__n == 0)
382 return __result;
383
384 __glibcxx_requires_cond(__it._M_sbuf,
385 _M_message(__gnu_debug::__msg_inc_istreambuf)
386 ._M_iterator(__it));
387 _CharT* __beg = __result;
388 __result += __it._M_sbuf->sgetn(__beg, __n);
389 __glibcxx_requires_cond(__result - __beg == __n,
390 _M_message(__gnu_debug::__msg_inc_istreambuf)
391 ._M_iterator(__it));
392 return __result;
393 }
394#endif // C++11
395
0002d5d2 396 template<typename _CharT>
105c6331
BK
397 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
398 istreambuf_iterator<_CharT> >::__type
0002d5d2 399 find(istreambuf_iterator<_CharT> __first,
976e25f4 400 istreambuf_iterator<_CharT> __last, const _CharT& __val)
0002d5d2 401 {
e324f9cb 402 typedef istreambuf_iterator<_CharT> __is_iterator_type;
0002d5d2
PC
403 typedef typename __is_iterator_type::traits_type traits_type;
404 typedef typename __is_iterator_type::streambuf_type streambuf_type;
e324f9cb 405 typedef typename traits_type::int_type int_type;
4e914524 406 const int_type __eof = traits_type::eof();
0002d5d2
PC
407
408 if (__first._M_sbuf && !__last._M_sbuf)
409 {
410 const int_type __ival = traits_type::to_int_type(__val);
411 streambuf_type* __sb = __first._M_sbuf;
412 int_type __c = __sb->sgetc();
4e914524 413 while (!traits_type::eq_int_type(__c, __eof)
0002d5d2
PC
414 && !traits_type::eq_int_type(__c, __ival))
415 {
416 streamsize __n = __sb->egptr() - __sb->gptr();
417 if (__n > 1)
418 {
419 const _CharT* __p = traits_type::find(__sb->gptr(),
420 __n, __val);
421 if (__p)
422 __n = __p - __sb->gptr();
1139a735 423 __sb->__safe_gbump(__n);
0002d5d2
PC
424 __c = __sb->sgetc();
425 }
426 else
427 __c = __sb->snextc();
428 }
429
4e914524 430 __first._M_c = __eof;
0002d5d2 431 }
4e914524 432
0002d5d2
PC
433 return __first;
434 }
435
e324f9cb
FD
436 template<typename _CharT, typename _Distance>
437 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
438 void>::__type
439 advance(istreambuf_iterator<_CharT>& __i, _Distance __n)
440 {
441 if (__n == 0)
442 return;
443
444 __glibcxx_assert(__n > 0);
445 __glibcxx_requires_cond(!__i._M_at_eof(),
446 _M_message(__gnu_debug::__msg_inc_istreambuf)
447 ._M_iterator(__i));
448
449 typedef istreambuf_iterator<_CharT> __is_iterator_type;
450 typedef typename __is_iterator_type::traits_type traits_type;
451 typedef typename __is_iterator_type::streambuf_type streambuf_type;
452 typedef typename traits_type::int_type int_type;
453 const int_type __eof = traits_type::eof();
454
455 streambuf_type* __sb = __i._M_sbuf;
456 while (__n > 0)
457 {
458 streamsize __size = __sb->egptr() - __sb->gptr();
459 if (__size > __n)
460 {
461 __sb->__safe_gbump(__n);
462 break;
463 }
464
465 __sb->__safe_gbump(__size);
466 __n -= __size;
467 if (traits_type::eq_int_type(__sb->underflow(), __eof))
468 {
469 __glibcxx_requires_cond(__n == 0,
470 _M_message(__gnu_debug::__msg_inc_istreambuf)
471 ._M_iterator(__i));
472 break;
473 }
474 }
475
476 __i._M_c = __eof;
477 }
478
8e32aa11
BK
479// @} group iterators
480
12ffa228
BK
481_GLIBCXX_END_NAMESPACE_VERSION
482} // namespace
3cbc7af0 483
b85381b9 484#endif