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 <__fwd/istream.h>
15 #include <__fwd/string.h>
16 #include <__iterator/default_sentinel.h>
17 #include <__iterator/iterator.h>
18 #include <__iterator/iterator_traits.h>
19 #include <__memory/addressof.h>
20 #include <cstddef>
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 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
29 template <class _Tp, class _CharT = char,
30           class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
31 class _LIBCPP_TEMPLATE_VIS istream_iterator
32 #if _LIBCPP_STD_VER <= 14 || !defined(_LIBCPP_ABI_NO_ITERATOR_BASES)
33     : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
34 #endif
35 {
36 _LIBCPP_SUPPRESS_DEPRECATED_POP
37 public:
38     typedef input_iterator_tag iterator_category;
39     typedef _Tp value_type;
40     typedef _Distance difference_type;
41     typedef const _Tp* pointer;
42     typedef const _Tp& reference;
43     typedef _CharT char_type;
44     typedef _Traits traits_type;
45     typedef basic_istream<_CharT,_Traits> istream_type;
46 private:
47     istream_type* __in_stream_;
48     _Tp __value_;
49 public:
50     _LIBCPP_HIDE_FROM_ABI _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
51 #if _LIBCPP_STD_VER >= 20
52     _LIBCPP_HIDE_FROM_ABI constexpr istream_iterator(default_sentinel_t) : istream_iterator() {}
53 #endif // _LIBCPP_STD_VER >= 20
54     _LIBCPP_HIDE_FROM_ABI istream_iterator(istream_type& __s) : __in_stream_(std::addressof(__s))
55         {
56             if (!(*__in_stream_ >> __value_))
57                 __in_stream_ = nullptr;
58         }
59 
60     _LIBCPP_HIDE_FROM_ABI const _Tp& operator*() const {return __value_;}
61     _LIBCPP_HIDE_FROM_ABI const _Tp* operator->() const {return std::addressof((operator*()));}
62     _LIBCPP_HIDE_FROM_ABI istream_iterator& operator++()
63         {
64             if (!(*__in_stream_ >> __value_))
65                 __in_stream_ = nullptr;
66             return *this;
67         }
68     _LIBCPP_HIDE_FROM_ABI istream_iterator  operator++(int)
69         {istream_iterator __t(*this); ++(*this); return __t;}
70 
71     template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
72     friend _LIBCPP_HIDE_FROM_ABI
73     bool
74     operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
75                const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
76 
77 #if _LIBCPP_STD_VER >= 20
78     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const istream_iterator& __i, default_sentinel_t) {
79       return __i.__in_stream_ == nullptr;
80     }
81 #endif // _LIBCPP_STD_VER >= 20
82 };
83 
84 template <class _Tp, class _CharT, class _Traits, class _Distance>
85 inline _LIBCPP_HIDE_FROM_ABI
86 bool
87 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
88            const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
89 {
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
96 bool
97 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
98            const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
99 {
100     return !(__x == __y);
101 }
102 #endif // _LIBCPP_STD_VER <= 17
103 
104 _LIBCPP_END_NAMESPACE_STD
105 
106 #endif // _LIBCPP___ITERATOR_ISTREAM_ITERATOR_H
107