stream_iterator.h 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  1. // Stream iterators
  2. // Copyright (C) 2001-2018 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // Under Section 7 of GPL version 3, you are granted additional
  14. // permissions described in the GCC Runtime Library Exception, version
  15. // 3.1, as published by the Free Software Foundation.
  16. // You should have received a copy of the GNU General Public License and
  17. // a copy of the GCC Runtime Library Exception along with this program;
  18. // see the files COPYING3 and COPYING.RUNTIME respectively. If not, see
  19. // <http://www.gnu.org/licenses/>.
  20. /** @file bits/stream_iterator.h
  21. * This is an internal header file, included by other library headers.
  22. * Do not attempt to use it directly. @headername{iterator}
  23. */
  24. #ifndef _STREAM_ITERATOR_H
  25. #define _STREAM_ITERATOR_H 1
  26. #pragma GCC system_header
  27. #include <debug/debug.h>
  28. namespace std _GLIBCXX_VISIBILITY(default)
  29. {
  30. _GLIBCXX_BEGIN_NAMESPACE_VERSION
  31. /**
  32. * @addtogroup iterators
  33. * @{
  34. */
  35. /// Provides input iterator semantics for streams.
  36. template<typename _Tp, typename _CharT = char,
  37. typename _Traits = char_traits<_CharT>, typename _Dist = ptrdiff_t>
  38. class istream_iterator
  39. : public iterator<input_iterator_tag, _Tp, _Dist, const _Tp*, const _Tp&>
  40. {
  41. public:
  42. typedef _CharT char_type;
  43. typedef _Traits traits_type;
  44. typedef basic_istream<_CharT, _Traits> istream_type;
  45. private:
  46. istream_type* _M_stream;
  47. _Tp _M_value;
  48. bool _M_ok;
  49. public:
  50. /// Construct end of input stream iterator.
  51. _GLIBCXX_CONSTEXPR istream_iterator()
  52. : _M_stream(0), _M_value(), _M_ok(false) {}
  53. /// Construct start of input stream iterator.
  54. istream_iterator(istream_type& __s)
  55. : _M_stream(std::__addressof(__s))
  56. { _M_read(); }
  57. istream_iterator(const istream_iterator& __obj)
  58. : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
  59. _M_ok(__obj._M_ok)
  60. { }
  61. const _Tp&
  62. operator*() const
  63. {
  64. __glibcxx_requires_cond(_M_ok,
  65. _M_message(__gnu_debug::__msg_deref_istream)
  66. ._M_iterator(*this));
  67. return _M_value;
  68. }
  69. const _Tp*
  70. operator->() const { return std::__addressof((operator*())); }
  71. istream_iterator&
  72. operator++()
  73. {
  74. __glibcxx_requires_cond(_M_ok,
  75. _M_message(__gnu_debug::__msg_inc_istream)
  76. ._M_iterator(*this));
  77. _M_read();
  78. return *this;
  79. }
  80. istream_iterator
  81. operator++(int)
  82. {
  83. __glibcxx_requires_cond(_M_ok,
  84. _M_message(__gnu_debug::__msg_inc_istream)
  85. ._M_iterator(*this));
  86. istream_iterator __tmp = *this;
  87. _M_read();
  88. return __tmp;
  89. }
  90. bool
  91. _M_equal(const istream_iterator& __x) const
  92. { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
  93. private:
  94. void
  95. _M_read()
  96. {
  97. _M_ok = (_M_stream && *_M_stream) ? true : false;
  98. if (_M_ok)
  99. {
  100. *_M_stream >> _M_value;
  101. _M_ok = *_M_stream ? true : false;
  102. }
  103. }
  104. };
  105. /// Return true if x and y are both end or not end, or x and y are the same.
  106. template<typename _Tp, typename _CharT, typename _Traits, typename _Dist>
  107. inline bool
  108. operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
  109. const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
  110. { return __x._M_equal(__y); }
  111. /// Return false if x and y are both end or not end, or x and y are the same.
  112. template <class _Tp, class _CharT, class _Traits, class _Dist>
  113. inline bool
  114. operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
  115. const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
  116. { return !__x._M_equal(__y); }
  117. /**
  118. * @brief Provides output iterator semantics for streams.
  119. *
  120. * This class provides an iterator to write to an ostream. The type Tp is
  121. * the only type written by this iterator and there must be an
  122. * operator<<(Tp) defined.
  123. *
  124. * @tparam _Tp The type to write to the ostream.
  125. * @tparam _CharT The ostream char_type.
  126. * @tparam _Traits The ostream char_traits.
  127. */
  128. template<typename _Tp, typename _CharT = char,
  129. typename _Traits = char_traits<_CharT> >
  130. class ostream_iterator
  131. : public iterator<output_iterator_tag, void, void, void, void>
  132. {
  133. public:
  134. //@{
  135. /// Public typedef
  136. typedef _CharT char_type;
  137. typedef _Traits traits_type;
  138. typedef basic_ostream<_CharT, _Traits> ostream_type;
  139. //@}
  140. private:
  141. ostream_type* _M_stream;
  142. const _CharT* _M_string;
  143. public:
  144. /// Construct from an ostream.
  145. ostream_iterator(ostream_type& __s)
  146. : _M_stream(std::__addressof(__s)), _M_string(0) {}
  147. /**
  148. * Construct from an ostream.
  149. *
  150. * The delimiter string @a c is written to the stream after every Tp
  151. * written to the stream. The delimiter is not copied, and thus must
  152. * not be destroyed while this iterator is in use.
  153. *
  154. * @param __s Underlying ostream to write to.
  155. * @param __c CharT delimiter string to insert.
  156. */
  157. ostream_iterator(ostream_type& __s, const _CharT* __c)
  158. : _M_stream(&__s), _M_string(__c) { }
  159. /// Copy constructor.
  160. ostream_iterator(const ostream_iterator& __obj)
  161. : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
  162. /// Writes @a value to underlying ostream using operator<<. If
  163. /// constructed with delimiter string, writes delimiter to ostream.
  164. ostream_iterator&
  165. operator=(const _Tp& __value)
  166. {
  167. __glibcxx_requires_cond(_M_stream != 0,
  168. _M_message(__gnu_debug::__msg_output_ostream)
  169. ._M_iterator(*this));
  170. *_M_stream << __value;
  171. if (_M_string) *_M_stream << _M_string;
  172. return *this;
  173. }
  174. ostream_iterator&
  175. operator*()
  176. { return *this; }
  177. ostream_iterator&
  178. operator++()
  179. { return *this; }
  180. ostream_iterator&
  181. operator++(int)
  182. { return *this; }
  183. };
  184. // @} group iterators
  185. _GLIBCXX_END_NAMESPACE_VERSION
  186. } // namespace
  187. #endif