libstdc++
stream_iterator.h
Go to the documentation of this file.
00001 // Stream iterators
00002 
00003 // Copyright (C) 2001-2018 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 3, 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 // Under Section 7 of GPL version 3, you are granted additional
00017 // permissions described in the GCC Runtime Library Exception, version
00018 // 3.1, as published by the Free Software Foundation.
00019 
00020 // You should have received a copy of the GNU General Public License and
00021 // a copy of the GCC Runtime Library Exception along with this program;
00022 // see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see
00023 // <http://www.gnu.org/licenses/>.
00024 
00025 /** @file bits/stream_iterator.h
00026  *  This is an internal header file, included by other library headers.
00027  *  Do not attempt to use it directly. @headername{iterator}
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 namespace std _GLIBCXX_VISIBILITY(default)
00038 {
00039 _GLIBCXX_BEGIN_NAMESPACE_VERSION
00040 
00041   /**
00042    * @addtogroup iterators
00043    * @{
00044    */
00045 
00046   /// Provides input iterator semantics for streams.
00047   template<typename _Tp, typename _CharT = char,
00048            typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
00049     class istream_iterator
00050     : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
00051     {
00052     public:
00053       typedef _CharT                         char_type;
00054       typedef _Traits                        traits_type;
00055       typedef basic_istream<_CharT, _Traits> istream_type;
00056 
00057     private:
00058       istream_type*     _M_stream;
00059       _Tp               _M_value;
00060       bool              _M_ok;
00061 
00062     public:
00063       ///  Construct end of input stream iterator.
00064       _GLIBCXX_CONSTEXPR istream_iterator()
00065       : _M_stream(0), _M_value(), _M_ok(false) {}
00066 
00067       ///  Construct start of input stream iterator.
00068       istream_iterator(istream_type& __s)
00069       : _M_stream(std::__addressof(__s))
00070       { _M_read(); }
00071 
00072       istream_iterator(const istream_iterator& __obj)
00073       : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
00074         _M_ok(__obj._M_ok)
00075       { }
00076 
00077       const _Tp&
00078       operator*() const
00079       {
00080         __glibcxx_requires_cond(_M_ok,
00081                                 _M_message(__gnu_debug::__msg_deref_istream)
00082                                 ._M_iterator(*this));
00083         return _M_value;
00084       }
00085 
00086       const _Tp*
00087       operator->() const { return std::__addressof((operator*())); }
00088 
00089       istream_iterator&
00090       operator++()
00091       {
00092         __glibcxx_requires_cond(_M_ok,
00093                                 _M_message(__gnu_debug::__msg_inc_istream)
00094                                 ._M_iterator(*this));
00095         _M_read();
00096         return *this;
00097       }
00098 
00099       istream_iterator
00100       operator++(int)
00101       {
00102         __glibcxx_requires_cond(_M_ok,
00103                                 _M_message(__gnu_debug::__msg_inc_istream)
00104                                 ._M_iterator(*this));
00105         istream_iterator __tmp = *this;
00106         _M_read();
00107         return __tmp;
00108       }
00109 
00110       bool
00111       _M_equal(const istream_iterator& __x) const
00112       { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
00113 
00114     private:
00115       void
00116       _M_read()
00117       {
00118         _M_ok = (_M_stream && *_M_stream) ? true : false;
00119         if (_M_ok)
00120           {
00121             *_M_stream >> _M_value;
00122             _M_ok = *_M_stream ? true : false;
00123           }
00124       }
00125     };
00126 
00127   ///  Return true if x and y are both end or not end, or x and y are the same.
00128   template<typename _Tp, typename _CharT, typename _Traits, typename _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   ///  Return false if x and y are both end or not end, or x and y are the same.
00135   template <class _Tp, class _CharT, class _Traits, class _Dist>
00136     inline bool
00137     operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00138                const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
00139     { return !__x._M_equal(__y); }
00140 
00141   /**
00142    *  @brief  Provides output iterator semantics for streams.
00143    *
00144    *  This class provides an iterator to write to an ostream.  The type Tp is
00145    *  the only type written by this iterator and there must be an
00146    *  operator<<(Tp) defined.
00147    *
00148    *  @tparam  _Tp  The type to write to the ostream.
00149    *  @tparam  _CharT  The ostream char_type.
00150    *  @tparam  _Traits  The ostream char_traits.
00151   */
00152   template<typename _Tp, typename _CharT = char,
00153            typename _Traits = char_traits<_CharT> >
00154     class ostream_iterator
00155     : public iterator<output_iterator_tag, void, void, void, void>
00156     {
00157     public:
00158       //@{
00159       /// Public typedef
00160       typedef _CharT                         char_type;
00161       typedef _Traits                        traits_type;
00162       typedef basic_ostream<_CharT, _Traits> ostream_type;
00163       //@}
00164 
00165     private:
00166       ostream_type*     _M_stream;
00167       const _CharT*     _M_string;
00168 
00169     public:
00170       /// Construct from an ostream.
00171       ostream_iterator(ostream_type& __s)
00172       : _M_stream(std::__addressof(__s)), _M_string(0) {}
00173 
00174       /**
00175        *  Construct from an ostream.
00176        *
00177        *  The delimiter string @a c is written to the stream after every Tp
00178        *  written to the stream.  The delimiter is not copied, and thus must
00179        *  not be destroyed while this iterator is in use.
00180        *
00181        *  @param  __s  Underlying ostream to write to.
00182        *  @param  __c  CharT delimiter string to insert.
00183       */
00184       ostream_iterator(ostream_type& __s, const _CharT* __c)
00185       : _M_stream(&__s), _M_string(__c)  { }
00186 
00187       /// Copy constructor.
00188       ostream_iterator(const ostream_iterator& __obj)
00189       : _M_stream(__obj._M_stream), _M_string(__obj._M_string)  { }
00190 
00191       /// Writes @a value to underlying ostream using operator<<.  If
00192       /// constructed with delimiter string, writes delimiter to ostream.
00193       ostream_iterator&
00194       operator=(const _Tp& __value)
00195       {
00196         __glibcxx_requires_cond(_M_stream != 0,
00197                                 _M_message(__gnu_debug::__msg_output_ostream)
00198                                 ._M_iterator(*this));
00199         *_M_stream << __value;
00200         if (_M_string) *_M_stream << _M_string;
00201         return *this;
00202       }
00203 
00204       ostream_iterator&
00205       operator*()
00206       { return *this; }
00207 
00208       ostream_iterator&
00209       operator++()
00210       { return *this; }
00211 
00212       ostream_iterator&
00213       operator++(int)
00214       { return *this; }
00215     };
00216 
00217   // @} group iterators
00218 
00219 _GLIBCXX_END_NAMESPACE_VERSION
00220 } // namespace
00221 
00222 #endif