/home/ntakagi/work/STLport-5.1.5/stlport/stl/_istreambuf_iterator.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 1999
00003  * Silicon Graphics Computer Systems, Inc.
00004  *
00005  * Copyright (c) 1999
00006  * Boris Fomitchev
00007  *
00008  * This material is provided "as is", with absolutely no warranty expressed
00009  * or implied. Any use is at your own risk.
00010  *
00011  * Permission to use or copy this software for any purpose is hereby granted
00012  * without fee, provided the above notices are retained on all copies.
00013  * Permission to modify the code and to distribute modified code is granted,
00014  * provided the above notices are retained, and a notice that the code was
00015  * modified is included with the above copyright notice.
00016  *
00017  */
00018 // WARNING: This is an internal header file, included by other C++
00019 // standard library headers.  You should not attempt to use this header
00020 // file directly.
00021 
00022 
00023 #ifndef _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
00024 #define _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H
00025 
00026 #ifndef _STLP_INTERNAL_ITERATOR_BASE_H
00027 # include <stl/_iterator_base.h>
00028 #endif
00029 
00030 #ifndef _STLP_INTERNAL_STREAMBUF
00031 # include <stl/_streambuf.h>
00032 #endif
00033 
00034 _STLP_BEGIN_NAMESPACE
00035 
00036 // defined in _istream.h
00037 template <class _CharT, class _Traits>
00038 extern basic_streambuf<_CharT, _Traits>* _STLP_CALL _M_get_istreambuf(basic_istream<_CharT, _Traits>& ) ;
00039 
00040 // We do not read any characters until operator* is called. operator* calls sgetc
00041 // unless the iterator is unchanged from the last call in which case a cached value is
00042 // used. Calls to operator++ use sbumpc.
00043 
00044 template<class _CharT, class _Traits>
00045 class istreambuf_iterator :
00046   public iterator<input_iterator_tag, _CharT, typename _Traits::off_type, _CharT*, _CharT&>
00047 {
00048 public:
00049   typedef _CharT                           char_type;
00050   typedef _Traits                          traits_type;
00051   typedef typename _Traits::int_type       int_type;
00052   typedef basic_streambuf<_CharT, _Traits> streambuf_type;
00053   typedef basic_istream<_CharT, _Traits>   istream_type;
00054 
00055   typedef input_iterator_tag               iterator_category;
00056   typedef _CharT                           value_type;
00057   typedef typename _Traits::off_type       difference_type;
00058   typedef const _CharT*                    pointer;
00059   typedef const _CharT&                    reference;
00060 
00061 public:
00062   istreambuf_iterator(streambuf_type* __p = 0) { this->_M_init(__p); }
00063   //  istreambuf_iterator(basic_istream<_CharT, _Traits>& __is) { this->_M_init(_M_get_istreambuf(__is)); }
00064   inline istreambuf_iterator(basic_istream<_CharT, _Traits>& __is);
00065 
00066   char_type operator*() const { this->_M_getc(); return _M_c; }
00067   istreambuf_iterator<_CharT, _Traits>& operator++()
00068       {
00069         _M_buf->sbumpc();
00070         _M_have_c = false;
00071         return *this;
00072       }
00073   istreambuf_iterator<_CharT, _Traits>  operator++(int);
00074 
00075   bool equal(const istreambuf_iterator<_CharT, _Traits>& __i) const {
00076     if (this->_M_buf)
00077       this->_M_getc();
00078     if (__i._M_buf)
00079       __i._M_getc();
00080     return this->_M_eof == __i._M_eof;
00081   }
00082 
00083 private:
00084   void _M_init(streambuf_type* __p) {
00085     _M_buf = __p;
00086     _M_eof = (__p == 0);
00087     _M_have_c = false;
00088   }
00089 
00090   void _M_getc() const {
00091     if (_M_have_c)
00092       return;
00093     int_type __c = _M_buf->sgetc();
00094 # if !defined (_STLP_NEED_MUTABLE) /* && ! defined (__SUNPRO_CC) */
00095     _M_c = traits_type::to_char_type(__c);
00096     _M_eof = traits_type::eq_int_type(__c, traits_type::eof());
00097     _M_have_c = true;
00098 # else
00099     typedef istreambuf_iterator<_CharT,_Traits> _Self;
00100     _Self* __that = __CONST_CAST(_Self*, this);
00101     __that->_M_c = __STATIC_CAST(_CharT, traits_type::to_char_type(__c));
00102     __that->_M_eof = traits_type::eq_int_type(__c, traits_type::eof());
00103     __that->_M_have_c = true;
00104 # endif
00105   }
00106 
00107 private:
00108   streambuf_type* _M_buf;
00109   mutable _CharT _M_c;
00110   mutable bool _M_eof;
00111   mutable bool _M_have_c;
00112 };
00113 
00114 template<class _CharT, class _Traits>
00115 inline istreambuf_iterator<_CharT, _Traits>::istreambuf_iterator(basic_istream<_CharT, _Traits>& __is)
00116 { this->_M_init(_M_get_istreambuf(__is)); }
00117 
00118 template<class _CharT, class _Traits>
00119 inline bool _STLP_CALL operator==(const istreambuf_iterator<_CharT, _Traits>& __x,
00120                                   const istreambuf_iterator<_CharT, _Traits>& __y) {
00121   return __x.equal(__y);
00122 }
00123 
00124 #ifdef _STLP_USE_SEPARATE_RELOPS_NAMESPACE
00125 
00126 template<class _CharT, class _Traits>
00127 inline bool _STLP_CALL operator!=(const istreambuf_iterator<_CharT, _Traits>& __x,
00128                                   const istreambuf_iterator<_CharT, _Traits>& __y) {
00129   return !__x.equal(__y);
00130 }
00131 
00132 #endif /* _STLP_USE_SEPARATE_RELOPS_NAMESPACE */
00133 
00134 # if defined (_STLP_USE_TEMPLATE_EXPORT)
00135 _STLP_EXPORT_TEMPLATE_CLASS istreambuf_iterator<char, char_traits<char> >;
00136 #  if defined (INSTANTIATE_WIDE_STREAMS)
00137 _STLP_EXPORT_TEMPLATE_CLASS istreambuf_iterator<wchar_t, char_traits<wchar_t> >;
00138 #  endif
00139 # endif /* _STLP_USE_TEMPLATE_EXPORT */
00140 
00141 # ifdef _STLP_USE_OLD_HP_ITERATOR_QUERIES
00142 template <class _CharT, class _Traits>
00143 inline input_iterator_tag _STLP_CALL iterator_category(const istreambuf_iterator<_CharT, _Traits>&) { return input_iterator_tag(); }
00144 template <class _CharT, class _Traits>
00145 inline streamoff* _STLP_CALL
00146 distance_type(const istreambuf_iterator<_CharT, _Traits>&) { return (streamoff*)0; }
00147 template <class _CharT, class _Traits>
00148 inline _CharT* _STLP_CALL value_type(const istreambuf_iterator<_CharT, _Traits>&) { return (_CharT*)0; }
00149 # endif
00150 
00151 template <class _CharT, class _Traits>
00152 istreambuf_iterator<_CharT, _Traits>
00153 istreambuf_iterator<_CharT, _Traits>::operator++(int) {
00154   _M_getc(); // __tmp should avoid any future actions under
00155   // underlined buffer---during call of operator *()
00156   // (due to buffer for *this and __tmp are the same).
00157   istreambuf_iterator<_CharT, _Traits> __tmp = *this;
00158   _M_buf->sbumpc();
00159   _M_have_c = false;
00160   return __tmp;
00161 }
00162 
00163 _STLP_END_NAMESPACE
00164 
00165 #endif /* _STLP_INTERNAL_ISTREAMBUF_ITERATOR_H */
00166 
00167 // Local Variables:
00168 // mode:C++
00169 // End:
00170 



Generated on Mon Mar 10 15:32:25 2008 by  doxygen 1.5.1