libstdc++
stream_iterator.h
Go to the documentation of this file.
1 // Stream iterators
2 
3 // Copyright (C) 2001-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/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  : _M_stream(0), _M_value(), _M_ok(false) {}
69 
70  /// Construct start of input stream iterator.
72  : _M_stream(std::__addressof(__s)), _M_ok(true)
73  { _M_read(); }
74 
76  : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
77  _M_ok(__obj._M_ok)
78  { }
79 
80 #if __cplusplus >= 201103L
81  istream_iterator& operator=(const istream_iterator&) = default;
82  ~istream_iterator() = default;
83 #endif
84 
85  const _Tp&
86  operator*() const
87  {
88  __glibcxx_requires_cond(_M_ok,
89  _M_message(__gnu_debug::__msg_deref_istream)
90  ._M_iterator(*this));
91  return _M_value;
92  }
93 
94  const _Tp*
95  operator->() const { return std::__addressof((operator*())); }
96 
98  operator++()
99  {
100  __glibcxx_requires_cond(_M_ok,
101  _M_message(__gnu_debug::__msg_inc_istream)
102  ._M_iterator(*this));
103  _M_read();
104  return *this;
105  }
106 
108  operator++(int)
109  {
110  __glibcxx_requires_cond(_M_ok,
111  _M_message(__gnu_debug::__msg_inc_istream)
112  ._M_iterator(*this));
113  istream_iterator __tmp = *this;
114  _M_read();
115  return __tmp;
116  }
117 
118  private:
119  bool
120  _M_equal(const istream_iterator& __x) const
121  {
122  // Ideally this would just return _M_stream == __x._M_stream,
123  // but code compiled with old versions never sets _M_stream to null.
124  return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream);
125  }
126 
127  void
128  _M_read()
129  {
130  if (_M_stream && !(*_M_stream >> _M_value))
131  {
132  _M_stream = 0;
133  _M_ok = false;
134  }
135  }
136 
137  /// Return true if the iterators refer to the same stream,
138  /// or are both at end-of-stream.
139  friend bool
141  { return __x._M_equal(__y); }
142 
143  /// Return true if the iterators refer to different streams,
144  /// or if one is at end-of-stream and the other is not.
145  friend bool
147  { return !__x._M_equal(__y); }
148  };
149 
150  /**
151  * @brief Provides output iterator semantics for streams.
152  *
153  * This class provides an iterator to write to an ostream. The type Tp is
154  * the only type written by this iterator and there must be an
155  * operator<<(Tp) defined.
156  *
157  * @tparam _Tp The type to write to the ostream.
158  * @tparam _CharT The ostream char_type.
159  * @tparam _Traits The ostream char_traits.
160  */
161  template<typename _Tp, typename _CharT = char,
162  typename _Traits = char_traits<_CharT> >
164  : public iterator<output_iterator_tag, void, void, void, void>
165  {
166  public:
167  //@{
168  /// Public typedef
169  typedef _CharT char_type;
170  typedef _Traits traits_type;
172  //@}
173 
174  private:
175  ostream_type* _M_stream;
176  const _CharT* _M_string;
177 
178  public:
179 #if __cplusplus > 201703L
180  constexpr ostream_iterator() noexcept
181  : _M_stream(nullptr), _M_string(nullptr) { }
182 #endif
183 
184  /// Construct from an ostream.
186  : _M_stream(std::__addressof(__s)), _M_string(0) {}
187 
188  /**
189  * Construct from an ostream.
190  *
191  * The delimiter string @a c is written to the stream after every Tp
192  * written to the stream. The delimiter is not copied, and thus must
193  * not be destroyed while this iterator is in use.
194  *
195  * @param __s Underlying ostream to write to.
196  * @param __c CharT delimiter string to insert.
197  */
198  ostream_iterator(ostream_type& __s, const _CharT* __c)
199  : _M_stream(std::__addressof(__s)), _M_string(__c) { }
200 
201  /// Copy constructor.
203  : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
204 
205 #if __cplusplus >= 201103L
206  ostream_iterator& operator=(const ostream_iterator&) = default;
207 #endif
208 
209  /// Writes @a value to underlying ostream using operator<<. If
210  /// constructed with delimiter string, writes delimiter to ostream.
212  operator=(const _Tp& __value)
213  {
214  __glibcxx_requires_cond(_M_stream != 0,
215  _M_message(__gnu_debug::__msg_output_ostream)
216  ._M_iterator(*this));
217  *_M_stream << __value;
218  if (_M_string)
219  *_M_stream << _M_string;
220  return *this;
221  }
222 
224  operator*()
225  { return *this; }
226 
228  operator++()
229  { return *this; }
230 
232  operator++(int)
233  { return *this; }
234  };
235 
236  // @} group iterators
237 
238 _GLIBCXX_END_NAMESPACE_VERSION
239 } // namespace
240 
241 #endif
std::__addressof
constexpr _Tp * __addressof(_Tp &__r) noexcept
Same as C++11 std::addressof.
Definition: move.h:49
std::ostream_iterator::ostream_iterator
ostream_iterator(ostream_type &__s)
Construct from an ostream.
Definition: stream_iterator.h:185
std::ostream_iterator::ostream_iterator
ostream_iterator(ostream_type &__s, const _CharT *__c)
Definition: stream_iterator.h:198
std::ostream_iterator::operator=
ostream_iterator & operator=(const _Tp &__value)
Writes value to underlying ostream using operator<<. If constructed with delimiter string,...
Definition: stream_iterator.h:212
std::ostream_iterator::char_type
_CharT char_type
Public typedef.
Definition: stream_iterator.h:169
std::ostream_iterator::ostream_iterator
ostream_iterator(const ostream_iterator &__obj)
Copy constructor.
Definition: stream_iterator.h:202
std
ISO C++ entities toplevel namespace is std.
debug.h
std::iterator
Common iterator class.
Definition: stl_iterator_base_types.h:127
std::istream_iterator::operator!=
friend bool operator!=(const istream_iterator &__x, const istream_iterator &__y)
Return true if the iterators refer to different streams, or if one is at end-of-stream and the other ...
Definition: stream_iterator.h:146
std::istream_iterator::istream_iterator
istream_iterator(istream_type &__s)
Construct start of input stream iterator.
Definition: stream_iterator.h:71
std::ostream_iterator
Provides output iterator semantics for streams.
Definition: stream_iterator.h:163
std::basic_ostream
Template class basic_ostream.
Definition: iosfwd:86
std::istream_iterator
Provides input iterator semantics for streams.
Definition: stream_iterator.h:49
std::istream_iterator::istream_iterator
constexpr istream_iterator()
Construct end of input stream iterator.
Definition: stream_iterator.h:67
std::istream_iterator::operator==
friend bool operator==(const istream_iterator &__x, const istream_iterator &__y)
Return true if the iterators refer to the same stream, or are both at end-of-stream.
Definition: stream_iterator.h:140
std::basic_istream
Template class basic_istream.
Definition: iosfwd:83