]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/stream_iterator.h
libstdc++: Fix noexcept-specifier for istream_iterator
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stream_iterator.h
CommitLineData
d27bba5e
BK
1// Stream iterators
2
8d9254fc 3// Copyright (C) 2001-2020 Free Software Foundation, Inc.
d27bba5e
BK
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
748086b7 8// Free Software Foundation; either version 3, or (at your option)
d27bba5e
BK
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
748086b7
JJ
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.
d27bba5e 19
748086b7
JJ
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/>.
d27bba5e 24
f910786b 25/** @file bits/stream_iterator.h
729e3d3f 26 * This is an internal header file, included by other library headers.
f910786b 27 * Do not attempt to use it directly. @headername{iterator}
729e3d3f
PE
28 */
29
3d7c150e
BK
30#ifndef _STREAM_ITERATOR_H
31#define _STREAM_ITERATOR_H 1
d27bba5e
BK
32
33#pragma GCC system_header
34
285b36d6
BK
35#include <debug/debug.h>
36
12ffa228
BK
37namespace std _GLIBCXX_VISIBILITY(default)
38{
39_GLIBCXX_BEGIN_NAMESPACE_VERSION
3cbc7af0 40
8e32aa11
BK
41 /**
42 * @addtogroup iterators
43 * @{
44 */
45
ffcec5c8 46 /// Provides input iterator semantics for streams.
ed6814f7
BI
47 template<typename _Tp, typename _CharT = char,
48 typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
49 class istream_iterator
15d72060 50 : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
d27bba5e
BK
51 {
52 public:
53 typedef _CharT char_type;
54 typedef _Traits traits_type;
55 typedef basic_istream<_CharT, _Traits> istream_type;
56
57 private:
ed6814f7
BI
58 istream_type* _M_stream;
59 _Tp _M_value;
638ad333
JW
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.
ed6814f7 63 bool _M_ok;
d27bba5e 64
ffcec5c8
JQ
65 public:
66 /// Construct end of input stream iterator.
94a86be0 67 _GLIBCXX_CONSTEXPR istream_iterator()
8b5bc374 68 : _M_stream(0), _M_value(), _M_ok(false) {}
75bef434 69
ffcec5c8 70 /// Construct start of input stream iterator.
15d72060 71 istream_iterator(istream_type& __s)
638ad333 72 : _M_stream(std::__addressof(__s)), _M_ok(true)
15d72060 73 { _M_read(); }
d27bba5e 74
ed6814f7
BI
75 istream_iterator(const istream_iterator& __obj)
76 : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
77 _M_ok(__obj._M_ok)
75bef434
BK
78 { }
79
120e8734
JW
80#if __cplusplus > 201703L
81 constexpr
8566286e
JW
82 istream_iterator(default_sentinel_t)
83 noexcept(is_nothrow_default_constructible_v<_Tp>)
120e8734
JW
84 : istream_iterator() { }
85#endif
86
a1417556
JW
87#if __cplusplus >= 201103L
88 istream_iterator& operator=(const istream_iterator&) = default;
638ad333 89 ~istream_iterator() = default;
a1417556
JW
90#endif
91
d27bba5e 92 const _Tp&
ed6814f7
BI
93 operator*() const
94 {
285b36d6
BK
95 __glibcxx_requires_cond(_M_ok,
96 _M_message(__gnu_debug::__msg_deref_istream)
97 ._M_iterator(*this));
98 return _M_value;
99 }
d27bba5e
BK
100
101 const _Tp*
9f9eb84e 102 operator->() const { return std::__addressof((operator*())); }
d27bba5e 103
ed6814f7
BI
104 istream_iterator&
105 operator++()
106 {
285b36d6
BK
107 __glibcxx_requires_cond(_M_ok,
108 _M_message(__gnu_debug::__msg_inc_istream)
109 ._M_iterator(*this));
ed6814f7
BI
110 _M_read();
111 return *this;
285b36d6 112 }
d27bba5e 113
ed6814f7
BI
114 istream_iterator
115 operator++(int)
d27bba5e 116 {
285b36d6
BK
117 __glibcxx_requires_cond(_M_ok,
118 _M_message(__gnu_debug::__msg_inc_istream)
ed6814f7 119 ._M_iterator(*this));
d27bba5e
BK
120 istream_iterator __tmp = *this;
121 _M_read();
122 return __tmp;
123 }
124
638ad333 125 private:
ed6814f7 126 bool
d27bba5e 127 _M_equal(const istream_iterator& __x) const
638ad333
JW
128 {
129 // Ideally this would just return _M_stream == __x._M_stream,
130 // but code compiled with old versions never sets _M_stream to null.
131 return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream);
132 }
d27bba5e 133
ed6814f7
BI
134 void
135 _M_read()
d27bba5e 136 {
638ad333
JW
137 if (_M_stream && !(*_M_stream >> _M_value))
138 {
139 _M_stream = 0;
140 _M_ok = false;
141 }
d27bba5e 142 }
ed6814f7 143
638ad333
JW
144 /// Return true if the iterators refer to the same stream,
145 /// or are both at end-of-stream.
146 friend bool
147 operator==(const istream_iterator& __x, const istream_iterator& __y)
148 { return __x._M_equal(__y); }
149
150 /// Return true if the iterators refer to different streams,
151 /// or if one is at end-of-stream and the other is not.
152 friend bool
153 operator!=(const istream_iterator& __x, const istream_iterator& __y)
154 { return !__x._M_equal(__y); }
120e8734
JW
155
156#if __cplusplus > 201703L
157 friend bool
158 operator==(const istream_iterator& __i, default_sentinel_t)
159 { return !__i._M_stream; }
160#endif
638ad333 161 };
d27bba5e 162
ffcec5c8
JQ
163 /**
164 * @brief Provides output iterator semantics for streams.
165 *
166 * This class provides an iterator to write to an ostream. The type Tp is
167 * the only type written by this iterator and there must be an
168 * operator<<(Tp) defined.
169 *
93c66bc6
BK
170 * @tparam _Tp The type to write to the ostream.
171 * @tparam _CharT The ostream char_type.
172 * @tparam _Traits The ostream char_traits.
ffcec5c8 173 */
ed6814f7 174 template<typename _Tp, typename _CharT = char,
d27bba5e 175 typename _Traits = char_traits<_CharT> >
ed6814f7 176 class ostream_iterator
15d72060 177 : public iterator<output_iterator_tag, void, void, void, void>
d27bba5e
BK
178 {
179 public:
ffcec5c8
JQ
180 //@{
181 /// Public typedef
120e8734
JW
182#if __cplusplus > 201703L
183 using difference_type = ptrdiff_t;
184#endif
d27bba5e
BK
185 typedef _CharT char_type;
186 typedef _Traits traits_type;
187 typedef basic_ostream<_CharT, _Traits> ostream_type;
ffcec5c8 188 //@}
d27bba5e
BK
189
190 private:
ed6814f7
BI
191 ostream_type* _M_stream;
192 const _CharT* _M_string;
d27bba5e
BK
193
194 public:
638ad333
JW
195#if __cplusplus > 201703L
196 constexpr ostream_iterator() noexcept
197 : _M_stream(nullptr), _M_string(nullptr) { }
198#endif
199
ffcec5c8 200 /// Construct from an ostream.
9f9eb84e
JW
201 ostream_iterator(ostream_type& __s)
202 : _M_stream(std::__addressof(__s)), _M_string(0) {}
75bef434 203
ffcec5c8
JQ
204 /**
205 * Construct from an ostream.
206 *
207 * The delimiter string @a c is written to the stream after every Tp
208 * written to the stream. The delimiter is not copied, and thus must
209 * not be destroyed while this iterator is in use.
210 *
93c66bc6
BK
211 * @param __s Underlying ostream to write to.
212 * @param __c CharT delimiter string to insert.
ffcec5c8 213 */
ed6814f7 214 ostream_iterator(ostream_type& __s, const _CharT* __c)
638ad333 215 : _M_stream(std::__addressof(__s)), _M_string(__c) { }
75bef434 216
ffcec5c8 217 /// Copy constructor.
75bef434
BK
218 ostream_iterator(const ostream_iterator& __obj)
219 : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
d27bba5e 220
a1417556
JW
221#if __cplusplus >= 201103L
222 ostream_iterator& operator=(const ostream_iterator&) = default;
223#endif
224
ffcec5c8
JQ
225 /// Writes @a value to underlying ostream using operator<<. If
226 /// constructed with delimiter string, writes delimiter to ostream.
ed6814f7
BI
227 ostream_iterator&
228 operator=(const _Tp& __value)
229 {
285b36d6
BK
230 __glibcxx_requires_cond(_M_stream != 0,
231 _M_message(__gnu_debug::__msg_output_ostream)
232 ._M_iterator(*this));
d27bba5e 233 *_M_stream << __value;
638ad333
JW
234 if (_M_string)
235 *_M_stream << _M_string;
d27bba5e
BK
236 return *this;
237 }
ed6814f7
BI
238
239 ostream_iterator&
15d72060
PC
240 operator*()
241 { return *this; }
ed6814f7
BI
242
243 ostream_iterator&
15d72060 244 operator++()
ed6814f7
BI
245 { return *this; }
246
247 ostream_iterator&
15d72060 248 operator++(int)
ed6814f7 249 { return *this; }
d27bba5e 250 };
3cbc7af0 251
8e32aa11
BK
252 // @} group iterators
253
12ffa228
BK
254_GLIBCXX_END_NAMESPACE_VERSION
255} // namespace
3cbc7af0 256
d27bba5e 257#endif