]>
Commit | Line | Data |
---|---|---|
d27bba5e BK |
1 | // Stream iterators |
2 | ||
94a86be0 | 3 | // Copyright (C) 2001, 2004, 2005, 2009, 2010 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 |
37 | namespace std _GLIBCXX_VISIBILITY(default) |
38 | { | |
39 | _GLIBCXX_BEGIN_NAMESPACE_VERSION | |
3cbc7af0 | 40 | |
8e32aa11 BK |
41 | /** |
42 | * @addtogroup iterators | |
43 | * @{ | |
44 | */ | |
45 | ||
ffcec5c8 | 46 | /// Provides input iterator semantics for streams. |
ed6814f7 BI |
47 | template<typename _Tp, typename _CharT = char, |
48 | typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t> | |
49 | class istream_iterator | |
15d72060 | 50 | : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&> |
d27bba5e BK |
51 | { |
52 | public: | |
53 | typedef _CharT char_type; | |
54 | typedef _Traits traits_type; | |
55 | typedef basic_istream<_CharT, _Traits> istream_type; | |
56 | ||
57 | private: | |
ed6814f7 BI |
58 | istream_type* _M_stream; |
59 | _Tp _M_value; | |
60 | bool _M_ok; | |
d27bba5e | 61 | |
ffcec5c8 JQ |
62 | public: |
63 | /// Construct end of input stream iterator. | |
94a86be0 | 64 | _GLIBCXX_CONSTEXPR istream_iterator() |
8b5bc374 | 65 | : _M_stream(0), _M_value(), _M_ok(false) {} |
75bef434 | 66 | |
ffcec5c8 | 67 | /// Construct start of input stream iterator. |
15d72060 PC |
68 | istream_iterator(istream_type& __s) |
69 | : _M_stream(&__s) | |
70 | { _M_read(); } | |
d27bba5e | 71 | |
ed6814f7 BI |
72 | istream_iterator(const istream_iterator& __obj) |
73 | : _M_stream(__obj._M_stream), _M_value(__obj._M_value), | |
74 | _M_ok(__obj._M_ok) | |
75bef434 BK |
75 | { } |
76 | ||
d27bba5e | 77 | const _Tp& |
ed6814f7 BI |
78 | operator*() const |
79 | { | |
285b36d6 BK |
80 | __glibcxx_requires_cond(_M_ok, |
81 | _M_message(__gnu_debug::__msg_deref_istream) | |
82 | ._M_iterator(*this)); | |
83 | return _M_value; | |
84 | } | |
d27bba5e BK |
85 | |
86 | const _Tp* | |
87 | operator->() const { return &(operator*()); } | |
88 | ||
ed6814f7 BI |
89 | istream_iterator& |
90 | operator++() | |
91 | { | |
285b36d6 BK |
92 | __glibcxx_requires_cond(_M_ok, |
93 | _M_message(__gnu_debug::__msg_inc_istream) | |
94 | ._M_iterator(*this)); | |
ed6814f7 BI |
95 | _M_read(); |
96 | return *this; | |
285b36d6 | 97 | } |
d27bba5e | 98 | |
ed6814f7 BI |
99 | istream_iterator |
100 | operator++(int) | |
d27bba5e | 101 | { |
285b36d6 BK |
102 | __glibcxx_requires_cond(_M_ok, |
103 | _M_message(__gnu_debug::__msg_inc_istream) | |
ed6814f7 | 104 | ._M_iterator(*this)); |
d27bba5e BK |
105 | istream_iterator __tmp = *this; |
106 | _M_read(); | |
107 | return __tmp; | |
108 | } | |
109 | ||
ed6814f7 | 110 | bool |
d27bba5e | 111 | _M_equal(const istream_iterator& __x) const |
15d72060 | 112 | { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); } |
d27bba5e | 113 | |
ed6814f7 BI |
114 | private: |
115 | void | |
116 | _M_read() | |
d27bba5e BK |
117 | { |
118 | _M_ok = (_M_stream && *_M_stream) ? true : false; | |
15d72060 | 119 | if (_M_ok) |
d27bba5e BK |
120 | { |
121 | *_M_stream >> _M_value; | |
122 | _M_ok = *_M_stream ? true : false; | |
123 | } | |
124 | } | |
125 | }; | |
ed6814f7 | 126 | |
ffcec5c8 | 127 | /// Return true if x and y are both end or not end, or x and y are the same. |
d27bba5e | 128 | template<typename _Tp, typename _CharT, typename _Traits, typename _Dist> |
ed6814f7 | 129 | inline bool |
d27bba5e | 130 | operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x, |
ed6814f7 | 131 | const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) |
d27bba5e BK |
132 | { return __x._M_equal(__y); } |
133 | ||
ffcec5c8 | 134 | /// Return false if x and y are both end or not end, or x and y are the same. |
d27bba5e | 135 | template <class _Tp, class _CharT, class _Traits, class _Dist> |
ed6814f7 | 136 | inline bool |
75bef434 | 137 | operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x, |
ed6814f7 | 138 | const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) |
75bef434 | 139 | { return !__x._M_equal(__y); } |
d27bba5e | 140 | |
ffcec5c8 JQ |
141 | /** |
142 | * @brief Provides output iterator semantics for streams. | |
143 | * | |
144 | * This class provides an iterator to write to an ostream. The type Tp is | |
145 | * the only type written by this iterator and there must be an | |
146 | * operator<<(Tp) defined. | |
147 | * | |
148 | * @param Tp The type to write to the ostream. | |
149 | * @param CharT The ostream char_type. | |
150 | * @param Traits The ostream char_traits. | |
151 | */ | |
ed6814f7 | 152 | template<typename _Tp, typename _CharT = char, |
d27bba5e | 153 | typename _Traits = char_traits<_CharT> > |
ed6814f7 | 154 | class ostream_iterator |
15d72060 | 155 | : public iterator<output_iterator_tag, void, void, void, void> |
d27bba5e BK |
156 | { |
157 | public: | |
ffcec5c8 JQ |
158 | //@{ |
159 | /// Public typedef | |
d27bba5e BK |
160 | typedef _CharT char_type; |
161 | typedef _Traits traits_type; | |
162 | typedef basic_ostream<_CharT, _Traits> ostream_type; | |
ffcec5c8 | 163 | //@} |
d27bba5e BK |
164 | |
165 | private: | |
ed6814f7 BI |
166 | ostream_type* _M_stream; |
167 | const _CharT* _M_string; | |
d27bba5e BK |
168 | |
169 | public: | |
ffcec5c8 | 170 | /// Construct from an ostream. |
d27bba5e | 171 | ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {} |
75bef434 | 172 | |
ffcec5c8 JQ |
173 | /** |
174 | * Construct from an ostream. | |
175 | * | |
176 | * The delimiter string @a c is written to the stream after every Tp | |
177 | * written to the stream. The delimiter is not copied, and thus must | |
178 | * not be destroyed while this iterator is in use. | |
179 | * | |
180 | * @param s Underlying ostream to write to. | |
181 | * @param c CharT delimiter string to insert. | |
182 | */ | |
ed6814f7 | 183 | ostream_iterator(ostream_type& __s, const _CharT* __c) |
75bef434 BK |
184 | : _M_stream(&__s), _M_string(__c) { } |
185 | ||
ffcec5c8 | 186 | /// Copy constructor. |
75bef434 BK |
187 | ostream_iterator(const ostream_iterator& __obj) |
188 | : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { } | |
d27bba5e | 189 | |
ffcec5c8 JQ |
190 | /// Writes @a value to underlying ostream using operator<<. If |
191 | /// constructed with delimiter string, writes delimiter to ostream. | |
ed6814f7 BI |
192 | ostream_iterator& |
193 | operator=(const _Tp& __value) | |
194 | { | |
285b36d6 BK |
195 | __glibcxx_requires_cond(_M_stream != 0, |
196 | _M_message(__gnu_debug::__msg_output_ostream) | |
197 | ._M_iterator(*this)); | |
d27bba5e BK |
198 | *_M_stream << __value; |
199 | if (_M_string) *_M_stream << _M_string; | |
200 | return *this; | |
201 | } | |
ed6814f7 BI |
202 | |
203 | ostream_iterator& | |
15d72060 PC |
204 | operator*() |
205 | { return *this; } | |
ed6814f7 BI |
206 | |
207 | ostream_iterator& | |
15d72060 | 208 | operator++() |
ed6814f7 BI |
209 | { return *this; } |
210 | ||
211 | ostream_iterator& | |
15d72060 | 212 | operator++(int) |
ed6814f7 | 213 | { return *this; } |
d27bba5e | 214 | }; |
3cbc7af0 | 215 | |
8e32aa11 BK |
216 | // @} group iterators |
217 | ||
12ffa228 BK |
218 | _GLIBCXX_END_NAMESPACE_VERSION |
219 | } // namespace | |
3cbc7af0 | 220 | |
d27bba5e | 221 | #endif |