File indexing completed on 2026-05-03 08:13:53
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #ifndef _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H
0011 #define _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H
0012
0013 #include <__config>
0014 #include <__fwd/istream.h>
0015 #include <__fwd/streambuf.h>
0016 #include <__iterator/default_sentinel.h>
0017 #include <__iterator/iterator.h>
0018 #include <__iterator/iterator_traits.h>
0019 #include <__string/char_traits.h>
0020 #include <iosfwd>
0021
0022 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
0023 # pragma GCC system_header
0024 #endif
0025
0026 _LIBCPP_BEGIN_NAMESPACE_STD
0027
0028 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
0029 template <class _CharT, class _Traits>
0030 class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
0031 #if _LIBCPP_STD_VER <= 14 || !defined(_LIBCPP_ABI_NO_ITERATOR_BASES)
0032 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type, _CharT*, _CharT>
0033 #endif
0034 {
0035 _LIBCPP_SUPPRESS_DEPRECATED_POP
0036
0037 public:
0038 typedef input_iterator_tag iterator_category;
0039 typedef _CharT value_type;
0040 typedef typename _Traits::off_type difference_type;
0041 typedef _CharT* pointer;
0042 typedef _CharT reference;
0043 typedef _CharT char_type;
0044 typedef _Traits traits_type;
0045 typedef typename _Traits::int_type int_type;
0046 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
0047 typedef basic_istream<_CharT, _Traits> istream_type;
0048
0049 private:
0050 mutable streambuf_type* __sbuf_;
0051
0052 class __proxy {
0053 char_type __keep_;
0054 streambuf_type* __sbuf_;
0055 _LIBCPP_HIDE_FROM_ABI explicit __proxy(char_type __c, streambuf_type* __s) : __keep_(__c), __sbuf_(__s) {}
0056 friend class istreambuf_iterator;
0057
0058 public:
0059 _LIBCPP_HIDE_FROM_ABI char_type operator*() const { return __keep_; }
0060 };
0061
0062 _LIBCPP_HIDE_FROM_ABI bool __test_for_eof() const {
0063 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
0064 __sbuf_ = nullptr;
0065 return __sbuf_ == nullptr;
0066 }
0067
0068 public:
0069 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
0070 #if _LIBCPP_STD_VER >= 20
0071 _LIBCPP_HIDE_FROM_ABI constexpr istreambuf_iterator(default_sentinel_t) noexcept : istreambuf_iterator() {}
0072 #endif
0073 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(istream_type& __s) _NOEXCEPT : __sbuf_(__s.rdbuf()) {}
0074 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(streambuf_type* __s) _NOEXCEPT : __sbuf_(__s) {}
0075 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(const __proxy& __p) _NOEXCEPT : __sbuf_(__p.__sbuf_) {}
0076
0077 _LIBCPP_HIDE_FROM_ABI char_type operator*() const { return static_cast<char_type>(__sbuf_->sgetc()); }
0078 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator& operator++() {
0079 __sbuf_->sbumpc();
0080 return *this;
0081 }
0082 _LIBCPP_HIDE_FROM_ABI __proxy operator++(int) { return __proxy(__sbuf_->sbumpc(), __sbuf_); }
0083
0084 _LIBCPP_HIDE_FROM_ABI bool equal(const istreambuf_iterator& __b) const {
0085 return __test_for_eof() == __b.__test_for_eof();
0086 }
0087
0088 #if _LIBCPP_STD_VER >= 20
0089 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const istreambuf_iterator& __i, default_sentinel_t) {
0090 return __i.__test_for_eof();
0091 }
0092 #endif
0093 };
0094
0095 template <class _CharT, class _Traits>
0096 inline _LIBCPP_HIDE_FROM_ABI bool
0097 operator==(const istreambuf_iterator<_CharT, _Traits>& __a, const istreambuf_iterator<_CharT, _Traits>& __b) {
0098 return __a.equal(__b);
0099 }
0100
0101 #if _LIBCPP_STD_VER <= 17
0102 template <class _CharT, class _Traits>
0103 inline _LIBCPP_HIDE_FROM_ABI bool
0104 operator!=(const istreambuf_iterator<_CharT, _Traits>& __a, const istreambuf_iterator<_CharT, _Traits>& __b) {
0105 return !__a.equal(__b);
0106 }
0107 #endif
0108
0109 _LIBCPP_END_NAMESPACE_STD
0110
0111 #endif