1/* An iterator wrapper that yields pointers instead of references. 2 Copyright (C) 2021-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 GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H 20#define GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H 21 22/* Wrap an iterator that yields references to objects so that it yields 23 pointers to objects instead. 24 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. */ 28 29template <typename IteratorType> 30struct reference_to_pointer_iterator 31{ 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; 38 39 /* Construct a reference_to_pointer_iterator, passing args to the underyling 40 iterator. */ 41 template <typename... Args> 42 reference_to_pointer_iterator (Args &&...args) 43 : m_it (std::forward<Args> (args)...) 44 {} 45 46 /* Create a past-the-end iterator. 47 48 Assumes that default-constructing an underlying iterator creates a 49 past-the-end iterator. */ 50 reference_to_pointer_iterator () 51 {} 52 53 /* Need these as the variadic constructor would be a better match 54 otherwise. */ 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; 58 59 reference_to_pointer_iterator &operator= (const reference_to_pointer_iterator &) = default; 60 reference_to_pointer_iterator &operator= (reference_to_pointer_iterator &&) = default; 61 62 value_type operator* () const 63 { return &*m_it; } 64 65 self_type &operator++ () 66 { 67 ++m_it; 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 77private: 78 /* The underlying iterator. */ 79 IteratorType m_it; 80}; 81 82#endif /* GDBSUPPORT_REFERENCE_TO_POINTER_ITERATOR_H */ 83