| 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_ISTREAM_ITERATOR_H |
| 11 | #define _LIBCPP___ITERATOR_ISTREAM_ITERATOR_H |
| 12 | |
| 13 | #include <__config> |
| 14 | #include <__cstddef/ptrdiff_t.h> |
| 15 | #include <__fwd/istream.h> |
| 16 | #include <__fwd/string.h> |
| 17 | #include <__iterator/default_sentinel.h> |
| 18 | #include <__iterator/iterator.h> |
| 19 | #include <__iterator/iterator_traits.h> |
| 20 | #include <__memory/addressof.h> |
| 21 | |
| 22 | #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER) |
| 23 | # pragma GCC system_header |
| 24 | #endif |
| 25 | |
| 26 | _LIBCPP_BEGIN_NAMESPACE_STD |
| 27 | |
| 28 | template <class _Tp, class _CharT = char, class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t> |
| 29 | class istream_iterator |
| 30 | : public __iterator_base<istream_iterator<_Tp, _CharT, _Traits, _Distance>, |
| 31 | input_iterator_tag, |
| 32 | _Tp, |
| 33 | _Distance, |
| 34 | const _Tp*, |
| 35 | const _Tp&> { |
| 36 | public: |
| 37 | typedef input_iterator_tag iterator_category; |
| 38 | typedef _Tp value_type; |
| 39 | typedef _Distance difference_type; |
| 40 | typedef const _Tp* pointer; |
| 41 | typedef const _Tp& reference; |
| 42 | typedef _CharT char_type; |
| 43 | typedef _Traits traits_type; |
| 44 | typedef basic_istream<_CharT, _Traits> istream_type; |
| 45 | |
| 46 | private: |
| 47 | istream_type* __in_stream_; |
| 48 | _Tp __value_; |
| 49 | |
| 50 | public: |
| 51 | _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {} |
| 52 | #if _LIBCPP_STD_VER >= 20 |
| 53 | _LIBCPP_HIDE_FROM_ABI constexpr istream_iterator(default_sentinel_t) : istream_iterator() {} |
| 54 | #endif // _LIBCPP_STD_VER >= 20 |
| 55 | _LIBCPP_HIDE_FROM_ABI istream_iterator(istream_type& __s) : __in_stream_(std::addressof(__s)) { |
| 56 | if (!(*__in_stream_ >> __value_)) |
| 57 | __in_stream_ = nullptr; |
| 58 | } |
| 59 | |
| 60 | // LWG3600 Changed the wording of the copy constructor. In libc++ this constructor |
| 61 | // can still be trivial after this change. |
| 62 | |
| 63 | _LIBCPP_HIDE_FROM_ABI const _Tp& operator*() const { return __value_; } |
| 64 | _LIBCPP_HIDE_FROM_ABI const _Tp* operator->() const { return std::addressof((operator*())); } |
| 65 | _LIBCPP_HIDE_FROM_ABI istream_iterator& operator++() { |
| 66 | if (!(*__in_stream_ >> __value_)) |
| 67 | __in_stream_ = nullptr; |
| 68 | return *this; |
| 69 | } |
| 70 | _LIBCPP_HIDE_FROM_ABI istream_iterator operator++(int) { |
| 71 | istream_iterator __t(*this); |
| 72 | ++(*this); |
| 73 | return __t; |
| 74 | } |
| 75 | |
| 76 | template <class _Up, class _CharU, class _TraitsU, class _DistanceU> |
| 77 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x, |
| 78 | const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y); |
| 79 | |
| 80 | #if _LIBCPP_STD_VER >= 20 |
| 81 | friend _LIBCPP_HIDE_FROM_ABI bool operator==(const istream_iterator& __i, default_sentinel_t) { |
| 82 | return __i.__in_stream_ == nullptr; |
| 83 | } |
| 84 | #endif // _LIBCPP_STD_VER >= 20 |
| 85 | }; |
| 86 | |
| 87 | template <class _Tp, class _CharT, class _Traits, class _Distance> |
| 88 | inline _LIBCPP_HIDE_FROM_ABI bool operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x, |
| 89 | const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y) { |
| 90 | return __x.__in_stream_ == __y.__in_stream_; |
| 91 | } |
| 92 | |
| 93 | #if _LIBCPP_STD_VER <= 17 |
| 94 | template <class _Tp, class _CharT, class _Traits, class _Distance> |
| 95 | inline _LIBCPP_HIDE_FROM_ABI bool operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x, |
| 96 | const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y) { |
| 97 | return !(__x == __y); |
| 98 | } |
| 99 | #endif // _LIBCPP_STD_VER <= 17 |
| 100 | |
| 101 | _LIBCPP_END_NAMESPACE_STD |
| 102 | |
| 103 | #endif // _LIBCPP___ITERATOR_ISTREAM_ITERATOR_H |
| 104 | |