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