1*404b540aSrobert // Stream iterators
2*404b540aSrobert
3*404b540aSrobert // Copyright (C) 2001, 2004, 2005 Free Software Foundation, Inc.
4*404b540aSrobert //
5*404b540aSrobert // This file is part of the GNU ISO C++ Library. This library is free
6*404b540aSrobert // software; you can redistribute it and/or modify it under the
7*404b540aSrobert // terms of the GNU General Public License as published by the
8*404b540aSrobert // Free Software Foundation; either version 2, or (at your option)
9*404b540aSrobert // any later version.
10*404b540aSrobert
11*404b540aSrobert // This library is distributed in the hope that it will be useful,
12*404b540aSrobert // but WITHOUT ANY WARRANTY; without even the implied warranty of
13*404b540aSrobert // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14*404b540aSrobert // GNU General Public License for more details.
15*404b540aSrobert
16*404b540aSrobert // You should have received a copy of the GNU General Public License along
17*404b540aSrobert // with this library; see the file COPYING. If not, write to the Free
18*404b540aSrobert // Software Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
19*404b540aSrobert // USA.
20*404b540aSrobert
21*404b540aSrobert // As a special exception, you may use this file as part of a free software
22*404b540aSrobert // library without restriction. Specifically, if other files instantiate
23*404b540aSrobert // templates or use macros or inline functions from this file, or you compile
24*404b540aSrobert // this file and link it with other files to produce an executable, this
25*404b540aSrobert // file does not by itself cause the resulting executable to be covered by
26*404b540aSrobert // the GNU General Public License. This exception does not however
27*404b540aSrobert // invalidate any other reasons why the executable file might be covered by
28*404b540aSrobert // the GNU General Public License.
29*404b540aSrobert
30*404b540aSrobert /** @file stream_iterator.h
31*404b540aSrobert * This is an internal header file, included by other library headers.
32*404b540aSrobert * You should not attempt to use it directly.
33*404b540aSrobert */
34*404b540aSrobert
35*404b540aSrobert #ifndef _STREAM_ITERATOR_H
36*404b540aSrobert #define _STREAM_ITERATOR_H 1
37*404b540aSrobert
38*404b540aSrobert #pragma GCC system_header
39*404b540aSrobert
40*404b540aSrobert #include <debug/debug.h>
41*404b540aSrobert
_GLIBCXX_BEGIN_NAMESPACE(std)42*404b540aSrobert _GLIBCXX_BEGIN_NAMESPACE(std)
43*404b540aSrobert
44*404b540aSrobert /// Provides input iterator semantics for streams.
45*404b540aSrobert template<typename _Tp, typename _CharT = char,
46*404b540aSrobert typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
47*404b540aSrobert class istream_iterator
48*404b540aSrobert : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
49*404b540aSrobert {
50*404b540aSrobert public:
51*404b540aSrobert typedef _CharT char_type;
52*404b540aSrobert typedef _Traits traits_type;
53*404b540aSrobert typedef basic_istream<_CharT, _Traits> istream_type;
54*404b540aSrobert
55*404b540aSrobert private:
56*404b540aSrobert istream_type* _M_stream;
57*404b540aSrobert _Tp _M_value;
58*404b540aSrobert bool _M_ok;
59*404b540aSrobert
60*404b540aSrobert public:
61*404b540aSrobert /// Construct end of input stream iterator.
62*404b540aSrobert istream_iterator()
63*404b540aSrobert : _M_stream(0), _M_value(), _M_ok(false) {}
64*404b540aSrobert
65*404b540aSrobert /// Construct start of input stream iterator.
66*404b540aSrobert istream_iterator(istream_type& __s)
67*404b540aSrobert : _M_stream(&__s)
68*404b540aSrobert { _M_read(); }
69*404b540aSrobert
70*404b540aSrobert istream_iterator(const istream_iterator& __obj)
71*404b540aSrobert : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
72*404b540aSrobert _M_ok(__obj._M_ok)
73*404b540aSrobert { }
74*404b540aSrobert
75*404b540aSrobert const _Tp&
76*404b540aSrobert operator*() const
77*404b540aSrobert {
78*404b540aSrobert __glibcxx_requires_cond(_M_ok,
79*404b540aSrobert _M_message(__gnu_debug::__msg_deref_istream)
80*404b540aSrobert ._M_iterator(*this));
81*404b540aSrobert return _M_value;
82*404b540aSrobert }
83*404b540aSrobert
84*404b540aSrobert const _Tp*
85*404b540aSrobert operator->() const { return &(operator*()); }
86*404b540aSrobert
87*404b540aSrobert istream_iterator&
88*404b540aSrobert operator++()
89*404b540aSrobert {
90*404b540aSrobert __glibcxx_requires_cond(_M_ok,
91*404b540aSrobert _M_message(__gnu_debug::__msg_inc_istream)
92*404b540aSrobert ._M_iterator(*this));
93*404b540aSrobert _M_read();
94*404b540aSrobert return *this;
95*404b540aSrobert }
96*404b540aSrobert
97*404b540aSrobert istream_iterator
98*404b540aSrobert operator++(int)
99*404b540aSrobert {
100*404b540aSrobert __glibcxx_requires_cond(_M_ok,
101*404b540aSrobert _M_message(__gnu_debug::__msg_inc_istream)
102*404b540aSrobert ._M_iterator(*this));
103*404b540aSrobert istream_iterator __tmp = *this;
104*404b540aSrobert _M_read();
105*404b540aSrobert return __tmp;
106*404b540aSrobert }
107*404b540aSrobert
108*404b540aSrobert bool
109*404b540aSrobert _M_equal(const istream_iterator& __x) const
110*404b540aSrobert { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
111*404b540aSrobert
112*404b540aSrobert private:
113*404b540aSrobert void
114*404b540aSrobert _M_read()
115*404b540aSrobert {
116*404b540aSrobert _M_ok = (_M_stream && *_M_stream) ? true : false;
117*404b540aSrobert if (_M_ok)
118*404b540aSrobert {
119*404b540aSrobert *_M_stream >> _M_value;
120*404b540aSrobert _M_ok = *_M_stream ? true : false;
121*404b540aSrobert }
122*404b540aSrobert }
123*404b540aSrobert };
124*404b540aSrobert
125*404b540aSrobert /// Return true if x and y are both end or not end, or x and y are the same.
126*404b540aSrobert template<typename _Tp, typename _CharT, typename _Traits, typename _Dist>
127*404b540aSrobert inline bool
128*404b540aSrobert operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
129*404b540aSrobert const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
130*404b540aSrobert { return __x._M_equal(__y); }
131*404b540aSrobert
132*404b540aSrobert /// Return false if x and y are both end or not end, or x and y are the same.
133*404b540aSrobert template <class _Tp, class _CharT, class _Traits, class _Dist>
134*404b540aSrobert inline bool
135*404b540aSrobert operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
136*404b540aSrobert const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
137*404b540aSrobert { return !__x._M_equal(__y); }
138*404b540aSrobert
139*404b540aSrobert /**
140*404b540aSrobert * @brief Provides output iterator semantics for streams.
141*404b540aSrobert *
142*404b540aSrobert * This class provides an iterator to write to an ostream. The type Tp is
143*404b540aSrobert * the only type written by this iterator and there must be an
144*404b540aSrobert * operator<<(Tp) defined.
145*404b540aSrobert *
146*404b540aSrobert * @param Tp The type to write to the ostream.
147*404b540aSrobert * @param CharT The ostream char_type.
148*404b540aSrobert * @param Traits The ostream char_traits.
149*404b540aSrobert */
150*404b540aSrobert template<typename _Tp, typename _CharT = char,
151*404b540aSrobert typename _Traits = char_traits<_CharT> >
152*404b540aSrobert class ostream_iterator
153*404b540aSrobert : public iterator<output_iterator_tag, void, void, void, void>
154*404b540aSrobert {
155*404b540aSrobert public:
156*404b540aSrobert //@{
157*404b540aSrobert /// Public typedef
158*404b540aSrobert typedef _CharT char_type;
159*404b540aSrobert typedef _Traits traits_type;
160*404b540aSrobert typedef basic_ostream<_CharT, _Traits> ostream_type;
161*404b540aSrobert //@}
162*404b540aSrobert
163*404b540aSrobert private:
164*404b540aSrobert ostream_type* _M_stream;
165*404b540aSrobert const _CharT* _M_string;
166*404b540aSrobert
167*404b540aSrobert public:
168*404b540aSrobert /// Construct from an ostream.
ostream_iterator(ostream_type & __s)169*404b540aSrobert ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
170*404b540aSrobert
171*404b540aSrobert /**
172*404b540aSrobert * Construct from an ostream.
173*404b540aSrobert *
174*404b540aSrobert * The delimiter string @a c is written to the stream after every Tp
175*404b540aSrobert * written to the stream. The delimiter is not copied, and thus must
176*404b540aSrobert * not be destroyed while this iterator is in use.
177*404b540aSrobert *
178*404b540aSrobert * @param s Underlying ostream to write to.
179*404b540aSrobert * @param c CharT delimiter string to insert.
180*404b540aSrobert */
ostream_iterator(ostream_type & __s,const _CharT * __c)181*404b540aSrobert ostream_iterator(ostream_type& __s, const _CharT* __c)
182*404b540aSrobert : _M_stream(&__s), _M_string(__c) { }
183*404b540aSrobert
184*404b540aSrobert /// Copy constructor.
ostream_iterator(const ostream_iterator & __obj)185*404b540aSrobert ostream_iterator(const ostream_iterator& __obj)
186*404b540aSrobert : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
187*404b540aSrobert
188*404b540aSrobert /// Writes @a value to underlying ostream using operator<<. If
189*404b540aSrobert /// constructed with delimiter string, writes delimiter to ostream.
190*404b540aSrobert ostream_iterator&
191*404b540aSrobert operator=(const _Tp& __value)
192*404b540aSrobert {
193*404b540aSrobert __glibcxx_requires_cond(_M_stream != 0,
194*404b540aSrobert _M_message(__gnu_debug::__msg_output_ostream)
195*404b540aSrobert ._M_iterator(*this));
196*404b540aSrobert *_M_stream << __value;
197*404b540aSrobert if (_M_string) *_M_stream << _M_string;
198*404b540aSrobert return *this;
199*404b540aSrobert }
200*404b540aSrobert
201*404b540aSrobert ostream_iterator&
202*404b540aSrobert operator*()
203*404b540aSrobert { return *this; }
204*404b540aSrobert
205*404b540aSrobert ostream_iterator&
206*404b540aSrobert operator++()
207*404b540aSrobert { return *this; }
208*404b540aSrobert
209*404b540aSrobert ostream_iterator&
210*404b540aSrobert operator++(int)
211*404b540aSrobert { return *this; }
212*404b540aSrobert };
213*404b540aSrobert
214*404b540aSrobert _GLIBCXX_END_NAMESPACE
215*404b540aSrobert
216*404b540aSrobert #endif
217