3 // Copyright (C) 2001-2023 Free Software Foundation, Inc.
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)
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.
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.
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/>.
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}
30 #ifndef _STREAM_ITERATOR_H
31 #define _STREAM_ITERATOR_H 1
33 #pragma GCC system_header
36 #include <bits/move.h>
37 #include <bits/stl_iterator_base_types.h>
38 #include <debug/debug.h>
40 namespace std
_GLIBCXX_VISIBILITY(default)
42 _GLIBCXX_BEGIN_NAMESPACE_VERSION
45 * @addtogroup iterators
49 // Ignore warnings about std::iterator.
50 #pragma GCC diagnostic push
51 #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
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
&>
60 typedef _CharT char_type
;
61 typedef _Traits traits_type
;
62 typedef basic_istream
<_CharT
, _Traits
> istream_type
;
65 istream_type
* _M_stream
;
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.
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) {}
78 /// Construct start of input stream iterator.
79 istream_iterator(istream_type
& __s
)
80 : _M_stream(std::__addressof(__s
)), _M_ok(true)
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
),
89 #if __cplusplus > 201703L && __cpp_lib_concepts
91 istream_iterator(default_sentinel_t
)
92 noexcept(is_nothrow_default_constructible_v
<_Tp
>)
93 : istream_iterator() { }
96 #if __cplusplus >= 201103L
97 istream_iterator
& operator=(const istream_iterator
&) = default;
98 ~istream_iterator() = default;
103 operator*() const _GLIBCXX_NOEXCEPT
105 __glibcxx_requires_cond(_M_ok
,
106 _M_message(__gnu_debug::__msg_deref_istream
)
107 ._M_iterator(*this));
113 operator->() const _GLIBCXX_NOEXCEPT
114 { return std::__addressof((operator*())); }
119 __glibcxx_requires_cond(_M_ok
,
120 _M_message(__gnu_debug::__msg_inc_istream
)
121 ._M_iterator(*this));
129 __glibcxx_requires_cond(_M_ok
,
130 _M_message(__gnu_debug::__msg_inc_istream
)
131 ._M_iterator(*this));
132 istream_iterator __tmp
= *this;
139 _M_equal(const istream_iterator
& __x
) const _GLIBCXX_NOEXCEPT
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
);
149 if (_M_stream
&& !(*_M_stream
>> _M_value
))
156 /// Return true if the iterators refer to the same stream,
157 /// or are both at end-of-stream.
160 operator==(const istream_iterator
& __x
, const istream_iterator
& __y
)
162 { return __x
._M_equal(__y
); }
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.
169 operator!=(const istream_iterator
& __x
, const istream_iterator
& __y
)
171 { return !__x
._M_equal(__y
); }
174 #if __cplusplus > 201703L && __cpp_lib_concepts
177 operator==(const istream_iterator
& __i
, default_sentinel_t
) noexcept
178 { return !__i
._M_stream
; }
183 * @brief Provides output iterator semantics for streams.
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.
189 * @tparam _Tp The type to write to the ostream.
190 * @tparam _CharT The ostream char_type.
191 * @tparam _Traits The ostream char_traits.
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>
201 #if __cplusplus > 201703L
202 using difference_type
= ptrdiff_t;
204 typedef _CharT char_type
;
205 typedef _Traits traits_type
;
206 typedef basic_ostream
<_CharT
, _Traits
> ostream_type
;
210 ostream_type
* _M_stream
;
211 const _CharT
* _M_string
;
214 /// Construct from an ostream.
215 ostream_iterator(ostream_type
& __s
) _GLIBCXX_NOEXCEPT
216 : _M_stream(std::__addressof(__s
)), _M_string(0) {}
219 * Construct from an ostream.
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.
225 * @param __s Underlying ostream to write to.
226 * @param __c CharT delimiter string to insert.
228 ostream_iterator(ostream_type
& __s
, const _CharT
* __c
) _GLIBCXX_NOEXCEPT
229 : _M_stream(std::__addressof(__s
)), _M_string(__c
) { }
231 /// Copy constructor.
232 ostream_iterator(const ostream_iterator
& __obj
) _GLIBCXX_NOEXCEPT
233 : _M_stream(__obj
._M_stream
), _M_string(__obj
._M_string
) { }
235 #if __cplusplus >= 201103L
236 ostream_iterator
& operator=(const ostream_iterator
&) = default;
239 /// Writes @a value to underlying ostream using operator<<. If
240 /// constructed with delimiter string, writes delimiter to ostream.
242 operator=(const _Tp
& __value
)
244 __glibcxx_requires_cond(_M_stream
!= 0,
245 _M_message(__gnu_debug::__msg_output_ostream
)
246 ._M_iterator(*this));
247 *_M_stream
<< __value
;
249 *_M_stream
<< _M_string
;
255 operator*() _GLIBCXX_NOEXCEPT
259 operator++() _GLIBCXX_NOEXCEPT
263 operator++(int) _GLIBCXX_NOEXCEPT
266 #pragma GCC diagnostic pop
268 /// @} group iterators
270 _GLIBCXX_END_NAMESPACE_VERSION