1 // -*- C++ -*- 2 //===----------------------------------------------------------------------===// 3 // 4 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 5 // See https://llvm.org/LICENSE.txt for license information. 6 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 7 // 8 //===----------------------------------------------------------------------===// 9 10 #ifndef _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H 11 #define _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H 12 13 #include <__config> 14 #include <__iterator/default_sentinel.h> 15 #include <__iterator/iterator.h> 16 #include <__iterator/iterator_traits.h> 17 #include <iosfwd> // for forward declaration of basic_streambuf 18 19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) 20 # pragma GCC system_header 21 #endif 22 23 _LIBCPP_BEGIN_NAMESPACE_STD 24 25 _LIBCPP_SUPPRESS_DEPRECATED_PUSH 26 template<class _CharT, class _Traits> 27 class _LIBCPP_TEMPLATE_VIS istreambuf_iterator 28 #if _LIBCPP_STD_VER <= 14 || !defined(_LIBCPP_ABI_NO_ITERATOR_BASES) 29 : public iterator<input_iterator_tag, _CharT, 30 typename _Traits::off_type, _CharT*, 31 _CharT> 32 #endif 33 { 34 _LIBCPP_SUPPRESS_DEPRECATED_POP 35 public: 36 typedef input_iterator_tag iterator_category; 37 typedef _CharT value_type; 38 typedef typename _Traits::off_type difference_type; 39 typedef _CharT* pointer; 40 typedef _CharT reference; 41 typedef _CharT char_type; 42 typedef _Traits traits_type; 43 typedef typename _Traits::int_type int_type; 44 typedef basic_streambuf<_CharT,_Traits> streambuf_type; 45 typedef basic_istream<_CharT,_Traits> istream_type; 46 private: 47 mutable streambuf_type* __sbuf_; 48 49 class __proxy 50 { 51 char_type __keep_; 52 streambuf_type* __sbuf_; 53 _LIBCPP_INLINE_VISIBILITY __proxy(char_type __c, streambuf_type* __s) 54 : __keep_(__c), __sbuf_(__s) {} 55 friend class istreambuf_iterator; 56 public: 57 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;} 58 }; 59 60 _LIBCPP_INLINE_VISIBILITY 61 bool __test_for_eof() const 62 { 63 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof())) 64 __sbuf_ = nullptr; 65 return __sbuf_ == nullptr; 66 } 67 public: 68 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {} 69 #if _LIBCPP_STD_VER > 17 70 _LIBCPP_INLINE_VISIBILITY constexpr istreambuf_iterator(default_sentinel_t) noexcept 71 : istreambuf_iterator() {} 72 #endif // _LIBCPP_STD_VER > 17 73 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT 74 : __sbuf_(__s.rdbuf()) {} 75 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT 76 : __sbuf_(__s) {} 77 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT 78 : __sbuf_(__p.__sbuf_) {} 79 80 _LIBCPP_INLINE_VISIBILITY char_type operator*() const 81 {return static_cast<char_type>(__sbuf_->sgetc());} 82 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++() 83 { 84 __sbuf_->sbumpc(); 85 return *this; 86 } 87 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int) 88 { 89 return __proxy(__sbuf_->sbumpc(), __sbuf_); 90 } 91 92 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const 93 {return __test_for_eof() == __b.__test_for_eof();} 94 95 #if _LIBCPP_STD_VER > 17 96 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const istreambuf_iterator& __i, default_sentinel_t) { 97 return __i.__test_for_eof(); 98 } 99 #endif // _LIBCPP_STD_VER > 17 100 }; 101 102 template <class _CharT, class _Traits> 103 inline _LIBCPP_INLINE_VISIBILITY 104 bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a, 105 const istreambuf_iterator<_CharT,_Traits>& __b) 106 {return __a.equal(__b);} 107 108 #if _LIBCPP_STD_VER <= 17 109 template <class _CharT, class _Traits> 110 inline _LIBCPP_INLINE_VISIBILITY 111 bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a, 112 const istreambuf_iterator<_CharT,_Traits>& __b) 113 {return !__a.equal(__b);} 114 #endif // _LIBCPP_STD_VER <= 17 115 116 _LIBCPP_END_NAMESPACE_STD 117 118 #endif // _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H 119