WMessageBox addButton and unique_ptr
Added by Simon M over 6 years ago
Hi,
I'm trying to move our application from Wt3 to Wt4.
So far it was pretty straightforward (although the migration py script could have offered something more, e.g. migrating the enums to enum class).
Now I was stuck a little at WMessageBox::addButton.
I first just changed my button ptr to a unique_ptr and got the compiler error use of deleted function.
Granted, I don't have that much experience with unique_ptr, but after a while I figured that I need to pass my button with std::move to addButton (this was a hint: https://redmine.emweb.be/boards/1/topics/14643?r=14773).
So I just used:
@
auto file = std::make_shared(mime, path);
file->suggestFileName("test.txt");
auto button = std::make_unique("Open File");
WLink link(file);
link.setTarget(Wt::LinkTarget::NewWindow);
button->setLink(link);
button->setDefault(true);
messageBox->addButton(std::move(button), Wt::StandardButton::Ok);@
I wanted to make sure, if this is the intended usage.
Maybe you could also add some more docs regarding Wt's new memory manegement.
Thanks!
Replies (3)
RE: WMessageBox addButton and unique_ptr - Added by lm at over 6 years ago
I'm glad you got your Wt::WMessageBox
worked out! Yes, that looks right.
By design, at most one std::unique_ptr
instance should point at any object at any point in time. If you were able to copy the pointer and do something like:
auto btn = std::make_unique<Wt::WPushButton>("Open File");
messageBox->addButton(btn);
then, there would be the local "btn" pointer, and a pointer to the same button in the messagebox. It's unclear at that point who should/may delete the button when they're done with it. Using std::unique_ptr
, however, after you move the button pointer, the local btn
is set to nullptr
because btn
longer owns the button. Not only is it clear that the local function should not delete the button, he cannot delete the button through that std::unique_ptr
any more.
Hopefully that adds some clarity. Usage will help solidify this in your mind.
RE: WMessageBox addButton and unique_ptr - Added by Roel Standaert over 6 years ago
There was a script to update enums to enum classes. Maybe I can add it to the source code again, but the problem with that script is that it was quite simple and would do a lot of substitutions it shouldn't do. In order to accurately switch out the enums for their enum class equivalents, you'd actually need something with a proper parser and type information, so it doesn't become overzealous. So while scripting the migration from enums to enum classes automatically may be possible, but implementing it properly is non-trivial. A script like that was used on the Wt 4 code base, after which I spent a lot of time tracking down all of the inappropriate replacements.
That code seems fine.
Documenting Wt's new memory management would boil down to explaining std::unique_ptr and move semantics, which is more of a general C topic. I'm not sure if we could do a better job documenting that than all of the sources on move semantics already out there. Check out Herb Sutter's 2016 CppCon talk for example: https://www.youtube.com/watch?v=JfmTagWcqoE
RE: WMessageBox addButton and unique_ptr - Added by Simon M over 6 years ago
Thanks for your answers!