]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/streambuf_iterator.h
jacks.exp (gcj_jacks_write): Enable "assert" constraint.
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
CommitLineData
725dc051
BK
1// Streambuf iterators
2
2e2a38cd 3// Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003
5c352a47 4// Free Software Foundation, Inc.
725dc051
BK
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
729e3d3f
PE
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
3d7c150e
BK
36#ifndef _STREAMBUF_ITERATOR_H
37#define _STREAMBUF_ITERATOR_H 1
725dc051 38
b0a85b86
GDR
39#pragma GCC system_header
40
2e2a38cd
BK
41#include <streambuf>
42
43// NB: Should specialize copy, find algorithms for streambuf iterators.
44
725dc051
BK
45namespace std
46{
725dc051 47 // 24.5.3 Template class istreambuf_iterator
39003c99 48 template<typename _CharT, typename _Traits>
725dc051
BK
49 class istreambuf_iterator
50 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
51 _CharT*, _CharT&>
52 {
53 public:
725dc051
BK
54 // Types:
55 typedef _CharT char_type;
56 typedef _Traits traits_type;
57 typedef typename _Traits::int_type int_type;
58 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
59 typedef basic_istream<_CharT, _Traits> istream_type;
725dc051 60
39003c99
BK
61 private:
62 // 24.5.3 istreambuf_iterator
63 // p 1
64 // If the end of stream is reached (streambuf_type::sgetc()
65 // returns traits_type::eof()), the iterator becomes equal to
66 // the "end of stream" iterator value.
67 // NB: This implementation assumes the "end of stream" value
68 // is EOF, or -1.
5c352a47 69 mutable streambuf_type* _M_sbuf;
39003c99
BK
70 int_type _M_c;
71
72 public:
725dc051 73 istreambuf_iterator() throw()
f13a69ec 74 : _M_sbuf(0), _M_c(traits_type::eof()) { }
725dc051
BK
75
76 istreambuf_iterator(istream_type& __s) throw()
f13a69ec 77 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
725dc051
BK
78
79 istreambuf_iterator(streambuf_type* __s) throw()
f13a69ec 80 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
725dc051 81
9875ea05 82 // NB: The result of operator*() on an end of stream is undefined.
725dc051
BK
83 char_type
84 operator*() const
9875ea05 85 { return traits_type::to_char_type(_M_get()); }
725dc051 86
b581eaf7 87 istreambuf_iterator&
725dc051
BK
88 operator++()
89 {
f13a69ec
BK
90 const int_type __eof = traits_type::eof();
91 if (_M_sbuf && traits_type::eq_int_type(_M_sbuf->sbumpc(), __eof))
5c352a47
BK
92 _M_sbuf = 0;
93 else
f13a69ec 94 _M_c = __eof;
725dc051
BK
95 return *this;
96 }
97
b581eaf7 98 istreambuf_iterator
725dc051
BK
99 operator++(int)
100 {
f13a69ec 101 const int_type __eof = traits_type::eof();
b581eaf7 102 istreambuf_iterator __old = *this;
f13a69ec
BK
103 if (_M_sbuf
104 && traits_type::eq_int_type((__old._M_c = _M_sbuf->sbumpc()),
105 __eof))
5c352a47
BK
106 _M_sbuf = 0;
107 else
f13a69ec 108 _M_c = __eof;
a85afd69 109 return __old;
725dc051 110 }
725dc051 111
f5677b15 112 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725dc051 113 // 110 istreambuf_iterator::equal not const
77cd227e 114 // NB: there is also number 111 (NAD, Future) pending on this function.
725dc051 115 bool
b581eaf7 116 equal(const istreambuf_iterator& __b) const
725dc051 117 {
5c352a47 118 const int_type __eof = traits_type::eof();
f13a69ec
BK
119 bool __thiseof = traits_type::eq_int_type(_M_get(), __eof);
120 bool __beof = traits_type::eq_int_type(__b._M_get(), __eof);
725dc051
BK
121 return (__thiseof && __beof || (!__thiseof && !__beof));
122 }
9875ea05
BK
123
124 private:
125 int_type
126 _M_get() const
127 {
f13a69ec
BK
128 const int_type __eof = traits_type::eof();
129 int_type __ret = __eof;
9875ea05
BK
130 if (_M_sbuf)
131 {
f13a69ec 132 if (!traits_type::eq_int_type(_M_c, __eof))
9875ea05
BK
133 __ret = _M_c;
134 else
f13a69ec 135 if (traits_type::eq_int_type((__ret = _M_sbuf->sgetc()), __eof))
9875ea05
BK
136 _M_sbuf = 0;
137 }
138 return __ret;
139 }
725dc051
BK
140 };
141
142 template<typename _CharT, typename _Traits>
143 inline bool
144 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
145 const istreambuf_iterator<_CharT, _Traits>& __b)
146 { return __a.equal(__b); }
147
148 template<typename _CharT, typename _Traits>
149 inline bool
150 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
151 const istreambuf_iterator<_CharT, _Traits>& __b)
152 { return !__a.equal(__b); }
5c352a47
BK
153
154 template<typename _CharT, typename _Traits>
155 class ostreambuf_iterator
156 : public iterator<output_iterator_tag, void, void, void, void>
157 {
158 public:
159 // Types:
160 typedef _CharT char_type;
161 typedef _Traits traits_type;
162 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
163 typedef basic_ostream<_CharT, _Traits> ostream_type;
164
165 private:
166 streambuf_type* _M_sbuf;
167 bool _M_failed;
168
169 public:
5c352a47
BK
170 ostreambuf_iterator(ostream_type& __s) throw ()
171 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
172
173 ostreambuf_iterator(streambuf_type* __s) throw ()
174 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
175
176 ostreambuf_iterator&
2e2a38cd
BK
177 operator=(_CharT __c)
178 {
179 if (!_M_failed &&
180 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
181 _M_failed = true;
182 return *this;
183 }
5c352a47
BK
184
185 ostreambuf_iterator&
186 operator*() throw()
187 { return *this; }
188
189 ostreambuf_iterator&
190 operator++(int) throw()
191 { return *this; }
192
193 ostreambuf_iterator&
194 operator++() throw()
195 { return *this; }
196
197 bool
198 failed() const throw()
199 { return _M_failed; }
5c352a47 200
2e2a38cd
BK
201 ostreambuf_iterator&
202 _M_put(const _CharT* __ws, streamsize __len)
203 {
53279c10
JQ
204 if (__builtin_expect(!_M_failed, true)
205 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len, false))
206 _M_failed = true;
2e2a38cd
BK
207 return *this;
208 }
209 };
b85381b9 210} // namespace std
b85381b9 211#endif