Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

sbuf_iter.h

Go to the documentation of this file.
00001 // Streambuf iterators
00002 
00003 // Copyright (C) 1997-2001 Free Software Foundation, Inc.
00004 //
00005 // This file is part of the GNU ISO C++ Library.  This library is free
00006 // software; you can redistribute it and/or modify it under the
00007 // terms of the GNU General Public License as published by the
00008 // Free Software Foundation; either version 2, or (at your option)
00009 // any later version.
00010 
00011 // This library is distributed in the hope that it will be useful,
00012 // but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 // GNU General Public License for more details.
00015 
00016 // You should have received a copy of the GNU General Public License along
00017 // with this library; see the file COPYING.  If not, write to the Free
00018 // Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307,
00019 // USA.
00020 
00021 // As a special exception, you may use this file as part of a free software
00022 // library without restriction.  Specifically, if other files instantiate
00023 // templates or use macros or inline functions from this file, or you compile
00024 // this file and link it with other files to produce an executable, this
00025 // file does not by itself cause the resulting executable to be covered by
00026 // the GNU General Public License.  This exception does not however
00027 // invalidate any other reasons why the executable file might be covered by
00028 // the GNU General Public License.
00029 
00030 // XXX Should specialize copy, find algorithms for streambuf iterators.
00031 
00032 #ifndef _CPP_BITS_SBUF_ITER_H
00033 #define _CPP_BITS_SBUF_ITER_H 1
00034 
00035 #pragma GCC system_header
00036 
00037 namespace std
00038 {
00039   template<typename _CharT, typename _Traits>
00040     class ostreambuf_iterator
00041     : public iterator<output_iterator_tag, _CharT, void, void, void>
00042     {
00043     public:
00044       // Types:
00045       typedef _CharT                           char_type;
00046       typedef _Traits                          traits_type;
00047       typedef basic_streambuf<_CharT, _Traits> streambuf_type;
00048       typedef basic_ostream<_CharT, _Traits>   ostream_type;
00049       
00050       inline 
00051       ostreambuf_iterator(ostream_type& __s) throw ()
00052       : _M_sbuf(__s.rdbuf()), _M_failed(!_M_sbuf) { }
00053       
00054       ostreambuf_iterator(streambuf_type* __s) throw ()
00055       : _M_sbuf(__s), _M_failed(!_M_sbuf) { }
00056 
00057       ostreambuf_iterator& 
00058       operator=(_CharT __c);
00059 
00060       ostreambuf_iterator& 
00061       operator*() throw()
00062       { return *this; }
00063 
00064       ostreambuf_iterator& 
00065       operator++(int) throw()
00066       { return *this; }
00067 
00068       ostreambuf_iterator& 
00069       operator++() throw()
00070       { return *this; }
00071 
00072       bool 
00073       failed() const throw()
00074       { return _M_failed; }
00075 
00076     private:
00077       streambuf_type*   _M_sbuf;
00078       bool      _M_failed;
00079     };
00080 
00081   template<typename _CharT, typename _Traits>
00082     inline ostreambuf_iterator<_CharT, _Traits>&
00083     ostreambuf_iterator<_CharT, _Traits>::operator=(_CharT __c)
00084     {
00085       if (!_M_failed && 
00086           _Traits::eq_int_type(_M_sbuf->sputc(__c),_Traits::eof()))
00087       _M_failed = true;
00088       return *this;
00089     }
00090 
00091 
00092   // 24.5.3 Template class istreambuf_iterator
00093   template<class _CharT, class _Traits>
00094     class istreambuf_iterator
00095     : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type,
00096                   _CharT*, _CharT&>
00097     {
00098     public:
00099 
00100       // Types:
00101       typedef _CharT                                char_type;
00102       typedef _Traits                               traits_type;
00103       typedef typename _Traits::int_type            int_type;
00104       typedef basic_streambuf<_CharT, _Traits>      streambuf_type;
00105       typedef basic_istream<_CharT, _Traits>            istream_type;
00106       // Non-standard Types:
00107       typedef istreambuf_iterator<_CharT, _Traits>  __istreambufiter_type;
00108 
00109       istreambuf_iterator() throw() 
00110       : _M_sbuf(NULL), _M_c(-2) { }
00111       
00112       istreambuf_iterator(istream_type& __s) throw()
00113       : _M_sbuf(__s.rdbuf()), _M_c(-2) { }
00114 
00115       istreambuf_iterator(streambuf_type* __s) throw()
00116       : _M_sbuf(__s), _M_c(-2) { }
00117        
00118       // NB: This should really have an int_type return
00119       // value, so "end of stream" postion can be checked without
00120       // hacking.
00121       char_type 
00122       operator*() const
00123       { 
00124     // The result of operator*() on an end of stream is undefined.
00125     char_type __ret;
00126     if (_M_sbuf && _M_c != static_cast<int_type>(-2))
00127       __ret = _M_c;
00128     else if (_M_sbuf)
00129       __ret = traits_type::to_char_type(_M_sbuf->sgetc()); 
00130     else
00131       __ret = static_cast<char_type>(traits_type::eof());
00132     return __ret;
00133       }
00134     
00135       __istreambufiter_type& 
00136       operator++()
00137       { 
00138     if (_M_sbuf)
00139       _M_sbuf->sbumpc();
00140     _M_c = -2;
00141     return *this; 
00142       }
00143 
00144 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00145       // 14882 says return a proxy object. It should be a const
00146       // proxy object, but since this class is not mandated, it
00147       // should allow this signature:
00148       const __istreambufiter_type
00149       operator++(int)
00150       {
00151     if (_M_sbuf)
00152       _M_c = _M_sbuf->sbumpc();
00153     return *this; 
00154       }
00155 #endif
00156 
00157       bool 
00158       equal(const __istreambufiter_type& __b)
00159       { 
00160     int_type __eof = traits_type::eof();
00161     bool __thiseof = !_M_sbuf || _M_sbuf->sgetc() == __eof;
00162     bool __beof = !__b._M_sbuf 
00163               || __b._M_sbuf->sgetc() == __eof;
00164     return (__thiseof && __beof || (!__thiseof && !__beof));
00165       }
00166 
00167 #ifdef _GLIBCPP_RESOLVE_LIB_DEFECTS
00168       // 110 istreambuf_iterator::equal not const
00169       // NB: there is also number 111 pending on this function.
00170       bool 
00171       equal(const __istreambufiter_type& __b) const
00172       {
00173     int_type __eof = traits_type::eof();
00174     bool __thiseof = !_M_sbuf || _M_sbuf->sgetc() == __eof;
00175     bool __beof = !__b._M_sbuf 
00176               || __b._M_sbuf->sgetc() == __eof;
00177     return (__thiseof && __beof || (!__thiseof && !__beof));
00178       }
00179 #endif
00180 
00181     private:
00182       // 24.5.3 istreambuf_iterator 
00183       // p 1 
00184       // If the end of stream is reached (streambuf_type::sgetc()
00185       // returns traits_type::eof()), the iterator becomes equal to
00186       // the "end of stream" iterator value.
00187       // NB: This implementation assumes the "end of stream" value
00188       // is EOF, or -1.
00189       streambuf_type*       _M_sbuf;  
00190       int_type          _M_c;
00191     };
00192 
00193   template<typename _CharT, typename _Traits>
00194     inline bool 
00195     operator==(const istreambuf_iterator<_CharT, _Traits>& __a,
00196            const istreambuf_iterator<_CharT, _Traits>& __b)
00197     { return __a.equal(__b); }
00198 
00199   template<typename _CharT, typename _Traits>
00200     inline bool 
00201     operator!=(const istreambuf_iterator<_CharT, _Traits>& __a,
00202            const istreambuf_iterator<_CharT, _Traits>& __b)
00203     { return !__a.equal(__b); }
00204 
00205 } // namespace std
00206 
00207 #endif

Generated at Tue May 1 16:28:38 2001 for libstdc++-v3 by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001