xref: /netbsd-src/external/gpl3/gdb/dist/gdbsupport/next-iterator.h (revision 5ba1f45f2a09259cc846f20c7c5501604d633c90)
18dffb485Schristos /* A "next" iterator for GDB, the GNU debugger.
2*5ba1f45fSchristos    Copyright (C) 2019-2024 Free Software Foundation, Inc.
38dffb485Schristos 
48dffb485Schristos    This file is part of GDB.
58dffb485Schristos 
68dffb485Schristos    This program is free software; you can redistribute it and/or modify
78dffb485Schristos    it under the terms of the GNU General Public License as published by
88dffb485Schristos    the Free Software Foundation; either version 3 of the License, or
98dffb485Schristos    (at your option) any later version.
108dffb485Schristos 
118dffb485Schristos    This program is distributed in the hope that it will be useful,
128dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
138dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
148dffb485Schristos    GNU General Public License for more details.
158dffb485Schristos 
168dffb485Schristos    You should have received a copy of the GNU General Public License
178dffb485Schristos    along with this program.  If not, see <http://www.gnu.org/licenses/>.  */
188dffb485Schristos 
198dffb485Schristos #ifndef COMMON_NEXT_ITERATOR_H
208dffb485Schristos #define COMMON_NEXT_ITERATOR_H
218dffb485Schristos 
224b169a6bSchristos #include "gdbsupport/iterator-range.h"
234b169a6bSchristos 
248dffb485Schristos /* An iterator that uses the 'next' field of a type to iterate.  This
258dffb485Schristos    can be used with various GDB types that are stored as linked
268dffb485Schristos    lists.  */
278dffb485Schristos 
288dffb485Schristos template<typename T>
298dffb485Schristos struct next_iterator
308dffb485Schristos {
318dffb485Schristos   typedef next_iterator self_type;
328dffb485Schristos   typedef T *value_type;
338dffb485Schristos   typedef T *&reference;
348dffb485Schristos   typedef T **pointer;
358dffb485Schristos   typedef std::forward_iterator_tag iterator_category;
368dffb485Schristos   typedef int difference_type;
378dffb485Schristos 
388dffb485Schristos   explicit next_iterator (T *item)
398dffb485Schristos     : m_item (item)
408dffb485Schristos   {
418dffb485Schristos   }
428dffb485Schristos 
438dffb485Schristos   /* Create a one-past-the-end iterator.  */
448dffb485Schristos   next_iterator ()
458dffb485Schristos     : m_item (nullptr)
468dffb485Schristos   {
478dffb485Schristos   }
488dffb485Schristos 
498dffb485Schristos   value_type operator* () const
508dffb485Schristos   {
518dffb485Schristos     return m_item;
528dffb485Schristos   }
538dffb485Schristos 
548dffb485Schristos   bool operator== (const self_type &other) const
558dffb485Schristos   {
568dffb485Schristos     return m_item == other.m_item;
578dffb485Schristos   }
588dffb485Schristos 
598dffb485Schristos   bool operator!= (const self_type &other) const
608dffb485Schristos   {
618dffb485Schristos     return m_item != other.m_item;
628dffb485Schristos   }
638dffb485Schristos 
648dffb485Schristos   self_type &operator++ ()
658dffb485Schristos   {
668dffb485Schristos     m_item = m_item->next;
678dffb485Schristos     return *this;
688dffb485Schristos   }
698dffb485Schristos 
708dffb485Schristos private:
718dffb485Schristos 
728dffb485Schristos   T *m_item;
738dffb485Schristos };
748dffb485Schristos 
754b169a6bSchristos /* A convenience wrapper to make a range type around a next_iterator.  */
768dffb485Schristos 
774b169a6bSchristos template <typename T>
784b169a6bSchristos using next_range = iterator_range<next_iterator<T>>;
798dffb485Schristos 
808dffb485Schristos #endif /* COMMON_NEXT_ITERATOR_H */
81