xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/iterator-range.h (revision 32d1c65c71fbdb65a012e8392a62a757dd6853e9)
1 /* A range adapter that wraps begin / end iterators.
2    Copyright (C) 2021-2024 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 GDBSUPPORT_ITERATOR_RANGE_H
20 #define GDBSUPPORT_ITERATOR_RANGE_H
21 
22 /* A wrapper that allows using ranged for-loops on a range described by two
23    iterators.  */
24 
25 template <typename IteratorType>
26 struct iterator_range
27 {
28   using iterator = IteratorType;
29 
30   /* Create an iterator_range using BEGIN as the begin iterator.
31 
32      Assume that the end iterator can be default-constructed.  */
33   template <typename... Args>
34   iterator_range (Args &&...args)
35     : m_begin (std::forward<Args> (args)...)
36   {}
37 
38   /* Create an iterator range using explicit BEGIN and END iterators.  */
39   template <typename... Args>
40   iterator_range (IteratorType begin, IteratorType end)
41     : m_begin (std::move (begin)), m_end (std::move (end))
42   {}
43 
44   /* Need these as the variadic constructor would be a better match
45      otherwise.  */
46   iterator_range (iterator_range &) = default;
47   iterator_range (const iterator_range &) = default;
48   iterator_range (iterator_range &&) = default;
49 
50   IteratorType begin () const
51   { return m_begin; }
52 
53   IteratorType end () const
54   { return m_end; }
55 
56   /* The number of items in this iterator_range.  */
57   std::size_t size () const
58   { return std::distance (m_begin, m_end); }
59 
60 private:
61   IteratorType m_begin, m_end;
62 };
63 
64 #endif /* GDBSUPPORT_ITERATOR_RANGE_H */
65