xref: /openbsd-src/gnu/llvm/libcxx/include/__iterator/istream_iterator.h (revision 1ad61ae0a79a724d2d3ec69e69c8e1d1ff6b53a0)
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/iterator.h>
15 #include <__iterator/iterator_traits.h>
16 #include <__memory/addressof.h>
17 #include <iosfwd> // for forward declarations of char_traits and basic_istream
18 
19 #if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
20 #pragma GCC system_header
21 #endif
22 
23 _LIBCPP_PUSH_MACROS
24 #include <__undef_macros>
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_INLINE_VISIBILITY _LIBCPP_CONSTEXPR istream_iterator() : __in_stream_(nullptr), __value_() {}
51     _LIBCPP_INLINE_VISIBILITY istream_iterator(istream_type& __s) : __in_stream_(_VSTD::addressof(__s))
52         {
53             if (!(*__in_stream_ >> __value_))
54                 __in_stream_ = nullptr;
55         }
56 
57     _LIBCPP_INLINE_VISIBILITY const _Tp& operator*() const {return __value_;}
58     _LIBCPP_INLINE_VISIBILITY const _Tp* operator->() const {return _VSTD::addressof((operator*()));}
59     _LIBCPP_INLINE_VISIBILITY istream_iterator& operator++()
60         {
61             if (!(*__in_stream_ >> __value_))
62                 __in_stream_ = nullptr;
63             return *this;
64         }
65     _LIBCPP_INLINE_VISIBILITY istream_iterator  operator++(int)
66         {istream_iterator __t(*this); ++(*this); return __t;}
67 
68     template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
69     friend _LIBCPP_INLINE_VISIBILITY
70     bool
71     operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
72                const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
73 
74     template <class _Up, class _CharU, class _TraitsU, class _DistanceU>
75     friend _LIBCPP_INLINE_VISIBILITY
76     bool
77     operator==(const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __x,
78                const istream_iterator<_Up, _CharU, _TraitsU, _DistanceU>& __y);
79 };
80 
81 template <class _Tp, class _CharT, class _Traits, class _Distance>
82 inline _LIBCPP_INLINE_VISIBILITY
83 bool
84 operator==(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
85            const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
86 {
87     return __x.__in_stream_ == __y.__in_stream_;
88 }
89 
90 template <class _Tp, class _CharT, class _Traits, class _Distance>
91 inline _LIBCPP_INLINE_VISIBILITY
92 bool
93 operator!=(const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __x,
94            const istream_iterator<_Tp, _CharT, _Traits, _Distance>& __y)
95 {
96     return !(__x == __y);
97 }
98 
99 _LIBCPP_END_NAMESPACE_STD
100 
101 _LIBCPP_POP_MACROS
102 
103 #endif // _LIBCPP___ITERATOR_ISTREAM_ITERATOR_H
104