]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/stream_iterator.h
libstdc++: Add attribute to features deprecated in C++17 [PR91260]
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stream_iterator.h
CommitLineData
d27bba5e
BK
1// Stream iterators
2
7adcbafe 3// Copyright (C) 2001-2022 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
de196e5d
JW
46// Ignore warnings about std::iterator.
47#pragma GCC diagnostic push
48#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
49
ffcec5c8 50 /// Provides input iterator semantics for streams.
ed6814f7
BI
51 template<typename _Tp, typename _CharT = char,
52 typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
53 class istream_iterator
15d72060 54 : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
d27bba5e
BK
55 {
56 public:
57 typedef _CharT char_type;
58 typedef _Traits traits_type;
59 typedef basic_istream<_CharT, _Traits> istream_type;
60
61 private:
ed6814f7
BI
62 istream_type* _M_stream;
63 _Tp _M_value;
638ad333
JW
64 // This bool becomes false at end-of-stream. It should be sufficient to
65 // check _M_stream != nullptr instead, but historically we did not set
66 // _M_stream to null when reaching the end, so we need to keep this flag.
ed6814f7 67 bool _M_ok;
d27bba5e 68
ffcec5c8
JQ
69 public:
70 /// Construct end of input stream iterator.
94a86be0 71 _GLIBCXX_CONSTEXPR istream_iterator()
901fa4cc 72 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Tp>::value)
8b5bc374 73 : _M_stream(0), _M_value(), _M_ok(false) {}
75bef434 74
ffcec5c8 75 /// Construct start of input stream iterator.
15d72060 76 istream_iterator(istream_type& __s)
638ad333 77 : _M_stream(std::__addressof(__s)), _M_ok(true)
15d72060 78 { _M_read(); }
d27bba5e 79
ed6814f7 80 istream_iterator(const istream_iterator& __obj)
901fa4cc 81 _GLIBCXX_NOEXCEPT_IF(is_nothrow_copy_constructible<_Tp>::value)
ed6814f7
BI
82 : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
83 _M_ok(__obj._M_ok)
75bef434
BK
84 { }
85
07522ae9 86#if __cplusplus > 201703L && __cpp_lib_concepts
120e8734 87 constexpr
8566286e
JW
88 istream_iterator(default_sentinel_t)
89 noexcept(is_nothrow_default_constructible_v<_Tp>)
120e8734
JW
90 : istream_iterator() { }
91#endif
92
a1417556
JW
93#if __cplusplus >= 201103L
94 istream_iterator& operator=(const istream_iterator&) = default;
638ad333 95 ~istream_iterator() = default;
a1417556
JW
96#endif
97
240b01b0 98 _GLIBCXX_NODISCARD
d27bba5e 99 const _Tp&
901fa4cc 100 operator*() const _GLIBCXX_NOEXCEPT
ed6814f7 101 {
285b36d6
BK
102 __glibcxx_requires_cond(_M_ok,
103 _M_message(__gnu_debug::__msg_deref_istream)
104 ._M_iterator(*this));
105 return _M_value;
106 }
d27bba5e 107
240b01b0 108 _GLIBCXX_NODISCARD
d27bba5e 109 const _Tp*
901fa4cc
JW
110 operator->() const _GLIBCXX_NOEXCEPT
111 { return std::__addressof((operator*())); }
d27bba5e 112
ed6814f7
BI
113 istream_iterator&
114 operator++()
115 {
285b36d6
BK
116 __glibcxx_requires_cond(_M_ok,
117 _M_message(__gnu_debug::__msg_inc_istream)
118 ._M_iterator(*this));
ed6814f7
BI
119 _M_read();
120 return *this;
285b36d6 121 }
d27bba5e 122
ed6814f7
BI
123 istream_iterator
124 operator++(int)
d27bba5e 125 {
285b36d6
BK
126 __glibcxx_requires_cond(_M_ok,
127 _M_message(__gnu_debug::__msg_inc_istream)
ed6814f7 128 ._M_iterator(*this));
d27bba5e
BK
129 istream_iterator __tmp = *this;
130 _M_read();
131 return __tmp;
132 }
133
638ad333 134 private:
ed6814f7 135 bool
901fa4cc 136 _M_equal(const istream_iterator& __x) const _GLIBCXX_NOEXCEPT
638ad333
JW
137 {
138 // Ideally this would just return _M_stream == __x._M_stream,
139 // but code compiled with old versions never sets _M_stream to null.
140 return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream);
141 }
d27bba5e 142
ed6814f7
BI
143 void
144 _M_read()
d27bba5e 145 {
638ad333
JW
146 if (_M_stream && !(*_M_stream >> _M_value))
147 {
148 _M_stream = 0;
149 _M_ok = false;
150 }
d27bba5e 151 }
ed6814f7 152
638ad333
JW
153 /// Return true if the iterators refer to the same stream,
154 /// or are both at end-of-stream.
240b01b0 155 _GLIBCXX_NODISCARD
638ad333
JW
156 friend bool
157 operator==(const istream_iterator& __x, const istream_iterator& __y)
901fa4cc 158 _GLIBCXX_NOEXCEPT
638ad333
JW
159 { return __x._M_equal(__y); }
160
240b01b0 161#if __cpp_impl_three_way_comparison < 201907L
638ad333
JW
162 /// Return true if the iterators refer to different streams,
163 /// or if one is at end-of-stream and the other is not.
240b01b0 164 _GLIBCXX_NODISCARD
638ad333
JW
165 friend bool
166 operator!=(const istream_iterator& __x, const istream_iterator& __y)
901fa4cc 167 _GLIBCXX_NOEXCEPT
638ad333 168 { return !__x._M_equal(__y); }
240b01b0 169#endif
120e8734 170
07522ae9 171#if __cplusplus > 201703L && __cpp_lib_concepts
240b01b0 172 [[nodiscard]]
120e8734 173 friend bool
901fa4cc 174 operator==(const istream_iterator& __i, default_sentinel_t) noexcept
120e8734
JW
175 { return !__i._M_stream; }
176#endif
638ad333 177 };
d27bba5e 178
ffcec5c8
JQ
179 /**
180 * @brief Provides output iterator semantics for streams.
181 *
182 * This class provides an iterator to write to an ostream. The type Tp is
183 * the only type written by this iterator and there must be an
184 * operator<<(Tp) defined.
185 *
93c66bc6
BK
186 * @tparam _Tp The type to write to the ostream.
187 * @tparam _CharT The ostream char_type.
188 * @tparam _Traits The ostream char_traits.
ffcec5c8 189 */
ed6814f7 190 template<typename _Tp, typename _CharT = char,
d27bba5e 191 typename _Traits = char_traits<_CharT> >
ed6814f7 192 class ostream_iterator
15d72060 193 : public iterator<output_iterator_tag, void, void, void, void>
d27bba5e
BK
194 {
195 public:
f0b88346 196 ///@{
ffcec5c8 197 /// Public typedef
120e8734
JW
198#if __cplusplus > 201703L
199 using difference_type = ptrdiff_t;
200#endif
d27bba5e
BK
201 typedef _CharT char_type;
202 typedef _Traits traits_type;
203 typedef basic_ostream<_CharT, _Traits> ostream_type;
f0b88346 204 ///@}
d27bba5e
BK
205
206 private:
ed6814f7
BI
207 ostream_type* _M_stream;
208 const _CharT* _M_string;
d27bba5e
BK
209
210 public:
ffcec5c8 211 /// Construct from an ostream.
901fa4cc 212 ostream_iterator(ostream_type& __s) _GLIBCXX_NOEXCEPT
9f9eb84e 213 : _M_stream(std::__addressof(__s)), _M_string(0) {}
75bef434 214
ffcec5c8
JQ
215 /**
216 * Construct from an ostream.
217 *
218 * The delimiter string @a c is written to the stream after every Tp
219 * written to the stream. The delimiter is not copied, and thus must
220 * not be destroyed while this iterator is in use.
221 *
93c66bc6
BK
222 * @param __s Underlying ostream to write to.
223 * @param __c CharT delimiter string to insert.
ffcec5c8 224 */
901fa4cc 225 ostream_iterator(ostream_type& __s, const _CharT* __c) _GLIBCXX_NOEXCEPT
638ad333 226 : _M_stream(std::__addressof(__s)), _M_string(__c) { }
75bef434 227
ffcec5c8 228 /// Copy constructor.
901fa4cc 229 ostream_iterator(const ostream_iterator& __obj) _GLIBCXX_NOEXCEPT
75bef434 230 : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
d27bba5e 231
a1417556
JW
232#if __cplusplus >= 201103L
233 ostream_iterator& operator=(const ostream_iterator&) = default;
234#endif
235
ffcec5c8
JQ
236 /// Writes @a value to underlying ostream using operator<<. If
237 /// constructed with delimiter string, writes delimiter to ostream.
ed6814f7
BI
238 ostream_iterator&
239 operator=(const _Tp& __value)
240 {
285b36d6
BK
241 __glibcxx_requires_cond(_M_stream != 0,
242 _M_message(__gnu_debug::__msg_output_ostream)
243 ._M_iterator(*this));
d27bba5e 244 *_M_stream << __value;
638ad333
JW
245 if (_M_string)
246 *_M_stream << _M_string;
d27bba5e
BK
247 return *this;
248 }
ed6814f7 249
240b01b0 250 _GLIBCXX_NODISCARD
ed6814f7 251 ostream_iterator&
901fa4cc 252 operator*() _GLIBCXX_NOEXCEPT
15d72060 253 { return *this; }
ed6814f7
BI
254
255 ostream_iterator&
901fa4cc 256 operator++() _GLIBCXX_NOEXCEPT
ed6814f7
BI
257 { return *this; }
258
259 ostream_iterator&
901fa4cc 260 operator++(int) _GLIBCXX_NOEXCEPT
ed6814f7 261 { return *this; }
d27bba5e 262 };
de196e5d 263#pragma GCC diagnostic pop
3cbc7af0 264
f0b88346 265 /// @} group iterators
8e32aa11 266
12ffa228
BK
267_GLIBCXX_END_NAMESPACE_VERSION
268} // namespace
3cbc7af0 269
d27bba5e 270#endif