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 explicit __proxy(char_type __c, streambuf_type* __s) 55 : __keep_(__c), __sbuf_(__s) {} 56 friend class istreambuf_iterator; 57 public: 58 _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;} 59 }; 60 61 _LIBCPP_INLINE_VISIBILITY __test_for_eof()62 bool __test_for_eof() const 63 { 64 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof())) 65 __sbuf_ = nullptr; 66 return __sbuf_ == nullptr; 67 } 68 public: istreambuf_iterator()69 _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {} 70 #if _LIBCPP_STD_VER > 17 istreambuf_iterator(default_sentinel_t)71 _LIBCPP_INLINE_VISIBILITY constexpr istreambuf_iterator(default_sentinel_t) noexcept 72 : istreambuf_iterator() {} 73 #endif // _LIBCPP_STD_VER > 17 istreambuf_iterator(istream_type & __s)74 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT 75 : __sbuf_(__s.rdbuf()) {} istreambuf_iterator(streambuf_type * __s)76 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT 77 : __sbuf_(__s) {} istreambuf_iterator(const __proxy & __p)78 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT 79 : __sbuf_(__p.__sbuf_) {} 80 81 _LIBCPP_INLINE_VISIBILITY char_type operator*() const 82 {return static_cast<char_type>(__sbuf_->sgetc());} 83 _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++() 84 { 85 __sbuf_->sbumpc(); 86 return *this; 87 } 88 _LIBCPP_INLINE_VISIBILITY __proxy operator++(int) 89 { 90 return __proxy(__sbuf_->sbumpc(), __sbuf_); 91 } 92 equal(const istreambuf_iterator & __b)93 _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const 94 {return __test_for_eof() == __b.__test_for_eof();} 95 96 #if _LIBCPP_STD_VER > 17 97 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const istreambuf_iterator& __i, default_sentinel_t) { 98 return __i.__test_for_eof(); 99 } 100 #endif // _LIBCPP_STD_VER > 17 101 }; 102 103 template <class _CharT, class _Traits> 104 inline _LIBCPP_INLINE_VISIBILITY 105 bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a, 106 const istreambuf_iterator<_CharT,_Traits>& __b) 107 {return __a.equal(__b);} 108 109 #if _LIBCPP_STD_VER <= 17 110 template <class _CharT, class _Traits> 111 inline _LIBCPP_INLINE_VISIBILITY 112 bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a, 113 const istreambuf_iterator<_CharT,_Traits>& __b) 114 {return !__a.equal(__b);} 115 #endif // _LIBCPP_STD_VER <= 17 116 117 _LIBCPP_END_NAMESPACE_STD 118 119 #endif // _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H 120