2003-12-26 Guilhem Lavaux <guilhem@kaffe.org>
[official-gcc.git] / libstdc++-v3 / include / bits / stream_iterator.h
blobcc67505dd69aa4c6289ba9ef95da7bd52e153cf3
1 // Stream iterators
3 // Copyright (C) 2001 Free Software Foundation, Inc.
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
8 // Free Software Foundation; either version 2, or (at your option)
9 // any later version.
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.
16 // You should have received a copy of the GNU General Public License along
17 // with this library; see the file COPYING. If not, write to the Free
18 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
19 // USA.
21 // As a special exception, you may use this file as part of a free software
22 // library without restriction. Specifically, if other files instantiate
23 // templates or use macros or inline functions from this file, or you compile
24 // this file and link it with other files to produce an executable, this
25 // file does not by itself cause the resulting executable to be covered by
26 // the GNU General Public License. This exception does not however
27 // invalidate any other reasons why the executable file might be covered by
28 // the GNU General Public License.
30 /** @file stream_iterator.h
31 * This is an internal header file, included by other library headers.
32 * You should not attempt to use it directly.
35 #ifndef _STREAM_ITERATOR_H
36 #define _STREAM_ITERATOR_H 1
38 #pragma GCC system_header
40 #include <debug/debug.h>
42 namespace std
44 template<typename _Tp, typename _CharT = char,
45 typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
46 class istream_iterator
47 : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
49 public:
50 typedef _CharT char_type;
51 typedef _Traits traits_type;
52 typedef basic_istream<_CharT, _Traits> istream_type;
54 private:
55 istream_type* _M_stream;
56 _Tp _M_value;
57 bool _M_ok;
59 public:
60 istream_iterator() : _M_stream(0), _M_ok(false) {}
62 istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
64 istream_iterator(const istream_iterator& __obj)
65 : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
66 _M_ok(__obj._M_ok)
67 { }
69 const _Tp&
70 operator*() const
72 __glibcxx_requires_cond(_M_ok,
73 _M_message(__gnu_debug::__msg_deref_istream)
74 ._M_iterator(*this));
75 return _M_value;
78 const _Tp*
79 operator->() const { return &(operator*()); }
81 istream_iterator&
82 operator++()
84 __glibcxx_requires_cond(_M_ok,
85 _M_message(__gnu_debug::__msg_inc_istream)
86 ._M_iterator(*this));
87 _M_read();
88 return *this;
91 istream_iterator
92 operator++(int)
94 __glibcxx_requires_cond(_M_ok,
95 _M_message(__gnu_debug::__msg_inc_istream)
96 ._M_iterator(*this));
97 istream_iterator __tmp = *this;
98 _M_read();
99 return __tmp;
102 bool
103 _M_equal(const istream_iterator& __x) const
104 { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream);}
106 private:
107 void
108 _M_read()
110 _M_ok = (_M_stream && *_M_stream) ? true : false;
111 if (_M_ok)
113 *_M_stream >> _M_value;
114 _M_ok = *_M_stream ? true : false;
119 template<typename _Tp, typename _CharT, typename _Traits, typename _Dist>
120 inline bool
121 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
122 const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
123 { return __x._M_equal(__y); }
125 template <class _Tp, class _CharT, class _Traits, class _Dist>
126 inline bool
127 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
128 const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
129 { return !__x._M_equal(__y); }
132 template<typename _Tp, typename _CharT = char,
133 typename _Traits = char_traits<_CharT> >
134 class ostream_iterator
135 : public iterator<output_iterator_tag, void, void, void, void>
137 public:
138 typedef _CharT char_type;
139 typedef _Traits traits_type;
140 typedef basic_ostream<_CharT, _Traits> ostream_type;
142 private:
143 ostream_type* _M_stream;
144 const _CharT* _M_string;
146 public:
147 ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
149 ostream_iterator(ostream_type& __s, const _CharT* __c)
150 : _M_stream(&__s), _M_string(__c) { }
152 ostream_iterator(const ostream_iterator& __obj)
153 : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
155 ostream_iterator&
156 operator=(const _Tp& __value)
158 __glibcxx_requires_cond(_M_stream != 0,
159 _M_message(__gnu_debug::__msg_output_ostream)
160 ._M_iterator(*this));
161 *_M_stream << __value;
162 if (_M_string) *_M_stream << _M_string;
163 return *this;
166 ostream_iterator&
167 operator*() { return *this; }
169 ostream_iterator&
170 operator++() { return *this; }
172 ostream_iterator&
173 operator++(int) { return *this; }
175 } // namespace std
176 #endif