Mirror of the gdb-patches mailing list
 help / color / mirror / Atom feed
From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 3/7] [gdbsupport] Factor out base_next_iterator
Date: Thu, 23 Apr 2026 08:35:26 +0200	[thread overview]
Message-ID: <20260423063530.1074175-4-tdevries@suse.de> (raw)
In-Reply-To: <20260423063530.1074175-1-tdevries@suse.de>

Template struct next_iterator<T> allows iterating over the "next" field of T.

I decided to generalize this to be able to use any field.

A first thought was to use macros, but nowadays we try to do things more in
native c++, so I didn't explore that further.

Instead, this patch factors out base_next_iterator, which contains all parts
not specific to "next", in other words, everything except the operator++.

Finally, I explored using a pointer-to-member template argument T::next, but
that meant that using an incomplete T was no longer allowed, so it's not
a drop-in replacement.  I also didn't explore this option further, and made
a note of it in the base_next_iterator comment.
---
 gdbsupport/next-iterator.h | 59 ++++++++++++++++++++++++++++++--------
 1 file changed, 47 insertions(+), 12 deletions(-)

diff --git a/gdbsupport/next-iterator.h b/gdbsupport/next-iterator.h
index 0c90428d349..1dee941a252 100644
--- a/gdbsupport/next-iterator.h
+++ b/gdbsupport/next-iterator.h
@@ -21,27 +21,43 @@
 
 #include "gdbsupport/iterator-range.h"
 
-/* An iterator that uses the 'next' field of a type to iterate.  This
-   can be used with various GDB types that are stored as linked
-   lists.  */
+/* An iterator base class for iterating over a field of a type.  In order to
+   form a functioning iterator, classes inheriting this should define an
+   operator++, which determines the actual field that is iterated over.
+
+   Instead of factoring out a base class, we could use something like this:
+
+     template<typename T, auto F = &T::next>
+     struct next_iterator
+     {
+       ...
+       self_type &operator++ ()
+       {
+	 m_item = m_item->*F;
+	 return *this;
+       }
+       ...
+     }
+
+  but that has the drawback that it doesn't work with incomplete T.  */
 
 template<typename T>
-struct next_iterator
+struct base_next_iterator
 {
-  typedef next_iterator self_type;
+  typedef base_next_iterator self_type;
   typedef T *value_type;
   typedef T *&reference;
   typedef T **pointer;
   typedef std::forward_iterator_tag iterator_category;
   typedef int difference_type;
 
-  explicit next_iterator (T *item)
+  explicit base_next_iterator (T *item)
     : m_item (item)
   {
   }
 
   /* Create a one-past-the-end iterator.  */
-  next_iterator ()
+  base_next_iterator ()
     : m_item (nullptr)
   {
   }
@@ -61,15 +77,34 @@ struct next_iterator
     return m_item != other.m_item;
   }
 
-  self_type &operator++ ()
+protected:
+
+  T *m_item;
+};
+
+/* An iterator that uses the 'next' field of a type to iterate.  This
+   can be used with various GDB types that are stored as linked
+   lists.  */
+
+template<typename T>
+struct next_iterator : base_next_iterator<T> {
+  typedef next_iterator self_type;
+  typedef T *value_type;
+  typedef T *&reference;
+  typedef T **pointer;
+
+  explicit next_iterator (T *item)
+    : base_next_iterator<T> (item)
   {
-    m_item = m_item->next;
-    return *this;
   }
 
-private:
+  next_iterator () = default;
 
-  T *m_item;
+  self_type &operator++ ()
+  {
+    this->m_item = this->m_item->next;
+    return *this;
+  }
 };
 
 /* A convenience wrapper to make a range type around a next_iterator.  */
-- 
2.51.0


  parent reply	other threads:[~2026-04-23  6:36 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-23  6:35 [PATCH 0/7] [gdb] Add superblocks range loops Tom de Vries
2026-04-23  6:35 ` [PATCH 1/7] [gdb] Use block::function_block Tom de Vries
2026-04-23 14:33   ` Tom Tromey
2026-04-24 12:19     ` Tom de Vries
2026-04-23  6:35 ` [PATCH 2/7] [gdbsupport] Add parameterless iterator_range constructor Tom de Vries
2026-04-23 14:34   ` Tom Tromey
2026-04-24 12:20     ` Tom de Vries
2026-04-23  6:35 ` Tom de Vries [this message]
     [not found]   ` <87340kpbwx.fsf@tromey.com>
2026-04-30  4:49     ` [PATCH 3/7] [gdbsupport] Factor out base_next_iterator Tom de Vries
2026-05-01 12:54       ` Tom de Vries
2026-05-01 13:00     ` Tom de Vries
2026-04-30 16:24   ` Simon Marchi
2026-04-30 19:09     ` Simon Marchi
2026-05-01 12:57       ` Tom Tromey
2026-05-01 13:20       ` Tom de Vries
2026-05-01 13:15     ` Tom de Vries
2026-04-23  6:35 ` [PATCH 4/7] [gdb] Add block::superblocks Tom de Vries
2026-04-27 11:11   ` Jan Vrany
2026-05-01 13:06     ` Tom de Vries
2026-04-30 16:24   ` Simon Marchi
2026-05-01 13:19     ` Tom de Vries
2026-04-23  6:35 ` [PATCH 5/7] [gdb] Use block::super_blocks Tom de Vries
2026-04-24 16:27   ` Tom Tromey
2026-04-24 21:24     ` Tom de Vries
2026-04-23  6:35 ` [PATCH 6/7] [gdb] Add block::function_blocks Tom de Vries
2026-04-23  6:35 ` [PATCH 7/7] [gdb] Use block::function_blocks Tom de Vries

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260423063530.1074175-4-tdevries@suse.de \
    --to=tdevries@suse.de \
    --cc=gdb-patches@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox