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