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