ViewGuard: Fix ctor and assign operator having different behaviours

The difference was very small: The ctor honoured View::inDtor()
while the assign op didn't. It's easy to make them behave the same
by creating a setView() method

As a side-effect the duplicate connect() is also gone now
This commit is contained in:
Sergio Martins
2022-04-05 20:23:57 +01:00
parent 4b62714ef5
commit fdc56b938e
2 changed files with 21 additions and 13 deletions

View File

@@ -15,13 +15,8 @@
using namespace KDDockWidgets;
ViewGuard::ViewGuard(View *view)
: v(view && view->inDtor() ? nullptr : view)
{
if (v) {
m_onDestroy = v->beingDestroyed.connect([this] {
v = nullptr;
});
}
setView(view);
}
ViewGuard::operator bool() const
@@ -53,15 +48,27 @@ View *ViewGuard::view() const
ViewGuard &ViewGuard::operator=(View *view)
{
if (view == v)
return *this;
setView(view);
return *this;
}
void ViewGuard::setView(View *view)
{
if (view == v)
return;
if (view && view->inDtor()) {
// We don't care about views that are already being in DTOR. They count as already deleted for what's ViewGuard concerned.
// This is rare anway, would need to require some reentrancy.
view = nullptr;
}
// Remove the previous connection
clear();
v = view;
m_onDestroy = v->beingDestroyed.connect([this] {
v = nullptr;
});
return *this;
if (v) {
m_onDestroy = v->beingDestroyed.connect([this] {
v = nullptr;
});
}
}

View File

@@ -35,6 +35,7 @@ public:
ViewGuard &operator=(View *);
private:
void setView(View *);
View *v = nullptr;
KDBindings::ConnectionHandle m_onDestroy;
};