_stream_iterator.h

00001 /*
00002  *
00003  * Copyright (c) 1994
00004  * Hewlett-Packard Company
00005  *
00006  * Copyright (c) 1996-1998
00007  * Silicon Graphics Computer Systems, Inc.
00008  *
00009  * Copyright (c) 1997
00010  * Moscow Center for SPARC Technology
00011  *
00012  * Copyright (c) 1999 
00013  * Boris Fomitchev
00014  *
00015  * This material is provided "as is", with absolutely no warranty expressed
00016  * or implied. Any use is at your own risk.
00017  *
00018  * Permission to use or copy this software for any purpose is hereby granted 
00019  * without fee, provided the above notices are retained on all copies.
00020  * Permission to modify the code and to distribute modified code is granted,
00021  * provided the above notices are retained, and a notice that the code was
00022  * modified is included with the above copyright notice.
00023  *
00024  */
00025 
00026 /* NOTE: This is an internal header file, included by other STL headers.
00027  *   You should not attempt to use it directly.
00028  */
00029 
00030 #if !defined (_STLP_INTERNAL_STREAM_ITERATOR_H) && ! defined (_STLP_USE_NO_IOSTREAMS)
00031 #define _STLP_INTERNAL_STREAM_ITERATOR_H
00032 
00033 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
00034 # include <stl/_iterator_base.h>
00035 #endif
00036 
00037 // streambuf_iterators predeclarations must appear first
00038 #ifndef _STLP_IOSFWD
00039 # include <iosfwd>
00040 #endif
00041 
00042 #ifndef _STLP_INTERNAL_ALGOBASE_H
00043 #include <stl/_algobase.h>
00044 #endif
00045 
00046 #if defined (_STLP_OWN_IOSTREAMS)
00047 
00048 #ifndef _STLP_INTERNAL_OSTREAMBUF_ITERATOR_H
00049 # include <stl/_ostreambuf_iterator.h>
00050 #endif
00051 
00052 #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
00053 # include <stl/_istreambuf_iterator.h>
00054 #endif
00055 
00056 #ifndef _STLP_INTERNAL_ISTREAM_H
00057 # include <stl/_istream.h>
00058 #endif
00059 #endif /* _STLP_OWN_IOSTREAMS */
00060 
00061 // istream_iterator and ostream_iterator look very different if we're
00062 // using new, templatized iostreams than if we're using the old cfront
00063 // version.
00064 
00065 # if defined (_STLP_USE_NEW_IOSTREAMS) 
00066 
00067 _STLP_BEGIN_NAMESPACE
00068 
00069 #  ifndef _STLP_LIMITED_DEFAULT_TEMPLATES
00070 template <class _Tp, 
00071           class _CharT = _STLP_DEFAULTCHAR, class _Traits = char_traits<_CharT>,
00072           class _Dist = ptrdiff_t> 
00073 #   define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _CharT, class _Traits, class _Dist
00074 #   define __ISI_TMPL_ARGUMENTS _Tp, _CharT, _Traits, _Dist
00075 class istream_iterator : public iterator<input_iterator_tag, _Tp , _Dist,
00076                          const _Tp*, const _Tp& > {
00077 #  else
00078 
00079 #   if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
00080 #    define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
00081 #    define __ISI_TMPL_ARGUMENTS        _Tp
00082 template <class _Tp>
00083 class istream_iterator : public iterator<input_iterator_tag, _Tp , ptrdiff_t, 
00084                          const _Tp*, const _Tp& > {
00085 #   else
00086 #    define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
00087 #    define __ISI_TMPL_ARGUMENTS        _Tp, _Dist
00088 template <class _Tp,__DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
00089 class istream_iterator : public iterator<input_iterator_tag, _Tp, _Dist , 
00090                                          const _Tp*, const _Tp& > {
00091 #   endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
00092 
00093 #  endif /* _STLP_LIMITED_DEFAULT_TEMPLATES */
00094 
00095 # ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
00096   typedef char _CharT;
00097   typedef char_traits<char> _Traits;
00098 #  if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
00099   typedef ptrdiff_t _Dist;
00100 #  endif
00101 # endif
00102 
00103   typedef istream_iterator< __ISI_TMPL_ARGUMENTS > _Self;
00104 public:
00105   typedef _CharT                         char_type;
00106   typedef _Traits                        traits_type;
00107   typedef basic_istream<_CharT, _Traits> istream_type;
00108 
00109   typedef input_iterator_tag             iterator_category;
00110   typedef _Tp                            value_type;
00111   typedef _Dist                          difference_type;
00112   typedef const _Tp*                     pointer;
00113   typedef const _Tp&                     reference;
00114 
00115   istream_iterator() : _M_stream(0), _M_ok(false) {}
00116   istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
00117 
00118   reference operator*() const { return _M_value; }
00119 
00120   _STLP_DEFINE_ARROW_OPERATOR
00121 
00122   _Self& operator++() { 
00123     _M_read(); 
00124     return *this;
00125   }
00126   _Self operator++(int)  {
00127     _Self __tmp = *this;
00128     _M_read();
00129     return __tmp;
00130   }
00131 
00132   bool _M_equal(const _Self& __x) const
00133     { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
00134 
00135 private:
00136   istream_type* _M_stream;
00137   _Tp _M_value;
00138   bool _M_ok;
00139 
00140   void _M_read() {
00141     _M_ok = (_M_stream && *_M_stream) ? true : false;
00142     if (_M_ok) {
00143       *_M_stream >> _M_value;
00144       _M_ok = *_M_stream ? true : false;
00145     }
00146   }
00147 };
00148 
00149 #ifndef _STLP_LIMITED_DEFAULT_TEMPLATES
00150 template <class _TpP,
00151           class _CharT = _STLP_DEFAULTCHAR, class _Traits = char_traits<_CharT> >
00152 #else
00153 template <class _TpP>
00154 #endif
00155 class ostream_iterator: public iterator<output_iterator_tag, void, void, void, void> {
00156 # ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
00157   typedef char _CharT;
00158   typedef char_traits<char> _Traits;
00159   typedef ostream_iterator<_TpP> _Self;
00160 # else
00161   typedef ostream_iterator<_TpP, _CharT, _Traits> _Self;
00162 # endif
00163 public:
00164   typedef _CharT                         char_type;
00165   typedef _Traits                        traits_type;
00166   typedef basic_ostream<_CharT, _Traits> ostream_type;
00167 
00168   typedef output_iterator_tag            iterator_category;
00169 
00170   ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
00171   ostream_iterator(ostream_type& __s, const _CharT* __c) 
00172     : _M_stream(&__s), _M_string(__c)  {}
00173   _Self& operator=(const _TpP& __value) { 
00174     *_M_stream << __value;
00175     if (_M_string) *_M_stream << _M_string;
00176     return *this;
00177   }
00178   _Self& operator*() { return *this; }
00179   _Self& operator++() { return *this; } 
00180   _Self& operator++(int) { return *this; } 
00181 private:
00182   ostream_type* _M_stream;
00183   const _CharT* _M_string;
00184 };
00185 
00186 # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
00187 #  ifdef _STLP_LIMITED_DEFAULT_TEMPLATES
00188 template <class _TpP>
00189 inline output_iterator_tag _STLP_CALL 
00190 iterator_category(const ostream_iterator<_TpP>&) { return output_iterator_tag(); }
00191 # else
00192 template <class _TpP, class _CharT, class _Traits>
00193 inline output_iterator_tag _STLP_CALL 
00194 iterator_category(const ostream_iterator<_TpP, _CharT, _Traits>&) { return output_iterator_tag(); }
00195 #  endif
00196 # endif
00197 
00198 _STLP_END_NAMESPACE
00199 
00200 # elif ! defined(_STLP_USE_NO_IOSTREAMS)
00201 
00202 _STLP_BEGIN_NAMESPACE
00203 
00204 #  if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
00205 #  define __ISI_TMPL_HEADER_ARGUMENTS class _Tp
00206 #  define __ISI_TMPL_ARGUMENTS        _Tp
00207 template <class _Tp>
00208 class istream_iterator : public iterator<input_iterator_tag, _Tp, ptrdiff_t, 
00209                          const _Tp*, const _Tp& > {
00210 #  else
00211 #  define __ISI_TMPL_HEADER_ARGUMENTS class _Tp, class _Dist
00212 #  define __ISI_TMPL_ARGUMENTS        _Tp, _Dist
00213 template <class _Tp, __DFL_TYPE_PARAM(_Dist, ptrdiff_t)>
00214 class istream_iterator : public iterator<input_iterator_tag, _Tp, _Dist, 
00215                          const _Tp*, const _Tp& > {
00216 #  endif
00217 
00218 protected:
00219   istream* _M_stream;
00220   _Tp _M_value;
00221   bool _M_end_marker;
00222   void _M_read() {
00223     _M_end_marker = (*_M_stream) ? true : false;
00224     if (_M_end_marker) *_M_stream >> _M_value;
00225     _M_end_marker = (*_M_stream) ? true : false;
00226 }
00227 public:
00228   typedef input_iterator_tag  iterator_category;
00229   typedef _Tp                 value_type;
00230   typedef _Dist               difference_type;
00231   typedef const _Tp*          pointer;
00232   typedef const _Tp&          reference;
00233 
00234   istream_iterator() : _M_stream(&cin), _M_end_marker(false) {}
00235   istream_iterator(istream& __s) : _M_stream(&__s) { _M_read(); }
00236   reference operator*() const { return _M_value; }
00237 
00238   _STLP_DEFINE_ARROW_OPERATOR
00239 
00240   istream_iterator< __ISI_TMPL_ARGUMENTS >& operator++() { 
00241     _M_read(); 
00242     return *this;
00243   }
00244   istream_iterator< __ISI_TMPL_ARGUMENTS > operator++(int)  {
00245     istream_iterator< __ISI_TMPL_ARGUMENTS > __tmp = *this;
00246     _M_read();
00247     return __tmp;
00248   }
00249   inline bool _M_equal(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) const {
00250     return (_M_stream == __y._M_stream &&
00251             _M_end_marker == __y._M_end_marker) ||
00252       _M_end_marker == false && __y._M_end_marker == false;
00253   }
00254 };
00255 
00256 template <class _Tp>
00257 class ostream_iterator {
00258 protected:
00259   ostream* _M_stream;
00260   const char* _M_string;
00261 public:
00262   typedef output_iterator_tag iterator_category;
00263 # ifdef _STLP_CLASS_PARTIAL_SPECIALIZATION
00264   typedef void                value_type;
00265   typedef void                difference_type;
00266   typedef void                pointer;
00267   typedef void                reference;
00268 # endif
00269   ostream_iterator(ostream& __s) : _M_stream(&__s), _M_string(0) {}
00270   ostream_iterator(ostream& __s, const char* __c) 
00271     : _M_stream(&__s), _M_string(__c)  {}
00272   ostream_iterator<_Tp>& operator=(const _Tp& __value) { 
00273     *_M_stream << __value;
00274     if (_M_string) *_M_stream << _M_string;
00275     return *this;
00276   }
00277   ostream_iterator<_Tp>& operator*() { return *this; }
00278   ostream_iterator<_Tp>& operator++() { return *this; } 
00279   ostream_iterator<_Tp>& operator++(int) { return *this; } 
00280 };
00281 
00282 # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
00283 template <class _Tp> inline output_iterator_tag 
00284 iterator_category(const ostream_iterator<_Tp>&) { return output_iterator_tag(); }
00285 #endif
00286 
00287 _STLP_END_NAMESPACE
00288 
00289 #endif /* _STLP_USE_NEW_IOSTREAMS */
00290 
00291 // form-independent definiotion of stream iterators
00292 _STLP_BEGIN_NAMESPACE
00293 
00294 template < __ISI_TMPL_HEADER_ARGUMENTS >
00295 inline bool _STLP_CALL 
00296 operator==(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
00297            const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) {
00298   return __x._M_equal(__y);
00299 }
00300 
00301 #  ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
00302 
00303 template < __ISI_TMPL_HEADER_ARGUMENTS >
00304 inline bool _STLP_CALL 
00305 operator!=(const istream_iterator< __ISI_TMPL_ARGUMENTS >& __x,
00306            const istream_iterator< __ISI_TMPL_ARGUMENTS >& __y) {
00307   return !__x._M_equal(__y);
00308 }
00309 
00310 #  endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
00311 
00312 # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
00313 template < __ISI_TMPL_HEADER_ARGUMENTS >
00314 inline input_iterator_tag _STLP_CALL 
00315 iterator_category(const istream_iterator< __ISI_TMPL_ARGUMENTS >&)
00316 { return input_iterator_tag(); }
00317 template < __ISI_TMPL_HEADER_ARGUMENTS >
00318 inline _Tp* _STLP_CALL 
00319 value_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Tp*) 0; }
00320 
00321 #  if defined (_STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS) && ! defined (_STLP_DEFAULT_TYPE_PARAM)
00322 template < __ISI_TMPL_HEADER_ARGUMENTS >
00323 inline ptrdiff_t* _STLP_CALL 
00324 distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (ptrdiff_t*)0; }
00325 #  else
00326 template < __ISI_TMPL_HEADER_ARGUMENTS >
00327 inline _Dist* _STLP_CALL 
00328 distance_type(const istream_iterator< __ISI_TMPL_ARGUMENTS >&) { return (_Dist*)0; }
00329 #  endif /* _STLP_MINIMUM_DEFAULT_TEMPLATE_PARAMS */
00330 
00331 # endif 
00332 
00333 _STLP_END_NAMESPACE
00334 
00335 #  undef __ISI_TMPL_HEADER_ARGUMENTS
00336 #  undef __ISI_TMPL_ARGUMENTS
00337 
00338 
00339 #endif /* _STLP_INTERNAL_STREAM_ITERATOR_H */
00340 
00341 // Local Variables:
00342 // mode:C++
00343 // End:

Generated on Mon Jun 5 10:20:47 2006 for Intelligence.kdevelop by  doxygen 1.4.6