# stream_iterator.h

Go to the documentation of this file.

    00001 // Stream iterators
    00002
    00003 // Copyright (C) 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
    00035 #ifndef _CPP_BITS_STREAM_ITERATOR_H
    00036 #define _CPP_BITS_STREAM_ITERATOR_H 1
    00037
    00038 #pragma GCC system_header
    00039
    00040 namespace std
    00041 {
    00042   template<typename _Tp, typename _CharT = char,
    00043            typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
    00044     class istream_iterator
    00045       : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
    00046     {
    00047     public:
    00048       typedef _CharT                         char_type;
    00049       typedef _Traits                        traits_type;
    00050       typedef basic_istream<_CharT, _Traits> istream_type;
    00051
    00052     private:
    00053       istream_type*     _M_stream;
    00054       _Tp       _M_value;
    00055       bool      _M_ok;
    00056
    00057     public:
    00058       istream_iterator() : _M_stream(0), _M_ok(false) {}
    00059
    00060       istream_iterator(istream_type& __s) : _M_stream(&__s) { _M_read(); }
    00061
    00062       istream_iterator(const istream_iterator& __obj)
    00063       : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
    00064         _M_ok(__obj._M_ok)
    00065       { }
    00066
    00067       const _Tp&
    00068       operator*() const { return _M_value; }
    00069
    00070       const _Tp*
    00071       operator->() const { return &(operator*()); }
    00072
    00073       istream_iterator&
    00074       operator++()
    00075       { _M_read(); return *this; }
    00076
    00077       istream_iterator
    00078       operator++(int)
    00079       {
    00080     istream_iterator __tmp = *this;
    00081     _M_read();
    00082     return __tmp;
    00083       }
    00084
    00085       bool
    00086       _M_equal(const istream_iterator& __x) const
    00087       { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream);}
    00088
    00089     private:
    00090       void
    00091       _M_read()
    00092       {
    00093     _M_ok = (_M_stream && *_M_stream) ? true : false;
    00094     if (_M_ok)
    00095       {
    00096         *_M_stream >> _M_value;
    00097         _M_ok = *_M_stream ? true : false;
    00098       }
    00099       }
    00100     };
    00101
    00102   template<typename _Tp, typename _CharT, typename _Traits, typename _Dist>
    00103     inline bool
    00104     operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
    00105            const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
    00106     { return __x._M_equal(__y); }
    00107
    00108   template <class _Tp, class _CharT, class _Traits, class _Dist>
    00109     inline bool
    00110     operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
    00111            const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
    00112     { return !__x._M_equal(__y); }
    00113
    00114
    00115   template<typename _Tp, typename _CharT = char,
    00116            typename _Traits = char_traits<_CharT> >
    00117     class ostream_iterator
    00118       : public iterator<output_iterator_tag, void, void, void, void>
    00119     {
    00120     public:
    00121       typedef _CharT                         char_type;
    00122       typedef _Traits                        traits_type;
    00123       typedef basic_ostream<_CharT, _Traits> ostream_type;
    00124
    00125     private:
    00126       ostream_type*     _M_stream;
    00127       const _CharT*     _M_string;
    00128
    00129     public:
    00130       ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
    00131
    00132       ostream_iterator(ostream_type& __s, const _CharT* __c)
    00133       : _M_stream(&__s), _M_string(__c)  { }
    00134
    00135       ostream_iterator(const ostream_iterator& __obj)
    00136       : _M_stream(__obj._M_stream), _M_string(__obj._M_string)  { }
    00137
    00138       ostream_iterator&
    00139       operator=(const _Tp& __value)
    00140       {
    00141     *_M_stream << __value;
    00142     if (_M_string) *_M_stream << _M_string;
    00143     return *this;
    00144       }
    00145
    00146       ostream_iterator&
    00147       operator*() { return *this; }
    00148
    00149       ostream_iterator&
    00150       operator++() { return *this; }
    00151
    00152       ostream_iterator&
    00153       operator++(int) { return *this; }
    00154     };
    00155 } // namespace std
    00156 #endif