]>
Commit | Line | Data |
---|---|---|
ec57e686 | 1 | // Stream iterators |
2 | ||
fbd26352 | 3 | // Copyright (C) 2001-2019 Free Software Foundation, Inc. |
ec57e686 | 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 | |
6bc9506f | 8 | // Free Software Foundation; either version 3, or (at your option) |
ec57e686 | 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 | ||
6bc9506f | 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. | |
ec57e686 | 19 | |
6bc9506f | 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/>. | |
ec57e686 | 24 | |
5846aeac | 25 | /** @file bits/stream_iterator.h |
7a472ef1 | 26 | * This is an internal header file, included by other library headers. |
5846aeac | 27 | * Do not attempt to use it directly. @headername{iterator} |
7a472ef1 | 28 | */ |
29 | ||
5a64d8cf | 30 | #ifndef _STREAM_ITERATOR_H |
31 | #define _STREAM_ITERATOR_H 1 | |
ec57e686 | 32 | |
33 | #pragma GCC system_header | |
34 | ||
d570f2e9 | 35 | #include <debug/debug.h> |
36 | ||
2948dd21 | 37 | namespace std _GLIBCXX_VISIBILITY(default) |
38 | { | |
39 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
1069247d | 40 | |
73337dee | 41 | /** |
42 | * @addtogroup iterators | |
43 | * @{ | |
44 | */ | |
45 | ||
ed73ad37 | 46 | /// Provides input iterator semantics for streams. |
bae9b8af | 47 | template<typename _Tp, typename _CharT = char, |
48 | typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t> | |
49 | class istream_iterator | |
713a6b5f | 50 | : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&> |
ec57e686 | 51 | { |
52 | public: | |
53 | typedef _CharT char_type; | |
54 | typedef _Traits traits_type; | |
55 | typedef basic_istream<_CharT, _Traits> istream_type; | |
56 | ||
57 | private: | |
bae9b8af | 58 | istream_type* _M_stream; |
59 | _Tp _M_value; | |
60 | bool _M_ok; | |
ec57e686 | 61 | |
ed73ad37 | 62 | public: |
63 | /// Construct end of input stream iterator. | |
c0000147 | 64 | _GLIBCXX_CONSTEXPR istream_iterator() |
16171c51 | 65 | : _M_stream(0), _M_value(), _M_ok(false) {} |
b128dd19 | 66 | |
ed73ad37 | 67 | /// Construct start of input stream iterator. |
713a6b5f | 68 | istream_iterator(istream_type& __s) |
8e3a3e94 | 69 | : _M_stream(std::__addressof(__s)) |
713a6b5f | 70 | { _M_read(); } |
ec57e686 | 71 | |
bae9b8af | 72 | istream_iterator(const istream_iterator& __obj) |
73 | : _M_stream(__obj._M_stream), _M_value(__obj._M_value), | |
74 | _M_ok(__obj._M_ok) | |
b128dd19 | 75 | { } |
76 | ||
16f1a26a | 77 | #if __cplusplus >= 201103L |
78 | istream_iterator& operator=(const istream_iterator&) = default; | |
79 | #endif | |
80 | ||
ec57e686 | 81 | const _Tp& |
bae9b8af | 82 | operator*() const |
83 | { | |
d570f2e9 | 84 | __glibcxx_requires_cond(_M_ok, |
85 | _M_message(__gnu_debug::__msg_deref_istream) | |
86 | ._M_iterator(*this)); | |
87 | return _M_value; | |
88 | } | |
ec57e686 | 89 | |
90 | const _Tp* | |
8e3a3e94 | 91 | operator->() const { return std::__addressof((operator*())); } |
ec57e686 | 92 | |
bae9b8af | 93 | istream_iterator& |
94 | operator++() | |
95 | { | |
d570f2e9 | 96 | __glibcxx_requires_cond(_M_ok, |
97 | _M_message(__gnu_debug::__msg_inc_istream) | |
98 | ._M_iterator(*this)); | |
bae9b8af | 99 | _M_read(); |
100 | return *this; | |
d570f2e9 | 101 | } |
ec57e686 | 102 | |
bae9b8af | 103 | istream_iterator |
104 | operator++(int) | |
ec57e686 | 105 | { |
d570f2e9 | 106 | __glibcxx_requires_cond(_M_ok, |
107 | _M_message(__gnu_debug::__msg_inc_istream) | |
bae9b8af | 108 | ._M_iterator(*this)); |
ec57e686 | 109 | istream_iterator __tmp = *this; |
110 | _M_read(); | |
111 | return __tmp; | |
112 | } | |
113 | ||
bae9b8af | 114 | bool |
ec57e686 | 115 | _M_equal(const istream_iterator& __x) const |
713a6b5f | 116 | { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); } |
ec57e686 | 117 | |
bae9b8af | 118 | private: |
119 | void | |
120 | _M_read() | |
ec57e686 | 121 | { |
122 | _M_ok = (_M_stream && *_M_stream) ? true : false; | |
713a6b5f | 123 | if (_M_ok) |
ec57e686 | 124 | { |
125 | *_M_stream >> _M_value; | |
126 | _M_ok = *_M_stream ? true : false; | |
127 | } | |
128 | } | |
129 | }; | |
bae9b8af | 130 | |
ed73ad37 | 131 | /// Return true if x and y are both end or not end, or x and y are the same. |
ec57e686 | 132 | template<typename _Tp, typename _CharT, typename _Traits, typename _Dist> |
bae9b8af | 133 | inline bool |
ec57e686 | 134 | operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x, |
bae9b8af | 135 | const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) |
ec57e686 | 136 | { return __x._M_equal(__y); } |
137 | ||
ed73ad37 | 138 | /// Return false if x and y are both end or not end, or x and y are the same. |
ec57e686 | 139 | template <class _Tp, class _CharT, class _Traits, class _Dist> |
bae9b8af | 140 | inline bool |
b128dd19 | 141 | operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x, |
bae9b8af | 142 | const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) |
b128dd19 | 143 | { return !__x._M_equal(__y); } |
ec57e686 | 144 | |
ed73ad37 | 145 | /** |
146 | * @brief Provides output iterator semantics for streams. | |
147 | * | |
148 | * This class provides an iterator to write to an ostream. The type Tp is | |
149 | * the only type written by this iterator and there must be an | |
150 | * operator<<(Tp) defined. | |
151 | * | |
e12e4f3b | 152 | * @tparam _Tp The type to write to the ostream. |
153 | * @tparam _CharT The ostream char_type. | |
154 | * @tparam _Traits The ostream char_traits. | |
ed73ad37 | 155 | */ |
bae9b8af | 156 | template<typename _Tp, typename _CharT = char, |
ec57e686 | 157 | typename _Traits = char_traits<_CharT> > |
bae9b8af | 158 | class ostream_iterator |
713a6b5f | 159 | : public iterator<output_iterator_tag, void, void, void, void> |
ec57e686 | 160 | { |
161 | public: | |
ed73ad37 | 162 | //@{ |
163 | /// Public typedef | |
ec57e686 | 164 | typedef _CharT char_type; |
165 | typedef _Traits traits_type; | |
166 | typedef basic_ostream<_CharT, _Traits> ostream_type; | |
ed73ad37 | 167 | //@} |
ec57e686 | 168 | |
169 | private: | |
bae9b8af | 170 | ostream_type* _M_stream; |
171 | const _CharT* _M_string; | |
ec57e686 | 172 | |
173 | public: | |
ed73ad37 | 174 | /// Construct from an ostream. |
8e3a3e94 | 175 | ostream_iterator(ostream_type& __s) |
176 | : _M_stream(std::__addressof(__s)), _M_string(0) {} | |
b128dd19 | 177 | |
ed73ad37 | 178 | /** |
179 | * Construct from an ostream. | |
180 | * | |
181 | * The delimiter string @a c is written to the stream after every Tp | |
182 | * written to the stream. The delimiter is not copied, and thus must | |
183 | * not be destroyed while this iterator is in use. | |
184 | * | |
e12e4f3b | 185 | * @param __s Underlying ostream to write to. |
186 | * @param __c CharT delimiter string to insert. | |
ed73ad37 | 187 | */ |
bae9b8af | 188 | ostream_iterator(ostream_type& __s, const _CharT* __c) |
b128dd19 | 189 | : _M_stream(&__s), _M_string(__c) { } |
190 | ||
ed73ad37 | 191 | /// Copy constructor. |
b128dd19 | 192 | ostream_iterator(const ostream_iterator& __obj) |
193 | : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { } | |
ec57e686 | 194 | |
16f1a26a | 195 | #if __cplusplus >= 201103L |
196 | ostream_iterator& operator=(const ostream_iterator&) = default; | |
197 | #endif | |
198 | ||
ed73ad37 | 199 | /// Writes @a value to underlying ostream using operator<<. If |
200 | /// constructed with delimiter string, writes delimiter to ostream. | |
bae9b8af | 201 | ostream_iterator& |
202 | operator=(const _Tp& __value) | |
203 | { | |
d570f2e9 | 204 | __glibcxx_requires_cond(_M_stream != 0, |
205 | _M_message(__gnu_debug::__msg_output_ostream) | |
206 | ._M_iterator(*this)); | |
ec57e686 | 207 | *_M_stream << __value; |
208 | if (_M_string) *_M_stream << _M_string; | |
209 | return *this; | |
210 | } | |
bae9b8af | 211 | |
212 | ostream_iterator& | |
713a6b5f | 213 | operator*() |
214 | { return *this; } | |
bae9b8af | 215 | |
216 | ostream_iterator& | |
713a6b5f | 217 | operator++() |
bae9b8af | 218 | { return *this; } |
219 | ||
220 | ostream_iterator& | |
713a6b5f | 221 | operator++(int) |
bae9b8af | 222 | { return *this; } |
ec57e686 | 223 | }; |
1069247d | 224 | |
73337dee | 225 | // @} group iterators |
226 | ||
2948dd21 | 227 | _GLIBCXX_END_NAMESPACE_VERSION |
228 | } // namespace | |
1069247d | 229 | |
ec57e686 | 230 | #endif |