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