xref: /netbsd-src/external/gpl3/gdb.old/dist/gdbsupport/reference-to-pointer-iterator.h (revision 6881a4007f077b54e5f51159c52b9b25f57deb0d)
1*6881a400Schristos /* An iterator wrapper that yields pointers instead of references.
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_REFERENCE_TO_POINTER_ITERATOR_H
20*6881a400Schristos #define GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H
21*6881a400Schristos 
22*6881a400Schristos /* Wrap an iterator that yields references to objects so that it yields
23*6881a400Schristos    pointers to objects instead.
24*6881a400Schristos 
25*6881a400Schristos    This is useful for example to bridge the gap between iterators on intrusive
26*6881a400Schristos    lists, which yield references, and the rest of GDB, which for legacy reasons
27*6881a400Schristos    expects to iterate on pointers.  */
28*6881a400Schristos 
29*6881a400Schristos template <typename IteratorType>
30*6881a400Schristos struct reference_to_pointer_iterator
31*6881a400Schristos {
32*6881a400Schristos   using self_type = reference_to_pointer_iterator;
33*6881a400Schristos   using value_type = typename IteratorType::value_type *;
34*6881a400Schristos   using reference = typename IteratorType::value_type *&;
35*6881a400Schristos   using pointer = typename IteratorType::value_type **;
36*6881a400Schristos   using iterator_category = typename IteratorType::iterator_category;
37*6881a400Schristos   using difference_type = typename IteratorType::difference_type;
38*6881a400Schristos 
39*6881a400Schristos   /* Construct a reference_to_pointer_iterator, passing args to the underyling
40*6881a400Schristos      iterator.  */
41*6881a400Schristos   template <typename... Args>
42*6881a400Schristos   reference_to_pointer_iterator (Args &&...args)
43*6881a400Schristos     : m_it (std::forward<Args> (args)...)
44*6881a400Schristos   {}
45*6881a400Schristos 
46*6881a400Schristos   /* Create a past-the-end iterator.
47*6881a400Schristos 
48*6881a400Schristos      Assumes that default-constructing an underlying iterator creates a
49*6881a400Schristos      past-the-end iterator.  */
50*6881a400Schristos   reference_to_pointer_iterator ()
51*6881a400Schristos   {}
52*6881a400Schristos 
53*6881a400Schristos   /* Need these as the variadic constructor would be a better match
54*6881a400Schristos      otherwise.  */
55*6881a400Schristos   reference_to_pointer_iterator (reference_to_pointer_iterator &) = default;
56*6881a400Schristos   reference_to_pointer_iterator (const reference_to_pointer_iterator &) = default;
57*6881a400Schristos   reference_to_pointer_iterator (reference_to_pointer_iterator &&) = default;
58*6881a400Schristos 
59*6881a400Schristos   reference_to_pointer_iterator &operator= (const reference_to_pointer_iterator &) = default;
60*6881a400Schristos   reference_to_pointer_iterator &operator= (reference_to_pointer_iterator &&) = default;
61*6881a400Schristos 
62*6881a400Schristos   value_type operator* () const
63*6881a400Schristos   { return &*m_it; }
64*6881a400Schristos 
65*6881a400Schristos   self_type &operator++ ()
66*6881a400Schristos   {
67*6881a400Schristos     ++m_it;
68*6881a400Schristos     return *this;
69*6881a400Schristos   }
70*6881a400Schristos 
71*6881a400Schristos   bool operator== (const self_type &other) const
72*6881a400Schristos   { return m_it == other.m_it; }
73*6881a400Schristos 
74*6881a400Schristos   bool operator!= (const self_type &other) const
75*6881a400Schristos   { return m_it != other.m_it; }
76*6881a400Schristos 
77*6881a400Schristos private:
78*6881a400Schristos   /* The underlying iterator.  */
79*6881a400Schristos   IteratorType m_it;
80*6881a400Schristos };
81*6881a400Schristos 
82*6881a400Schristos #endif /* GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H */
83