stream_iterator.h 8.1 KB

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