Home | History | Annotate | Line # | Download | only in gdbsupport
next-iterator.h revision 1.1.1.3
      1 /* A "next" iterator for GDB, the GNU debugger.
      2    Copyright (C) 2019-2024 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 COMMON_NEXT_ITERATOR_H
     20 #define COMMON_NEXT_ITERATOR_H
     21 
     22 #include "gdbsupport/iterator-range.h"
     23 
     24 /* An iterator that uses the 'next' field of a type to iterate.  This
     25    can be used with various GDB types that are stored as linked
     26    lists.  */
     27 
     28 template<typename T>
     29 struct next_iterator
     30 {
     31   typedef next_iterator self_type;
     32   typedef T *value_type;
     33   typedef T *&reference;
     34   typedef T **pointer;
     35   typedef std::forward_iterator_tag iterator_category;
     36   typedef int difference_type;
     37 
     38   explicit next_iterator (T *item)
     39     : m_item (item)
     40   {
     41   }
     42 
     43   /* Create a one-past-the-end iterator.  */
     44   next_iterator ()
     45     : m_item (nullptr)
     46   {
     47   }
     48 
     49   value_type operator* () const
     50   {
     51     return m_item;
     52   }
     53 
     54   bool operator== (const self_type &other) const
     55   {
     56     return m_item == other.m_item;
     57   }
     58 
     59   bool operator!= (const self_type &other) const
     60   {
     61     return m_item != other.m_item;
     62   }
     63 
     64   self_type &operator++ ()
     65   {
     66     m_item = m_item->next;
     67     return *this;
     68   }
     69 
     70 private:
     71 
     72   T *m_item;
     73 };
     74 
     75 /* A convenience wrapper to make a range type around a next_iterator.  */
     76 
     77 template <typename T>
     78 using next_range = iterator_range<next_iterator<T>>;
     79 
     80 #endif /* COMMON_NEXT_ITERATOR_H */
     81