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