1 | |
2 | |
3 | |
4 | |
5 | |
6 | |
7 | |
8 | |
9 | |
10 | |
11 | |
12 | |
13 | |
14 | |
15 | |
16 | |
17 | |
18 | |
19 | |
20 | |
21 | |
22 | |
23 | |
24 | |
25 | |
26 | |
27 | |
28 | |
29 | |
30 | #ifndef _STREAM_ITERATOR_H |
31 | #define _STREAM_ITERATOR_H 1 |
32 | |
33 | #pragma GCC system_header |
34 | |
35 | #include <debug/debug.h> |
36 | |
37 | namespace std _GLIBCXX_VISIBILITY(default) |
38 | { |
39 | _GLIBCXX_BEGIN_NAMESPACE_VERSION |
40 | |
41 | |
42 | |
43 | |
44 | |
45 | |
46 | |
47 | template<typename _Tp, typename _CharT = char, |
48 | typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t> |
49 | class istream_iterator |
50 | : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&> |
51 | { |
52 | public: |
53 | typedef _CharT char_type; |
54 | typedef _Traits traits_type; |
55 | typedef basic_istream<_CharT, _Traits> istream_type; |
56 | |
57 | private: |
58 | istream_type* _M_stream; |
59 | _Tp _M_value; |
60 | bool _M_ok; |
61 | |
62 | public: |
63 | |
64 | _GLIBCXX_CONSTEXPR istream_iterator() |
65 | : _M_stream(0), _M_value(), _M_ok(false) {} |
66 | |
67 | |
68 | istream_iterator(istream_type& __s) |
69 | : _M_stream(std::__addressof(__s)) |
70 | { _M_read(); } |
71 | |
72 | istream_iterator(const istream_iterator& __obj) |
73 | : _M_stream(__obj._M_stream), _M_value(__obj._M_value), |
74 | _M_ok(__obj._M_ok) |
75 | { } |
76 | |
77 | const _Tp& |
78 | operator*() const |
79 | { |
80 | __glibcxx_requires_cond(_M_ok, |
81 | _M_message(__gnu_debug::__msg_deref_istream) |
82 | ._M_iterator(*this)); |
83 | return _M_value; |
84 | } |
85 | |
86 | const _Tp* |
87 | operator->() const { return std::__addressof((operator*())); } |
88 | |
89 | istream_iterator& |
90 | operator++() |
91 | { |
92 | __glibcxx_requires_cond(_M_ok, |
93 | _M_message(__gnu_debug::__msg_inc_istream) |
94 | ._M_iterator(*this)); |
95 | _M_read(); |
96 | return *this; |
97 | } |
98 | |
99 | istream_iterator |
100 | operator++(int) |
101 | { |
102 | __glibcxx_requires_cond(_M_ok, |
103 | _M_message(__gnu_debug::__msg_inc_istream) |
104 | ._M_iterator(*this)); |
105 | istream_iterator __tmp = *this; |
106 | _M_read(); |
107 | return __tmp; |
108 | } |
109 | |
110 | bool |
111 | _M_equal(const istream_iterator& __x) const |
112 | { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); } |
113 | |
114 | private: |
115 | void |
116 | _M_read() |
117 | { |
118 | _M_ok = (_M_stream && *_M_stream) ? true : false; |
119 | if (_M_ok) |
120 | { |
121 | *_M_stream >> _M_value; |
122 | _M_ok = *_M_stream ? true : false; |
123 | } |
124 | } |
125 | }; |
126 | |
127 | |
128 | template<typename _Tp, typename _CharT, typename _Traits, typename _Dist> |
129 | inline bool |
130 | operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x, |
131 | const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) |
132 | { return __x._M_equal(__y); } |
133 | |
134 | |
135 | template <class _Tp, class _CharT, class _Traits, class _Dist> |
136 | inline bool |
137 | operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x, |
138 | const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y) |
139 | { return !__x._M_equal(__y); } |
140 | |
141 | |
142 | |
143 | |
144 | |
145 | |
146 | |
147 | |
148 | |
149 | |
150 | |
151 | |
152 | template<typename _Tp, typename _CharT = char, |
153 | typename _Traits = char_traits<_CharT> > |
154 | class ostream_iterator |
155 | : public iterator<output_iterator_tag, void, void, void, void> |
156 | { |
157 | public: |
158 | |
159 | |
160 | typedef _CharT char_type; |
161 | typedef _Traits traits_type; |
162 | typedef basic_ostream<_CharT, _Traits> ostream_type; |
163 | |
164 | |
165 | private: |
166 | ostream_type* _M_stream; |
167 | const _CharT* _M_string; |
168 | |
169 | public: |
170 | |
171 | ostream_iterator(ostream_type& __s) |
172 | : _M_stream(std::__addressof(__s)), _M_string(0) {} |
173 | |
174 | |
175 | |
176 | |
177 | |
178 | |
179 | |
180 | |
181 | |
182 | |
183 | |
184 | ostream_iterator(ostream_type& __s, const _CharT* __c) |
185 | : _M_stream(&__s), _M_string(__c) { } |
186 | |
187 | |
188 | ostream_iterator(const ostream_iterator& __obj) |
189 | : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { } |
190 | |
191 | |
192 | |
193 | ostream_iterator& |
194 | operator=(const _Tp& __value) |
195 | { |
196 | __glibcxx_requires_cond(_M_stream != 0, |
197 | _M_message(__gnu_debug::__msg_output_ostream) |
198 | ._M_iterator(*this)); |
199 | *_M_stream << __value; |
200 | if (_M_string) *_M_stream << _M_string; |
201 | return *this; |
202 | } |
203 | |
204 | ostream_iterator& |
205 | operator*() |
206 | { return *this; } |
207 | |
208 | ostream_iterator& |
209 | operator++() |
210 | { return *this; } |
211 | |
212 | ostream_iterator& |
213 | operator++(int) |
214 | { return *this; } |
215 | }; |
216 | |
217 | |
218 | |
219 | _GLIBCXX_END_NAMESPACE_VERSION |
220 | } |
221 | |
222 | #endif |
223 | |