3 // Copyright (C) 1997-2018 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
,
53 #if __cplusplus >= 201103L
64 typedef _CharT char_type
;
65 typedef _Traits traits_type
;
66 typedef typename
_Traits::int_type int_type
;
67 typedef basic_streambuf
<_CharT
, _Traits
> streambuf_type
;
68 typedef basic_istream
<_CharT
, _Traits
> istream_type
;
71 template<typename _CharT2
>
72 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
73 ostreambuf_iterator
<_CharT2
> >::__type
74 copy(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
75 ostreambuf_iterator
<_CharT2
>);
77 template<bool _IsMove
, typename _CharT2
>
78 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
80 __copy_move_a2(istreambuf_iterator
<_CharT2
>,
81 istreambuf_iterator
<_CharT2
>, _CharT2
*);
83 template<typename _CharT2
>
84 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
85 istreambuf_iterator
<_CharT2
> >::__type
86 find(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
89 template<typename _CharT2
, typename _Distance
>
90 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
92 advance(istreambuf_iterator
<_CharT2
>&, _Distance
);
95 // 24.5.3 istreambuf_iterator
97 // If the end of stream is reached (streambuf_type::sgetc()
98 // returns traits_type::eof()), the iterator becomes equal to
99 // the "end of stream" iterator value.
100 // NB: This implementation assumes the "end of stream" value
102 mutable streambuf_type
* _M_sbuf
;
106 /// Construct end of input stream iterator.
107 _GLIBCXX_CONSTEXPR
istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
108 : _M_sbuf(0), _M_c(traits_type::eof()) { }
110 #if __cplusplus >= 201103L
111 istreambuf_iterator(const istreambuf_iterator
&) noexcept
= default;
113 ~istreambuf_iterator() = default;
116 /// Construct start of input stream iterator.
117 istreambuf_iterator(istream_type
& __s
) _GLIBCXX_USE_NOEXCEPT
118 : _M_sbuf(__s
.rdbuf()), _M_c(traits_type::eof()) { }
120 /// Construct start of streambuf iterator.
121 istreambuf_iterator(streambuf_type
* __s
) _GLIBCXX_USE_NOEXCEPT
122 : _M_sbuf(__s
), _M_c(traits_type::eof()) { }
124 /// Return the current character pointed to by iterator. This returns
125 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
126 /// operator*() on an end of stream is undefined.
130 int_type __c
= _M_get();
132 #ifdef _GLIBCXX_DEBUG_PEDANTIC
133 // Dereferencing a past-the-end istreambuf_iterator is a
134 // libstdc++ extension
135 __glibcxx_requires_cond(!_S_is_eof(__c
),
136 _M_message(__gnu_debug::__msg_deref_istreambuf
)
137 ._M_iterator(*this));
139 return traits_type::to_char_type(__c
);
142 /// Advance the iterator. Calls streambuf.sbumpc().
146 __glibcxx_requires_cond(_M_sbuf
&&
147 (!_S_is_eof(_M_c
) || !_S_is_eof(_M_sbuf
->sgetc())),
148 _M_message(__gnu_debug::__msg_inc_istreambuf
)
149 ._M_iterator(*this));
152 _M_c
= traits_type::eof();
156 /// Advance the iterator. Calls streambuf.sbumpc().
160 __glibcxx_requires_cond(_M_sbuf
&&
161 (!_S_is_eof(_M_c
) || !_S_is_eof(_M_sbuf
->sgetc())),
162 _M_message(__gnu_debug::__msg_inc_istreambuf
)
163 ._M_iterator(*this));
165 istreambuf_iterator __old
= *this;
166 __old
._M_c
= _M_sbuf
->sbumpc();
167 _M_c
= traits_type::eof();
171 // _GLIBCXX_RESOLVE_LIB_DEFECTS
172 // 110 istreambuf_iterator::equal not const
173 // NB: there is also number 111 (NAD) relevant to this function.
174 /// Return true both iterators are end or both are not end.
176 equal(const istreambuf_iterator
& __b
) const
177 { return _M_at_eof() == __b
._M_at_eof(); }
183 int_type __ret
= _M_c
;
184 if (_M_sbuf
&& _S_is_eof(__ret
) && _S_is_eof(__ret
= _M_sbuf
->sgetc()))
191 { return _S_is_eof(_M_get()); }
194 _S_is_eof(int_type __c
)
196 const int_type __eof
= traits_type::eof();
197 return traits_type::eq_int_type(__c
, __eof
);
201 template<typename _CharT
, typename _Traits
>
203 operator==(const istreambuf_iterator
<_CharT
, _Traits
>& __a
,
204 const istreambuf_iterator
<_CharT
, _Traits
>& __b
)
205 { return __a
.equal(__b
); }
207 template<typename _CharT
, typename _Traits
>
209 operator!=(const istreambuf_iterator
<_CharT
, _Traits
>& __a
,
210 const istreambuf_iterator
<_CharT
, _Traits
>& __b
)
211 { return !__a
.equal(__b
); }
213 /// Provides output iterator semantics for streambufs.
214 template<typename _CharT
, typename _Traits
>
215 class ostreambuf_iterator
216 : public iterator
<output_iterator_tag
, void, void, void, void>
222 typedef _CharT char_type
;
223 typedef _Traits traits_type
;
224 typedef basic_streambuf
<_CharT
, _Traits
> streambuf_type
;
225 typedef basic_ostream
<_CharT
, _Traits
> ostream_type
;
228 template<typename _CharT2
>
229 friend typename
__gnu_cxx::__enable_if
<__is_char
<_CharT2
>::__value
,
230 ostreambuf_iterator
<_CharT2
> >::__type
231 copy(istreambuf_iterator
<_CharT2
>, istreambuf_iterator
<_CharT2
>,
232 ostreambuf_iterator
<_CharT2
>);
235 streambuf_type
* _M_sbuf
;
239 /// Construct output iterator from ostream.
240 ostreambuf_iterator(ostream_type
& __s
) _GLIBCXX_USE_NOEXCEPT
241 : _M_sbuf(__s
.rdbuf()), _M_failed(!_M_sbuf
) { }
243 /// Construct output iterator from streambuf.
244 ostreambuf_iterator(streambuf_type
* __s
) _GLIBCXX_USE_NOEXCEPT
245 : _M_sbuf(__s
), _M_failed(!_M_sbuf
) { }
247 /// Write character to streambuf. Calls streambuf.sputc().
249 operator=(_CharT __c
)
252 _Traits::eq_int_type(_M_sbuf
->sputc(__c
), _Traits::eof()))
272 /// Return true if previous operator=() failed.
274 failed() const _GLIBCXX_USE_NOEXCEPT
275 { return _M_failed
; }
278 _M_put(const _CharT
* __ws
, streamsize __len
)
280 if (__builtin_expect(!_M_failed
, true)
281 && __builtin_expect(this->_M_sbuf
->sputn(__ws
, __len
) != __len
,
288 // Overloads for streambuf iterators.
289 template<typename _CharT
>
290 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
291 ostreambuf_iterator
<_CharT
> >::__type
292 copy(istreambuf_iterator
<_CharT
> __first
,
293 istreambuf_iterator
<_CharT
> __last
,
294 ostreambuf_iterator
<_CharT
> __result
)
296 if (__first
._M_sbuf
&& !__last
._M_sbuf
&& !__result
._M_failed
)
299 __copy_streambufs_eof(__first
._M_sbuf
, __result
._M_sbuf
, __ineof
);
301 __result
._M_failed
= true;
306 template<bool _IsMove
, typename _CharT
>
307 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
308 ostreambuf_iterator
<_CharT
> >::__type
309 __copy_move_a2(_CharT
* __first
, _CharT
* __last
,
310 ostreambuf_iterator
<_CharT
> __result
)
312 const streamsize __num
= __last
- __first
;
314 __result
._M_put(__first
, __num
);
318 template<bool _IsMove
, typename _CharT
>
319 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
320 ostreambuf_iterator
<_CharT
> >::__type
321 __copy_move_a2(const _CharT
* __first
, const _CharT
* __last
,
322 ostreambuf_iterator
<_CharT
> __result
)
324 const streamsize __num
= __last
- __first
;
326 __result
._M_put(__first
, __num
);
330 template<bool _IsMove
, typename _CharT
>
331 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
333 __copy_move_a2(istreambuf_iterator
<_CharT
> __first
,
334 istreambuf_iterator
<_CharT
> __last
, _CharT
* __result
)
336 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
337 typedef typename
__is_iterator_type::traits_type traits_type
;
338 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
339 typedef typename
traits_type::int_type int_type
;
341 if (__first
._M_sbuf
&& !__last
._M_sbuf
)
343 streambuf_type
* __sb
= __first
._M_sbuf
;
344 int_type __c
= __sb
->sgetc();
345 while (!traits_type::eq_int_type(__c
, traits_type::eof()))
347 const streamsize __n
= __sb
->egptr() - __sb
->gptr();
350 traits_type::copy(__result
, __sb
->gptr(), __n
);
351 __sb
->__safe_gbump(__n
);
353 __c
= __sb
->underflow();
357 *__result
++ = traits_type::to_char_type(__c
);
358 __c
= __sb
->snextc();
365 template<typename _CharT
>
366 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
367 istreambuf_iterator
<_CharT
> >::__type
368 find(istreambuf_iterator
<_CharT
> __first
,
369 istreambuf_iterator
<_CharT
> __last
, const _CharT
& __val
)
371 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
372 typedef typename
__is_iterator_type::traits_type traits_type
;
373 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
374 typedef typename
traits_type::int_type int_type
;
375 const int_type __eof
= traits_type::eof();
377 if (__first
._M_sbuf
&& !__last
._M_sbuf
)
379 const int_type __ival
= traits_type::to_int_type(__val
);
380 streambuf_type
* __sb
= __first
._M_sbuf
;
381 int_type __c
= __sb
->sgetc();
382 while (!traits_type::eq_int_type(__c
, __eof
)
383 && !traits_type::eq_int_type(__c
, __ival
))
385 streamsize __n
= __sb
->egptr() - __sb
->gptr();
388 const _CharT
* __p
= traits_type::find(__sb
->gptr(),
391 __n
= __p
- __sb
->gptr();
392 __sb
->__safe_gbump(__n
);
396 __c
= __sb
->snextc();
399 __first
._M_c
= __eof
;
405 template<typename _CharT
, typename _Distance
>
406 typename
__gnu_cxx::__enable_if
<__is_char
<_CharT
>::__value
,
408 advance(istreambuf_iterator
<_CharT
>& __i
, _Distance __n
)
413 __glibcxx_assert(__n
> 0);
414 __glibcxx_requires_cond(!__i
._M_at_eof(),
415 _M_message(__gnu_debug::__msg_inc_istreambuf
)
418 typedef istreambuf_iterator
<_CharT
> __is_iterator_type
;
419 typedef typename
__is_iterator_type::traits_type traits_type
;
420 typedef typename
__is_iterator_type::streambuf_type streambuf_type
;
421 typedef typename
traits_type::int_type int_type
;
422 const int_type __eof
= traits_type::eof();
424 streambuf_type
* __sb
= __i
._M_sbuf
;
427 streamsize __size
= __sb
->egptr() - __sb
->gptr();
430 __sb
->__safe_gbump(__n
);
434 __sb
->__safe_gbump(__size
);
436 if (traits_type::eq_int_type(__sb
->underflow(), __eof
))
438 __glibcxx_requires_cond(__n
== 0,
439 _M_message(__gnu_debug::__msg_inc_istreambuf
)
448 // @} group iterators
450 _GLIBCXX_END_NAMESPACE_VERSION