libstdc++: streambuf_iterator.h Source File

libstdc++

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