00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00035 #ifndef _STREAM_ITERATOR_H
00036 #define _STREAM_ITERATOR_H 1
00037
00038 #pragma GCC system_header
00039
00040 #include <debug/debug.h>
00041
00042 namespace std
00043 {
00045 template<typename _Tp, typename _CharT = char,
00046 typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
00047 class istream_iterator
00048 : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
00049 {
00050 public:
00051 typedef _CharT char_type;
00052 typedef _Traits traits_type;
00053 typedef basic_istream<_CharT, _Traits> istream_type;
00054
00055 private:
00056 istream_type* _M_stream;
00057 _Tp _M_value;
00058 bool _M_ok;
00059
00060 public:
00062 istream_iterator()
00063 : _M_stream(0), _M_ok(false) {}
00064
00066 istream_iterator(istream_type& __s)
00067 : _M_stream(&__s)
00068 { _M_read(); }
00069
00070 istream_iterator(const istream_iterator& __obj)
00071 : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
00072 _M_ok(__obj._M_ok)
00073 { }
00074
00075 const _Tp&
00076 operator*() const
00077 {
00078 __glibcxx_requires_cond(_M_ok,
00079 _M_message(__gnu_debug::__msg_deref_istream)
00080 ._M_iterator(*this));
00081 return _M_value;
00082 }
00083
00084 const _Tp*
00085 operator->() const { return &(operator*()); }
00086
00087 istream_iterator&
00088 operator++()
00089 {
00090 __glibcxx_requires_cond(_M_ok,
00091 _M_message(__gnu_debug::__msg_inc_istream)
00092 ._M_iterator(*this));
00093 _M_read();
00094 return *this;
00095 }
00096
00097 istream_iterator
00098 operator++(int)
00099 {
00100 __glibcxx_requires_cond(_M_ok,
00101 _M_message(__gnu_debug::__msg_inc_istream)
00102 ._M_iterator(*this));
00103 istream_iterator __tmp = *this;
00104 _M_read();
00105 return __tmp;
00106 }
00107
00108 bool
00109 _M_equal(const istream_iterator& __x) const
00110 { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
00111
00112 private:
00113 void
00114 _M_read()
00115 {
00116 _M_ok = (_M_stream && *_M_stream) ? true : false;
00117 if (_M_ok)
00118 {
00119 *_M_stream >> _M_value;
00120 _M_ok = *_M_stream ? true : false;
00121 }
00122 }
00123 };
00124
00126 template<typename _Tp, typename _CharT, typename _Traits, typename _Dist>
00127 inline bool
00128 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00129 const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
00130 { return __x._M_equal(__y); }
00131
00133 template <class _Tp, class _CharT, class _Traits, class _Dist>
00134 inline bool
00135 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00136 const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
00137 { return !__x._M_equal(__y); }
00138
00150 template<typename _Tp, typename _CharT = char,
00151 typename _Traits = char_traits<_CharT> >
00152 class ostream_iterator
00153 : public iterator<output_iterator_tag, void, void, void, void>
00154 {
00155 public:
00157
00158 typedef _CharT char_type;
00159 typedef _Traits traits_type;
00160 typedef basic_ostream<_CharT, _Traits> ostream_type;
00162
00163 private:
00164 ostream_type* _M_stream;
00165 const _CharT* _M_string;
00166
00167 public:
00169 ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
00170
00181 ostream_iterator(ostream_type& __s, const _CharT* __c)
00182 : _M_stream(&__s), _M_string(__c) { }
00183
00185 ostream_iterator(const ostream_iterator& __obj)
00186 : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
00187
00190 ostream_iterator&
00191 operator=(const _Tp& __value)
00192 {
00193 __glibcxx_requires_cond(_M_stream != 0,
00194 _M_message(__gnu_debug::__msg_output_ostream)
00195 ._M_iterator(*this));
00196 *_M_stream << __value;
00197 if (_M_string) *_M_stream << _M_string;
00198 return *this;
00199 }
00200
00201 ostream_iterator&
00202 operator*()
00203 { return *this; }
00204
00205 ostream_iterator&
00206 operator++()
00207 { return *this; }
00208
00209 ostream_iterator&
00210 operator++(int)
00211 { return *this; }
00212 };
00213 }
00214 #endif