95 lines · c
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-exception7//8//===----------------------------------------------------------------------===//9 10#ifndef _LIBCPP___CXX03___ITERATOR_ISTREAMBUF_ITERATOR_H11#define _LIBCPP___CXX03___ITERATOR_ISTREAMBUF_ITERATOR_H12 13#include <__cxx03/__config>14#include <__cxx03/__fwd/istream.h>15#include <__cxx03/__fwd/streambuf.h>16#include <__cxx03/__iterator/iterator.h>17#include <__cxx03/__iterator/iterator_traits.h>18 19#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)20# pragma GCC system_header21#endif22 23_LIBCPP_BEGIN_NAMESPACE_STD24 25_LIBCPP_SUPPRESS_DEPRECATED_PUSH26template <class _CharT, class _Traits>27class _LIBCPP_TEMPLATE_VIS istreambuf_iterator28 : public iterator<input_iterator_tag, _CharT, typename _Traits::off_type, _CharT*, _CharT> {29 _LIBCPP_SUPPRESS_DEPRECATED_POP30 31public:32 typedef input_iterator_tag iterator_category;33 typedef _CharT value_type;34 typedef typename _Traits::off_type difference_type;35 typedef _CharT* pointer;36 typedef _CharT reference;37 typedef _CharT char_type;38 typedef _Traits traits_type;39 typedef typename _Traits::int_type int_type;40 typedef basic_streambuf<_CharT, _Traits> streambuf_type;41 typedef basic_istream<_CharT, _Traits> istream_type;42 43private:44 mutable streambuf_type* __sbuf_;45 46 class __proxy {47 char_type __keep_;48 streambuf_type* __sbuf_;49 _LIBCPP_HIDE_FROM_ABI explicit __proxy(char_type __c, streambuf_type* __s) : __keep_(__c), __sbuf_(__s) {}50 friend class istreambuf_iterator;51 52 public:53 _LIBCPP_HIDE_FROM_ABI char_type operator*() const { return __keep_; }54 };55 56 _LIBCPP_HIDE_FROM_ABI bool __test_for_eof() const {57 if (__sbuf_ && traits_type::eq_int_type(__sbuf_->sgetc(), traits_type::eof()))58 __sbuf_ = nullptr;59 return __sbuf_ == nullptr;60 }61 62public:63 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator() _NOEXCEPT : __sbuf_(nullptr) {}64 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(istream_type& __s) _NOEXCEPT : __sbuf_(__s.rdbuf()) {}65 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(streambuf_type* __s) _NOEXCEPT : __sbuf_(__s) {}66 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator(const __proxy& __p) _NOEXCEPT : __sbuf_(__p.__sbuf_) {}67 68 _LIBCPP_HIDE_FROM_ABI char_type operator*() const { return static_cast<char_type>(__sbuf_->sgetc()); }69 _LIBCPP_HIDE_FROM_ABI istreambuf_iterator& operator++() {70 __sbuf_->sbumpc();71 return *this;72 }73 _LIBCPP_HIDE_FROM_ABI __proxy operator++(int) { return __proxy(__sbuf_->sbumpc(), __sbuf_); }74 75 _LIBCPP_HIDE_FROM_ABI bool equal(const istreambuf_iterator& __b) const {76 return __test_for_eof() == __b.__test_for_eof();77 }78};79 80template <class _CharT, class _Traits>81inline _LIBCPP_HIDE_FROM_ABI bool82operator==(const istreambuf_iterator<_CharT, _Traits>& __a, const istreambuf_iterator<_CharT, _Traits>& __b) {83 return __a.equal(__b);84}85 86template <class _CharT, class _Traits>87inline _LIBCPP_HIDE_FROM_ABI bool88operator!=(const istreambuf_iterator<_CharT, _Traits>& __a, const istreambuf_iterator<_CharT, _Traits>& __b) {89 return !__a.equal(__b);90}91 92_LIBCPP_END_NAMESPACE_STD93 94#endif // _LIBCPP___CXX03___ITERATOR_ISTREAMBUF_ITERATOR_H95