Replace Gtk::manage() with std::unique_ptr<>?
- From: Murray Cumming <murrayc murrayc com>
- To: gtkmm-list <gtkmm-list gnome org>
- Subject: Replace Gtk::manage() with std::unique_ptr<>?
- Date: Fri, 05 Feb 2016 14:44:38 +0100
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?
--
Murray Cumming
murrayc murrayc com
www.murrayc.com
[
Date Prev][
Date Next] [
Thread Prev][Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]