1/*
2 *
3 * Copyright (c) 1994
4 * Hewlett-Packard Company
5 *
6 * Copyright (c) 1996-1998
7 * Silicon Graphics Computer Systems, Inc.
8 *
9 * Copyright (c) 1997
10 * Moscow Center for SPARC Technology
11 *
12 * Copyright (c) 1999
13 * Boris Fomitchev
14 *
15 * This material is provided "as is", with absolutely no warranty expressed
16 * or implied. Any use is at your own risk.
17 *
18 * Permission to use or copy this software for any purpose is hereby granted
19 * without fee, provided the above notices are retained on all copies.
20 * Permission to modify the code and to distribute modified code is granted,
21 * provided the above notices are retained, and a notice that the code was
22 * modified is included with the above copyright notice.
23 *
24 */
25
26/* NOTE: This is an internal header file, included by other STL headers.
27 *   You should not attempt to use it directly.
28 */
29
30#if !defined (_STLP_INTERNAL_STREAM_ITERATOR_H) && !defined (_STLP_USE_NO_IOSTREAMS)
31#define _STLP_INTERNAL_STREAM_ITERATOR_H
32
33#ifndef _STLP_INTERNAL_ITERATOR_BASE_H
34#  include <stl/_iterator_base.h>
35#endif
36
37// streambuf_iterators predeclarations must appear first
38#ifndef _STLP_INTERNAL_IOSFWD
39#  include <stl/_iosfwd.h>
40#endif
41
42#ifndef _STLP_INTERNAL_ALGOBASE_H
43#  include <stl/_algobase.h>
44#endif
45
46#ifndef _STLP_INTERNAL_OSTREAMBUF_ITERATOR_H
47#  include <stl/_ostreambuf_iterator.h>
48#endif
49
50#ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
51#  include <stl/_istreambuf_iterator.h>
52#endif
53
54#ifndef _STLP_INTERNAL_ISTREAM
55#  include <stl/_istream.h>
56#endif
57
58// istream_iterator and ostream_iterator look very different if we're
59// using new, templatized iostreams than if we're using the old cfront
60// version.
61
62_STLP_BEGIN_NAMESPACE
63
64#if !defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
65#  define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _CharT, class _Traits, class _Dist
66#  define __ISI_TMPL_ARGUMENTS _Tp, _CharT, _Traits, _Dist
67template <class _Tp,
68          class _CharT = char, class _Traits = char_traits<_CharT>,
69          class _Dist = ptrdiff_t>
70class istream_iterator : public iterator<input_iterator_tag, _Tp , _Dist,
71                                         const _Tp*, const _Tp& > {
72#else
73#  if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && !defined (_STLP_DEFAULT_TYPE_PARAM)
74#    define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
75#    define __ISI_TMPL_ARGUMENTS        _Tp
76template <class _Tp>
77class istream_iterator : public iterator<input_iterator_tag, _Tp , ptrdiff_t,
78                                         const _Tp*, const _Tp& > {
79#  else
80#    define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
81#    define __ISI_TMPL_ARGUMENTS        _Tp, _Dist
82template <class _Tp, _STLP_DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
83class istream_iterator : public iterator<input_iterator_tag, _Tp, _Dist ,
84                                         const _Tp*, const _Tp& > {
85#  endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
86#endif /* _STLP_LIMITED_DEFAULT_TEMPLATES */
87
88#if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
89  typedef char _CharT;
90  typedef char_traits<char> _Traits;
91#  if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && !defined (_STLP_DEFAULT_TYPE_PARAM)
92  typedef ptrdiff_t _Dist;
93#  endif
94#endif
95
96  typedef istream_iterator< __ISI_TMPL_ARGUMENTS > _Self;
97public:
98  typedef _CharT                         char_type;
99  typedef _Traits                        traits_type;
100  typedef basic_istream<_CharT, _Traits> istream_type;
101
102  typedef input_iterator_tag             iterator_category;
103  typedef _Tp                            value_type;
104  typedef _Dist                          difference_type;
105  typedef const _Tp*                     pointer;
106  typedef const _Tp&                     reference;
107
108  istream_iterator() : _M_stream(0), _M_ok(false), _M_read_done(true) {}
109  istream_iterator(istream_type& __s) : _M_stream(&__s), _M_ok(false), _M_read_done(false) {}
110
111  reference operator*() const {
112    if (!_M_read_done) {
113      _M_read();
114    }
115    return _M_value;
116  }
117
118  _STLP_DEFINE_ARROW_OPERATOR
119
120  _Self& operator++() {
121    _M_read();
122    return *this;
123  }
124  _Self operator++(int)  {
125    _Self __tmp = *this;
126    _M_read();
127    return __tmp;
128  }
129
130  bool _M_equal(const _Self& __x) const {
131    if (!_M_read_done) {
132      _M_read();
133    }
134    if (!__x._M_read_done) {
135      __x._M_read();
136    }
137    return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream);
138  }
139
140private:
141  istream_type* _M_stream;
142  mutable _Tp _M_value;
143  mutable bool _M_ok;
144  mutable bool _M_read_done;
145
146  void _M_read() const {
147    _STLP_MUTABLE(_Self, _M_ok) = ((_M_stream != 0) && !_M_stream->fail());
148    if (_M_ok) {
149      *_M_stream >> _STLP_MUTABLE(_Self, _M_value);
150      _STLP_MUTABLE(_Self, _M_ok) = !_M_stream->fail();
151    }
152    _STLP_MUTABLE(_Self, _M_read_done) = true;
153  }
154};
155
156#if !defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
157template <class _TpP,
158          class _CharT = char, class _Traits = char_traits<_CharT> >
159#else
160template <class _TpP>
161#endif
162class ostream_iterator: public iterator<output_iterator_tag, void, void, void, void> {
163#if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
164  typedef char _CharT;
165  typedef char_traits<char> _Traits;
166  typedef ostream_iterator<_TpP> _Self;
167#else
168  typedef ostream_iterator<_TpP, _CharT, _Traits> _Self;
169#endif
170public:
171  typedef _CharT                         char_type;
172  typedef _Traits                        traits_type;
173  typedef basic_ostream<_CharT, _Traits> ostream_type;
174
175  typedef output_iterator_tag            iterator_category;
176
177  ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
178  ostream_iterator(ostream_type& __s, const _CharT* __c)
179    : _M_stream(&__s), _M_string(__c)  {}
180  _Self& operator=(const _TpP& __val) {
181    *_M_stream << __val;
182    if (_M_string) *_M_stream << _M_string;
183    return *this;
184  }
185  _Self& operator*() { return *this; }
186  _Self& operator++() { return *this; }
187  _Self& operator++(int) { return *this; }
188private:
189  ostream_type* _M_stream;
190  const _CharT* _M_string;
191};
192
193#if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
194#  if defined (_STLP_LIMITED_DEFAULT_TEMPLATES)
195template <class _TpP>
196inline output_iterator_tag _STLP_CALL
197iterator_category(const ostream_iterator<_TpP>&) { return output_iterator_tag(); }
198#  else
199template <class _TpP, class _CharT, class _Traits>
200inline output_iterator_tag _STLP_CALL
201iterator_category(const ostream_iterator<_TpP, _CharT, _Traits>&) { return output_iterator_tag(); }
202#  endif
203#endif
204
205_STLP_END_NAMESPACE
206
207// form-independent definiotion of stream iterators
208_STLP_BEGIN_NAMESPACE
209
210template < __ISI_TMPL_HEADER_ARGUMENTS >
211inline bool _STLP_CALL
212operator==(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
213           const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y)
214{ return __x._M_equal(__y); }
215
216#if defined (_STLP_USE_SEPARATE_RELOPS_NAMESPACE)
217template < __ISI_TMPL_HEADER_ARGUMENTS >
218inline bool _STLP_CALL
219operator!=(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
220           const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y)
221{ return !__x._M_equal(__y); }
222#endif
223
224#if defined (_STLP_USE_OLD_HP_ITERATOR_QUERIES)
225template < __ISI_TMPL_HEADER_ARGUMENTS >
226inline input_iterator_tag _STLP_CALL
227iterator_category(const istream_iterator< __ISI_TMPL_ARGUMENTS >&)
228{ return input_iterator_tag(); }
229template < __ISI_TMPL_HEADER_ARGUMENTS >
230inline _Tp* _STLP_CALL
231value_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Tp*) 0; }
232
233#  if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && !defined (_STLP_DEFAULT_TYPE_PARAM)
234template < __ISI_TMPL_HEADER_ARGUMENTS >
235inline ptrdiff_t* _STLP_CALL
236distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (ptrdiff_t*)0; }
237#  else
238template < __ISI_TMPL_HEADER_ARGUMENTS >
239inline _Dist* _STLP_CALL
240distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Dist*)0; }
241#  endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
242#endif
243
244_STLP_END_NAMESPACE
245
246#undef __ISI_TMPL_HEADER_ARGUMENTS
247#undef __ISI_TMPL_ARGUMENTS
248
249#endif /* _STLP_INTERNAL_STREAM_ITERATOR_H */
250
251// Local Variables:
252// mode:C++
253// End:
254