stream_iterator.h

Go to the documentation of this file.
00001 // Stream iterators 00002 00003 // Copyright (C) 2001, 2004 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 /** @file stream_iterator.h 00031 * This is an internal header file, included by other library headers. 00032 * You should not attempt to use it directly. 00033 */ 00034 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 { 00044 /// Provides input iterator semantics for streams. 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: 00061 /// Construct end of input stream iterator. 00062 istream_iterator() 00063 : _M_stream(0), _M_ok(false) {} 00064 00065 /// Construct start of input stream iterator. 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 00125 /// Return true if x and y are both end or not end, or x and y are the same. 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 00132 /// Return false if x and y are both end or not end, or x and y are the same. 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 00139 /** 00140 * @brief Provides output iterator semantics for streams. 00141 * 00142 * This class provides an iterator to write to an ostream. The type Tp is 00143 * the only type written by this iterator and there must be an 00144 * operator<<(Tp) defined. 00145 * 00146 * @param Tp The type to write to the ostream. 00147 * @param CharT The ostream char_type. 00148 * @param Traits The ostream char_traits. 00149 */ 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: 00156 //@{ 00157 /// Public typedef 00158 typedef _CharT char_type; 00159 typedef _Traits traits_type; 00160 typedef basic_ostream<_CharT, _Traits> ostream_type; 00161 //@} 00162 00163 private: 00164 ostream_type* _M_stream; 00165 const _CharT* _M_string; 00166 00167 public: 00168 /// Construct from an ostream. 00169 ostream_iterator(ostream_type& __s) : _M_stream(&__s), _M_string(0) {} 00170 00171 /** 00172 * Construct from an ostream. 00173 * 00174 * The delimiter string @a c is written to the stream after every Tp 00175 * written to the stream. The delimiter is not copied, and thus must 00176 * not be destroyed while this iterator is in use. 00177 * 00178 * @param s Underlying ostream to write to. 00179 * @param c CharT delimiter string to insert. 00180 */ 00181 ostream_iterator(ostream_type& __s, const _CharT* __c) 00182 : _M_stream(&__s), _M_string(__c) { } 00183 00184 /// Copy constructor. 00185 ostream_iterator(const ostream_iterator& __obj) 00186 : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { } 00187 00188 /// Writes @a value to underlying ostream using operator<<. If 00189 /// constructed with delimiter string, writes delimiter to ostream. 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 } // namespace std 00214 #endif

Generated on Wed Jun 9 11:19:18 2004 for libstdc++-v3 Source by doxygen 1.3.7