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