Re: Replace Gtk::manage() with std::unique_ptr<>?



So, I've read most of this conversation, and it seems this simple
alternative hasn't come up yet:

  // Old code
  auto button = Gtk::manage(new Gtk::Button("a button");
  button->show();
  container.add(button);

  // New code
  auto button = container.add_managed<Gtk::Button>("a button");
  button->show();

I don't know how feasible it is to create loads of new member functions
with a _managed (or a different) suffix, but API-wise I think reducing
the creation and insertion into the container to one statement would
make sense with std::unique_ptr.

Am 05.02.2016 um 14:47 schrieb Murray Cumming:
The trend in modern C++ is to use std::unique_ptr<> by default to
express ownership, usually via std::make_unique() (in C++14), insead of
using a raw pointer via a "naked new".

So, unless you know something else is necessary, this would be good:
  auto thing = std::make_unique<Thing>();
instead of this:
  auto thing = new Thing();
or
  Thing* thing = new Thing();


It's also considered wise to receive a std::unique_ptr as a parameter
if the method really plans to take ownership. For instance:
  void Foo::take_thing(std::shared_ptr<Thing> thing);


So I was wondering if we could use this idea instead of Gtk::manage(),
which has much the same sense of "take ownership", and eventually
deprecate Gtk::manage().

Then we could do this, for instance:
  auto button = std::make_unique<Gtk::Button>("a button");
  button->show();
  container.add(std::move(button));
instead of this:
  auto button = Gtk::manage(new Gtk::Button>("a button"));
  button->show();
  container.add(*button);

This would work too, I think:
  container.add(std::make_unique<Gtk::Button>("a button"));


Then we would be using standard C++ syntax/API instead of custom gtkmm
API.


However, it would need us to add overloads for methods that currently
take Widget& parameters. But I think that's doable. For instance:

void Container::add(std::unique_ptr<Widget> widget)
{
  add(*(Gtk::manage(widget.release())));
}


Thoughts?




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]