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 <__fwd/istream.h>
15#include <__fwd/streambuf.h>
16#include <__iterator/default_sentinel.h>
17#include <__iterator/iterator.h>
18#include <__iterator/iterator_traits.h>
19#include <__string/char_traits.h>
20#include <iosfwd>
21
22#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
23# pragma GCC system_header
24#endif
25
26_LIBCPP_BEGIN_NAMESPACE_STD
27
28template <class _CharT, class _Traits>
29class istreambuf_iterator
30 : public __iterator_base<istreambuf_iterator<_CharT, _Traits>,
31 input_iterator_tag,
32 _CharT,
33 typename _Traits::off_type,
34 _CharT*,
35 _CharT> {
36public:
37 typedef input_iterator_tag iterator_category;
38 typedef _CharT value_type;
39 typedef typename _Traits::off_type difference_type;
40 typedef _CharT* pointer;
41 typedef _CharT reference;
42 typedef _CharT char_type;
43 typedef _Traits traits_type;
44 typedef typename _Traits::int_type int_type;
45 typedef basic_streambuf<_CharT, _Traits> streambuf_type;
46 typedef basic_istream<_CharT, _Traits> istream_type;
47
48private:
49 mutable streambuf_type* __sbuf_;
50
51 class __proxy {
52 char_type __keep_;
53 streambuf_type* __sbuf_;
54 _LIBCPP_HIDE_FROM_ABI explicit __proxy(char_type __c, streambuf_type* __s) : __keep_(__c), __sbuf_(__s) {}
55 friend class istreambuf_iterator;
56
57 public:
58 _LIBCPP_HIDE_FROM_ABI char_type operator*() const { return __keep_; }
59 };
60
61 _LIBCPP_HIDE_FROM_ABI bool __test_for_eof() const {
62 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
63 __sbuf_ = nullptr;
64 return __sbuf_ == nullptr;
65 }
66
67public:
68 _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
69#if _LIBCPP_STD_VER >= 20
70 _LIBCPP_HIDE_FROM_ABI constexpr istreambuf_iterator(default_sentinel_t) noexcept : istreambuf_iterator() {}
71#endif // _LIBCPP_STD_VER >= 20
72 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(istream_type& __s) _NOEXCEPT : __sbuf_(__s.rdbuf()) {}
73 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(streambuf_type* __s) _NOEXCEPT : __sbuf_(__s) {}
74 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(const __proxy& __p) _NOEXCEPT : __sbuf_(__p.__sbuf_) {}
75
76 _LIBCPP_HIDE_FROM_ABI char_type operator*() const { return static_cast<char_type>(__sbuf_->sgetc()); }
77 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator& operator++() {
78 __sbuf_->sbumpc();
79 return *this;
80 }
81 _LIBCPP_HIDE_FROM_ABI __proxy operator++(int) { return __proxy(__sbuf_->sbumpc(), __sbuf_); }
82
83 _LIBCPP_HIDE_FROM_ABI bool equal(const istreambuf_iterator& __b) const {
84 return __test_for_eof() == __b.__test_for_eof();
85 }
86
87#if _LIBCPP_STD_VER >= 20
88 friend _LIBCPP_HIDE_FROM_ABI bool operator==(const istreambuf_iterator& __i, default_sentinel_t) {
89 return __i.__test_for_eof();
90 }
91#endif // _LIBCPP_STD_VER >= 20
92};
93
94template <class _CharT, class _Traits>
95inline _LIBCPP_HIDE_FROM_ABI bool
96operator==(const istreambuf_iterator<_CharT, _Traits>& __a, const istreambuf_iterator<_CharT, _Traits>& __b) {
97 return __a.equal(__b);
98}
99
100#if _LIBCPP_STD_VER <= 17
101template <class _CharT, class _Traits>
102inline _LIBCPP_HIDE_FROM_ABI bool
103operator!=(const istreambuf_iterator<_CharT, _Traits>& __a, const istreambuf_iterator<_CharT, _Traits>& __b) {
104 return !__a.equal(__b);
105}
106#endif // _LIBCPP_STD_VER <= 17
107
108_LIBCPP_END_NAMESPACE_STD
109
110#endif // _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H
111