From: Trevor Saunders <tbsaunde@tbsaunde.org>
To: Tom Tromey <tom@tromey.com>
Cc: gdb-patches@sourceware.org
Subject: Re: [RFA 1/4] Convert observers to C++
Date: Mon, 03 Oct 2016 17:32:00 -0000 [thread overview]
Message-ID: <20161003173939.ldh2tmdjbpvqmyxt@ball> (raw)
In-Reply-To: <1475468542-11446-2-git-send-email-tom@tromey.com>
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<typename FuncType>
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<FuncType>::iterator iter_type;
std::forward_list<FuncType> m_observers;
};
template<typename A>
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
next prev parent reply other threads:[~2016-10-03 17:32 UTC|newest]
Thread overview: 6+ messages / expand[flat|nested] mbox.gz Atom feed top
2016-10-03 4:22 [RFA 0/4] Conver " Tom Tromey
2016-10-03 4:22 ` [RFA 4/4] Change observer tests to use selftest framework Tom Tromey
2016-10-03 4:22 ` [RFA 2/4] Change types to match observer.attach Tom Tromey
2016-10-03 4:22 ` [RFA 1/4] Convert observers to C++ Tom Tromey
2016-10-03 17:32 ` Trevor Saunders [this message]
2016-10-03 4:22 ` [RFA 3/4] Update all observer uses Tom Tromey
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=20161003173939.ldh2tmdjbpvqmyxt@ball \
--to=tbsaunde@tbsaunde.org \
--cc=gdb-patches@sourceware.org \
--cc=tom@tromey.com \
/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