From: Tom de Vries <tdevries@suse.de>
To: gdb-patches@sourceware.org
Subject: [PATCH 06/10] [gdbsupport] Factor out base_next_iterator
Date: Fri, 1 May 2026 14:45:00 +0200 [thread overview]
Message-ID: <20260501124504.2233495-7-tdevries@suse.de> (raw)
In-Reply-To: <20260501124504.2233495-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++.
I also explored using a pointer-to-member template argument T::next:
...
template<typename T, auto F = &T::next>
struct next_iterator
{
...
self_type &operator++ ()
{
m_item = m_item->*F;
return *this;
}
...
}
...
but that meant that using an incomplete T was no longer allowed (see
test_next_iterator_incomplete_type), so it's not a drop-in replacement.
---
gdbsupport/next-iterator.h | 36 ++++++++++++++++++++++++++----------
1 file changed, 26 insertions(+), 10 deletions(-)
diff --git a/gdbsupport/next-iterator.h b/gdbsupport/next-iterator.h
index 9f0aba7f8c4..feee2c2a7c7 100644
--- a/gdbsupport/next-iterator.h
+++ b/gdbsupport/next-iterator.h
@@ -21,27 +21,27 @@
#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 a next
+ function, which determines the actual field that is iterated over. */
-template<typename T>
-struct next_iterator
+template<typename T, class D>
+struct base_next_iterator
{
- using self_type = next_iterator;
+ using self_type = base_next_iterator;
using value_type = T *;
using reference = T *&;
using pointer = T **;
using iterator_category = std::forward_iterator_tag;
using difference_type = int;
- 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)
{
}
@@ -63,15 +63,31 @@ struct next_iterator
self_type &operator++ ()
{
- m_item = m_item->next;
+ m_item = static_cast<D *>(this)->next ();
return *this;
}
-private:
+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. Note that we're using CRTP here. */
+
+template<typename T>
+struct next_iterator : base_next_iterator<T, next_iterator<T>>
+{
+ using base_next_iterator<T, next_iterator<T>>::base_next_iterator;
+ using base_next_iterator<T, next_iterator<T>>::m_item;
+
+ T *next ()
+ {
+ return m_item->next;
+ }
+};
+
/* A convenience wrapper to make a range type around a next_iterator. */
template <typename T>
--
2.51.0
next prev parent reply other threads:[~2026-05-01 12:46 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-01 12:44 [PATCH 00/10] [gdb] Add superblocks range loops Tom de Vries
2026-05-01 12:44 ` [PATCH 01/10] [gdb] Use block::containing_function Tom de Vries
2026-05-01 12:44 ` [PATCH 02/10] [gdb] Factor out block::containing_function_block Tom de Vries
2026-05-01 12:44 ` [PATCH 03/10] [gdb] Use block::containing_function_block Tom de Vries
2026-05-01 12:44 ` [PATCH 04/10] [gdb] Add unit test for next_iterator Tom de Vries
2026-05-01 12:44 ` [PATCH 05/10] [gdbsupport] Use using instead of typedef in next_iterator Tom de Vries
2026-05-01 12:45 ` Tom de Vries [this message]
2026-05-08 19:17 ` [PATCH 06/10] [gdbsupport] Factor out base_next_iterator Tom Tromey
2026-05-01 12:45 ` [PATCH 07/10] [gdb] Add block::block_and_superblocks Tom de Vries
2026-05-01 12:45 ` [PATCH 08/10] [gdb] Use block::block_and_superblocks Tom de Vries
2026-05-08 19:41 ` Tom Tromey
2026-05-01 12:45 ` [PATCH 09/10] [gdb] Add block::block_and_superblocks_in_fn Tom de Vries
2026-05-01 12:45 ` [PATCH 10/10] [gdb] Use block::block_and_superblocks_in_fn Tom de Vries
2026-06-09 12:40 ` [PATCH 00/10] [gdb] Add superblocks range loops 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=20260501124504.2233495-7-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