xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/iterator-range.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* A range adapter that wraps begin / end iterators.
2*6881a400Schristos    Copyright (C) 2021-2023 Free Software Foundation, Inc.
3*6881a400Schristos 
4*6881a400Schristos    This file is part of GDB.
5*6881a400Schristos 
6*6881a400Schristos    This program is free software; you can redistribute it and/or modify
7*6881a400Schristos    it under the terms of the GNU General Public License as published by
8*6881a400Schristos    the Free Software Foundation; either version 3 of the License, or
9*6881a400Schristos    (at your option) any later version.
10*6881a400Schristos 
11*6881a400Schristos    This program is distributed in the hope that it will be useful,
12*6881a400Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
13*6881a400Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14*6881a400Schristos    GNU General Public License for more details.
15*6881a400Schristos 
16*6881a400Schristos    You should have received a copy of the GNU General Public License
17*6881a400Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
18*6881a400Schristos 
19*6881a400Schristos #ifndef GDBSUPPORT_ITERATOR_RANGE_H
20*6881a400Schristos #define GDBSUPPORT_ITERATOR_RANGE_H
21*6881a400Schristos 
22*6881a400Schristos /* A wrapper that allows using ranged for-loops on a range described by two
23*6881a400Schristos    iterators.  */
24*6881a400Schristos 
25*6881a400Schristos template <typename IteratorType>
26*6881a400Schristos struct iterator_range
27*6881a400Schristos {
28*6881a400Schristos   using iterator = IteratorType;
29*6881a400Schristos 
30*6881a400Schristos   /* Create an iterator_range using BEGIN as the begin iterator.
31*6881a400Schristos 
32*6881a400Schristos      Assume that the end iterator can be default-constructed.  */
33*6881a400Schristos   template <typename... Args>
34*6881a400Schristos   iterator_range (Args &&...args)
35*6881a400Schristos     : m_begin (std::forward<Args> (args)...)
36*6881a400Schristos   {}
37*6881a400Schristos 
38*6881a400Schristos   /* Create an iterator range using explicit BEGIN and END iterators.  */
39*6881a400Schristos   template <typename... Args>
40*6881a400Schristos   iterator_range (IteratorType begin, IteratorType end)
41*6881a400Schristos     : m_begin (std::move (begin)), m_end (std::move (end))
42*6881a400Schristos   {}
43*6881a400Schristos 
44*6881a400Schristos   /* Need these as the variadic constructor would be a better match
45*6881a400Schristos      otherwise.  */
46*6881a400Schristos   iterator_range (iterator_range &) = default;
47*6881a400Schristos   iterator_range (const iterator_range &) = default;
48*6881a400Schristos   iterator_range (iterator_range &&) = default;
49*6881a400Schristos 
50*6881a400Schristos   IteratorType begin () const
51*6881a400Schristos   { return m_begin; }
52*6881a400Schristos 
53*6881a400Schristos   IteratorType end () const
54*6881a400Schristos   { return m_end; }
55*6881a400Schristos 
56*6881a400Schristos   /* The number of items in this iterator_range.  */
57*6881a400Schristos   std::size_t size () const
58*6881a400Schristos   { return std::distance (m_begin, m_end); }
59*6881a400Schristos 
60*6881a400Schristos private:
61*6881a400Schristos   IteratorType m_begin, m_end;
62*6881a400Schristos };
63*6881a400Schristos 
64*6881a400Schristos #endif /* GDBSUPPORT_ITERATOR_RANGE_H */
65