libstdc++
stream_iterator.h
Go to the documentation of this file.
1 // Stream iterators
2 
3 // Copyright (C) 2001-2025 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/stream_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 _STREAM_ITERATOR_H
31 #define _STREAM_ITERATOR_H 1
32 
33 #ifdef _GLIBCXX_SYSHDR
34 #pragma GCC system_header
35 #endif
36 
37 #include <iosfwd>
38 #include <bits/move.h>
40 #include <debug/debug.h>
41 
42 namespace std _GLIBCXX_VISIBILITY(default)
43 {
44 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 
46  /**
47  * @addtogroup iterators
48  * @{
49  */
50 
51 // Ignore warnings about std::iterator.
52 #pragma GCC diagnostic push
53 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
54 
55  /// Provides input iterator semantics for streams.
56  template<typename _Tp, typename _CharT = char,
57  typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
59  : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
60  {
61  public:
62  typedef _CharT char_type;
63  typedef _Traits traits_type;
65 
66  private:
67  istream_type* _M_stream;
68  _Tp _M_value;
69  // This bool becomes false at end-of-stream. It should be sufficient to
70  // check _M_stream != nullptr instead, but historically we did not set
71  // _M_stream to null when reaching the end, so we need to keep this flag.
72  bool _M_ok;
73 
74  public:
75  /// Construct end of input stream iterator.
76  _GLIBCXX_CONSTEXPR istream_iterator()
77  _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Tp>::value)
78  : _M_stream(0), _M_value(), _M_ok(false) {}
79 
80  /// Construct start of input stream iterator.
82  : _M_stream(std::__addressof(__s)), _M_ok(true)
83  { _M_read(); }
84 
85  _GLIBCXX_CONSTEXPR
87  _GLIBCXX_NOEXCEPT_IF(is_nothrow_copy_constructible<_Tp>::value)
88  : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
89  _M_ok(__obj._M_ok)
90  { }
91 
92 #if __cplusplus > 201703L && __cpp_lib_concepts
93  constexpr
94  istream_iterator(default_sentinel_t)
95  noexcept(is_nothrow_default_constructible_v<_Tp>)
96  : istream_iterator() { }
97 #endif
98 
99 #if __cplusplus >= 201103L
100  istream_iterator& operator=(const istream_iterator&) = default;
101  ~istream_iterator() = default;
102 #endif
103 
104  _GLIBCXX_NODISCARD
105  const _Tp&
106  operator*() const _GLIBCXX_NOEXCEPT
107  {
108  __glibcxx_requires_cond(_M_ok,
109  _M_message(__gnu_debug::__msg_deref_istream)
110  ._M_iterator(*this));
111  return _M_value;
112  }
113 
114  _GLIBCXX_NODISCARD
115  const _Tp*
116  operator->() const _GLIBCXX_NOEXCEPT
117  { return std::__addressof((operator*())); }
118 
120  operator++()
121  {
122  __glibcxx_requires_cond(_M_ok,
123  _M_message(__gnu_debug::__msg_inc_istream)
124  ._M_iterator(*this));
125  _M_read();
126  return *this;
127  }
128 
130  operator++(int)
131  {
132  __glibcxx_requires_cond(_M_ok,
133  _M_message(__gnu_debug::__msg_inc_istream)
134  ._M_iterator(*this));
135  istream_iterator __tmp = *this;
136  _M_read();
137  return __tmp;
138  }
139 
140  private:
141  bool
142  _M_equal(const istream_iterator& __x) const _GLIBCXX_NOEXCEPT
143  {
144  // Ideally this would just return _M_stream == __x._M_stream,
145  // but code compiled with old versions never sets _M_stream to null.
146  return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream);
147  }
148 
149  void
150  _M_read()
151  {
152  if (_M_stream && !(*_M_stream >> _M_value))
153  {
154  _M_stream = 0;
155  _M_ok = false;
156  }
157  }
158 
159  /// Return true if the iterators refer to the same stream,
160  /// or are both at end-of-stream.
161  _GLIBCXX_NODISCARD
162  friend bool
164  _GLIBCXX_NOEXCEPT
165  { return __x._M_equal(__y); }
166 
167 #if __cpp_impl_three_way_comparison < 201907L
168  /// Return true if the iterators refer to different streams,
169  /// or if one is at end-of-stream and the other is not.
170  _GLIBCXX_NODISCARD
171  friend bool
172  operator!=(const istream_iterator& __x, const istream_iterator& __y)
173  _GLIBCXX_NOEXCEPT
174  { return !__x._M_equal(__y); }
175 #endif
176 
177 #if __cplusplus > 201703L && __cpp_lib_concepts
178  [[nodiscard]]
179  friend bool
180  operator==(const istream_iterator& __i, default_sentinel_t) noexcept
181  { return !__i._M_stream; }
182 #endif
183  };
184 
185  /**
186  * @brief Provides output iterator semantics for streams.
187  *
188  * This class provides an iterator to write to an ostream. The type Tp is
189  * the only type written by this iterator and there must be an
190  * operator<<(Tp) defined.
191  *
192  * @tparam _Tp The type to write to the ostream.
193  * @tparam _CharT The ostream char_type.
194  * @tparam _Traits The ostream char_traits.
195  */
196  template<typename _Tp, typename _CharT = char,
197  typename _Traits = char_traits<_CharT> >
199  : public iterator<output_iterator_tag, void, void, void, void>
200  {
201  public:
202  ///@{
203  /// Public typedef
204 #if __cplusplus > 201703L
205  using difference_type = ptrdiff_t;
206 #endif
207  typedef _CharT char_type;
208  typedef _Traits traits_type;
210  ///@}
211 
212  private:
213  ostream_type* _M_stream;
214  const _CharT* _M_string;
215 
216  public:
217  /// Construct from an ostream.
218  ostream_iterator(ostream_type& __s) _GLIBCXX_NOEXCEPT
219  : _M_stream(std::__addressof(__s)), _M_string(0) {}
220 
221  /**
222  * Construct from an ostream.
223  *
224  * The delimiter string @a c is written to the stream after every Tp
225  * written to the stream. The delimiter is not copied, and thus must
226  * not be destroyed while this iterator is in use.
227  *
228  * @param __s Underlying ostream to write to.
229  * @param __c CharT delimiter string to insert.
230  */
231  ostream_iterator(ostream_type& __s, const _CharT* __c) _GLIBCXX_NOEXCEPT
232  : _M_stream(std::__addressof(__s)), _M_string(__c) { }
233 
234  /// Copy constructor.
235  ostream_iterator(const ostream_iterator& __obj) _GLIBCXX_NOEXCEPT
236  : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
237 
238 #if __cplusplus >= 201103L
239  ostream_iterator& operator=(const ostream_iterator&) = default;
240 #endif
241 
242  /// Writes @a value to underlying ostream using operator<<. If
243  /// constructed with delimiter string, writes delimiter to ostream.
245  operator=(const _Tp& __value)
246  {
247  __glibcxx_requires_cond(_M_stream != 0,
248  _M_message(__gnu_debug::__msg_output_ostream)
249  ._M_iterator(*this));
250  *_M_stream << __value;
251  if (_M_string)
252  *_M_stream << _M_string;
253  return *this;
254  }
255 
256  _GLIBCXX_NODISCARD
258  operator*() _GLIBCXX_NOEXCEPT
259  { return *this; }
260 
262  operator++() _GLIBCXX_NOEXCEPT
263  { return *this; }
264 
266  operator++(int) _GLIBCXX_NOEXCEPT
267  { return *this; }
268  };
269 #pragma GCC diagnostic pop
270 
271  /// @} group iterators
272 
273 _GLIBCXX_END_NAMESPACE_VERSION
274 } // namespace
275 
276 #endif
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:52
ISO C++ entities toplevel namespace is std.
Template class basic_istream.
Definition: istream:63
Template class basic_ostream.
Definition: ostream.h:67
is_nothrow_default_constructible
Definition: type_traits:1245
is_nothrow_copy_constructible
Definition: type_traits:1254
Common iterator class.
Provides input iterator semantics for streams.
istream_iterator(istream_type &__s)
Construct start of input stream iterator.
constexpr istream_iterator() noexcept(/*conditional */)
Construct end of input stream iterator.
friend bool operator==(const istream_iterator &__x, const istream_iterator &__y) noexcept
Return true if the iterators refer to the same stream, or are both at end-of-stream.
Provides output iterator semantics for streams.
_CharT char_type
Public typedef.
ostream_iterator & operator=(const _Tp &__value)
Writes value to underlying ostream using operator<<. If constructed with delimiter string,...
ostream_iterator(ostream_type &__s) noexcept
Construct from an ostream.
ostream_iterator(ostream_type &__s, const _CharT *__c) noexcept
_Traits traits_type
Public typedef.
ptrdiff_t difference_type
Public typedef.
basic_ostream< _CharT, _Traits > ostream_type
Public typedef.
ostream_iterator(const ostream_iterator &__obj) noexcept
Copy constructor.