]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/stream_iterator.h
Update copyright years.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / stream_iterator.h
1 // Stream iterators
2
3 // Copyright (C) 2001-2023 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 <iosfwd>
36 #include <bits/move.h>
37 #include <bits/stl_iterator_base_types.h>
38 #include <debug/debug.h>
39
40 namespace std _GLIBCXX_VISIBILITY(default)
41 {
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
43
44 /**
45 * @addtogroup iterators
46 * @{
47 */
48
49 // Ignore warnings about std::iterator.
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
52
53 /// Provides input iterator semantics for streams.
54 template<typename _Tp, typename _CharT = char,
55 typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
56 class istream_iterator
57 : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
58 {
59 public:
60 typedef _CharT char_type;
61 typedef _Traits traits_type;
62 typedef basic_istream<_CharT, _Traits> istream_type;
63
64 private:
65 istream_type* _M_stream;
66 _Tp _M_value;
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.
70 bool _M_ok;
71
72 public:
73 /// Construct end of input stream iterator.
74 _GLIBCXX_CONSTEXPR istream_iterator()
75 _GLIBCXX_NOEXCEPT_IF(is_nothrow_default_constructible<_Tp>::value)
76 : _M_stream(0), _M_value(), _M_ok(false) {}
77
78 /// Construct start of input stream iterator.
79 istream_iterator(istream_type& __s)
80 : _M_stream(std::__addressof(__s)), _M_ok(true)
81 { _M_read(); }
82
83 istream_iterator(const istream_iterator& __obj)
84 _GLIBCXX_NOEXCEPT_IF(is_nothrow_copy_constructible<_Tp>::value)
85 : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
86 _M_ok(__obj._M_ok)
87 { }
88
89 #if __cplusplus > 201703L && __cpp_lib_concepts
90 constexpr
91 istream_iterator(default_sentinel_t)
92 noexcept(is_nothrow_default_constructible_v<_Tp>)
93 : istream_iterator() { }
94 #endif
95
96 #if __cplusplus >= 201103L
97 istream_iterator& operator=(const istream_iterator&) = default;
98 ~istream_iterator() = default;
99 #endif
100
101 _GLIBCXX_NODISCARD
102 const _Tp&
103 operator*() const _GLIBCXX_NOEXCEPT
104 {
105 __glibcxx_requires_cond(_M_ok,
106 _M_message(__gnu_debug::__msg_deref_istream)
107 ._M_iterator(*this));
108 return _M_value;
109 }
110
111 _GLIBCXX_NODISCARD
112 const _Tp*
113 operator->() const _GLIBCXX_NOEXCEPT
114 { return std::__addressof((operator*())); }
115
116 istream_iterator&
117 operator++()
118 {
119 __glibcxx_requires_cond(_M_ok,
120 _M_message(__gnu_debug::__msg_inc_istream)
121 ._M_iterator(*this));
122 _M_read();
123 return *this;
124 }
125
126 istream_iterator
127 operator++(int)
128 {
129 __glibcxx_requires_cond(_M_ok,
130 _M_message(__gnu_debug::__msg_inc_istream)
131 ._M_iterator(*this));
132 istream_iterator __tmp = *this;
133 _M_read();
134 return __tmp;
135 }
136
137 private:
138 bool
139 _M_equal(const istream_iterator& __x) const _GLIBCXX_NOEXCEPT
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 }
145
146 void
147 _M_read()
148 {
149 if (_M_stream && !(*_M_stream >> _M_value))
150 {
151 _M_stream = 0;
152 _M_ok = false;
153 }
154 }
155
156 /// Return true if the iterators refer to the same stream,
157 /// or are both at end-of-stream.
158 _GLIBCXX_NODISCARD
159 friend bool
160 operator==(const istream_iterator& __x, const istream_iterator& __y)
161 _GLIBCXX_NOEXCEPT
162 { return __x._M_equal(__y); }
163
164 #if __cpp_impl_three_way_comparison < 201907L
165 /// Return true if the iterators refer to different streams,
166 /// or if one is at end-of-stream and the other is not.
167 _GLIBCXX_NODISCARD
168 friend bool
169 operator!=(const istream_iterator& __x, const istream_iterator& __y)
170 _GLIBCXX_NOEXCEPT
171 { return !__x._M_equal(__y); }
172 #endif
173
174 #if __cplusplus > 201703L && __cpp_lib_concepts
175 [[nodiscard]]
176 friend bool
177 operator==(const istream_iterator& __i, default_sentinel_t) noexcept
178 { return !__i._M_stream; }
179 #endif
180 };
181
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 *
189 * @tparam _Tp The type to write to the ostream.
190 * @tparam _CharT The ostream char_type.
191 * @tparam _Traits The ostream char_traits.
192 */
193 template<typename _Tp, typename _CharT = char,
194 typename _Traits = char_traits<_CharT> >
195 class ostream_iterator
196 : public iterator<output_iterator_tag, void, void, void, void>
197 {
198 public:
199 ///@{
200 /// Public typedef
201 #if __cplusplus > 201703L
202 using difference_type = ptrdiff_t;
203 #endif
204 typedef _CharT char_type;
205 typedef _Traits traits_type;
206 typedef basic_ostream<_CharT, _Traits> ostream_type;
207 ///@}
208
209 private:
210 ostream_type* _M_stream;
211 const _CharT* _M_string;
212
213 public:
214 /// Construct from an ostream.
215 ostream_iterator(ostream_type& __s) _GLIBCXX_NOEXCEPT
216 : _M_stream(std::__addressof(__s)), _M_string(0) {}
217
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 *
225 * @param __s Underlying ostream to write to.
226 * @param __c CharT delimiter string to insert.
227 */
228 ostream_iterator(ostream_type& __s, const _CharT* __c) _GLIBCXX_NOEXCEPT
229 : _M_stream(std::__addressof(__s)), _M_string(__c) { }
230
231 /// Copy constructor.
232 ostream_iterator(const ostream_iterator& __obj) _GLIBCXX_NOEXCEPT
233 : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
234
235 #if __cplusplus >= 201103L
236 ostream_iterator& operator=(const ostream_iterator&) = default;
237 #endif
238
239 /// Writes @a value to underlying ostream using operator<<. If
240 /// constructed with delimiter string, writes delimiter to ostream.
241 ostream_iterator&
242 operator=(const _Tp& __value)
243 {
244 __glibcxx_requires_cond(_M_stream != 0,
245 _M_message(__gnu_debug::__msg_output_ostream)
246 ._M_iterator(*this));
247 *_M_stream << __value;
248 if (_M_string)
249 *_M_stream << _M_string;
250 return *this;
251 }
252
253 _GLIBCXX_NODISCARD
254 ostream_iterator&
255 operator*() _GLIBCXX_NOEXCEPT
256 { return *this; }
257
258 ostream_iterator&
259 operator++() _GLIBCXX_NOEXCEPT
260 { return *this; }
261
262 ostream_iterator&
263 operator++(int) _GLIBCXX_NOEXCEPT
264 { return *this; }
265 };
266 #pragma GCC diagnostic pop
267
268 /// @} group iterators
269
270 _GLIBCXX_END_NAMESPACE_VERSION
271 } // namespace
272
273 #endif