From patchwork Fri May 3 23:12:01 2019 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Tom Tromey X-Patchwork-Id: 32508 Received: (qmail 129786 invoked by alias); 3 May 2019 23:12:36 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Delivered-To: mailing list gdb-patches@sourceware.org Received: (qmail 129693 invoked by uid 89); 3 May 2019 23:12:36 -0000 Authentication-Results: sourceware.org; auth=none X-Spam-SWARE-Status: No, score=-17.5 required=5.0 tests=AWL, BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, RCVD_IN_DNSWL_NONE, SPF_HELO_PASS autolearn=ham version=3.3.1 spammy= X-HELO: gateway23.websitewelcome.com Received: from gateway23.websitewelcome.com (HELO gateway23.websitewelcome.com) (192.185.48.251) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Fri, 03 May 2019 23:12:34 +0000 Received: from cm14.websitewelcome.com (cm14.websitewelcome.com [100.42.49.7]) by gateway23.websitewelcome.com (Postfix) with ESMTP id 82334457D for ; Fri, 3 May 2019 18:12:33 -0500 (CDT) Received: from box5379.bluehost.com ([162.241.216.53]) by cmsmtp with SMTP id MhM1hLRgm2qH7MhM1he338; Fri, 03 May 2019 18:12:33 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=tromey.com; s=default; h=References:In-Reply-To:Message-Id:Date:Subject:Cc:To:From: Sender:Reply-To:MIME-Version:Content-Type:Content-Transfer-Encoding: Content-ID:Content-Description:Resent-Date:Resent-From:Resent-Sender: Resent-To:Resent-Cc:Resent-Message-ID:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=OXiIybE5uuvUqPbCrCdyNIWbJpHN8/dajpdfgPlavvA=; b=lVikihRUu96+8aV/vXuQEPCXqG 1C+UJGUe8DEYabPEI7O/28akr6ZuGVRImFsBcq8LiCKhFIl5BbzF25vg5TCAEmfI0xjbHpEWvpyoE plSe3YH5guUFA1of+gtF7hcVi; Received: from 97-122-168-123.hlrn.qwest.net ([97.122.168.123]:37502 helo=bapiya.Home) by box5379.bluehost.com with esmtpsa (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.91) (envelope-from ) id 1hMhM1-003AAO-Ax; Fri, 03 May 2019 18:12:33 -0500 From: Tom Tromey To: gdb-patches@sourceware.org Cc: Tom Tromey Subject: [PATCH v2 01/31] Add a type-safe C++ interface to a registry Date: Fri, 3 May 2019 17:12:01 -0600 Message-Id: <20190503231231.8954-2-tom@tromey.com> In-Reply-To: <20190503231231.8954-1-tom@tromey.com> References: <20190503231231.8954-1-tom@tromey.com> This changes DECLARE_REGISTRY to add a type-safe interface. This interface is a C++ class that handles the details of registering a key, and provides various useful methods, including policy-based cleanup. 2019-04-22 Tom Tromey * registry.h (DECLARE_REGISTRY): Define the _key class. --- gdb/ChangeLog | 4 +++ gdb/registry.h | 68 +++++++++++++++++++++++++++++++++++++++++++++----- 2 files changed, 66 insertions(+), 6 deletions(-) diff --git a/gdb/registry.h b/gdb/registry.h index 3881e29b54f..683d905f763 100644 --- a/gdb/registry.h +++ b/gdb/registry.h @@ -20,6 +20,8 @@ #ifndef REGISTRY_H #define REGISTRY_H +#include + /* The macros here implement a template type and functions for associating some user data with a container object. @@ -243,11 +245,65 @@ typedef void (*registry_ ## TAG ## _callback) (struct TAG *, void *); \ extern const struct TAG ## _data *register_ ## TAG ## _data (void); \ extern const struct TAG ## _data *register_ ## TAG ## _data_with_cleanup \ (registry_ ## TAG ## _callback save, registry_ ## TAG ## _callback free); \ -extern void clear_ ## TAG ## _data (struct TAG *); \ -extern void set_ ## TAG ## _data (struct TAG *, \ - const struct TAG ## _data *data, \ - void *value); \ -extern void *TAG ## _data (struct TAG *, \ - const struct TAG ## _data *data); +extern void clear_ ## TAG ## _data (struct TAG *); \ +extern void set_ ## TAG ## _data (struct TAG *, \ + const struct TAG ## _data *data, \ + void *value); \ +extern void *TAG ## _data (struct TAG *, \ + const struct TAG ## _data *data); \ + \ +template> \ +class TAG ## _key \ +{ \ +public: \ + \ + TAG ## _key () \ + : m_key (register_ ## TAG ## _data_with_cleanup (nullptr, \ + cleanup)) \ + { \ + } \ + \ + DATA *get (struct TAG *obj) const \ + { \ + return (DATA *) TAG ## _data (obj, m_key); \ + } \ + \ + void set (struct TAG *obj, DATA *data) const \ + { \ + set_ ## TAG ## _data (obj, m_key, data); \ + } \ + \ + template \ + typename std::enable_if>::value, \ + Dummy>::type \ + emplace (struct TAG *obj, Args &&...args) const \ + { \ + DATA *result = new DATA (std::forward (args)...); \ + set (obj, result); \ + return result; \ + } \ + \ + void clear (struct TAG *obj) const \ + { \ + DATA *datum = get (obj); \ + if (datum != nullptr) \ + { \ + cleanup (obj, datum); \ + set (obj, nullptr); \ + } \ + } \ + \ +private: \ + \ + static void cleanup (struct TAG *obj, void *arg) \ + { \ + DATA *datum = (DATA *) arg; \ + Deleter d; \ + d (datum); \ + } \ + \ + const struct TAG ## _data *m_key; \ +}; #endif /* REGISTRY_H */