xref: /freebsd-src/contrib/llvm-project/libcxx/include/__iterator/istreambuf_iterator.h (revision cb14a3fe5122c879eae1fb480ed7ce82a699ddb6)
1fe6060f1SDimitry Andric // -*- C++ -*-
2fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
3fe6060f1SDimitry Andric //
4fe6060f1SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5fe6060f1SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
6fe6060f1SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7fe6060f1SDimitry Andric //
8fe6060f1SDimitry Andric //===----------------------------------------------------------------------===//
9fe6060f1SDimitry Andric 
10fe6060f1SDimitry Andric #ifndef _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H
11fe6060f1SDimitry Andric #define _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H
12fe6060f1SDimitry Andric 
13fe6060f1SDimitry Andric #include <__config>
145f757f3fSDimitry Andric #include <__fwd/istream.h>
155f757f3fSDimitry Andric #include <__fwd/streambuf.h>
1681ad6265SDimitry Andric #include <__iterator/default_sentinel.h>
17fe6060f1SDimitry Andric #include <__iterator/iterator.h>
18fe6060f1SDimitry Andric #include <__iterator/iterator_traits.h>
19fe6060f1SDimitry Andric 
20fe6060f1SDimitry Andric #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21fe6060f1SDimitry Andric #  pragma GCC system_header
22fe6060f1SDimitry Andric #endif
23fe6060f1SDimitry Andric 
24fe6060f1SDimitry Andric _LIBCPP_BEGIN_NAMESPACE_STD
25fe6060f1SDimitry Andric 
26fe6060f1SDimitry Andric _LIBCPP_SUPPRESS_DEPRECATED_PUSH
27fe6060f1SDimitry Andric template <class _CharT, class _Traits>
28fe6060f1SDimitry Andric class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
29fe6060f1SDimitry Andric #if _LIBCPP_STD_VER <= 14 || !defined(_LIBCPP_ABI_NO_ITERATOR_BASES)
30*cb14a3feSDimitry Andric     : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type, _CharT*, _CharT>
31fe6060f1SDimitry Andric #endif
32fe6060f1SDimitry Andric {
33fe6060f1SDimitry Andric   _LIBCPP_SUPPRESS_DEPRECATED_POP
34*cb14a3feSDimitry Andric 
35fe6060f1SDimitry Andric public:
36fe6060f1SDimitry Andric   typedef input_iterator_tag iterator_category;
37fe6060f1SDimitry Andric   typedef _CharT value_type;
38fe6060f1SDimitry Andric   typedef typename _Traits::off_type difference_type;
39fe6060f1SDimitry Andric   typedef _CharT* pointer;
40fe6060f1SDimitry Andric   typedef _CharT reference;
41fe6060f1SDimitry Andric   typedef _CharT char_type;
42fe6060f1SDimitry Andric   typedef _Traits traits_type;
43fe6060f1SDimitry Andric   typedef typename _Traits::int_type int_type;
44fe6060f1SDimitry Andric   typedef basic_streambuf<_CharT, _Traits> streambuf_type;
45fe6060f1SDimitry Andric   typedef basic_istream<_CharT, _Traits> istream_type;
46*cb14a3feSDimitry Andric 
47fe6060f1SDimitry Andric private:
48fe6060f1SDimitry Andric   mutable streambuf_type* __sbuf_;
49fe6060f1SDimitry Andric 
50*cb14a3feSDimitry Andric   class __proxy {
51fe6060f1SDimitry Andric     char_type __keep_;
52fe6060f1SDimitry Andric     streambuf_type* __sbuf_;
__proxy(char_type __c,streambuf_type * __s)53*cb14a3feSDimitry Andric     _LIBCPP_HIDE_FROM_ABI explicit __proxy(char_type __c, streambuf_type* __s) : __keep_(__c), __sbuf_(__s) {}
54fe6060f1SDimitry Andric     friend class istreambuf_iterator;
55*cb14a3feSDimitry Andric 
56fe6060f1SDimitry Andric   public:
575f757f3fSDimitry Andric     _LIBCPP_HIDE_FROM_ABI char_type operator*() const { return __keep_; }
58fe6060f1SDimitry Andric   };
59fe6060f1SDimitry Andric 
__test_for_eof()60*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI bool __test_for_eof() const {
61fe6060f1SDimitry Andric     if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
62fe6060f1SDimitry Andric       __sbuf_ = nullptr;
63fe6060f1SDimitry Andric     return __sbuf_ == nullptr;
64fe6060f1SDimitry Andric   }
65*cb14a3feSDimitry Andric 
66fe6060f1SDimitry Andric public:
istreambuf_iterator()675f757f3fSDimitry Andric   _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
6806c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
istreambuf_iterator(default_sentinel_t)69*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI constexpr istreambuf_iterator(default_sentinel_t) noexcept : istreambuf_iterator() {}
7006c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
istreambuf_iterator(istream_type & __s)71*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(istream_type& __s) _NOEXCEPT : __sbuf_(__s.rdbuf()) {}
istreambuf_iterator(streambuf_type * __s)72*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(streambuf_type* __s) _NOEXCEPT : __sbuf_(__s) {}
istreambuf_iterator(const __proxy & __p)73*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(const __proxy& __p) _NOEXCEPT : __sbuf_(__p.__sbuf_) {}
74fe6060f1SDimitry Andric 
75*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI char_type operator*() const { return static_cast<char_type>(__sbuf_->sgetc()); }
76*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI istreambuf_iterator& operator++() {
77fe6060f1SDimitry Andric     __sbuf_->sbumpc();
78fe6060f1SDimitry Andric     return *this;
79fe6060f1SDimitry Andric   }
80*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI __proxy operator++(int) { return __proxy(__sbuf_->sbumpc(), __sbuf_); }
81fe6060f1SDimitry Andric 
equal(const istreambuf_iterator & __b)82*cb14a3feSDimitry Andric   _LIBCPP_HIDE_FROM_ABI bool equal(const istreambuf_iterator& __b) const {
83*cb14a3feSDimitry Andric     return __test_for_eof() == __b.__test_for_eof();
84*cb14a3feSDimitry Andric   }
8581ad6265SDimitry Andric 
8606c3fb27SDimitry Andric #if _LIBCPP_STD_VER >= 20
8781ad6265SDimitry Andric   friend _LIBCPP_HIDE_FROM_ABI bool operator==(const istreambuf_iterator& __i, default_sentinel_t) {
8881ad6265SDimitry Andric     return __i.__test_for_eof();
8981ad6265SDimitry Andric   }
9006c3fb27SDimitry Andric #endif // _LIBCPP_STD_VER >= 20
91fe6060f1SDimitry Andric };
92fe6060f1SDimitry Andric 
93fe6060f1SDimitry Andric template <class _CharT, class _Traits>
94*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI bool
95*cb14a3feSDimitry Andric operator==(const istreambuf_iterator<_CharT, _Traits>& __a, const istreambuf_iterator<_CharT, _Traits>& __b) {
96*cb14a3feSDimitry Andric   return __a.equal(__b);
97*cb14a3feSDimitry Andric }
98fe6060f1SDimitry Andric 
9981ad6265SDimitry Andric #if _LIBCPP_STD_VER <= 17
100fe6060f1SDimitry Andric template <class _CharT, class _Traits>
101*cb14a3feSDimitry Andric inline _LIBCPP_HIDE_FROM_ABI bool
102*cb14a3feSDimitry Andric operator!=(const istreambuf_iterator<_CharT, _Traits>& __a, const istreambuf_iterator<_CharT, _Traits>& __b) {
103*cb14a3feSDimitry Andric   return !__a.equal(__b);
104*cb14a3feSDimitry Andric }
10581ad6265SDimitry Andric #endif // _LIBCPP_STD_VER <= 17
106fe6060f1SDimitry Andric 
107fe6060f1SDimitry Andric _LIBCPP_END_NAMESPACE_STD
108fe6060f1SDimitry Andric 
109fe6060f1SDimitry Andric #endif // _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H
110