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