4 * Hewlett-Packard Company
6 * Copyright (c) 1996-1998
7 * Silicon Graphics Computer Systems, Inc.
10 * Moscow Center for SPARC Technology
15 * This material is provided "as is", with absolutely no warranty expressed
16 * or implied. Any use is at your own risk.
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.
26 /* NOTE: This is an internal header file, included by other STL headers.
27 * You should not attempt to use it directly.
30 #if !defined (_STLP_INTERNAL_STREAM_ITERATOR_H) && ! defined (_STLP_USE_NO_IOSTREAMS)
31 #define _STLP_INTERNAL_STREAM_ITERATOR_H
33 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
34 # include <stl/_iterator_base.h>
37 // streambuf_iterators predeclarations must appear first
42 #ifndef _STLP_INTERNAL_ALGOBASE_H
43 #include <stl/_algobase.h>
46 #if defined (_STLP_OWN_IOSTREAMS)
48 #ifndef _STLP_INTERNAL_OSTREAMBUF_ITERATOR_H
49 # include <stl/_ostreambuf_iterator.h>
52 #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
53 # include <stl/_istreambuf_iterator.h>
56 #ifndef _STLP_INTERNAL_ISTREAM_H
57 # include <stl/_istream.h>
59 #endif /* _STLP_OWN_IOSTREAMS */
61 // istream_iterator and ostream_iterator look very different if we're
62 // using new, templatized iostreams than if we're using the old cfront
65 # if defined (_STLP_USE_NEW_IOSTREAMS)
69 # ifndef _STLP_LIMITED_DEFAULT_TEMPLATES
71 class _CharT = _STLP_DEFAULTCHAR, class _Traits = char_traits<_CharT>,
72 class _Dist = ptrdiff_t>
73 # define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _CharT, class _Traits, class _Dist
74 # define __ISI_TMPL_ARGUMENTS _Tp, _CharT, _Traits, _Dist
75 class istream_iterator : public iterator<input_iterator_tag, _Tp , _Dist,
76 const _Tp*, const _Tp& > {
79 # if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
80 # define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
81 # define __ISI_TMPL_ARGUMENTS _Tp
83 class istream_iterator : public iterator<input_iterator_tag, _Tp , ptrdiff_t,
84 const _Tp*, const _Tp& > {
86 # define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
87 # define __ISI_TMPL_ARGUMENTS _Tp, _Dist
88 template <class _Tp,__DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
89 class istream_iterator : public iterator<input_iterator_tag, _Tp, _Dist ,
90 const _Tp*, const _Tp& > {
91 # endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
93 # endif /* _STLP_LIMITED_DEFAULT_TEMPLATES */
95 # ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
97 typedef char_traits<char> _Traits;
98 # if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
99 typedef ptrdiff_t _Dist;
103 typedef istream_iterator< __ISI_TMPL_ARGUMENTS > _Self;
105 typedef _CharT char_type;
106 typedef _Traits traits_type;
107 typedef basic_istream<_CharT, _Traits> istream_type;
109 typedef input_iterator_tag iterator_category;
110 typedef _Tp value_type;
111 typedef _Dist difference_type;
112 typedef const _Tp* pointer;
113 typedef const _Tp& reference;
115 istream_iterator() : _M_stream(0), _M_ok(false) {}
116 istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
118 reference operator*() const { return _M_value; }
120 _STLP_DEFINE_ARROW_OPERATOR
122 _Self& operator++() {
126 _Self operator++(int) {
132 bool _M_equal(const _Self& __x) const
133 { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
136 istream_type* _M_stream;
141 _M_ok = (_M_stream && *_M_stream) ? true : false;
143 *_M_stream >> _M_value;
144 _M_ok = *_M_stream ? true : false;
149 #ifndef _STLP_LIMITED_DEFAULT_TEMPLATES
150 template <class _TpP,
151 class _CharT = _STLP_DEFAULTCHAR, class _Traits = char_traits<_CharT> >
153 template <class _TpP>
155 class ostream_iterator: public iterator<output_iterator_tag, void, void, void, void> {
156 # ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
158 typedef char_traits<char> _Traits;
159 typedef ostream_iterator<_TpP> _Self;
161 typedef ostream_iterator<_TpP, _CharT, _Traits> _Self;
164 typedef _CharT char_type;
165 typedef _Traits traits_type;
166 typedef basic_ostream<_CharT, _Traits> ostream_type;
168 typedef output_iterator_tag iterator_category;
170 ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
171 ostream_iterator(ostream_type& __s, const _CharT* __c)
172 : _M_stream(&__s), _M_string(__c) {}
173 _Self& operator=(const _TpP& __val) {
175 if (_M_string) *_M_stream << _M_string;
178 _Self& operator*() { return *this; }
179 _Self& operator++() { return *this; }
180 _Self& operator++(int) { return *this; }
182 ostream_type* _M_stream;
183 const _CharT* _M_string;
186 # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
187 # ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
188 template <class _TpP>
189 inline output_iterator_tag _STLP_CALL
190 iterator_category(const ostream_iterator<_TpP>&) { return output_iterator_tag(); }
192 template <class _TpP, class _CharT, class _Traits>
193 inline output_iterator_tag _STLP_CALL
194 iterator_category(const ostream_iterator<_TpP, _CharT, _Traits>&) { return output_iterator_tag(); }
200 # elif ! defined(_STLP_USE_NO_IOSTREAMS)
202 _STLP_BEGIN_NAMESPACE
204 # if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
205 # define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
206 # define __ISI_TMPL_ARGUMENTS _Tp
208 class istream_iterator : public iterator<input_iterator_tag, _Tp, ptrdiff_t,
209 const _Tp*, const _Tp& > {
211 # define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
212 # define __ISI_TMPL_ARGUMENTS _Tp, _Dist
213 template <class _Tp, __DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
214 class istream_iterator : public iterator<input_iterator_tag, _Tp, _Dist,
215 const _Tp*, const _Tp& > {
223 _M_end_marker = (*_M_stream) ? true : false;
224 if (_M_end_marker) *_M_stream >> _M_value;
225 _M_end_marker = (*_M_stream) ? true : false;
228 typedef input_iterator_tag iterator_category;
229 typedef _Tp value_type;
230 typedef _Dist difference_type;
231 typedef const _Tp* pointer;
232 typedef const _Tp& reference;
234 istream_iterator() : _M_stream(&cin), _M_end_marker(false) {}
235 istream_iterator(istream& __s) : _M_stream(&__s) { _M_read(); }
236 reference operator*() const { return _M_value; }
238 _STLP_DEFINE_ARROW_OPERATOR
240 istream_iterator< __ISI_TMPL_ARGUMENTS >& operator++() {
244 istream_iterator< __ISI_TMPL_ARGUMENTS > operator++(int) {
245 istream_iterator< __ISI_TMPL_ARGUMENTS > __tmp = *this;
249 inline bool _M_equal(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) const {
250 return (_M_stream == __y._M_stream &&
251 _M_end_marker == __y._M_end_marker) ||
252 _M_end_marker == false && __y._M_end_marker == false;
257 class ostream_iterator {
260 const char* _M_string;
262 typedef output_iterator_tag iterator_category;
263 # ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
264 typedef void value_type;
265 typedef void difference_type;
266 typedef void pointer;
267 typedef void reference;
269 ostream_iterator(ostream& __s) : _M_stream(&__s), _M_string(0) {}
270 ostream_iterator(ostream& __s, const char* __c)
271 : _M_stream(&__s), _M_string(__c) {}
272 ostream_iterator<_Tp>& operator=(const _Tp& __val) {
274 if (_M_string) *_M_stream << _M_string;
277 ostream_iterator<_Tp>& operator*() { return *this; }
278 ostream_iterator<_Tp>& operator++() { return *this; }
279 ostream_iterator<_Tp>& operator++(int) { return *this; }
282 # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
283 template <class _Tp> inline output_iterator_tag
284 iterator_category(const ostream_iterator<_Tp>&) { return output_iterator_tag(); }
289 #endif /* _STLP_USE_NEW_IOSTREAMS */
291 // form-independent definiotion of stream iterators
292 _STLP_BEGIN_NAMESPACE
294 template < __ISI_TMPL_HEADER_ARGUMENTS >
295 inline bool _STLP_CALL
296 operator==(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
297 const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) {
298 return __x._M_equal(__y);
301 # ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
303 template < __ISI_TMPL_HEADER_ARGUMENTS >
304 inline bool _STLP_CALL
305 operator!=(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
306 const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) {
307 return !__x._M_equal(__y);
310 # endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
312 # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
313 template < __ISI_TMPL_HEADER_ARGUMENTS >
314 inline input_iterator_tag _STLP_CALL
315 iterator_category(const istream_iterator< __ISI_TMPL_ARGUMENTS >&)
316 { return input_iterator_tag(); }
317 template < __ISI_TMPL_HEADER_ARGUMENTS >
318 inline _Tp* _STLP_CALL
319 value_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Tp*) 0; }
321 # if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
322 template < __ISI_TMPL_HEADER_ARGUMENTS >
323 inline ptrdiff_t* _STLP_CALL
324 distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (ptrdiff_t*)0; }
326 template < __ISI_TMPL_HEADER_ARGUMENTS >
327 inline _Dist* _STLP_CALL
328 distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Dist*)0; }
329 # endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
335 # undef __ISI_TMPL_HEADER_ARGUMENTS
336 # undef __ISI_TMPL_ARGUMENTS
339 #endif /* _STLP_INTERNAL_STREAM_ITERATOR_H */