From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (qmail 40075 invoked by alias); 3 Oct 2016 17:32:30 -0000 Mailing-List: contact gdb-patches-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: gdb-patches-owner@sourceware.org Received: (qmail 40063 invoked by uid 89); 3 Oct 2016 17:32:29 -0000 Authentication-Results: sourceware.org; auth=none X-Virus-Found: No X-Spam-SWARE-Status: No, score=-4.5 required=5.0 tests=BAYES_00,RP_MATCHES_RCVD,SPF_HELO_PASS,SPF_PASS autolearn=ham version=3.3.2 spammy=FINAL, hoc X-HELO: paperclip.tbsaunde.org Received: from tbsaunde.org (HELO paperclip.tbsaunde.org) (66.228.47.254) by sourceware.org (qpsmtpd/0.93/v0.84-503-g423c35a) with ESMTP; Mon, 03 Oct 2016 17:32:28 +0000 Received: from ball (unknown [IPv6:2607:f0c8:8000:80e0:56ee:75ff:fe52:afb9]) by paperclip.tbsaunde.org (Postfix) with ESMTPSA id B6A96C079; Mon, 3 Oct 2016 17:32:26 +0000 (UTC) Date: Mon, 03 Oct 2016 17:32:00 -0000 From: Trevor Saunders To: Tom Tromey Cc: gdb-patches@sourceware.org Subject: Re: [RFA 1/4] Convert observers to C++ Message-ID: <20161003173939.ldh2tmdjbpvqmyxt@ball> References: <1475468542-11446-1-git-send-email-tom@tromey.com> <1475468542-11446-2-git-send-email-tom@tromey.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <1475468542-11446-2-git-send-email-tom@tromey.com> User-Agent: NeoMutt/20160910 (1.7.0) X-SW-Source: 2016-10/txt/msg00018.txt.bz2 On Sun, Oct 02, 2016 at 10:22:19PM -0600, Tom Tromey wrote: > This converts observers from using a special source-generating script > to be plain C++. > > This translation is less ideal than I'd like in a couple of ways. > Still, I think it's a mild improvement, on the basis that ordinary > code is preferable to an ad hoc code generator. > > First, it introduces a gdb analogue to std::function, the real one not > being available until C++11. > > Second, it works around the lack of variadic templates in a mildly > ugly way. > > In C++11 it would be ~300 lines of code shorter. fwiw I think you can reduce the boilerplate in the observer classes with something like this. template class observer_base { public: void add_observer (const FuncType &func) { m_observers.push_front (func); } void remove_observer (const FuncType &func) { m_observers.remove (func); } protected: typedef std::forward_list::iterator iter_type; std::forward_list m_observers; }; template class observer_1 FINAL : public observer_base { public: observer_1 (const char *name) : m_name (name) {} void notify (A a) { // loop over m_observers } private: const char *m_name; }; unfortunately you need to keep the constructors in the subclasses because you don't have inheriting constructors. Its perhaps more complicated, but with final I believe all the notify calls will get devirtualized by a reasonable compiler and you will end up with code basically identical to what you wrote. Trev