1 /* An iterator wrapper that yields pointers instead of references.
2 Copyright (C) 2021-2022 Free Software Foundation, Inc.
4 This file is part of GDB.
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.
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.
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/>. */
19 #ifndef GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H
20 #define GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H
22 /* Wrap an iterator that yields references to objects so that it yields
23 pointers to objects instead.
25 This is useful for example to bridge the gap between iterators on intrusive
26 lists, which yield references, and the rest of GDB, which for legacy reasons
27 expects to iterate on pointers. */
29 template <typename IteratorType
>
30 struct reference_to_pointer_iterator
32 using self_type
= reference_to_pointer_iterator
;
33 using value_type
= typename
IteratorType::value_type
*;
34 using reference
= typename
IteratorType::value_type
*&;
35 using pointer
= typename
IteratorType::value_type
**;
36 using iterator_category
= typename
IteratorType::iterator_category
;
37 using difference_type
= typename
IteratorType::difference_type
;
39 /* Construct a reference_to_pointer_iterator, passing args to the underyling
41 template <typename
... Args
>
42 reference_to_pointer_iterator (Args
&&...args
)
43 : m_it (std::forward
<Args
> (args
)...)
46 /* Create a past-the-end iterator.
48 Assumes that default-constructing an underlying iterator creates a
49 past-the-end iterator. */
50 reference_to_pointer_iterator ()
53 /* Need these as the variadic constructor would be a better match
55 reference_to_pointer_iterator (reference_to_pointer_iterator
&) = default;
56 reference_to_pointer_iterator (const reference_to_pointer_iterator
&) = default;
57 reference_to_pointer_iterator (reference_to_pointer_iterator
&&) = default;
59 reference_to_pointer_iterator
&operator= (const reference_to_pointer_iterator
&) = default;
60 reference_to_pointer_iterator
&operator= (reference_to_pointer_iterator
&&) = default;
62 value_type
operator* () const
65 self_type
&operator++ ()
71 bool operator== (const self_type
&other
) const
72 { return m_it
== other
.m_it
; }
74 bool operator!= (const self_type
&other
) const
75 { return m_it
!= other
.m_it
; }
78 /* The underlying iterator. */
82 #endif /* GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H */