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>
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 > 201703L && __cpp_lib_concepts
119  constexpr istreambuf_iterator(default_sentinel_t) noexcept
120  : istreambuf_iterator() { }
121 #endif
122 
123 #if __cplusplus >= 201103L
124  istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
125 
126  ~istreambuf_iterator() = default;
127 #endif
128 
129  /// Construct start of input stream iterator.
130  istreambuf_iterator(istream_type& __s) _GLIBCXX_USE_NOEXCEPT
131  : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
132 
133  /// Construct start of streambuf iterator.
134  istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
135  : _M_sbuf(__s), _M_c(traits_type::eof()) { }
136 
137 #if __cplusplus >= 201103L
139  operator=(const istreambuf_iterator&) noexcept = default;
140 #endif
141 
142  /// Return the current character pointed to by iterator. This returns
143  /// streambuf.sgetc(). It cannot be assigned. NB: The result of
144  /// operator*() on an end of stream is undefined.
145  char_type
146  operator*() const
147  {
148  int_type __c = _M_get();
149 
150 #ifdef _GLIBCXX_DEBUG_PEDANTIC
151  // Dereferencing a past-the-end istreambuf_iterator is a
152  // libstdc++ extension
153  __glibcxx_requires_cond(!_S_is_eof(__c),
154  _M_message(__gnu_debug::__msg_deref_istreambuf)
155  ._M_iterator(*this));
156 #endif
157  return traits_type::to_char_type(__c);
158  }
159 
160  /// Advance the iterator. Calls streambuf.sbumpc().
163  {
164  __glibcxx_requires_cond(_M_sbuf &&
165  (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
166  _M_message(__gnu_debug::__msg_inc_istreambuf)
167  ._M_iterator(*this));
168 
169  _M_sbuf->sbumpc();
170  _M_c = traits_type::eof();
171  return *this;
172  }
173 
174  /// Advance the iterator. Calls streambuf.sbumpc().
177  {
178  __glibcxx_requires_cond(_M_sbuf &&
179  (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
180  _M_message(__gnu_debug::__msg_inc_istreambuf)
181  ._M_iterator(*this));
182 
183  istreambuf_iterator __old = *this;
184  __old._M_c = _M_sbuf->sbumpc();
185  _M_c = traits_type::eof();
186  return __old;
187  }
188 
189  // _GLIBCXX_RESOLVE_LIB_DEFECTS
190  // 110 istreambuf_iterator::equal not const
191  // NB: there is also number 111 (NAD) relevant to this function.
192  /// Return true both iterators are end or both are not end.
193  bool
194  equal(const istreambuf_iterator& __b) const
195  { return _M_at_eof() == __b._M_at_eof(); }
196 
197  private:
198  int_type
199  _M_get() const
200  {
201  int_type __ret = _M_c;
202  if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc()))
203  _M_sbuf = 0;
204  return __ret;
205  }
206 
207  bool
208  _M_at_eof() const
209  { return _S_is_eof(_M_get()); }
210 
211  static bool
212  _S_is_eof(int_type __c)
213  {
214  const int_type __eof = traits_type::eof();
215  return traits_type::eq_int_type(__c, __eof);
216  }
217 
218 #if __cplusplus > 201703L && __cpp_lib_concepts
219  friend bool
220  operator==(const istreambuf_iterator& __i, default_sentinel_t __s)
221  { return __i._M_at_eof(); }
222 #endif
223  };
224 
225  template<typename _CharT, typename _Traits>
226  inline bool
227  operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
228  const istreambuf_iterator<_CharT, _Traits>& __b)
229  { return __a.equal(__b); }
230 
231  template<typename _CharT, typename _Traits>
232  inline bool
233  operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
234  const istreambuf_iterator<_CharT, _Traits>& __b)
235  { return !__a.equal(__b); }
236 
237  /// Provides output iterator semantics for streambufs.
238  template<typename _CharT, typename _Traits>
240  : public iterator<output_iterator_tag, void, void, void, void>
241  {
242  public:
243  // Types:
244  ///@{
245  /// Public typedefs
246 #if __cplusplus > 201703L
247  using difference_type = ptrdiff_t;
248 #endif
249  typedef _CharT char_type;
250  typedef _Traits traits_type;
253  ///@}
254 
255  template<typename _CharT2>
256  friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
260 
261  private:
262  streambuf_type* _M_sbuf;
263  bool _M_failed;
264 
265  public:
266 
267 #if __cplusplus > 201703L
268  constexpr
269  ostreambuf_iterator() noexcept
270  : _M_sbuf(nullptr), _M_failed(true) { }
271 #endif
272 
273  /// Construct output iterator from ostream.
274  ostreambuf_iterator(ostream_type& __s) _GLIBCXX_USE_NOEXCEPT
275  : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
276 
277  /// Construct output iterator from streambuf.
278  ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
279  : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
280 
281  /// Write character to streambuf. Calls streambuf.sputc().
283  operator=(_CharT __c)
284  {
285  if (!_M_failed &&
286  _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
287  _M_failed = true;
288  return *this;
289  }
290 
291  /// Return *this.
294  { return *this; }
295 
296  /// Return *this.
299  { return *this; }
300 
301  /// Return *this.
304  { return *this; }
305 
306  /// Return true if previous operator=() failed.
307  bool
308  failed() const _GLIBCXX_USE_NOEXCEPT
309  { return _M_failed; }
310 
312  _M_put(const _CharT* __ws, streamsize __len)
313  {
314  if (__builtin_expect(!_M_failed, true)
315  && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
316  false))
317  _M_failed = true;
318  return *this;
319  }
320  };
321 
322  // Overloads for streambuf iterators.
323  template<typename _CharT>
324  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
325  ostreambuf_iterator<_CharT> >::__type
326  copy(istreambuf_iterator<_CharT> __first,
327  istreambuf_iterator<_CharT> __last,
328  ostreambuf_iterator<_CharT> __result)
329  {
330  if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
331  {
332  bool __ineof;
333  __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
334  if (!__ineof)
335  __result._M_failed = true;
336  }
337  return __result;
338  }
339 
340  template<bool _IsMove, typename _CharT>
341  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
342  ostreambuf_iterator<_CharT> >::__type
343  __copy_move_a2(_CharT* __first, _CharT* __last,
344  ostreambuf_iterator<_CharT> __result)
345  {
346  const streamsize __num = __last - __first;
347  if (__num > 0)
348  __result._M_put(__first, __num);
349  return __result;
350  }
351 
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(const _CharT* __first, const _CharT* __last,
356  ostreambuf_iterator<_CharT> __result)
357  {
358  const streamsize __num = __last - __first;
359  if (__num > 0)
360  __result._M_put(__first, __num);
361  return __result;
362  }
363 
364  template<bool _IsMove, typename _CharT>
365  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
366  _CharT*>::__type
367  __copy_move_a2(istreambuf_iterator<_CharT> __first,
368  istreambuf_iterator<_CharT> __last, _CharT* __result)
369  {
370  typedef istreambuf_iterator<_CharT> __is_iterator_type;
371  typedef typename __is_iterator_type::traits_type traits_type;
372  typedef typename __is_iterator_type::streambuf_type streambuf_type;
373  typedef typename traits_type::int_type int_type;
374 
375  if (__first._M_sbuf && !__last._M_sbuf)
376  {
377  streambuf_type* __sb = __first._M_sbuf;
378  int_type __c = __sb->sgetc();
379  while (!traits_type::eq_int_type(__c, traits_type::eof()))
380  {
381  const streamsize __n = __sb->egptr() - __sb->gptr();
382  if (__n > 1)
383  {
384  traits_type::copy(__result, __sb->gptr(), __n);
385  __sb->__safe_gbump(__n);
386  __result += __n;
387  __c = __sb->underflow();
388  }
389  else
390  {
391  *__result++ = traits_type::to_char_type(__c);
392  __c = __sb->snextc();
393  }
394  }
395  }
396  return __result;
397  }
398 
399 #if __cplusplus >= 201103L
400  template<typename _CharT, typename _Size>
401  __enable_if_t<__is_char<_CharT>::__value, _CharT*>
402  __copy_n_a(istreambuf_iterator<_CharT> __it, _Size __n, _CharT* __result)
403  {
404  if (__n == 0)
405  return __result;
406 
407  __glibcxx_requires_cond(__it._M_sbuf,
408  _M_message(__gnu_debug::__msg_inc_istreambuf)
409  ._M_iterator(__it));
410  _CharT* __beg = __result;
411  __result += __it._M_sbuf->sgetn(__beg, __n);
412  __glibcxx_requires_cond(__result - __beg == __n,
413  _M_message(__gnu_debug::__msg_inc_istreambuf)
414  ._M_iterator(__it));
415  return __result;
416  }
417 #endif // C++11
418 
419  template<typename _CharT>
420  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
421  istreambuf_iterator<_CharT> >::__type
422  find(istreambuf_iterator<_CharT> __first,
423  istreambuf_iterator<_CharT> __last, const _CharT& __val)
424  {
425  typedef istreambuf_iterator<_CharT> __is_iterator_type;
426  typedef typename __is_iterator_type::traits_type traits_type;
427  typedef typename __is_iterator_type::streambuf_type streambuf_type;
428  typedef typename traits_type::int_type int_type;
429  const int_type __eof = traits_type::eof();
430 
431  if (__first._M_sbuf && !__last._M_sbuf)
432  {
433  const int_type __ival = traits_type::to_int_type(__val);
434  streambuf_type* __sb = __first._M_sbuf;
435  int_type __c = __sb->sgetc();
436  while (!traits_type::eq_int_type(__c, __eof)
437  && !traits_type::eq_int_type(__c, __ival))
438  {
439  streamsize __n = __sb->egptr() - __sb->gptr();
440  if (__n > 1)
441  {
442  const _CharT* __p = traits_type::find(__sb->gptr(),
443  __n, __val);
444  if (__p)
445  __n = __p - __sb->gptr();
446  __sb->__safe_gbump(__n);
447  __c = __sb->sgetc();
448  }
449  else
450  __c = __sb->snextc();
451  }
452 
453  __first._M_c = __eof;
454  }
455 
456  return __first;
457  }
458 
459  template<typename _CharT, typename _Distance>
460  typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
461  void>::__type
462  advance(istreambuf_iterator<_CharT>& __i, _Distance __n)
463  {
464  if (__n == 0)
465  return;
466 
467  __glibcxx_assert(__n > 0);
468  __glibcxx_requires_cond(!__i._M_at_eof(),
469  _M_message(__gnu_debug::__msg_inc_istreambuf)
470  ._M_iterator(__i));
471 
472  typedef istreambuf_iterator<_CharT> __is_iterator_type;
473  typedef typename __is_iterator_type::traits_type traits_type;
474  typedef typename __is_iterator_type::streambuf_type streambuf_type;
475  typedef typename traits_type::int_type int_type;
476  const int_type __eof = traits_type::eof();
477 
478  streambuf_type* __sb = __i._M_sbuf;
479  while (__n > 0)
480  {
481  streamsize __size = __sb->egptr() - __sb->gptr();
482  if (__size > __n)
483  {
484  __sb->__safe_gbump(__n);
485  break;
486  }
487 
488  __sb->__safe_gbump(__size);
489  __n -= __size;
490  if (traits_type::eq_int_type(__sb->underflow(), __eof))
491  {
492  __glibcxx_requires_cond(__n == 0,
493  _M_message(__gnu_debug::__msg_inc_istreambuf)
494  ._M_iterator(__i));
495  break;
496  }
497  }
498 
499  __i._M_c = __eof;
500  }
501 
502 /// @} group iterators
503 
504 _GLIBCXX_END_NAMESPACE_VERSION
505 } // namespace
506 
507 #endif
ISO C++ entities toplevel namespace is std.
ptrdiff_t streamsize
Integral type for I/O operation counts and buffer sizes.
Definition: postypes.h:98
constexpr void advance(_InputIterator &__i, _Distance __n)
A generalization of pointer arithmetic.
The actual work of input and output (interface).
Definition: streambuf:123
streamsize sputn(const char_type *__s, streamsize __n)
Entry point for all single-character output functions.
Definition: streambuf:457
int_type sbumpc()
Getting the next character.
Definition: streambuf:323
int_type sgetc()
Getting the next character.
Definition: streambuf:345
int_type sputc(char_type __c)
Entry point for all single-character output functions.
Definition: streambuf:431
Template class basic_istream.
Definition: istream:59
Template class basic_ostream.
Definition: ostream:59
Provides input iterator semantics for streambufs.
basic_streambuf< _CharT, _Traits > streambuf_type
Public typedefs.
istreambuf_iterator & operator++()
Advance the iterator. Calls streambuf.sbumpc().
constexpr istreambuf_iterator() noexcept
Construct end of input stream iterator.
basic_istream< _CharT, _Traits > istream_type
Public typedefs.
char_type operator*() const
Return the current character pointed to by iterator. This returns streambuf.sgetc()....
bool equal(const istreambuf_iterator &__b) const
Return true both iterators are end or both are not end.
_Traits traits_type
Public typedefs.
istreambuf_iterator(istream_type &__s) noexcept
Construct start of input stream iterator.
istreambuf_iterator operator++(int)
Advance the iterator. Calls streambuf.sbumpc().
_CharT char_type
Public typedefs.
istreambuf_iterator(streambuf_type *__s) noexcept
Construct start of streambuf iterator.
_Traits::int_type int_type
Public typedefs.
Provides output iterator semantics for streambufs.
ostreambuf_iterator & operator++()
Return *this.
ostreambuf_iterator & operator*()
Return *this.
bool failed() const noexcept
Return true if previous operator=() failed.
_Traits traits_type
Public typedefs.
basic_ostream< _CharT, _Traits > ostream_type
Public typedefs.
basic_streambuf< _CharT, _Traits > streambuf_type
Public typedefs.
ostreambuf_iterator & operator=(_CharT __c)
Write character to streambuf. Calls streambuf.sputc().
ostreambuf_iterator(ostream_type &__s) noexcept
Construct output iterator from ostream.
ostreambuf_iterator & operator++(int)
Return *this.
_CharT char_type
Public typedefs.
ostreambuf_iterator(streambuf_type *__s) noexcept
Construct output iterator from streambuf.
Common iterator class.
void difference_type
Distance between iterators is represented as this type.