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
00031
00032
00033
00034
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 }
00156 #endif