This is the mail archive of the gdb-patches@sourceware.org mailing list for the GDB project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: [RFA 1/4] Convert observers to C++


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]