]> git.ipfire.org Git - thirdparty/gcc.git/blame - libstdc++-v3/include/bits/streambuf_iterator.h
Fix failing tests caused by duplicate dg-options
[thirdparty/gcc.git] / libstdc++-v3 / include / bits / streambuf_iterator.h
CommitLineData
725dc051
BK
1// Streambuf iterators
2
cbe34bb5 3// Copyright (C) 1997-2017 Free Software Foundation, Inc.
725dc051
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)
725dc051
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.
19
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/>.
725dc051 24
f910786b 25/** @file bits/streambuf_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 _STREAMBUF_ITERATOR_H
31#define _STREAMBUF_ITERATOR_H 1
725dc051 32
b0a85b86
GDR
33#pragma GCC system_header
34
2e2a38cd 35#include <streambuf>
285b36d6 36#include <debug/debug.h>
2e2a38cd 37
12ffa228
BK
38namespace std _GLIBCXX_VISIBILITY(default)
39{
40_GLIBCXX_BEGIN_NAMESPACE_VERSION
0002d5d2 41
8e32aa11
BK
42 /**
43 * @addtogroup iterators
44 * @{
45 */
46
725dc051 47 // 24.5.3 Template class istreambuf_iterator
ffcec5c8 48 /// Provides input iterator semantics for streambufs.
39003c99 49 template<typename _CharT, typename _Traits>
725dc051
BK
50 class istreambuf_iterator
51 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
ebb6e4af 52 _CharT*,
734f5023 53#if __cplusplus >= 201103L
ebb6e4af
PC
54 // LWG 445.
55 _CharT>
56#else
57 _CharT&>
58#endif
725dc051
BK
59 {
60 public:
725dc051 61 // Types:
ffcec5c8
JQ
62 //@{
63 /// Public typedefs
ed6814f7
BI
64 typedef _CharT char_type;
65 typedef _Traits traits_type;
66 typedef typename _Traits::int_type int_type;
67 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
68 typedef basic_istream<_CharT, _Traits> istream_type;
ffcec5c8 69 //@}
725dc051 70
0002d5d2 71 template<typename _CharT2>
105c6331
BK
72 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
73 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
74 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
75 ostreambuf_iterator<_CharT2>);
76
f0112db9 77 template<bool _IsMove, typename _CharT2>
105c6331
BK
78 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
79 _CharT2*>::__type
f0112db9
PC
80 __copy_move_a2(istreambuf_iterator<_CharT2>,
81 istreambuf_iterator<_CharT2>, _CharT2*);
0002d5d2
PC
82
83 template<typename _CharT2>
105c6331
BK
84 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
85 istreambuf_iterator<_CharT2> >::__type
0002d5d2 86 find(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
976e25f4 87 const _CharT2&);
0002d5d2 88
39003c99 89 private:
ed6814f7
BI
90 // 24.5.3 istreambuf_iterator
91 // p 1
39003c99
BK
92 // If the end of stream is reached (streambuf_type::sgetc()
93 // returns traits_type::eof()), the iterator becomes equal to
94 // the "end of stream" iterator value.
95 // NB: This implementation assumes the "end of stream" value
96 // is EOF, or -1.
ed6814f7 97 mutable streambuf_type* _M_sbuf;
4e914524 98 int_type _M_c;
39003c99
BK
99
100 public:
ffcec5c8 101 /// Construct end of input stream iterator.
23e4c4ee 102 _GLIBCXX_CONSTEXPR istreambuf_iterator() _GLIBCXX_USE_NOEXCEPT
f13a69ec 103 : _M_sbuf(0), _M_c(traits_type::eof()) { }
ed6814f7 104
734f5023 105#if __cplusplus >= 201103L
23e4c4ee
PC
106 istreambuf_iterator(const istreambuf_iterator&) noexcept = default;
107
108 ~istreambuf_iterator() = default;
109#endif
110
ffcec5c8 111 /// Construct start of input stream iterator.
23e4c4ee 112 istreambuf_iterator(istream_type& __s) _GLIBCXX_USE_NOEXCEPT
f13a69ec 113 : _M_sbuf(__s.rdbuf()), _M_c(traits_type::eof()) { }
725dc051 114
ffcec5c8 115 /// Construct start of streambuf iterator.
23e4c4ee 116 istreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
f13a69ec 117 : _M_sbuf(__s), _M_c(traits_type::eof()) { }
ed6814f7 118
ffcec5c8
JQ
119 /// Return the current character pointed to by iterator. This returns
120 /// streambuf.sgetc(). It cannot be assigned. NB: The result of
121 /// operator*() on an end of stream is undefined.
ed6814f7 122 char_type
725dc051 123 operator*() const
ed6814f7 124 {
4e914524
PO
125 int_type __c = _M_get();
126
285b36d6
BK
127#ifdef _GLIBCXX_DEBUG_PEDANTIC
128 // Dereferencing a past-the-end istreambuf_iterator is a
129 // libstdc++ extension
4e914524 130 __glibcxx_requires_cond(!_S_is_eof(__c),
285b36d6 131 _M_message(__gnu_debug::__msg_deref_istreambuf)
ed6814f7 132 ._M_iterator(*this));
285b36d6 133#endif
4e914524 134 return traits_type::to_char_type(__c);
285b36d6 135 }
ffcec5c8
JQ
136
137 /// Advance the iterator. Calls streambuf.sbumpc().
ed6814f7 138 istreambuf_iterator&
725dc051 139 operator++()
ed6814f7 140 {
4e914524
PO
141 __glibcxx_requires_cond(_M_sbuf &&
142 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
285b36d6 143 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 144 ._M_iterator(*this));
4e914524
PO
145
146 _M_sbuf->sbumpc();
147 _M_c = traits_type::eof();
ed6814f7 148 return *this;
725dc051
BK
149 }
150
ffcec5c8 151 /// Advance the iterator. Calls streambuf.sbumpc().
b581eaf7 152 istreambuf_iterator
725dc051
BK
153 operator++(int)
154 {
4e914524
PO
155 __glibcxx_requires_cond(_M_sbuf &&
156 (!_S_is_eof(_M_c) || !_S_is_eof(_M_sbuf->sgetc())),
285b36d6 157 _M_message(__gnu_debug::__msg_inc_istreambuf)
ed6814f7 158 ._M_iterator(*this));
285b36d6 159
b581eaf7 160 istreambuf_iterator __old = *this;
4e914524
PO
161 __old._M_c = _M_sbuf->sbumpc();
162 _M_c = traits_type::eof();
ed6814f7 163 return __old;
725dc051 164 }
725dc051 165
f5677b15 166 // _GLIBCXX_RESOLVE_LIB_DEFECTS
725dc051 167 // 110 istreambuf_iterator::equal not const
0e7300bb 168 // NB: there is also number 111 (NAD) relevant to this function.
ffcec5c8 169 /// Return true both iterators are end or both are not end.
ed6814f7 170 bool
b581eaf7 171 equal(const istreambuf_iterator& __b) const
6bfcbf0d 172 { return _M_at_eof() == __b._M_at_eof(); }
9875ea05
BK
173
174 private:
ed6814f7 175 int_type
9875ea05 176 _M_get() const
ed6814f7 177 {
4e914524
PO
178 int_type __ret = _M_c;
179 if (_M_sbuf && _S_is_eof(__ret) && _S_is_eof(__ret = _M_sbuf->sgetc()))
180 _M_sbuf = 0;
9875ea05
BK
181 return __ret;
182 }
285b36d6 183
ed6814f7 184 bool
285b36d6 185 _M_at_eof() const
4e914524
PO
186 { return _S_is_eof(_M_get()); }
187
188 static bool
189 _S_is_eof(int_type __c)
285b36d6
BK
190 {
191 const int_type __eof = traits_type::eof();
4e914524 192 return traits_type::eq_int_type(__c, __eof);
285b36d6 193 }
725dc051
BK
194 };
195
196 template<typename _CharT, typename _Traits>
ed6814f7 197 inline bool
725dc051
BK
198 operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
199 const istreambuf_iterator<_CharT, _Traits>& __b)
200 { return __a.equal(__b); }
201
202 template<typename _CharT, typename _Traits>
ed6814f7 203 inline bool
725dc051
BK
204 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
205 const istreambuf_iterator<_CharT, _Traits>& __b)
206 { return !__a.equal(__b); }
5c352a47 207
ffcec5c8 208 /// Provides output iterator semantics for streambufs.
5c352a47
BK
209 template<typename _CharT, typename _Traits>
210 class ostreambuf_iterator
211 : public iterator<output_iterator_tag, void, void, void, void>
212 {
213 public:
214 // Types:
ffcec5c8
JQ
215 //@{
216 /// Public typedefs
5c352a47
BK
217 typedef _CharT char_type;
218 typedef _Traits traits_type;
219 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
220 typedef basic_ostream<_CharT, _Traits> ostream_type;
ffcec5c8 221 //@}
5c352a47 222
0002d5d2 223 template<typename _CharT2>
105c6331
BK
224 friend typename __gnu_cxx::__enable_if<__is_char<_CharT2>::__value,
225 ostreambuf_iterator<_CharT2> >::__type
0002d5d2
PC
226 copy(istreambuf_iterator<_CharT2>, istreambuf_iterator<_CharT2>,
227 ostreambuf_iterator<_CharT2>);
228
5c352a47 229 private:
ed6814f7
BI
230 streambuf_type* _M_sbuf;
231 bool _M_failed;
5c352a47
BK
232
233 public:
ffcec5c8 234 /// Construct output iterator from ostream.
23e4c4ee 235 ostreambuf_iterator(ostream_type& __s) _GLIBCXX_USE_NOEXCEPT
5c352a47 236 : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
ed6814f7 237
ffcec5c8 238 /// Construct output iterator from streambuf.
23e4c4ee 239 ostreambuf_iterator(streambuf_type* __s) _GLIBCXX_USE_NOEXCEPT
5c352a47
BK
240 : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
241
ffcec5c8 242 /// Write character to streambuf. Calls streambuf.sputc().
ed6814f7 243 ostreambuf_iterator&
2e2a38cd
BK
244 operator=(_CharT __c)
245 {
ed6814f7 246 if (!_M_failed &&
2e2a38cd
BK
247 _Traits::eq_int_type(_M_sbuf->sputc(__c), _Traits::eof()))
248 _M_failed = true;
249 return *this;
250 }
5c352a47 251
ffcec5c8 252 /// Return *this.
ed6814f7 253 ostreambuf_iterator&
e0ec69c9 254 operator*()
5c352a47
BK
255 { return *this; }
256
ffcec5c8 257 /// Return *this.
ed6814f7 258 ostreambuf_iterator&
e0ec69c9 259 operator++(int)
5c352a47
BK
260 { return *this; }
261
ffcec5c8 262 /// Return *this.
ed6814f7 263 ostreambuf_iterator&
e0ec69c9 264 operator++()
5c352a47
BK
265 { return *this; }
266
ffcec5c8 267 /// Return true if previous operator=() failed.
ed6814f7 268 bool
23e4c4ee 269 failed() const _GLIBCXX_USE_NOEXCEPT
5c352a47 270 { return _M_failed; }
5c352a47 271
ed6814f7 272 ostreambuf_iterator&
2e2a38cd
BK
273 _M_put(const _CharT* __ws, streamsize __len)
274 {
53279c10 275 if (__builtin_expect(!_M_failed, true)
15d72060
PC
276 && __builtin_expect(this->_M_sbuf->sputn(__ws, __len) != __len,
277 false))
53279c10 278 _M_failed = true;
2e2a38cd
BK
279 return *this;
280 }
281 };
3cbc7af0 282
0002d5d2
PC
283 // Overloads for streambuf iterators.
284 template<typename _CharT>
105c6331
BK
285 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
286 ostreambuf_iterator<_CharT> >::__type
0002d5d2
PC
287 copy(istreambuf_iterator<_CharT> __first,
288 istreambuf_iterator<_CharT> __last,
289 ostreambuf_iterator<_CharT> __result)
290 {
291 if (__first._M_sbuf && !__last._M_sbuf && !__result._M_failed)
292 {
293 bool __ineof;
294 __copy_streambufs_eof(__first._M_sbuf, __result._M_sbuf, __ineof);
295 if (!__ineof)
296 __result._M_failed = true;
297 }
298 return __result;
299 }
300
f0112db9 301 template<bool _IsMove, typename _CharT>
105c6331
BK
302 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
303 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
304 __copy_move_a2(_CharT* __first, _CharT* __last,
305 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
306 {
307 const streamsize __num = __last - __first;
308 if (__num > 0)
309 __result._M_put(__first, __num);
310 return __result;
311 }
312
f0112db9 313 template<bool _IsMove, typename _CharT>
105c6331
BK
314 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
315 ostreambuf_iterator<_CharT> >::__type
f0112db9
PC
316 __copy_move_a2(const _CharT* __first, const _CharT* __last,
317 ostreambuf_iterator<_CharT> __result)
0002d5d2
PC
318 {
319 const streamsize __num = __last - __first;
320 if (__num > 0)
321 __result._M_put(__first, __num);
322 return __result;
323 }
324
f0112db9 325 template<bool _IsMove, typename _CharT>
105c6331
BK
326 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
327 _CharT*>::__type
f0112db9
PC
328 __copy_move_a2(istreambuf_iterator<_CharT> __first,
329 istreambuf_iterator<_CharT> __last, _CharT* __result)
0002d5d2
PC
330 {
331 typedef istreambuf_iterator<_CharT> __is_iterator_type;
332 typedef typename __is_iterator_type::traits_type traits_type;
333 typedef typename __is_iterator_type::streambuf_type streambuf_type;
334 typedef typename traits_type::int_type int_type;
335
336 if (__first._M_sbuf && !__last._M_sbuf)
337 {
338 streambuf_type* __sb = __first._M_sbuf;
339 int_type __c = __sb->sgetc();
340 while (!traits_type::eq_int_type(__c, traits_type::eof()))
341 {
342 const streamsize __n = __sb->egptr() - __sb->gptr();
343 if (__n > 1)
344 {
345 traits_type::copy(__result, __sb->gptr(), __n);
1139a735 346 __sb->__safe_gbump(__n);
0002d5d2
PC
347 __result += __n;
348 __c = __sb->underflow();
349 }
350 else
351 {
352 *__result++ = traits_type::to_char_type(__c);
353 __c = __sb->snextc();
354 }
355 }
356 }
357 return __result;
358 }
359
360 template<typename _CharT>
105c6331
BK
361 typename __gnu_cxx::__enable_if<__is_char<_CharT>::__value,
362 istreambuf_iterator<_CharT> >::__type
0002d5d2 363 find(istreambuf_iterator<_CharT> __first,
976e25f4 364 istreambuf_iterator<_CharT> __last, const _CharT& __val)
0002d5d2
PC
365 {
366 typedef istreambuf_iterator<_CharT> __is_iterator_type;
367 typedef typename __is_iterator_type::traits_type traits_type;
368 typedef typename __is_iterator_type::streambuf_type streambuf_type;
369 typedef typename traits_type::int_type int_type;
4e914524 370 const int_type __eof = traits_type::eof();
0002d5d2
PC
371
372 if (__first._M_sbuf && !__last._M_sbuf)
373 {
374 const int_type __ival = traits_type::to_int_type(__val);
375 streambuf_type* __sb = __first._M_sbuf;
376 int_type __c = __sb->sgetc();
4e914524 377 while (!traits_type::eq_int_type(__c, __eof)
0002d5d2
PC
378 && !traits_type::eq_int_type(__c, __ival))
379 {
380 streamsize __n = __sb->egptr() - __sb->gptr();
381 if (__n > 1)
382 {
383 const _CharT* __p = traits_type::find(__sb->gptr(),
384 __n, __val);
385 if (__p)
386 __n = __p - __sb->gptr();
1139a735 387 __sb->__safe_gbump(__n);
0002d5d2
PC
388 __c = __sb->sgetc();
389 }
390 else
391 __c = __sb->snextc();
392 }
393
4e914524 394 __first._M_c = __eof;
0002d5d2 395 }
4e914524 396
0002d5d2
PC
397 return __first;
398 }
399
8e32aa11
BK
400// @} group iterators
401
12ffa228
BK
402_GLIBCXX_END_NAMESPACE_VERSION
403} // namespace
3cbc7af0 404
b85381b9 405#endif