stream_iterator.h 8.2 KB

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