• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
20 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
21 #  pragma GCC system_header
22 #endif
23 
24 _LIBCPP_BEGIN_NAMESPACE_STD
25 
26 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
27 template<class _CharT, class _Traits>
28 class _LIBCPP_TEMPLATE_VIS istreambuf_iterator
29 #if _LIBCPP_STD_VER <= 14 || !defined(_LIBCPP_ABI_NO_ITERATOR_BASES)
30     : public iterator<input_iterator_tag, _CharT,
31                       typename _Traits::off_type, _CharT*,
32                       _CharT>
33 #endif
34 {
35 _LIBCPP_SUPPRESS_DEPRECATED_POP
36 public:
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 private:
48     mutable streambuf_type* __sbuf_;
49 
50     class __proxy
51     {
52         char_type __keep_;
53         streambuf_type* __sbuf_;
54         _LIBCPP_INLINE_VISIBILITY
__proxy(char_type __c,streambuf_type * __s)55         explicit __proxy(char_type __c, streambuf_type* __s)
56             : __keep_(__c), __sbuf_(__s) {}
57         friend class istreambuf_iterator;
58     public:
59         _LIBCPP_INLINE_VISIBILITY char_type operator*() const {return __keep_;}
60     };
61 
62     _LIBCPP_INLINE_VISIBILITY
__test_for_eof()63     bool __test_for_eof() const
64     {
65         if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))
66             __sbuf_ = nullptr;
67         return __sbuf_ == nullptr;
68     }
69 public:
istreambuf_iterator()70     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}
71 #if _LIBCPP_STD_VER >= 20
istreambuf_iterator(default_sentinel_t)72     _LIBCPP_INLINE_VISIBILITY constexpr istreambuf_iterator(default_sentinel_t) noexcept
73         : istreambuf_iterator() {}
74 #endif // _LIBCPP_STD_VER >= 20
istreambuf_iterator(istream_type & __s)75     _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(istream_type& __s) _NOEXCEPT
76         : __sbuf_(__s.rdbuf()) {}
istreambuf_iterator(streambuf_type * __s)77     _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(streambuf_type* __s) _NOEXCEPT
78         : __sbuf_(__s) {}
istreambuf_iterator(const __proxy & __p)79     _LIBCPP_INLINE_VISIBILITY istreambuf_iterator(const __proxy& __p) _NOEXCEPT
80         : __sbuf_(__p.__sbuf_) {}
81 
82     _LIBCPP_INLINE_VISIBILITY char_type  operator*() const
83         {return static_cast<char_type>(__sbuf_->sgetc());}
84     _LIBCPP_INLINE_VISIBILITY istreambuf_iterator& operator++()
85         {
86             __sbuf_->sbumpc();
87             return *this;
88         }
89     _LIBCPP_INLINE_VISIBILITY __proxy              operator++(int)
90         {
91             return __proxy(__sbuf_->sbumpc(), __sbuf_);
92         }
93 
equal(const istreambuf_iterator & __b)94     _LIBCPP_INLINE_VISIBILITY bool equal(const istreambuf_iterator& __b) const
95         {return __test_for_eof() == __b.__test_for_eof();}
96 
97 #if _LIBCPP_STD_VER >= 20
98     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const istreambuf_iterator& __i, default_sentinel_t) {
99       return __i.__test_for_eof();
100     }
101 #endif // _LIBCPP_STD_VER >= 20
102 };
103 
104 template <class _CharT, class _Traits>
105 inline _LIBCPP_INLINE_VISIBILITY
106 bool operator==(const istreambuf_iterator<_CharT,_Traits>& __a,
107                 const istreambuf_iterator<_CharT,_Traits>& __b)
108                 {return __a.equal(__b);}
109 
110 #if _LIBCPP_STD_VER <= 17
111 template <class _CharT, class _Traits>
112 inline _LIBCPP_INLINE_VISIBILITY
113 bool operator!=(const istreambuf_iterator<_CharT,_Traits>& __a,
114                 const istreambuf_iterator<_CharT,_Traits>& __b)
115                 {return !__a.equal(__b);}
116 #endif // _LIBCPP_STD_VER <= 17
117 
118 _LIBCPP_END_NAMESPACE_STD
119 
120 #endif // _LIBCPP___ITERATOR_ISTREAMBUF_ITERATOR_H
121