3 // Copyright (C) 1997-2021 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 // 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
,
58 #if __cplusplus < 201103L
59 typedef _CharT
& reference
; // Changed to _CharT by LWG 445
60 #elif __cplusplus > 201703L
61 // _GLIBCXX_RESOLVE_LIB_DEFECTS
62 // 3188. istreambuf_iterator::pointer should not be unspecified
66 typedef _CharT char_type
;
67 typedef _Traits traits_type
;
68 typedef typename
_Traits::int_type int_type
;
69 typedef basic_streambuf
<_CharT
, _Traits
> streambuf_type
;
70 typedef basic_istream
<_CharT
, _Traits
> istream_type
;
73 template<typename _CharT2
>
74 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
75 ostreambuf_iterator
<_CharT2
> >::__type
76 copy(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
77 ostreambuf_iterator
<_CharT2
>);
79 template<bool _IsMove
, typename _CharT2
>
80 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
82 __copy_move_a2(istreambuf_iterator
<_CharT2
>,
83 istreambuf_iterator
<_CharT2
>, _CharT2
*);
85 template<typename _CharT2
, typename _Size
>
86 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
88 __copy_n_a(istreambuf_iterator
<_CharT2
>, _Size
, _CharT2
*, bool);
90 template<typename _CharT2
>
91 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
92 istreambuf_iterator
<_CharT2
> >::__type
93 find(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
96 template<typename _CharT2
, typename _Distance
>
97 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
99 advance(istreambuf_iterator
<_CharT2
>&, _Distance
);
102 // 24.5.3 istreambuf_iterator
104 // If the end of stream is reached (streambuf_type::sgetc()
105 // returns traits_type::eof()), the iterator becomes equal to
106 // the "end of stream" iterator value.
107 // NB: This implementation assumes the "end of stream" value
109 mutable streambuf_type
* _M_sbuf
;
113 /// Construct end of input stream iterator.
114 _GLIBCXX_CONSTEXPR
istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
115 : _M_sbuf(0), _M_c(traits_type::eof()) { }
117 #if __cplusplus > 201703L && __cpp_lib_concepts
118 constexpr istreambuf_iterator(default_sentinel_t
) noexcept
119 : istreambuf_iterator() { }
122 #if __cplusplus >= 201103L
123 istreambuf_iterator(const istreambuf_iterator
&) noexcept
= default;
125 ~istreambuf_iterator() = default;
128 /// Construct start of input stream iterator.
129 istreambuf_iterator(istream_type
& __s
) _GLIBCXX_USE_NOEXCEPT
130 : _M_sbuf(__s
.rdbuf()), _M_c(traits_type::eof()) { }
132 /// Construct start of streambuf iterator.
133 istreambuf_iterator(streambuf_type
* __s
) _GLIBCXX_USE_NOEXCEPT
134 : _M_sbuf(__s
), _M_c(traits_type::eof()) { }
136 #if __cplusplus >= 201103L
138 operator=(const istreambuf_iterator
&) noexcept
= default;
141 /// Return the current character pointed to by iterator. This returns
142 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
143 /// operator*() on an end of stream is undefined.
147 int_type __c
= _M_get();
149 #ifdef _GLIBCXX_DEBUG_PEDANTIC
150 // Dereferencing a past-the-end istreambuf_iterator is a
151 // libstdc++ extension
152 __glibcxx_requires_cond(!_S_is_eof(__c
),
153 _M_message(__gnu_debug::__msg_deref_istreambuf
)
154 ._M_iterator(*this));
156 return traits_type::to_char_type(__c
);
159 /// Advance the iterator. Calls streambuf.sbumpc().
163 __glibcxx_requires_cond(_M_sbuf
&&
164 (!_S_is_eof(_M_c
) || !_S_is_eof(_M_sbuf
->sgetc())),
165 _M_message(__gnu_debug::__msg_inc_istreambuf
)
166 ._M_iterator(*this));
169 _M_c
= traits_type::eof();
173 /// Advance the iterator. Calls streambuf.sbumpc().
177 __glibcxx_requires_cond(_M_sbuf
&&
178 (!_S_is_eof(_M_c
) || !_S_is_eof(_M_sbuf
->sgetc())),
179 _M_message(__gnu_debug::__msg_inc_istreambuf
)
180 ._M_iterator(*this));
182 istreambuf_iterator __old
= *this;
183 __old
._M_c
= _M_sbuf
->sbumpc();
184 _M_c
= traits_type::eof();
188 // _GLIBCXX_RESOLVE_LIB_DEFECTS
189 // 110 istreambuf_iterator::equal not const
190 // NB: there is also number 111 (NAD) relevant to this function.
191 /// Return true both iterators are end or both are not end.
193 equal(const istreambuf_iterator
& __b
) const
194 { return _M_at_eof() == __b
._M_at_eof(); }
200 int_type __ret
= _M_c
;
201 if (_M_sbuf
&& _S_is_eof(__ret
) && _S_is_eof(__ret
= _M_sbuf
->sgetc()))
208 { return _S_is_eof(_M_get()); }
211 _S_is_eof(int_type __c
)
213 const int_type __eof
= traits_type::eof();
214 return traits_type::eq_int_type(__c
, __eof
);
217 #if __cplusplus > 201703L && __cpp_lib_concepts
219 operator==(const istreambuf_iterator
& __i
, default_sentinel_t __s
)
220 { return __i
._M_at_eof(); }
224 template<typename _CharT
, typename _Traits
>
226 operator==(const istreambuf_iterator
<_CharT
, _Traits
>& __a
,
227 const istreambuf_iterator
<_CharT
, _Traits
>& __b
)
228 { return __a
.equal(__b
); }
230 template<typename _CharT
, typename _Traits
>
232 operator!=(const istreambuf_iterator
<_CharT
, _Traits
>& __a
,
233 const istreambuf_iterator
<_CharT
, _Traits
>& __b
)
234 { return !__a
.equal(__b
); }
236 /// Provides output iterator semantics for streambufs.
237 template<typename _CharT
, typename _Traits
>
238 class ostreambuf_iterator
239 : public iterator
<output_iterator_tag
, void, void, void, void>
245 #if __cplusplus > 201703L
246 using difference_type
= ptrdiff_t;
248 typedef _CharT char_type
;
249 typedef _Traits traits_type
;
250 typedef basic_streambuf
<_CharT
, _Traits
> streambuf_type
;
251 typedef basic_ostream
<_CharT
, _Traits
> ostream_type
;
254 template<typename _CharT2
>
255 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
256 ostreambuf_iterator
<_CharT2
> >::__type
257 copy(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
258 ostreambuf_iterator
<_CharT2
>);
261 streambuf_type
* _M_sbuf
;
266 #if __cplusplus > 201703L
268 ostreambuf_iterator() noexcept
269 : _M_sbuf(nullptr), _M_failed(true) { }
272 /// Construct output iterator from ostream.
273 ostreambuf_iterator(ostream_type
& __s
) _GLIBCXX_USE_NOEXCEPT
274 : _M_sbuf(__s
.rdbuf()), _M_failed(!_M_sbuf
) { }
276 /// Construct output iterator from streambuf.
277 ostreambuf_iterator(streambuf_type
* __s
) _GLIBCXX_USE_NOEXCEPT
278 : _M_sbuf(__s
), _M_failed(!_M_sbuf
) { }
280 /// Write character to streambuf. Calls streambuf.sputc().
282 operator=(_CharT __c
)
285 _Traits::eq_int_type(_M_sbuf
->sputc(__c
), _Traits::eof()))
305 /// Return true if previous operator=() failed.
307 failed() const _GLIBCXX_USE_NOEXCEPT
308 { return _M_failed
; }
311 _M_put(const _CharT
* __ws
, streamsize __len
)
313 if (__builtin_expect(!_M_failed
, true)
314 && __builtin_expect(this->_M_sbuf
->sputn(__ws
, __len
) != __len
,
321 // Overloads for streambuf iterators.
322 template<typename _CharT
>
323 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
324 ostreambuf_iterator
<_CharT
> >::__type
325 copy(istreambuf_iterator
<_CharT
> __first
,
326 istreambuf_iterator
<_CharT
> __last
,
327 ostreambuf_iterator
<_CharT
> __result
)
329 if (__first
._M_sbuf
&& !__last
._M_sbuf
&& !__result
._M_failed
)
332 __copy_streambufs_eof(__first
._M_sbuf
, __result
._M_sbuf
, __ineof
);
334 __result
._M_failed
= true;
339 template<bool _IsMove
, typename _CharT
>
340 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
341 ostreambuf_iterator
<_CharT
> >::__type
342 __copy_move_a2(_CharT
* __first
, _CharT
* __last
,
343 ostreambuf_iterator
<_CharT
> __result
)
345 const streamsize __num
= __last
- __first
;
347 __result
._M_put(__first
, __num
);
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(const _CharT
* __first
, const _CharT
* __last
,
355 ostreambuf_iterator
<_CharT
> __result
)
357 const streamsize __num
= __last
- __first
;
359 __result
._M_put(__first
, __num
);
363 template<bool _IsMove
, typename _CharT
>
364 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
366 __copy_move_a2(istreambuf_iterator
<_CharT
> __first
,
367 istreambuf_iterator
<_CharT
> __last
, _CharT
* __result
)
369 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
370 typedef typename
__is_iterator_type::traits_type traits_type
;
371 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
372 typedef typename
traits_type::int_type int_type
;
374 if (__first
._M_sbuf
&& !__last
._M_sbuf
)
376 streambuf_type
* __sb
= __first
._M_sbuf
;
377 int_type __c
= __sb
->sgetc();
378 while (!traits_type::eq_int_type(__c
, traits_type::eof()))
380 const streamsize __n
= __sb
->egptr() - __sb
->gptr();
383 traits_type::copy(__result
, __sb
->gptr(), __n
);
384 __sb
->__safe_gbump(__n
);
386 __c
= __sb
->underflow();
390 *__result
++ = traits_type::to_char_type(__c
);
391 __c
= __sb
->snextc();
398 template<typename _CharT
, typename _Size
>
399 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
401 __copy_n_a(istreambuf_iterator
<_CharT
> __it
, _Size __n
, _CharT
* __result
,
402 bool __strict
__attribute__((__unused__
)))
407 __glibcxx_requires_cond(__it
._M_sbuf
,
408 _M_message(__gnu_debug::__msg_inc_istreambuf
)
410 _CharT
* __beg
= __result
;
411 __result
+= __it
._M_sbuf
->sgetn(__beg
, __n
);
412 __glibcxx_requires_cond(!__strict
|| __result
- __beg
== __n
,
413 _M_message(__gnu_debug::__msg_inc_istreambuf
)
418 template<typename _CharT
>
419 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
420 istreambuf_iterator
<_CharT
> >::__type
421 find(istreambuf_iterator
<_CharT
> __first
,
422 istreambuf_iterator
<_CharT
> __last
, const _CharT
& __val
)
424 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
425 typedef typename
__is_iterator_type::traits_type traits_type
;
426 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
427 typedef typename
traits_type::int_type int_type
;
428 const int_type __eof
= traits_type::eof();
430 if (__first
._M_sbuf
&& !__last
._M_sbuf
)
432 const int_type __ival
= traits_type::to_int_type(__val
);
433 streambuf_type
* __sb
= __first
._M_sbuf
;
434 int_type __c
= __sb
->sgetc();
435 while (!traits_type::eq_int_type(__c
, __eof
)
436 && !traits_type::eq_int_type(__c
, __ival
))
438 streamsize __n
= __sb
->egptr() - __sb
->gptr();
441 const _CharT
* __p
= traits_type::find(__sb
->gptr(),
444 __n
= __p
- __sb
->gptr();
445 __sb
->__safe_gbump(__n
);
449 __c
= __sb
->snextc();
452 __first
._M_c
= __eof
;
458 template<typename _CharT
, typename _Distance
>
459 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
461 advance(istreambuf_iterator
<_CharT
>& __i
, _Distance __n
)
466 __glibcxx_assert(__n
> 0);
467 __glibcxx_requires_cond(!__i
._M_at_eof(),
468 _M_message(__gnu_debug::__msg_inc_istreambuf
)
471 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
472 typedef typename
__is_iterator_type::traits_type traits_type
;
473 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
474 typedef typename
traits_type::int_type int_type
;
475 const int_type __eof
= traits_type::eof();
477 streambuf_type
* __sb
= __i
._M_sbuf
;
480 streamsize __size
= __sb
->egptr() - __sb
->gptr();
483 __sb
->__safe_gbump(__n
);
487 __sb
->__safe_gbump(__size
);
489 if (traits_type::eq_int_type(__sb
->underflow(), __eof
))
491 __glibcxx_requires_cond(__n
== 0,
492 _M_message(__gnu_debug::__msg_inc_istreambuf
)
501 // @} group iterators
503 _GLIBCXX_END_NAMESPACE_VERSION