1 1.1 christos /* A "next" iterator for GDB, the GNU debugger. 2 1.1 christos Copyright (C) 2019-2020 Free Software Foundation, Inc. 3 1.1 christos 4 1.1 christos This file is part of GDB. 5 1.1 christos 6 1.1 christos This program is free software; you can redistribute it and/or modify 7 1.1 christos it under the terms of the GNU General Public License as published by 8 1.1 christos the Free Software Foundation; either version 3 of the License, or 9 1.1 christos (at your option) any later version. 10 1.1 christos 11 1.1 christos This program is distributed in the hope that it will be useful, 12 1.1 christos but WITHOUT ANY WARRANTY; without even the implied warranty of 13 1.1 christos MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 14 1.1 christos GNU General Public License for more details. 15 1.1 christos 16 1.1 christos You should have received a copy of the GNU General Public License 17 1.1 christos along with this program. If not, see <http://www.gnu.org/licenses/>. */ 18 1.1 christos 19 1.1 christos #ifndef COMMON_NEXT_ITERATOR_H 20 1.1 christos #define COMMON_NEXT_ITERATOR_H 21 1.1 christos 22 1.1 christos /* An iterator that uses the 'next' field of a type to iterate. This 23 1.1 christos can be used with various GDB types that are stored as linked 24 1.1 christos lists. */ 25 1.1 christos 26 1.1 christos template<typename T> 27 1.1 christos struct next_iterator 28 1.1 christos { 29 1.1 christos typedef next_iterator self_type; 30 1.1 christos typedef T *value_type; 31 1.1 christos typedef T *&reference; 32 1.1 christos typedef T **pointer; 33 1.1 christos typedef std::forward_iterator_tag iterator_category; 34 1.1 christos typedef int difference_type; 35 1.1 christos 36 1.1 christos explicit next_iterator (T *item) 37 1.1 christos : m_item (item) 38 1.1 christos { 39 1.1 christos } 40 1.1 christos 41 1.1 christos /* Create a one-past-the-end iterator. */ 42 1.1 christos next_iterator () 43 1.1 christos : m_item (nullptr) 44 1.1 christos { 45 1.1 christos } 46 1.1 christos 47 1.1 christos value_type operator* () const 48 1.1 christos { 49 1.1 christos return m_item; 50 1.1 christos } 51 1.1 christos 52 1.1 christos bool operator== (const self_type &other) const 53 1.1 christos { 54 1.1 christos return m_item == other.m_item; 55 1.1 christos } 56 1.1 christos 57 1.1 christos bool operator!= (const self_type &other) const 58 1.1 christos { 59 1.1 christos return m_item != other.m_item; 60 1.1 christos } 61 1.1 christos 62 1.1 christos self_type &operator++ () 63 1.1 christos { 64 1.1 christos m_item = m_item->next; 65 1.1 christos return *this; 66 1.1 christos } 67 1.1 christos 68 1.1 christos private: 69 1.1 christos 70 1.1 christos T *m_item; 71 1.1 christos }; 72 1.1 christos 73 1.1 christos /* A range adapter that allows iterating over a linked list. */ 74 1.1 christos 75 1.1 christos template<typename T, typename Iterator = next_iterator<T>> 76 1.1 christos class next_adapter 77 1.1 christos { 78 1.1 christos public: 79 1.1 christos 80 1.1 christos explicit next_adapter (T *item) 81 1.1 christos : m_item (item) 82 1.1 christos { 83 1.1 christos } 84 1.1 christos 85 1.1 christos using iterator = Iterator; 86 1.1 christos 87 1.1 christos iterator begin () const 88 1.1 christos { 89 1.1 christos return iterator (m_item); 90 1.1 christos } 91 1.1 christos 92 1.1 christos iterator end () const 93 1.1 christos { 94 1.1 christos return iterator (); 95 1.1 christos } 96 1.1 christos 97 1.1 christos private: 98 1.1 christos 99 1.1 christos T *m_item; 100 1.1 christos }; 101 1.1 christos 102 1.1 christos #endif /* COMMON_NEXT_ITERATOR_H */ 103