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
00030 #ifndef _STREAM_ITERATOR_H
00031 #define _STREAM_ITERATOR_H 1
00032
00033 #pragma GCC system_header
00034
00035 #include <debug/debug.h>
00036
00037 _GLIBCXX_BEGIN_NAMESPACE(std)
00038
00039
00040 template<typename _Tp, typename _CharT = char,
00041 typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
00042 class istream_iterator
00043 : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
00044 {
00045 public:
00046 typedef _CharT char_type;
00047 typedef _Traits traits_type;
00048 typedef basic_istream<_CharT, _Traits> istream_type;
00049
00050 private:
00051 istream_type* _M_stream;
00052 _Tp _M_value;
00053 bool _M_ok;
00054
00055 public:
00056
00057 istream_iterator()
00058 : _M_stream(0), _M_value(), _M_ok(false) {}
00059
00060
00061 istream_iterator(istream_type& __s)
00062 : _M_stream(&__s)
00063 { _M_read(); }
00064
00065 istream_iterator(const istream_iterator& __obj)
00066 : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
00067 _M_ok(__obj._M_ok)
00068 { }
00069
00070 const _Tp&
00071 operator*() const
00072 {
00073 __glibcxx_requires_cond(_M_ok,
00074 _M_message(__gnu_debug::__msg_deref_istream)
00075 ._M_iterator(*this));
00076 return _M_value;
00077 }
00078
00079 const _Tp*
00080 operator->() const { return &(operator*()); }
00081
00082 istream_iterator&
00083 operator++()
00084 {
00085 __glibcxx_requires_cond(_M_ok,
00086 _M_message(__gnu_debug::__msg_inc_istream)
00087 ._M_iterator(*this));
00088 _M_read();
00089 return *this;
00090 }
00091
00092 istream_iterator
00093 operator++(int)
00094 {
00095 __glibcxx_requires_cond(_M_ok,
00096 _M_message(__gnu_debug::__msg_inc_istream)
00097 ._M_iterator(*this));
00098 istream_iterator __tmp = *this;
00099 _M_read();
00100 return __tmp;
00101 }
00102
00103 bool
00104 _M_equal(const istream_iterator& __x) const
00105 { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
00106
00107 private:
00108 void
00109 _M_read()
00110 {
00111 _M_ok = (_M_stream && *_M_stream) ? true : false;
00112 if (_M_ok)
00113 {
00114 *_M_stream >> _M_value;
00115 _M_ok = *_M_stream ? true : false;
00116 }
00117 }
00118 };
00119
00120
00121 template<typename _Tp, typename _CharT, typename _Traits, typename _Dist>
00122 inline bool
00123 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00124 const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
00125 { return __x._M_equal(__y); }
00126
00127
00128 template <class _Tp, class _CharT, class _Traits, class _Dist>
00129 inline bool
00130 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00131 const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
00132 { return !__x._M_equal(__y); }
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145 template<typename _Tp, typename _CharT = char,
00146 typename _Traits = char_traits<_CharT> >
00147 class ostream_iterator
00148 : public iterator<output_iterator_tag, void, void, void, void>
00149 {
00150 public:
00151
00152
00153 typedef _CharT char_type;
00154 typedef _Traits traits_type;
00155 typedef basic_ostream<_CharT, _Traits> ostream_type;
00156
00157
00158 private:
00159 ostream_type* _M_stream;
00160 const _CharT* _M_string;
00161
00162 public:
00163
00164 ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {}
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176 ostream_iterator(ostream_type& __s, const _CharT* __c)
00177 : _M_stream(&__s), _M_string(__c) { }
00178
00179
00180 ostream_iterator(const ostream_iterator& __obj)
00181 : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
00182
00183
00184
00185 ostream_iterator&
00186 operator=(const _Tp& __value)
00187 {
00188 __glibcxx_requires_cond(_M_stream != 0,
00189 _M_message(__gnu_debug::__msg_output_ostream)
00190 ._M_iterator(*this));
00191 *_M_stream << __value;
00192 if (_M_string) *_M_stream << _M_string;
00193 return *this;
00194 }
00195
00196 ostream_iterator&
00197 operator*()
00198 { return *this; }
00199
00200 ostream_iterator&
00201 operator++()
00202 { return *this; }
00203
00204 ostream_iterator&
00205 operator++(int)
00206 { return *this; }
00207 };
00208
00209 _GLIBCXX_END_NAMESPACE
00210
00211 #endif