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