OSDN Git Service

2009-11-06 Benjamin Kosnik <bkoz@redhat.com>
[pf3gnuchains/gcc-fork.git] / libstdc++-v3 / include / bits / stream_iterator.h
1 // Stream iterators
2
3 // Copyright (C) 2001, 2004, 2005, 2009 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 3, or (at your option)
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
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/>.
24
25 /** @file stream_iterator.h
26  *  This is an internal header file, included by other library headers.
27  *  You should not attempt to use it directly.
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 _GLIBCXX_BEGIN_NAMESPACE(std)
38
39   /**
40    * @addtogroup iterators
41    * @{
42    */
43
44   /// Provides input iterator semantics for streams.
45   template<typename _Tp, typename _CharT = char,
46            typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
47     class istream_iterator
48     : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
49     {
50     public:
51       typedef _CharT                         char_type;
52       typedef _Traits                        traits_type;
53       typedef basic_istream<_CharT, _Traits> istream_type;
54
55     private:
56       istream_type*     _M_stream;
57       _Tp               _M_value;
58       bool              _M_ok;
59
60     public:
61       ///  Construct end of input stream iterator.
62       istream_iterator()
63       : _M_stream(0), _M_value(), _M_ok(false) {}
64
65       ///  Construct start of input stream iterator.
66       istream_iterator(istream_type& __s)
67       : _M_stream(&__s)
68       { _M_read(); }
69
70       istream_iterator(const istream_iterator& __obj)
71       : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
72         _M_ok(__obj._M_ok)
73       { }
74
75       const _Tp&
76       operator*() const
77       {
78         __glibcxx_requires_cond(_M_ok,
79                                 _M_message(__gnu_debug::__msg_deref_istream)
80                                 ._M_iterator(*this));
81         return _M_value;
82       }
83
84       const _Tp*
85       operator->() const { return &(operator*()); }
86
87       istream_iterator&
88       operator++()
89       {
90         __glibcxx_requires_cond(_M_ok,
91                                 _M_message(__gnu_debug::__msg_inc_istream)
92                                 ._M_iterator(*this));
93         _M_read();
94         return *this;
95       }
96
97       istream_iterator
98       operator++(int)
99       {
100         __glibcxx_requires_cond(_M_ok,
101                                 _M_message(__gnu_debug::__msg_inc_istream)
102                                 ._M_iterator(*this));
103         istream_iterator __tmp = *this;
104         _M_read();
105         return __tmp;
106       }
107
108       bool
109       _M_equal(const istream_iterator& __x) const
110       { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
111
112     private:
113       void
114       _M_read()
115       {
116         _M_ok = (_M_stream && *_M_stream) ? true : false;
117         if (_M_ok)
118           {
119             *_M_stream >> _M_value;
120             _M_ok = *_M_stream ? true : false;
121           }
122       }
123     };
124
125   ///  Return true if x and y are both end or not end, or x and y are the same.
126   template<typename _Tp, typename _CharT, typename _Traits, typename _Dist>
127     inline bool
128     operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
129                const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
130     { return __x._M_equal(__y); }
131
132   ///  Return false if x and y are both end or not end, or x and y are the same.
133   template <class _Tp, class _CharT, class _Traits, class _Dist>
134     inline bool
135     operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
136                const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
137     { return !__x._M_equal(__y); }
138
139   /**
140    *  @brief  Provides output iterator semantics for streams.
141    *
142    *  This class provides an iterator to write to an ostream.  The type Tp is
143    *  the only type written by this iterator and there must be an
144    *  operator<<(Tp) defined.
145    *
146    *  @param  Tp  The type to write to the ostream.
147    *  @param  CharT  The ostream char_type.
148    *  @param  Traits  The ostream char_traits.
149   */
150   template<typename _Tp, typename _CharT = char,
151            typename _Traits = char_traits<_CharT> >
152     class ostream_iterator
153     : public iterator<output_iterator_tag, void, void, void, void>
154     {
155     public:
156       //@{
157       /// Public typedef
158       typedef _CharT                         char_type;
159       typedef _Traits                        traits_type;
160       typedef basic_ostream<_CharT, _Traits> ostream_type;
161       //@}
162
163     private:
164       ostream_type*     _M_stream;
165       const _CharT*     _M_string;
166
167     public:
168       /// Construct from an ostream.
169       ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
170
171       /**
172        *  Construct from an ostream.
173        *
174        *  The delimiter string @a c is written to the stream after every Tp
175        *  written to the stream.  The delimiter is not copied, and thus must
176        *  not be destroyed while this iterator is in use.
177        *
178        *  @param  s  Underlying ostream to write to.
179        *  @param  c  CharT delimiter string to insert.
180       */
181       ostream_iterator(ostream_type& __s, const _CharT* __c)
182       : _M_stream(&__s), _M_string(__c)  { }
183
184       /// Copy constructor.
185       ostream_iterator(const ostream_iterator& __obj)
186       : _M_stream(__obj._M_stream), _M_string(__obj._M_string)  { }
187
188       /// Writes @a value to underlying ostream using operator<<.  If
189       /// constructed with delimiter string, writes delimiter to ostream.
190       ostream_iterator&
191       operator=(const _Tp& __value)
192       {
193         __glibcxx_requires_cond(_M_stream != 0,
194                                 _M_message(__gnu_debug::__msg_output_ostream)
195                                 ._M_iterator(*this));
196         *_M_stream << __value;
197         if (_M_string) *_M_stream << _M_string;
198         return *this;
199       }
200
201       ostream_iterator&
202       operator*()
203       { return *this; }
204
205       ostream_iterator&
206       operator++()
207       { return *this; }
208
209       ostream_iterator&
210       operator++(int)
211       { return *this; }
212     };
213
214   // @} group iterators
215
216 _GLIBCXX_END_NAMESPACE
217
218 #endif