3 // Copyright (C) 1997-2022 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 #pragma GCC system_header
36 #include <debug/debug.h>
38 namespace std
_GLIBCXX_VISIBILITY(default)
40 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43 * @addtogroup iterators
47 // Ignore warnings about std::iterator.
48 #pragma GCC diagnostic push
49 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
50 // 24.5.3 Template class istreambuf_iterator
51 /// Provides input iterator semantics for streambufs.
52 template<typename _CharT
, typename _Traits
>
53 class istreambuf_iterator
54 : public iterator
<input_iterator_tag
, _CharT
, typename
_Traits::off_type
,
61 #if __cplusplus < 201103L
62 typedef _CharT
& reference
; // Changed to _CharT by LWG 445
63 #elif __cplusplus > 201703L
64 // _GLIBCXX_RESOLVE_LIB_DEFECTS
65 // 3188. istreambuf_iterator::pointer should not be unspecified
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
;
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
>);
82 template<bool _IsMove
, typename _CharT2
>
83 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
85 __copy_move_a2(istreambuf_iterator
<_CharT2
>,
86 istreambuf_iterator
<_CharT2
>, _CharT2
*);
88 template<typename _CharT2
, typename _Size
>
89 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
91 __copy_n_a(istreambuf_iterator
<_CharT2
>, _Size
, _CharT2
*, bool);
93 template<typename _CharT2
>
94 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
95 istreambuf_iterator
<_CharT2
> >::__type
96 find(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
99 template<typename _CharT2
, typename _Distance
>
100 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
102 advance(istreambuf_iterator
<_CharT2
>&, _Distance
);
105 // 24.5.3 istreambuf_iterator
107 // If the end of stream is reached (streambuf_type::sgetc()
108 // returns traits_type::eof()), the iterator becomes equal to
109 // the "end of stream" iterator value.
110 // NB: This implementation assumes the "end of stream" value
112 mutable streambuf_type
* _M_sbuf
;
116 /// Construct end of input stream iterator.
117 _GLIBCXX_CONSTEXPR
istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
118 : _M_sbuf(0), _M_c(traits_type::eof()) { }
120 #if __cplusplus > 201703L && __cpp_lib_concepts
121 constexpr istreambuf_iterator(default_sentinel_t
) noexcept
122 : istreambuf_iterator() { }
125 #if __cplusplus >= 201103L
126 istreambuf_iterator(const istreambuf_iterator
&) noexcept
= default;
128 ~istreambuf_iterator() = default;
131 /// Construct start of input stream iterator.
132 istreambuf_iterator(istream_type
& __s
) _GLIBCXX_USE_NOEXCEPT
133 : _M_sbuf(__s
.rdbuf()), _M_c(traits_type::eof()) { }
135 /// Construct start of streambuf iterator.
136 istreambuf_iterator(streambuf_type
* __s
) _GLIBCXX_USE_NOEXCEPT
137 : _M_sbuf(__s
), _M_c(traits_type::eof()) { }
139 #if __cplusplus >= 201103L
141 operator=(const istreambuf_iterator
&) noexcept
= default;
144 /// Return the current character pointed to by iterator. This returns
145 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
146 /// operator*() on an end of stream is undefined.
151 int_type __c
= _M_get();
153 #ifdef _GLIBCXX_DEBUG_PEDANTIC
154 // Dereferencing a past-the-end istreambuf_iterator is a
155 // libstdc++ extension
156 __glibcxx_requires_cond(!_S_is_eof(__c
),
157 _M_message(__gnu_debug::__msg_deref_istreambuf
)
158 ._M_iterator(*this));
160 return traits_type::to_char_type(__c
);
163 /// Advance the iterator. Calls streambuf.sbumpc().
167 __glibcxx_requires_cond(_M_sbuf
&&
168 (!_S_is_eof(_M_c
) || !_S_is_eof(_M_sbuf
->sgetc())),
169 _M_message(__gnu_debug::__msg_inc_istreambuf
)
170 ._M_iterator(*this));
173 _M_c
= traits_type::eof();
177 /// Advance the iterator. Calls streambuf.sbumpc().
181 __glibcxx_requires_cond(_M_sbuf
&&
182 (!_S_is_eof(_M_c
) || !_S_is_eof(_M_sbuf
->sgetc())),
183 _M_message(__gnu_debug::__msg_inc_istreambuf
)
184 ._M_iterator(*this));
186 istreambuf_iterator __old
= *this;
187 __old
._M_c
= _M_sbuf
->sbumpc();
188 _M_c
= traits_type::eof();
192 // _GLIBCXX_RESOLVE_LIB_DEFECTS
193 // 110 istreambuf_iterator::equal not const
194 // NB: there is also number 111 (NAD) relevant to this function.
195 /// Return true both iterators are end or both are not end.
198 equal(const istreambuf_iterator
& __b
) const
199 { return _M_at_eof() == __b
._M_at_eof(); }
205 int_type __ret
= _M_c
;
206 if (_M_sbuf
&& _S_is_eof(__ret
) && _S_is_eof(__ret
= _M_sbuf
->sgetc()))
213 { return _S_is_eof(_M_get()); }
216 _S_is_eof(int_type __c
)
218 const int_type __eof
= traits_type::eof();
219 return traits_type::eq_int_type(__c
, __eof
);
222 #if __cplusplus > 201703L && __cpp_lib_concepts
225 operator==(const istreambuf_iterator
& __i
, default_sentinel_t __s
)
226 { return __i
._M_at_eof(); }
230 template<typename _CharT
, typename _Traits
>
233 operator==(const istreambuf_iterator
<_CharT
, _Traits
>& __a
,
234 const istreambuf_iterator
<_CharT
, _Traits
>& __b
)
235 { return __a
.equal(__b
); }
237 #if __cpp_impl_three_way_comparison < 201907L
238 template<typename _CharT
, typename _Traits
>
241 operator!=(const istreambuf_iterator
<_CharT
, _Traits
>& __a
,
242 const istreambuf_iterator
<_CharT
, _Traits
>& __b
)
243 { return !__a
.equal(__b
); }
246 /// Provides output iterator semantics for streambufs.
247 template<typename _CharT
, typename _Traits
>
248 class ostreambuf_iterator
249 : public iterator
<output_iterator_tag
, void, void, void, void>
255 #if __cplusplus > 201703L
256 using difference_type
= ptrdiff_t;
258 typedef _CharT char_type
;
259 typedef _Traits traits_type
;
260 typedef basic_streambuf
<_CharT
, _Traits
> streambuf_type
;
261 typedef basic_ostream
<_CharT
, _Traits
> ostream_type
;
264 template<typename _CharT2
>
265 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
266 ostreambuf_iterator
<_CharT2
> >::__type
267 copy(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
268 ostreambuf_iterator
<_CharT2
>);
271 streambuf_type
* _M_sbuf
;
276 #if __cplusplus > 201703L
278 ostreambuf_iterator() noexcept
279 : _M_sbuf(nullptr), _M_failed(true) { }
282 /// Construct output iterator from ostream.
283 ostreambuf_iterator(ostream_type
& __s
) _GLIBCXX_USE_NOEXCEPT
284 : _M_sbuf(__s
.rdbuf()), _M_failed(!_M_sbuf
) { }
286 /// Construct output iterator from streambuf.
287 ostreambuf_iterator(streambuf_type
* __s
) _GLIBCXX_USE_NOEXCEPT
288 : _M_sbuf(__s
), _M_failed(!_M_sbuf
) { }
290 /// Write character to streambuf. Calls streambuf.sputc().
292 operator=(_CharT __c
)
295 _Traits::eq_int_type(_M_sbuf
->sputc(__c
), _Traits::eof()))
316 /// Return true if previous operator=() failed.
319 failed() const _GLIBCXX_USE_NOEXCEPT
320 { return _M_failed
; }
323 _M_put(const _CharT
* __ws
, streamsize __len
)
325 if (__builtin_expect(!_M_failed
, true)
326 && __builtin_expect(this->_M_sbuf
->sputn(__ws
, __len
) != __len
,
332 #pragma GCC diagnostic pop
334 // Overloads for streambuf iterators.
335 template<typename _CharT
>
336 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
337 ostreambuf_iterator
<_CharT
> >::__type
338 copy(istreambuf_iterator
<_CharT
> __first
,
339 istreambuf_iterator
<_CharT
> __last
,
340 ostreambuf_iterator
<_CharT
> __result
)
342 if (__first
._M_sbuf
&& !__last
._M_sbuf
&& !__result
._M_failed
)
345 __copy_streambufs_eof(__first
._M_sbuf
, __result
._M_sbuf
, __ineof
);
347 __result
._M_failed
= true;
352 template<bool _IsMove
, typename _CharT
>
353 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
354 ostreambuf_iterator
<_CharT
> >::__type
355 __copy_move_a2(_CharT
* __first
, _CharT
* __last
,
356 ostreambuf_iterator
<_CharT
> __result
)
358 const streamsize __num
= __last
- __first
;
360 __result
._M_put(__first
, __num
);
364 template<bool _IsMove
, typename _CharT
>
365 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
366 ostreambuf_iterator
<_CharT
> >::__type
367 __copy_move_a2(const _CharT
* __first
, const _CharT
* __last
,
368 ostreambuf_iterator
<_CharT
> __result
)
370 const streamsize __num
= __last
- __first
;
372 __result
._M_put(__first
, __num
);
376 template<bool _IsMove
, typename _CharT
>
377 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
379 __copy_move_a2(istreambuf_iterator
<_CharT
> __first
,
380 istreambuf_iterator
<_CharT
> __last
, _CharT
* __result
)
382 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
383 typedef typename
__is_iterator_type::traits_type traits_type
;
384 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
385 typedef typename
traits_type::int_type int_type
;
387 if (__first
._M_sbuf
&& !__last
._M_sbuf
)
389 streambuf_type
* __sb
= __first
._M_sbuf
;
390 int_type __c
= __sb
->sgetc();
391 while (!traits_type::eq_int_type(__c
, traits_type::eof()))
393 const streamsize __n
= __sb
->egptr() - __sb
->gptr();
396 traits_type::copy(__result
, __sb
->gptr(), __n
);
397 __sb
->__safe_gbump(__n
);
399 __c
= __sb
->underflow();
403 *__result
++ = traits_type::to_char_type(__c
);
404 __c
= __sb
->snextc();
411 template<typename _CharT
, typename _Size
>
412 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
414 __copy_n_a(istreambuf_iterator
<_CharT
> __it
, _Size __n
, _CharT
* __result
,
415 bool __strict
__attribute__((__unused__
)))
420 __glibcxx_requires_cond(__it
._M_sbuf
,
421 _M_message(__gnu_debug::__msg_inc_istreambuf
)
423 _CharT
* __beg
= __result
;
424 __result
+= __it
._M_sbuf
->sgetn(__beg
, __n
);
425 __glibcxx_requires_cond(!__strict
|| __result
- __beg
== __n
,
426 _M_message(__gnu_debug::__msg_inc_istreambuf
)
431 template<typename _CharT
>
432 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
433 istreambuf_iterator
<_CharT
> >::__type
434 find(istreambuf_iterator
<_CharT
> __first
,
435 istreambuf_iterator
<_CharT
> __last
, const _CharT
& __val
)
437 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
438 typedef typename
__is_iterator_type::traits_type traits_type
;
439 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
440 typedef typename
traits_type::int_type int_type
;
441 const int_type __eof
= traits_type::eof();
443 if (__first
._M_sbuf
&& !__last
._M_sbuf
)
445 const int_type __ival
= traits_type::to_int_type(__val
);
446 streambuf_type
* __sb
= __first
._M_sbuf
;
447 int_type __c
= __sb
->sgetc();
448 while (!traits_type::eq_int_type(__c
, __eof
)
449 && !traits_type::eq_int_type(__c
, __ival
))
451 streamsize __n
= __sb
->egptr() - __sb
->gptr();
454 const _CharT
* __p
= traits_type::find(__sb
->gptr(),
457 __n
= __p
- __sb
->gptr();
458 __sb
->__safe_gbump(__n
);
462 __c
= __sb
->snextc();
465 __first
._M_c
= __eof
;
471 template<typename _CharT
, typename _Distance
>
472 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
474 advance(istreambuf_iterator
<_CharT
>& __i
, _Distance __n
)
479 __glibcxx_assert(__n
> 0);
480 __glibcxx_requires_cond(!__i
._M_at_eof(),
481 _M_message(__gnu_debug::__msg_inc_istreambuf
)
484 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
485 typedef typename
__is_iterator_type::traits_type traits_type
;
486 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
487 typedef typename
traits_type::int_type int_type
;
488 const int_type __eof
= traits_type::eof();
490 streambuf_type
* __sb
= __i
._M_sbuf
;
493 streamsize __size
= __sb
->egptr() - __sb
->gptr();
496 __sb
->__safe_gbump(__n
);
500 __sb
->__safe_gbump(__size
);
502 if (traits_type::eq_int_type(__sb
->underflow(), __eof
))
504 __glibcxx_requires_cond(__n
== 0,
505 _M_message(__gnu_debug::__msg_inc_istreambuf
)
514 /// @} group iterators
516 _GLIBCXX_END_NAMESPACE_VERSION