libstdc++
stream_iterator.h
Go to the documentation of this file.
00001 // Stream iterators
00002 
00003 // Copyright (C) 2001-2019 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 #if __cplusplus >= 201103L
00078       istream_iterator& operator=(const istream_iterator&) = default;
00079 #endif
00080 
00081       const _Tp&
00082       operator*() const
00083       {
00084         __glibcxx_requires_cond(_M_ok,
00085                                 _M_message(__gnu_debug::__msg_deref_istream)
00086                                 ._M_iterator(*this));
00087         return _M_value;
00088       }
00089 
00090       const _Tp*
00091       operator->() const { return std::__addressof((operator*())); }
00092 
00093       istream_iterator&
00094       operator++()
00095       {
00096         __glibcxx_requires_cond(_M_ok,
00097                                 _M_message(__gnu_debug::__msg_inc_istream)
00098                                 ._M_iterator(*this));
00099         _M_read();
00100         return *this;
00101       }
00102 
00103       istream_iterator
00104       operator++(int)
00105       {
00106         __glibcxx_requires_cond(_M_ok,
00107                                 _M_message(__gnu_debug::__msg_inc_istream)
00108                                 ._M_iterator(*this));
00109         istream_iterator __tmp = *this;
00110         _M_read();
00111         return __tmp;
00112       }
00113 
00114       bool
00115       _M_equal(const istream_iterator& __x) const
00116       { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
00117 
00118     private:
00119       void
00120       _M_read()
00121       {
00122         _M_ok = (_M_stream && *_M_stream) ? true : false;
00123         if (_M_ok)
00124           {
00125             *_M_stream >> _M_value;
00126             _M_ok = *_M_stream ? true : false;
00127           }
00128       }
00129     };
00130 
00131   ///  Return true if x and y are both end or not end, or x and y are the same.
00132   template<typename _Tp, typename _CharT, typename _Traits, typename _Dist>
00133     inline bool
00134     operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00135                const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
00136     { return __x._M_equal(__y); }
00137 
00138   ///  Return false if x and y are both end or not end, or x and y are the same.
00139   template <class _Tp, class _CharT, class _Traits, class _Dist>
00140     inline bool
00141     operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
00142                const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
00143     { return !__x._M_equal(__y); }
00144 
00145   /**
00146    *  @brief  Provides output iterator semantics for streams.
00147    *
00148    *  This class provides an iterator to write to an ostream.  The type Tp is
00149    *  the only type written by this iterator and there must be an
00150    *  operator<<(Tp) defined.
00151    *
00152    *  @tparam  _Tp  The type to write to the ostream.
00153    *  @tparam  _CharT  The ostream char_type.
00154    *  @tparam  _Traits  The ostream char_traits.
00155   */
00156   template<typename _Tp, typename _CharT = char,
00157            typename _Traits = char_traits<_CharT> >
00158     class ostream_iterator
00159     : public iterator<output_iterator_tag, void, void, void, void>
00160     {
00161     public:
00162       //@{
00163       /// Public typedef
00164       typedef _CharT                         char_type;
00165       typedef _Traits                        traits_type;
00166       typedef basic_ostream<_CharT, _Traits> ostream_type;
00167       //@}
00168 
00169     private:
00170       ostream_type*     _M_stream;
00171       const _CharT*     _M_string;
00172 
00173     public:
00174       /// Construct from an ostream.
00175       ostream_iterator(ostream_type& __s)
00176       : _M_stream(std::__addressof(__s)), _M_string(0) {}
00177 
00178       /**
00179        *  Construct from an ostream.
00180        *
00181        *  The delimiter string @a c is written to the stream after every Tp
00182        *  written to the stream.  The delimiter is not copied, and thus must
00183        *  not be destroyed while this iterator is in use.
00184        *
00185        *  @param  __s  Underlying ostream to write to.
00186        *  @param  __c  CharT delimiter string to insert.
00187       */
00188       ostream_iterator(ostream_type& __s, const _CharT* __c)
00189       : _M_stream(&__s), _M_string(__c)  { }
00190 
00191       /// Copy constructor.
00192       ostream_iterator(const ostream_iterator& __obj)
00193       : _M_stream(__obj._M_stream), _M_string(__obj._M_string)  { }
00194 
00195 #if __cplusplus >= 201103L
00196       ostream_iterator& operator=(const ostream_iterator&) = default;
00197 #endif
00198 
00199       /// Writes @a value to underlying ostream using operator<<.  If
00200       /// constructed with delimiter string, writes delimiter to ostream.
00201       ostream_iterator&
00202       operator=(const _Tp& __value)
00203       {
00204         __glibcxx_requires_cond(_M_stream != 0,
00205                                 _M_message(__gnu_debug::__msg_output_ostream)
00206                                 ._M_iterator(*this));
00207         *_M_stream << __value;
00208         if (_M_string) *_M_stream << _M_string;
00209         return *this;
00210       }
00211 
00212       ostream_iterator&
00213       operator*()
00214       { return *this; }
00215 
00216       ostream_iterator&
00217       operator++()
00218       { return *this; }
00219 
00220       ostream_iterator&
00221       operator++(int)
00222       { return *this; }
00223     };
00224 
00225   // @} group iterators
00226 
00227 _GLIBCXX_END_NAMESPACE_VERSION
00228 } // namespace
00229 
00230 #endif