This is the mail archive of the
gdb-patches@sourceware.org
mailing list for the GDB project.
Re: [RFA] Convert observers to C++
- From: Pedro Alves <palves at redhat dot com>
- To: Tom Tromey <tom at tromey dot com>
- Cc: gdb-patches at sourceware dot org
- Date: Mon, 12 Feb 2018 18:37:06 +0000
- Subject: Re: [RFA] Convert observers to C++
- Authentication-results: sourceware.org; auth=none
- References: <20171105013039.9135-1-tom@tromey.com> <20f70b2b-bf93-a6b6-f2a4-82e14c807e05@redhat.com> <87zi4haeak.fsf@tromey.com>
Hi Tom,
Thanks!
On 02/10/2018 12:17 AM, Tom Tromey wrote:
>
>>> + m_observers.erase (m_observers.begin () + f);
>
> Pedro> Hmm, this looks incorrect. "attach" returns an index
> Pedro> as token. And then detach uses "std::vector::erase()",
> Pedro> which removes the element at the index and then shifts
> Pedro> the rest of the elements left to fill in the space
> Pedro> left over by the removed element(s). So that invalidates
> Pedro> all token after the element erased.
>
> Yes, thanks for catching this.
>
> I figure that observers aren't very space-sensitive, so I just put a
> counter into the observer and then put the value into the vector, like:
>
> std::vector<std::pair<size_t, func_type>> m_observers;
My initial reaction was "but counter of what?", but I took a
look at the patch, and I see what you mean. I'll say more in
response to your new patch.
>
> Pedro> This could be constexpr and then defined in-class, right?
>
> Did it.
>
> Pedro> (I wonder about splitting core/lib from uses of the core.
> Pedro> I.e., defining the struct observer core mechanism in one
> Pedro> header, and then define the actual observers in a separate header.
> Pedro> I'm basically pondering about e.g., using observers under common/
> Pedro> or in gdbserver/. Like e.g., "common/observer.h" and then
> Pedro> "gdb/observers.h" and "gdbserver/observers.h", etc. But we can cross
> Pedro> that bridge when we get to it, too.)
>
> I did this. Also I realized that calling the class "observer" is
> somewhat wrong, as the callback is the observer -- so I made
> common/observable.h and I renamed the class to "class observable".
Yeah, the current documentation in master calls them "subjects":
/* An observer is an entity who is interested in being notified when GDB
reaches certain states, or certain events occur in GDB. The entity being
observed is called the Subject. To receive notifications, the observer
attaches a callback to the subject. One subject can have several
observers.
though "observable" does seem better to me.
Thanks,
Pedro Alves