3 // Copyright (C) 1997-2025 Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
30 #ifndef _STREAMBUF_ITERATOR_H
31 #define _STREAMBUF_ITERATOR_H 1
33 #ifdef _GLIBCXX_SYSHDR
34 #pragma GCC system_header
38 #include <bits/stl_iterator_base_types.h>
39 #include <debug/debug.h>
41 namespace std
_GLIBCXX_VISIBILITY(default)
43 _GLIBCXX_BEGIN_NAMESPACE_VERSION
46 * @addtogroup iterators
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
,
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
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
;
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
>);
85 template<bool _IsMove
, typename _CharT2
>
86 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
88 __copy_move_a2(istreambuf_iterator
<_CharT2
>,
89 istreambuf_iterator
<_CharT2
>, _CharT2
*);
91 template<typename _CharT2
, typename _Size
>
92 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
94 __copy_n_a(istreambuf_iterator
<_CharT2
>, _Size
, _CharT2
*, bool);
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
>,
102 template<typename _CharT2
, typename _Distance
>
103 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
105 advance(istreambuf_iterator
<_CharT2
>&, _Distance
);
108 // 24.5.3 istreambuf_iterator
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
115 mutable streambuf_type
* _M_sbuf
;
119 /// Construct end of input stream iterator.
120 _GLIBCXX_CONSTEXPR
istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
121 : _M_sbuf(0), _M_c(traits_type::eof()) { }
123 #if __cplusplus > 201703L && __cpp_lib_concepts
124 constexpr istreambuf_iterator(default_sentinel_t
) noexcept
125 : istreambuf_iterator() { }
128 #if __cplusplus >= 201103L
129 istreambuf_iterator(const istreambuf_iterator
&) noexcept
= default;
131 ~istreambuf_iterator() = default;
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()) { }
138 /// Construct start of streambuf iterator.
139 istreambuf_iterator(streambuf_type
* __s
) _GLIBCXX_USE_NOEXCEPT
140 : _M_sbuf(__s
), _M_c(traits_type::eof()) { }
142 #if __cplusplus >= 201103L
144 operator=(const istreambuf_iterator
&) noexcept
= default;
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.
154 int_type __c
= _M_get();
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));
163 return traits_type::to_char_type(__c
);
166 /// Advance the iterator. Calls streambuf.sbumpc().
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));
176 _M_c
= traits_type::eof();
180 /// Advance the iterator. Calls streambuf.sbumpc().
184 __glibcxx_requires_cond(_M_sbuf
&&
185 (!_S_is_eof(_M_c
) || !_S_is_eof(_M_sbuf
->sgetc())),
186 _M_message(__gnu_debug::__msg_inc_istreambuf
)
187 ._M_iterator(*this));
189 istreambuf_iterator __old
= *this;
190 __old
._M_c
= _M_sbuf
->sbumpc();
191 _M_c
= traits_type::eof();
195 // _GLIBCXX_RESOLVE_LIB_DEFECTS
196 // 110 istreambuf_iterator::equal not const
197 // NB: there is also number 111 (NAD) relevant to this function.
198 /// Return true both iterators are end or both are not end.
201 equal(const istreambuf_iterator
& __b
) const
202 { return _M_at_eof() == __b
._M_at_eof(); }
208 int_type __ret
= _M_c
;
209 if (_M_sbuf
&& _S_is_eof(__ret
) && _S_is_eof(__ret
= _M_sbuf
->sgetc()))
216 { return _S_is_eof(_M_get()); }
219 _S_is_eof(int_type __c
)
221 const int_type __eof
= traits_type::eof();
222 return traits_type::eq_int_type(__c
, __eof
);
225 #if __cplusplus > 201703L && __cpp_lib_concepts
228 operator==(const istreambuf_iterator
& __i
, default_sentinel_t
)
229 { return __i
._M_at_eof(); }
233 template<typename _CharT
, typename _Traits
>
236 operator==(const istreambuf_iterator
<_CharT
, _Traits
>& __a
,
237 const istreambuf_iterator
<_CharT
, _Traits
>& __b
)
238 { return __a
.equal(__b
); }
240 #if __cpp_impl_three_way_comparison < 201907L
241 template<typename _CharT
, typename _Traits
>
244 operator!=(const istreambuf_iterator
<_CharT
, _Traits
>& __a
,
245 const istreambuf_iterator
<_CharT
, _Traits
>& __b
)
246 { return !__a
.equal(__b
); }
249 /// Provides output iterator semantics for streambufs.
250 template<typename _CharT
, typename _Traits
>
251 class ostreambuf_iterator
252 : public iterator
<output_iterator_tag
, void, void, void, void>
258 #if __cplusplus > 201703L
259 using difference_type
= ptrdiff_t;
261 typedef _CharT char_type
;
262 typedef _Traits traits_type
;
263 typedef basic_streambuf
<_CharT
, _Traits
> streambuf_type
;
264 typedef basic_ostream
<_CharT
, _Traits
> ostream_type
;
267 template<typename _CharT2
>
268 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
269 ostreambuf_iterator
<_CharT2
> >::__type
270 copy(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
271 ostreambuf_iterator
<_CharT2
>);
274 streambuf_type
* _M_sbuf
;
279 #if __cplusplus > 201703L
281 ostreambuf_iterator() noexcept
282 : _M_sbuf(nullptr), _M_failed(true) { }
285 /// Construct output iterator from ostream.
286 ostreambuf_iterator(ostream_type
& __s
) _GLIBCXX_USE_NOEXCEPT
287 : _M_sbuf(__s
.rdbuf()), _M_failed(!_M_sbuf
) { }
289 /// Construct output iterator from streambuf.
290 ostreambuf_iterator(streambuf_type
* __s
) _GLIBCXX_USE_NOEXCEPT
291 : _M_sbuf(__s
), _M_failed(!_M_sbuf
) { }
293 /// Write character to streambuf. Calls streambuf.sputc().
295 operator=(_CharT __c
)
298 _Traits::eq_int_type(_M_sbuf
->sputc(__c
), _Traits::eof()))
319 /// Return true if previous operator=() failed.
322 failed() const _GLIBCXX_USE_NOEXCEPT
323 { return _M_failed
; }
326 _M_put(const _CharT
* __ws
, streamsize __len
)
328 if (__builtin_expect(!_M_failed
, true)
329 && __builtin_expect(this->_M_sbuf
->sputn(__ws
, __len
) != __len
,
335 #pragma GCC diagnostic pop
337 // Overloads for streambuf iterators.
338 template<typename _CharT
>
339 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
340 ostreambuf_iterator
<_CharT
> >::__type
341 copy(istreambuf_iterator
<_CharT
> __first
,
342 istreambuf_iterator
<_CharT
> __last
,
343 ostreambuf_iterator
<_CharT
> __result
)
345 if (__first
._M_sbuf
&& !__last
._M_sbuf
&& !__result
._M_failed
)
348 __copy_streambufs_eof(__first
._M_sbuf
, __result
._M_sbuf
, __ineof
);
350 __result
._M_failed
= true;
355 template<bool _IsMove
, typename _CharT
>
356 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
357 ostreambuf_iterator
<_CharT
> >::__type
358 __copy_move_a2(_CharT
* __first
, _CharT
* __last
,
359 ostreambuf_iterator
<_CharT
> __result
)
361 const streamsize __num
= __last
- __first
;
363 __result
._M_put(__first
, __num
);
367 template<bool _IsMove
, typename _CharT
>
368 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
369 ostreambuf_iterator
<_CharT
> >::__type
370 __copy_move_a2(const _CharT
* __first
, const _CharT
* __last
,
371 ostreambuf_iterator
<_CharT
> __result
)
373 const streamsize __num
= __last
- __first
;
375 __result
._M_put(__first
, __num
);
379 template<bool _IsMove
, typename _CharT
>
380 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
382 __copy_move_a2(istreambuf_iterator
<_CharT
> __first
,
383 istreambuf_iterator
<_CharT
> __last
, _CharT
* __result
)
385 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
386 typedef typename
__is_iterator_type::traits_type traits_type
;
387 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
388 typedef typename
traits_type::int_type int_type
;
390 if (__first
._M_sbuf
&& !__last
._M_sbuf
)
392 streambuf_type
* __sb
= __first
._M_sbuf
;
393 int_type __c
= __sb
->sgetc();
394 while (!traits_type::eq_int_type(__c
, traits_type::eof()))
396 const streamsize __n
= __sb
->egptr() - __sb
->gptr();
399 traits_type::copy(__result
, __sb
->gptr(), __n
);
400 __sb
->__safe_gbump(__n
);
402 __c
= __sb
->underflow();
406 *__result
++ = traits_type::to_char_type(__c
);
407 __c
= __sb
->snextc();
414 template<typename _CharT
, typename _Size
>
415 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
417 __copy_n_a(istreambuf_iterator
<_CharT
> __it
, _Size __n
, _CharT
* __result
,
418 bool __strict
__attribute__((__unused__
)))
423 __glibcxx_requires_cond(__it
._M_sbuf
,
424 _M_message(__gnu_debug::__msg_inc_istreambuf
)
426 _CharT
* __beg
= __result
;
427 __result
+= __it
._M_sbuf
->sgetn(__beg
, __n
);
428 __glibcxx_requires_cond(!__strict
|| __result
- __beg
== __n
,
429 _M_message(__gnu_debug::__msg_inc_istreambuf
)
434 template<typename _CharT
>
435 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
436 istreambuf_iterator
<_CharT
> >::__type
437 find(istreambuf_iterator
<_CharT
> __first
,
438 istreambuf_iterator
<_CharT
> __last
, const _CharT
& __val
)
440 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
441 typedef typename
__is_iterator_type::traits_type traits_type
;
442 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
443 typedef typename
traits_type::int_type int_type
;
444 const int_type __eof
= traits_type::eof();
446 if (__first
._M_sbuf
&& !__last
._M_sbuf
)
448 const int_type __ival
= traits_type::to_int_type(__val
);
449 streambuf_type
* __sb
= __first
._M_sbuf
;
450 int_type __c
= __sb
->sgetc();
451 while (!traits_type::eq_int_type(__c
, __eof
)
452 && !traits_type::eq_int_type(__c
, __ival
))
454 streamsize __n
= __sb
->egptr() - __sb
->gptr();
457 const _CharT
* __p
= traits_type::find(__sb
->gptr(),
460 __n
= __p
- __sb
->gptr();
461 __sb
->__safe_gbump(__n
);
465 __c
= __sb
->snextc();
468 __first
._M_c
= __eof
;
474 template<typename _CharT
, typename _Distance
>
475 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
477 advance(istreambuf_iterator
<_CharT
>& __i
, _Distance __n
)
482 __glibcxx_assert(__n
> 0);
483 __glibcxx_requires_cond(!__i
._M_at_eof(),
484 _M_message(__gnu_debug::__msg_inc_istreambuf
)
487 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
488 typedef typename
__is_iterator_type::traits_type traits_type
;
489 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
490 typedef typename
traits_type::int_type int_type
;
491 const int_type __eof
= traits_type::eof();
493 streambuf_type
* __sb
= __i
._M_sbuf
;
496 streamsize __size
= __sb
->egptr() - __sb
->gptr();
499 __sb
->__safe_gbump(__n
);
503 __sb
->__safe_gbump(__size
);
505 if (traits_type::eq_int_type(__sb
->underflow(), __eof
))
507 __glibcxx_requires_cond(__n
== 0,
508 _M_message(__gnu_debug::__msg_inc_istreambuf
)
517 /// @} group iterators
519 _GLIBCXX_END_NAMESPACE_VERSION