stream_iterator.h 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. // Stream iterators
  2. // Copyright (C) 2001-2020 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. // This bool becomes false at end-of-stream. It should be sufficient to
  49. // check _M_stream != nullptr instead, but historically we did not set
  50. // _M_stream to null when reaching the end, so we need to keep this flag.
  51. bool _M_ok;
  52. public:
  53. /// Construct end of input stream iterator.
  54. _GLIBCXX_CONSTEXPR istream_iterator()
  55. : _M_stream(0), _M_value(), _M_ok(false) {}
  56. /// Construct start of input stream iterator.
  57. istream_iterator(istream_type& __s)
  58. : _M_stream(std::__addressof(__s)), _M_ok(true)
  59. { _M_read(); }
  60. istream_iterator(const istream_iterator& __obj)
  61. : _M_stream(__obj._M_stream), _M_value(__obj._M_value),
  62. _M_ok(__obj._M_ok)
  63. { }
  64. #if __cplusplus > 201703L && __cpp_lib_concepts
  65. constexpr
  66. istream_iterator(default_sentinel_t)
  67. noexcept(is_nothrow_default_constructible_v<_Tp>)
  68. : istream_iterator() { }
  69. #endif
  70. #if __cplusplus >= 201103L
  71. istream_iterator& operator=(const istream_iterator&) = default;
  72. ~istream_iterator() = default;
  73. #endif
  74. const _Tp&
  75. operator*() const
  76. {
  77. __glibcxx_requires_cond(_M_ok,
  78. _M_message(__gnu_debug::__msg_deref_istream)
  79. ._M_iterator(*this));
  80. return _M_value;
  81. }
  82. const _Tp*
  83. operator->() const { return std::__addressof((operator*())); }
  84. istream_iterator&
  85. operator++()
  86. {
  87. __glibcxx_requires_cond(_M_ok,
  88. _M_message(__gnu_debug::__msg_inc_istream)
  89. ._M_iterator(*this));
  90. _M_read();
  91. return *this;
  92. }
  93. istream_iterator
  94. operator++(int)
  95. {
  96. __glibcxx_requires_cond(_M_ok,
  97. _M_message(__gnu_debug::__msg_inc_istream)
  98. ._M_iterator(*this));
  99. istream_iterator __tmp = *this;
  100. _M_read();
  101. return __tmp;
  102. }
  103. private:
  104. bool
  105. _M_equal(const istream_iterator& __x) const
  106. {
  107. // Ideally this would just return _M_stream == __x._M_stream,
  108. // but code compiled with old versions never sets _M_stream to null.
  109. return (_M_ok == __x._M_ok) && (!_M_ok || _M_stream == __x._M_stream);
  110. }
  111. void
  112. _M_read()
  113. {
  114. if (_M_stream && !(*_M_stream >> _M_value))
  115. {
  116. _M_stream = 0;
  117. _M_ok = false;
  118. }
  119. }
  120. /// Return true if the iterators refer to the same stream,
  121. /// or are both at end-of-stream.
  122. friend bool
  123. operator==(const istream_iterator& __x, const istream_iterator& __y)
  124. { return __x._M_equal(__y); }
  125. /// Return true if the iterators refer to different streams,
  126. /// or if one is at end-of-stream and the other is not.
  127. friend bool
  128. operator!=(const istream_iterator& __x, const istream_iterator& __y)
  129. { return !__x._M_equal(__y); }
  130. #if __cplusplus > 201703L && __cpp_lib_concepts
  131. friend bool
  132. operator==(const istream_iterator& __i, default_sentinel_t)
  133. { return !__i._M_stream; }
  134. #endif
  135. };
  136. /**
  137. * @brief Provides output iterator semantics for streams.
  138. *
  139. * This class provides an iterator to write to an ostream. The type Tp is
  140. * the only type written by this iterator and there must be an
  141. * operator<<(Tp) defined.
  142. *
  143. * @tparam _Tp The type to write to the ostream.
  144. * @tparam _CharT The ostream char_type.
  145. * @tparam _Traits The ostream char_traits.
  146. */
  147. template<typename _Tp, typename _CharT = char,
  148. typename _Traits = char_traits<_CharT> >
  149. class ostream_iterator
  150. : public iterator<output_iterator_tag, void, void, void, void>
  151. {
  152. public:
  153. //@{
  154. /// Public typedef
  155. #if __cplusplus > 201703L
  156. using difference_type = ptrdiff_t;
  157. #endif
  158. typedef _CharT char_type;
  159. typedef _Traits traits_type;
  160. typedef basic_ostream<_CharT, _Traits> ostream_type;
  161. //@}
  162. private:
  163. ostream_type* _M_stream;
  164. const _CharT* _M_string;
  165. public:
  166. #if __cplusplus > 201703L
  167. constexpr ostream_iterator() noexcept
  168. : _M_stream(nullptr), _M_string(nullptr) { }
  169. #endif
  170. /// Construct from an ostream.
  171. ostream_iterator(ostream_type& __s)
  172. : _M_stream(std::__addressof(__s)), _M_string(0) {}
  173. /**
  174. * Construct from an ostream.
  175. *
  176. * The delimiter string @a c is written to the stream after every Tp
  177. * written to the stream. The delimiter is not copied, and thus must
  178. * not be destroyed while this iterator is in use.
  179. *
  180. * @param __s Underlying ostream to write to.
  181. * @param __c CharT delimiter string to insert.
  182. */
  183. ostream_iterator(ostream_type& __s, const _CharT* __c)
  184. : _M_stream(std::__addressof(__s)), _M_string(__c) { }
  185. /// Copy constructor.
  186. ostream_iterator(const ostream_iterator& __obj)
  187. : _M_stream(__obj._M_stream), _M_string(__obj._M_string) { }
  188. #if __cplusplus >= 201103L
  189. ostream_iterator& operator=(const ostream_iterator&) = default;
  190. #endif
  191. /// Writes @a value to underlying ostream using operator<<. If
  192. /// constructed with delimiter string, writes delimiter to ostream.
  193. ostream_iterator&
  194. operator=(const _Tp& __value)
  195. {
  196. __glibcxx_requires_cond(_M_stream != 0,
  197. _M_message(__gnu_debug::__msg_output_ostream)
  198. ._M_iterator(*this));
  199. *_M_stream << __value;
  200. if (_M_string)
  201. *_M_stream << _M_string;
  202. return *this;
  203. }
  204. ostream_iterator&
  205. operator*()
  206. { return *this; }
  207. ostream_iterator&
  208. operator++()
  209. { return *this; }
  210. ostream_iterator&
  211. operator++(int)
  212. { return *this; }
  213. };
  214. // @} group iterators
  215. _GLIBCXX_END_NAMESPACE_VERSION
  216. } // namespace
  217. #endif