Remove path name from test case
[binutils-gdb.git] / gdbsupport / filtered-iterator.h
1 /* A forward filtered iterator for GDB, the GNU debugger.
2 Copyright (C) 2018-2023 Free Software Foundation, Inc.
3
4 This file is part of GDB.
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <http://www.gnu.org/licenses/>. */
18
19 #ifndef COMMON_FILTERED_ITERATOR_H
20 #define COMMON_FILTERED_ITERATOR_H
21
22 #include "gdbsupport/invoke-result.h"
23
24 /* A filtered iterator. This wraps BaseIterator and automatically
25 skips elements that FilterFunc filters out. Requires that
26 default-constructing a BaseIterator creates a valid one-past-end
27 iterator. */
28
29 template<typename BaseIterator, typename FilterFunc>
30 class filtered_iterator
31 {
32 public:
33 typedef filtered_iterator self_type;
34 typedef typename BaseIterator::value_type value_type;
35 typedef typename BaseIterator::reference reference;
36 typedef typename BaseIterator::pointer pointer;
37 typedef typename BaseIterator::iterator_category iterator_category;
38 typedef typename BaseIterator::difference_type difference_type;
39
40 /* Construct by forwarding all arguments to the underlying
41 iterator. */
42 template<typename... Args>
43 explicit filtered_iterator (Args &&...args)
44 : m_it (std::forward<Args> (args)...)
45 { skip_filtered (); }
46
47 /* Create a one-past-end iterator. */
48 filtered_iterator () = default;
49
50 /* Need these as the variadic constructor would be a better match
51 otherwise. */
52 filtered_iterator (filtered_iterator &) = default;
53 filtered_iterator (const filtered_iterator &) = default;
54 filtered_iterator (filtered_iterator &&) = default;
55 filtered_iterator (const filtered_iterator &&other)
56 : filtered_iterator (static_cast<const filtered_iterator &> (other))
57 {}
58
59 typename gdb::invoke_result<decltype(&BaseIterator::operator*),
60 BaseIterator>::type
61 operator* () const
62 { return *m_it; }
63
64 self_type &operator++ ()
65 {
66 ++m_it;
67 skip_filtered ();
68 return *this;
69 }
70
71 bool operator== (const self_type &other) const
72 { return m_it == other.m_it; }
73
74 bool operator!= (const self_type &other) const
75 { return m_it != other.m_it; }
76
77 private:
78
79 void skip_filtered ()
80 {
81 for (; m_it != m_end; ++m_it)
82 if (m_filter (*m_it))
83 break;
84 }
85
86 private:
87 FilterFunc m_filter {};
88 BaseIterator m_it {};
89 BaseIterator m_end {};
90 };
91
92 #endif /* COMMON_FILTERED_ITERATOR_H */