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 <__iterator/default_sentinel.h>
15 #include <__iterator/iterator.h>
16 #include <__iterator/iterator_traits.h>
17 #include <__memory/addressof.h>
18 #include <cstddef>
19 #include <iosfwd> // for forward declarations of char_traits and basic_istream
20 
21 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
22 #  pragma GCC system_header
23 #endif
24 
25 _LIBCPP_BEGIN_NAMESPACE_STD
26 
27 _LIBCPP_SUPPRESS_DEPRECATED_PUSH
28 template <class _Tp, class _CharT = char,
29           class _Traits = char_traits<_CharT>, class _Distance = ptrdiff_t>
30 class _LIBCPP_TEMPLATE_VIS istream_iterator
31 #if _LIBCPP_STD_VER <= 14 || !defined(_LIBCPP_ABI_NO_ITERATOR_BASES)
32     : public iterator<input_iterator_tag, _Tp, _Distance, const _Tp*, const _Tp&>
33 #endif
34 {
35 _LIBCPP_SUPPRESS_DEPRECATED_POP
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 private:
46     istream_type* __in_stream_;
47     _Tp __value_;
48 public:
49     _LIBCPP_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
50 #if _LIBCPP_STD_VER >= 20
51     _LIBCPP_HIDE_FROM_ABI constexpr istream_iterator(default_sentinel_t) : istream_iterator() {}
52 #endif // _LIBCPP_STD_VER >= 20
53     _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
54         {
55             if (!(*__in_stream_ >> __value_))
56                 __in_stream_ = nullptr;
57         }
58 
59     _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
60     _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
61     _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
62         {
63             if (!(*__in_stream_ >> __value_))
64                 __in_stream_ = nullptr;
65             return *this;
66         }
67     _LIBCPP_INLINE_VISIBILITY istream_iterator  operator++(int)
68         {istream_iterator __t(*this); ++(*this); return __t;}
69 
70     template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
71     friend _LIBCPP_INLINE_VISIBILITY
72     bool
73     operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
74                const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
75 
76 #if _LIBCPP_STD_VER >= 20
77     friend _LIBCPP_HIDE_FROM_ABI bool operator==(const istream_iterator& __i, default_sentinel_t) {
78       return __i.__in_stream_ == nullptr;
79     }
80 #endif // _LIBCPP_STD_VER >= 20
81 };
82 
83 template <class _Tp, class _CharT, class _Traits, class _Distance>
84 inline _LIBCPP_INLINE_VISIBILITY
85 bool
86 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
87            const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
88 {
89     return __x.__in_stream_ == __y.__in_stream_;
90 }
91 
92 #if _LIBCPP_STD_VER <= 17
93 template <class _Tp, class _CharT, class _Traits, class _Distance>
94 inline _LIBCPP_INLINE_VISIBILITY
95 bool
96 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
97            const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
98 {
99     return !(__x == __y);
100 }
101 #endif // _LIBCPP_STD_VER <= 17
102 
103 _LIBCPP_END_NAMESPACE_STD
104 
105 #endif // _LIBCPP___ITERATOR_ISTREAM_ITERATOR_H
106