stream_iterator.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. // Stream iterators
  2. // Copyright (C) 2001-2019 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. #if __cplusplus >= 201103L
  62. istream_iterator& operator=(const istream_iterator&) = default;
  63. #endif
  64. const _Tp&
  65. operator*() const
  66. {
  67. __glibcxx_requires_cond(_M_ok,
  68. _M_message(__gnu_debug::__msg_deref_istream)
  69. ._M_iterator(*this));
  70. return _M_value;
  71. }
  72. const _Tp*
  73. operator->() const { return std::__addressof((operator*())); }
  74. istream_iterator&
  75. operator++()
  76. {
  77. __glibcxx_requires_cond(_M_ok,
  78. _M_message(__gnu_debug::__msg_inc_istream)
  79. ._M_iterator(*this));
  80. _M_read();
  81. return *this;
  82. }
  83. istream_iterator
  84. operator++(int)
  85. {
  86. __glibcxx_requires_cond(_M_ok,
  87. _M_message(__gnu_debug::__msg_inc_istream)
  88. ._M_iterator(*this));
  89. istream_iterator __tmp = *this;
  90. _M_read();
  91. return __tmp;
  92. }
  93. bool
  94. _M_equal(const istream_iterator& __x) const
  95. { return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream); }
  96. private:
  97. void
  98. _M_read()
  99. {
  100. _M_ok = (_M_stream && *_M_stream) ? true : false;
  101. if (_M_ok)
  102. {
  103. *_M_stream >> _M_value;
  104. _M_ok = *_M_stream ? true : false;
  105. }
  106. }
  107. };
  108. /// Return true if x and y are both end or not end, or x and y are the same.
  109. template<typename _Tp, typename _CharT, typename _Traits, typename _Dist>
  110. inline bool
  111. operator==(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
  112. const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
  113. { return __x._M_equal(__y); }
  114. /// Return false if x and y are both end or not end, or x and y are the same.
  115. template <class _Tp, class _CharT, class _Traits, class _Dist>
  116. inline bool
  117. operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __x,
  118. const istream_iterator<_Tp, _CharT, _Traits, _Dist>& __y)
  119. { return !__x._M_equal(__y); }
  120. /**
  121. * @brief Provides output iterator semantics for streams.
  122. *
  123. * This class provides an iterator to write to an ostream. The type Tp is
  124. * the only type written by this iterator and there must be an
  125. * operator<<(Tp) defined.
  126. *
  127. * @tparam _Tp The type to write to the ostream.
  128. * @tparam _CharT The ostream char_type.
  129. * @tparam _Traits The ostream char_traits.
  130. */
  131. template<typename _Tp, typename _CharT = char,
  132. typename _Traits = char_traits<_CharT> >
  133. class ostream_iterator
  134. : public iterator<output_iterator_tag, void, void, void, void>
  135. {
  136. public:
  137. //@{
  138. /// Public typedef
  139. typedef _CharT char_type;
  140. typedef _Traits traits_type;
  141. typedef basic_ostream<_CharT, _Traits> ostream_type;
  142. //@}
  143. private:
  144. ostream_type* _M_stream;
  145. const _CharT* _M_string;
  146. public:
  147. /// Construct from an ostream.
  148. ostream_iterator(ostream_type& __s)
  149. : _M_stream(std::__addressof(__s)), _M_string(0) {}
  150. /**
  151. * Construct from an ostream.
  152. *
  153. * The delimiter string @a c is written to the stream after every Tp
  154. * written to the stream. The delimiter is not copied, and thus must
  155. * not be destroyed while this iterator is in use.
  156. *
  157. * @param __s Underlying ostream to write to.
  158. * @param __c CharT delimiter string to insert.
  159. */
  160. ostream_iterator(ostream_type& __s, const _CharT* __c)
  161. : _M_stream(&__s), _M_string(__c) { }
  162. /// Copy constructor.
  163. ostream_iterator(const ostream_iterator& __obj)
  164. : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
  165. #if __cplusplus >= 201103L
  166. ostream_iterator& operator=(const ostream_iterator&) = default;
  167. #endif
  168. /// Writes @a value to underlying ostream using operator<<. If
  169. /// constructed with delimiter string, writes delimiter to ostream.
  170. ostream_iterator&
  171. operator=(const _Tp& __value)
  172. {
  173. __glibcxx_requires_cond(_M_stream != 0,
  174. _M_message(__gnu_debug::__msg_output_ostream)
  175. ._M_iterator(*this));
  176. *_M_stream << __value;
  177. if (_M_string) *_M_stream << _M_string;
  178. return *this;
  179. }
  180. ostream_iterator&
  181. operator*()
  182. { return *this; }
  183. ostream_iterator&
  184. operator++()
  185. { return *this; }
  186. ostream_iterator&
  187. operator++(int)
  188. { return *this; }
  189. };
  190. // @} group iterators
  191. _GLIBCXX_END_NAMESPACE_VERSION
  192. } // namespace
  193. #endif