]> git.ipfire.org Git - thirdparty/gcc.git/blob - libstdc++-v3/include/bits/streambuf_iterator.h
streambuf_iterator.h (class istreambuf_iterator): Consistently use _M_c to cache...
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
1 // Streambuf iterators
2
3 // Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004
4 // Free Software Foundation, Inc.
5 //
6 // This file is part of the GNU ISO C++ Library. This library is free
7 // software; you can redistribute it and/or modify it under the
8 // terms of the GNU General Public License as published by the
9 // Free Software Foundation; either version 2, or (at your option)
10 // any later version.
11
12 // This library is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // GNU General Public License for more details.
16
17 // You should have received a copy of the GNU General Public License along
18 // with this library; see the file COPYING. If not, write to the Free
19 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
20 // USA.
21
22 // As a special exception, you may use this file as part of a free software
23 // library without restriction. Specifically, if other files instantiate
24 // templates or use macros or inline functions from this file, or you compile
25 // this file and link it with other files to produce an executable, this
26 // file does not by itself cause the resulting executable to be covered by
27 // the GNU General Public License. This exception does not however
28 // invalidate any other reasons why the executable file might be covered by
29 // the GNU General Public License.
30
31 /** @file streambuf_iterator.h
32 * This is an internal header file, included by other library headers.
33 * You should not attempt to use it directly.
34 */
35
36 #ifndef _STREAMBUF_ITERATOR_H
37 #define _STREAMBUF_ITERATOR_H 1
38
39 #pragma GCC system_header
40
41 #include <streambuf>
42 #include <debug/debug.h>
43
44 // NB: Should specialize copy, find algorithms for streambuf iterators.
45
46 namespace std
47 {
48 // 24.5.3 Template class istreambuf_iterator
49 /// Provides input iterator semantics for streambufs.
50 template<typename _CharT, typename _Traits>
51 class istreambuf_iterator
52 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
53 _CharT*, _CharT&>
54 {
55 public:
56 // Types:
57 //@{
58 /// Public typedefs
59 typedef _CharT char_type;
60 typedef _Traits traits_type;
61 typedef typename _Traits::int_type int_type;
62 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
63 typedef basic_istream<_CharT, _Traits> istream_type;
64 //@}
65
66 private:
67 // 24.5.3 istreambuf_iterator
68 // p 1
69 // If the end of stream is reached (streambuf_type::sgetc()
70 // returns traits_type::eof()), the iterator becomes equal to
71 // the "end of stream" iterator value.
72 // NB: This implementation assumes the "end of stream" value
73 // is EOF, or -1.
74 mutable streambuf_type* _M_sbuf;
75 mutable int_type _M_c;
76
77 public:
78 /// Construct end of input stream iterator.
79 istreambuf_iterator() throw()
80 : _M_sbuf(0), _M_c(traits_type::eof()) { }
81
82 /// Construct start of input stream iterator.
83 istreambuf_iterator(istream_type& __s) throw()
84 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
85
86 /// Construct start of streambuf iterator.
87 istreambuf_iterator(streambuf_type* __s) throw()
88 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
89
90 /// Return the current character pointed to by iterator. This returns
91 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
92 /// operator*() on an end of stream is undefined.
93 char_type
94 operator*() const
95 {
96 #ifdef _GLIBCXX_DEBUG_PEDANTIC
97 // Dereferencing a past-the-end istreambuf_iterator is a
98 // libstdc++ extension
99 __glibcxx_requires_cond(!_M_at_eof(),
100 _M_message(__gnu_debug::__msg_deref_istreambuf)
101 ._M_iterator(*this));
102 #endif
103 return traits_type::to_char_type(_M_get());
104 }
105
106 /// Advance the iterator. Calls streambuf.sbumpc().
107 istreambuf_iterator&
108 operator++()
109 {
110 __glibcxx_requires_cond(!_M_at_eof(),
111 _M_message(__gnu_debug::__msg_inc_istreambuf)
112 ._M_iterator(*this));
113 const int_type __eof = traits_type::eof();
114 if (_M_sbuf && traits_type::eq_int_type(_M_sbuf->sbumpc(), __eof))
115 _M_sbuf = 0;
116 else
117 _M_c = __eof;
118 return *this;
119 }
120
121 /// Advance the iterator. Calls streambuf.sbumpc().
122 istreambuf_iterator
123 operator++(int)
124 {
125 __glibcxx_requires_cond(!_M_at_eof(),
126 _M_message(__gnu_debug::__msg_inc_istreambuf)
127 ._M_iterator(*this));
128
129 const int_type __eof = traits_type::eof();
130 istreambuf_iterator __old = *this;
131 if (_M_sbuf
132 && traits_type::eq_int_type((__old._M_c = _M_sbuf->sbumpc()),
133 __eof))
134 _M_sbuf = 0;
135 else
136 _M_c = __eof;
137 return __old;
138 }
139
140 // _GLIBCXX_RESOLVE_LIB_DEFECTS
141 // 110 istreambuf_iterator::equal not const
142 // NB: there is also number 111 (NAD, Future) pending on this function.
143 /// Return true both iterators are end or both are not end.
144 bool
145 equal(const istreambuf_iterator& __b) const
146 {
147 const bool __thiseof = _M_at_eof();
148 const bool __beof = __b._M_at_eof();
149 return (__thiseof && __beof || (!__thiseof && !__beof));
150 }
151
152 private:
153 int_type
154 _M_get() const
155 {
156 const int_type __eof = traits_type::eof();
157 int_type __ret = __eof;
158 if (_M_sbuf)
159 {
160 if (!traits_type::eq_int_type(_M_c, __eof)
161 || !traits_type::eq_int_type((_M_c = _M_sbuf->sgetc()),
162 __eof))
163 __ret = _M_c;
164 else
165 _M_sbuf = 0;
166 }
167 return __ret;
168 }
169
170 bool
171 _M_at_eof() const
172 {
173 const int_type __eof = traits_type::eof();
174 return traits_type::eq_int_type(_M_get(), __eof);
175 }
176 };
177
178 template<typename _CharT, typename _Traits>
179 inline bool
180 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
181 const istreambuf_iterator<_CharT, _Traits>& __b)
182 { return __a.equal(__b); }
183
184 template<typename _CharT, typename _Traits>
185 inline bool
186 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
187 const istreambuf_iterator<_CharT, _Traits>& __b)
188 { return !__a.equal(__b); }
189
190 /// Provides output iterator semantics for streambufs.
191 template<typename _CharT, typename _Traits>
192 class ostreambuf_iterator
193 : public iterator<output_iterator_tag, void, void, void, void>
194 {
195 public:
196 // Types:
197 //@{
198 /// Public typedefs
199 typedef _CharT char_type;
200 typedef _Traits traits_type;
201 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
202 typedef basic_ostream<_CharT, _Traits> ostream_type;
203 //@}
204
205 private:
206 streambuf_type* _M_sbuf;
207 bool _M_failed;
208
209 public:
210 /// Construct output iterator from ostream.
211 ostreambuf_iterator(ostream_type& __s) throw ()
212 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
213
214 /// Construct output iterator from streambuf.
215 ostreambuf_iterator(streambuf_type* __s) throw ()
216 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
217
218 /// Write character to streambuf. Calls streambuf.sputc().
219 ostreambuf_iterator&
220 operator=(_CharT __c)
221 {
222 if (!_M_failed &&
223 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
224 _M_failed = true;
225 return *this;
226 }
227
228 /// Return *this.
229 ostreambuf_iterator&
230 operator*()
231 { return *this; }
232
233 /// Return *this.
234 ostreambuf_iterator&
235 operator++(int)
236 { return *this; }
237
238 /// Return *this.
239 ostreambuf_iterator&
240 operator++()
241 { return *this; }
242
243 /// Return true if previous operator=() failed.
244 bool
245 failed() const throw()
246 { return _M_failed; }
247
248 ostreambuf_iterator&
249 _M_put(const _CharT* __ws, streamsize __len)
250 {
251 if (__builtin_expect(!_M_failed, true)
252 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
253 false))
254 _M_failed = true;
255 return *this;
256 }
257 };
258 } // namespace std
259 #endif