From patchwork Fri May 1 12:45:00 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom de Vries X-Patchwork-Id: 134322 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id CD0384BA23FF for ; Fri, 1 May 2026 12:47:04 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CD0384BA23FF Authentication-Results: sourceware.org; dkim=pass (1024-bit key, unprotected) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=bHkc4uYE; dkim=pass header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=cVbFeXcH; dkim=pass (1024-bit key) header.d=suse.de header.i=@suse.de header.a=rsa-sha256 header.s=susede2_rsa header.b=bHkc4uYE; dkim=neutral header.d=suse.de header.i=@suse.de header.a=ed25519-sha256 header.s=susede2_ed25519 header.b=cVbFeXcH X-Original-To: gdb-patches@sourceware.org Delivered-To: gdb-patches@sourceware.org Received: from smtp-out2.suse.de (smtp-out2.suse.de [195.135.223.131]) by sourceware.org (Postfix) with ESMTPS id 53E7E4099CDE for ; Fri, 1 May 2026 12:45:30 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 53E7E4099CDE Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=suse.de ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 53E7E4099CDE Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=195.135.223.131 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777639530; cv=none; b=I716IB3V6na8AO92329NF19CXaSMn3mVvlr3CzzihWf9d7OexzGPDJuWCyD2KlKNfKd36itxiTTNtk7xEpUuNM4Z113pVMw6TyS4LZU0/7wIb3Uhe6EF+qnoF5+ty0O/Nh2xRAah3B2IrUZ9I85W0Wmn+zC3c9AXrG/oIp3krT4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1777639530; c=relaxed/simple; bh=IYaeTAyiiMHRGXSO8HN8xq5bQ5yBTpgkrlIghj8oWf8=; h=DKIM-Signature:DKIM-Signature:DKIM-Signature:DKIM-Signature:From: To:Subject:Date:Message-ID:MIME-Version; b=hJ43eUrSguyg67/+tkW3EzAVezHaPa2Z3QuTbgzp5c6OTh04fQhsauEvNNEAk5u3zY13d81/282ZQRKDqN+OcX97hkR0nyRP0/ceAGY3yZmvi4eQxtsg8hkKSOZ+avsoxdQI+mLUETg6jGznv30ZvGos/MsejltXzY0eoX69+l0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 53E7E4099CDE Received: from imap1.dmz-prg2.suse.org (unknown [10.150.64.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 785745BD6D for ; Fri, 1 May 2026 12:45:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1777639506; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1GxVp7ACS61j9Nb0dw2thkWPr46UWHEAtlrXQJhE1ZE=; b=bHkc4uYEPbA8HdHjQrYWc4VsjBMj2W8B/P+X3RkpYN9o2s1oqZHX0Cd+dGgx3aB5QBDS/c THpgquLteqTHyW206CggVbPxZ/KqZopHtQdaFrTU/KLUA90nVJxMMe6x7dFqUHCYv+JR2d sZyhQcDWTAQArtXxnjpH/Ri26jehiuw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1777639506; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1GxVp7ACS61j9Nb0dw2thkWPr46UWHEAtlrXQJhE1ZE=; b=cVbFeXcHAZdm52B/f1WsjcYVKVxGoNM5xZP9kKRuUuh9DIik1GdYIxZU8JQoaVObqAQpHM i4fSlzTxA2HkTRDg== Authentication-Results: smtp-out2.suse.de; none DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_rsa; t=1777639506; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1GxVp7ACS61j9Nb0dw2thkWPr46UWHEAtlrXQJhE1ZE=; b=bHkc4uYEPbA8HdHjQrYWc4VsjBMj2W8B/P+X3RkpYN9o2s1oqZHX0Cd+dGgx3aB5QBDS/c THpgquLteqTHyW206CggVbPxZ/KqZopHtQdaFrTU/KLUA90nVJxMMe6x7dFqUHCYv+JR2d sZyhQcDWTAQArtXxnjpH/Ri26jehiuw= DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=suse.de; s=susede2_ed25519; t=1777639506; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=1GxVp7ACS61j9Nb0dw2thkWPr46UWHEAtlrXQJhE1ZE=; b=cVbFeXcHAZdm52B/f1WsjcYVKVxGoNM5xZP9kKRuUuh9DIik1GdYIxZU8JQoaVObqAQpHM i4fSlzTxA2HkTRDg== Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id 610ED593B3 for ; Fri, 1 May 2026 12:45:06 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id 2GI2FlKg9Gn5ZwAAD6G6ig (envelope-from ) for ; Fri, 01 May 2026 12:45:06 +0000 From: Tom de Vries To: gdb-patches@sourceware.org Subject: [PATCH 06/10] [gdbsupport] Factor out base_next_iterator Date: Fri, 1 May 2026 14:45:00 +0200 Message-ID: <20260501124504.2233495-7-tdevries@suse.de> X-Mailer: git-send-email 2.51.0 In-Reply-To: <20260501124504.2233495-1-tdevries@suse.de> References: <20260501124504.2233495-1-tdevries@suse.de> MIME-Version: 1.0 X-Spamd-Result: default: False [-2.80 / 50.00]; BAYES_HAM(-3.00)[99.99%]; NEURAL_HAM_LONG(-1.00)[-1.000]; MID_CONTAINS_FROM(1.00)[]; R_MISSING_CHARSET(0.50)[]; NEURAL_HAM_SHORT(-0.20)[-1.000]; MIME_GOOD(-0.10)[text/plain]; FUZZY_RATELIMITED(0.00)[rspamd.com]; RCVD_VIA_SMTP_AUTH(0.00)[]; RCPT_COUNT_ONE(0.00)[1]; ARC_NA(0.00)[]; DKIM_SIGNED(0.00)[suse.de:s=susede2_rsa,suse.de:s=susede2_ed25519]; DBL_BLOCKED_OPENRESOLVER(0.00)[imap1.dmz-prg2.suse.org:helo,suse.de:mid]; FROM_EQ_ENVFROM(0.00)[]; FROM_HAS_DN(0.00)[]; MIME_TRACE(0.00)[0:+]; RCVD_COUNT_TWO(0.00)[2]; TO_MATCH_ENVRCPT_ALL(0.00)[]; TO_DN_NONE(0.00)[]; PREVIOUSLY_DELIVERED(0.00)[gdb-patches@sourceware.org]; RCVD_TLS_ALL(0.00)[] X-Spam-Score: -2.80 X-Spam-Level: X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: gdb-patches@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gdb-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gdb-patches-bounces~patchwork=sourceware.org@sourceware.org Template struct next_iterator 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 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 -struct next_iterator +template +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(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 +struct next_iterator : base_next_iterator> +{ + using base_next_iterator>::base_next_iterator; + using base_next_iterator>::m_item; + + T *next () + { + return m_item->next; + } +}; + /* A convenience wrapper to make a range type around a next_iterator. */ template